Module: StraddlePay::Webhook::Signature
- Defined in:
- lib/straddle_pay/webhook.rb
Overview
Low-level signature verification following the Svix protocol.
Constant Summary collapse
- HEADER_PREFIXES =
%w[svix webhook].freeze
Class Method Summary collapse
-
.compute_signature(msg_id, timestamp, payload, secret) ⇒ String
Computes the expected HMAC-SHA256 signature for a webhook payload.
-
.generate_header(msg_id:, timestamp:, payload:, secret:) ⇒ Hash
Generates Svix-compatible headers for testing.
-
.verify_header(payload, headers, secret, tolerance: nil) ⇒ true
Verifies the webhook signature header against the payload.
Class Method Details
.compute_signature(msg_id, timestamp, payload, secret) ⇒ String
Computes the expected HMAC-SHA256 signature for a webhook payload.
64 65 66 67 68 69 |
# File 'lib/straddle_pay/webhook.rb', line 64 def compute_signature(msg_id, , payload, secret) key = decode_secret(secret) signed_content = "#{msg_id}.#{timestamp}.#{payload}" digest = OpenSSL::HMAC.digest("SHA256", key, signed_content) [digest].pack("m0") end |
.generate_header(msg_id:, timestamp:, payload:, secret:) ⇒ Hash
Generates Svix-compatible headers for testing.
78 79 80 81 82 83 84 85 86 |
# File 'lib/straddle_pay/webhook.rb', line 78 def generate_header(msg_id:, timestamp:, payload:, secret:) ts = .to_s sig = compute_signature(msg_id, ts, payload, secret) { "svix-id" => msg_id, "svix-timestamp" => ts, "svix-signature" => "v1,#{sig}" } end |
.verify_header(payload, headers, secret, tolerance: nil) ⇒ true
Verifies the webhook signature header against the payload.
50 51 52 53 54 55 |
# File 'lib/straddle_pay/webhook.rb', line 50 def verify_header(payload, headers, secret, tolerance: nil) msg_id, , signature = extract_headers(headers) (, tolerance) if tolerance expected = compute_signature(msg_id, , payload, secret) verify_signature(expected, signature) end |