Module: Payhub::WebhookEvent
- Defined in:
- lib/payhub/webhook.rb
Constant Summary collapse
- DEFAULT_TOLERANCE_SECONDS =
300
Class Method Summary collapse
-
.verify(secret, body, header, tolerance_seconds: DEFAULT_TOLERANCE_SECONDS, now: nil) ⇒ Object
Verify a webhook delivery and return the decoded event.
Class Method Details
.verify(secret, body, header, tolerance_seconds: DEFAULT_TOLERANCE_SECONDS, now: nil) ⇒ Object
Verify a webhook delivery and return the decoded event. Raises Payhub::MalformedHeaderError, TimestampOutOfToleranceError, or InvalidSignatureError.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/payhub/webhook.rb', line 42 def verify(secret, body, header, tolerance_seconds: DEFAULT_TOLERANCE_SECONDS, now: nil) secret_b = secret.is_a?(String) ? secret.b : secret.to_s.b body_b = body.is_a?(String) ? body.b : body.to_s.b t, v1 = parse_header(header) wall_now = now || Time.now.to_i skew = (wall_now - t).abs raise TimestampOutOfToleranceError.new(skew) if skew > tolerance_seconds signed = "#{t}.".b + body_b expected = OpenSSL::HMAC.hexdigest("SHA256", secret_b, signed) raise InvalidSignatureError, "Hub-Signature v1 does not match" unless secure_compare(expected, v1) decode_payload(body_b) end |