26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/postscale/webhook_verification.rb', line 26
def verify_signature(body, , secrets, now: Time.now.to_i, tolerance_seconds: DEFAULT_TOLERANCE_SECONDS)
candidates = Array(secrets).compact.reject { |secret| secret.to_s.empty? }
return invalid("missing_secret", "At least one webhook secret is required.") if candidates.empty?
parsed = ()
return parsed if parsed.is_a?(WebhookVerificationResult)
timestamp, signatures = parsed
if (now.to_i - timestamp).abs > tolerance_seconds
return invalid(
"timestamp_outside_tolerance",
"Webhook signature timestamp is outside the allowed tolerance.",
timestamp
)
end
signed_payload = "#{timestamp}.#{body}"
candidates.each do |secret|
expected = OpenSSL::HMAC.hexdigest("SHA256", secret.to_s, signed_payload)
signatures.each do |signature|
return WebhookVerificationResult.new(valid: true, timestamp: timestamp) if secure_compare(expected, signature)
end
end
invalid("signature_mismatch", "Webhook signature does not match.", timestamp)
end
|