Class: Revolut::WebhookEvent

Inherits:
Resource show all
Defined in:
lib/revolut/resources/webhook_event.rb

Class Method Summary collapse

Methods inherited from Resource

coerce_with, create, delete, http_client, #initialize, list, retrieve, skip_coertion_for, #to_json, to_proc, update

Constructor Details

This class inherits a constructor from Revolut::Resource

Class Method Details

.construct_from(request, signing_secret) ⇒ WebhookEvent

Constructs a new instance of the WebhookEvent class from a request object and a signing secret.

Parameters:

  • request (ActionDispatch::Request)

    The request object containing the webhook event data.

  • signing_secret (String)

    The signing secret used to verify the signature of the webhook event.

Returns:

  • (WebhookEvent)

    A new instance of the WebhookEvent class.

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/revolut/resources/webhook_event.rb', line 14

def self.construct_from(request, signing_secret)
  json = request.body.read
  timestamp = request.headers["Revolut-Request-Timestamp"]
  header_signatures = request.headers["Revolut-Signature"].split(",")
  payload_to_sign = "v1.#{timestamp}.#{json}"
  digest = OpenSSL::Digest.new("sha256")
  signature_digest = "v1=" + OpenSSL::HMAC.hexdigest(digest, signing_secret, payload_to_sign)

  if header_signatures.include? signature_digest
    new(JSON.parse(json))
  else
    raise Revolut::SignatureVerificationError, "Signature verification failed"
  end
end