Module: Airwallex::Webhook

Defined in:
lib/airwallex/webhook.rb

Defined Under Namespace

Classes: Event

Constant Summary collapse

DEFAULT_TOLERANCE =

5 minutes

300

Class Method Summary collapse

Class Method Details

.construct_event(payload, signature, timestamp, secret:, tolerance: DEFAULT_TOLERANCE) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/airwallex/webhook.rb', line 11

def construct_event(payload, signature, timestamp, secret:, tolerance: DEFAULT_TOLERANCE)
  verify_signature(payload, signature, timestamp, secret, tolerance)

  data = JSON.parse(payload)
  Event.new(data)
rescue JSON::ParserError => e
  raise SignatureVerificationError, "Invalid payload: #{e.message}"
end

.verify_signature(payload, signature, timestamp, secret, tolerance) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/airwallex/webhook.rb', line 20

def verify_signature(payload, signature, timestamp, secret, tolerance)
  verify_timestamp(timestamp, tolerance)

  expected_signature = compute_signature(timestamp, payload, secret)

  unless secure_compare(expected_signature, signature)
    raise SignatureVerificationError, "Signature verification failed"
  end

  true
end