Class: GoCardlessPro::Webhook
- Inherits:
-
Object
- Object
- GoCardlessPro::Webhook
- Defined in:
- lib/gocardless_pro/webhook.rb
Defined Under Namespace
Classes: InvalidSignatureError
Class Method Summary collapse
-
.parse(options = {}) ⇒ Array<GoCardlessPro::Resources::Event>
Validates that a webhook was genuinely sent by GoCardless using ‘.signature_valid?`, and then parses it into an array of `GoCardlessPro::Resources::Event` objects representing each event included in the webhook.
-
.signature_valid?(options = {}) ⇒ Boolean
Validates that a webhook was genuinely sent by GoCardless by computing its signature using the body and your webhook endpoint secret, and comparing that with the signature included in the ‘Webhook-Signature` header.
Class Method Details
.parse(options = {}) ⇒ Array<GoCardlessPro::Resources::Event>
Validates that a webhook was genuinely sent by GoCardless using ‘.signature_valid?`, and then parses it into an array of `GoCardlessPro::Resources::Event` objects representing each event included in the webhook
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/gocardless_pro/webhook.rb', line 24 def parse( = {}) () unless signature_valid?(request_body: [:request_body], signature_header: [:signature_header], webhook_endpoint_secret: [:webhook_endpoint_secret]) raise InvalidSignatureError, "This webhook doesn't appear to be a genuine " \ 'webhook from GoCardless, because the signature ' \ "header doesn't match the signature computed" \ ' with your webhook endpoint secret.' end events = JSON.parse([:request_body])['events'] events.map { |event| Resources::Event.new(event) } end |
.signature_valid?(options = {}) ⇒ Boolean
Validates that a webhook was genuinely sent by GoCardless by computing its signature using the body and your webhook endpoint secret, and comparing that with the signature included in the ‘Webhook-Signature` header
53 54 55 56 57 58 59 60 61 |
# File 'lib/gocardless_pro/webhook.rb', line 53 def signature_valid?( = {}) () computed_signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), [:webhook_endpoint_secret], [:request_body]) secure_compare([:signature_header], computed_signature) end |