Class: Cryptopay::Callbacks

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptopay/callbacks.rb

Instance Method Summary collapse

Constructor Details

#initialize(secret) ⇒ Callbacks

Returns a new instance of Callbacks.



5
6
7
# File 'lib/cryptopay/callbacks.rb', line 5

def initialize(secret)
  @secret = secret
end

Instance Method Details

#verify(body, signature) ⇒ Object

Verifies callback body and builds Callback object

Parameters:

  • body (String)

    Raw request body string

  • signature (String)

    Value of the X-Cryptopay-Signature header

Returns:

  • (Object)

    Callback

Raises:



13
14
15
16
17
18
19
20
21
# File 'lib/cryptopay/callbacks.rb', line 13

def verify(body, signature)
  expected_signature = OpenSSL::HMAC.hexdigest('SHA256', secret, body)

  equals = secure_compare(signature, expected_signature)
  raise(SignatureVerificationError, 'Signature mismatch') unless equals

  data = JSON.parse(body, symbolize_names: true)
  Callback.build_from_hash(data)
end