Module: Airwallex::Webhook

Defined in:
lib/airwallex/webhook.rb

Defined Under Namespace

Classes: Event

Constant Summary collapse

DEFAULT_TOLERANCE =

5 minutes

300
MS_THRESHOLD =

Timestamps below this are seconds, at/above are milliseconds. Valid until year 2286.

10_000_000_000

Class Method Summary collapse

Class Method Details

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



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

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



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

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