Class: Attio::Util::WebhookSignature
- Inherits:
-
Object
- Object
- Attio::Util::WebhookSignature
- Defined in:
- lib/attio/util/webhook_signature.rb
Overview
Verifies webhook signatures from Attio to ensure authenticity
Defined Under Namespace
Classes: Handler, SignatureVerificationError
Constant Summary collapse
- SIGNATURE_HEADER =
HTTP header containing the webhook signature
"x-attio-signature"- TIMESTAMP_HEADER =
HTTP header containing the request timestamp
"x-attio-timestamp"- TOLERANCE_SECONDS =
5 minutes
300
Class Method Summary collapse
-
.calculate_signature(payload, timestamp, secret) ⇒ Object
Calculate signature for a payload.
-
.extract_from_headers(headers) ⇒ Object
Extract signature from headers.
-
.verify(payload:, signature:, timestamp:, secret:, tolerance: TOLERANCE_SECONDS) ⇒ Object
Verify webhook signature (returns boolean).
-
.verify!(payload:, signature:, timestamp:, secret:, tolerance: TOLERANCE_SECONDS) ⇒ Object
Verify webhook signature (raises exception on failure).
Class Method Details
.calculate_signature(payload, timestamp, secret) ⇒ Object
Calculate signature for a payload
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/attio/util/webhook_signature.rb', line 43 def calculate_signature(payload, , secret) # Ensure payload is a string payload_string = payload.is_a?(String) ? payload : JSON.generate(payload) # Create the signed payload signed_payload = "#{timestamp}.#{payload_string}" # Calculate HMAC hmac = OpenSSL::HMAC.hexdigest("SHA256", secret, signed_payload) # Return in the format Attio uses "v1=#{hmac}" end |
.extract_from_headers(headers) ⇒ Object
Extract signature from headers
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/attio/util/webhook_signature.rb', line 58 def extract_from_headers(headers) signature = headers[SIGNATURE_HEADER] || headers[SIGNATURE_HEADER.upcase] || headers[SIGNATURE_HEADER.tr("-", "_").upcase] = headers[TIMESTAMP_HEADER] || headers[TIMESTAMP_HEADER.upcase] || headers[TIMESTAMP_HEADER.tr("-", "_").upcase] raise SignatureVerificationError, "Missing signature header: #{SIGNATURE_HEADER}" unless signature raise SignatureVerificationError, "Missing timestamp header: #{TIMESTAMP_HEADER}" unless { signature: signature, timestamp: } end |
.verify(payload:, signature:, timestamp:, secret:, tolerance: TOLERANCE_SECONDS) ⇒ Object
Verify webhook signature (returns boolean)
35 36 37 38 39 40 |
# File 'lib/attio/util/webhook_signature.rb', line 35 def verify(payload:, signature:, timestamp:, secret:, tolerance: TOLERANCE_SECONDS) verify!(payload: payload, signature: signature, timestamp: , secret: secret, tolerance: tolerance) true rescue SignatureVerificationError false end |
.verify!(payload:, signature:, timestamp:, secret:, tolerance: TOLERANCE_SECONDS) ⇒ Object
Verify webhook signature (raises exception on failure)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/attio/util/webhook_signature.rb', line 19 def verify!(payload:, signature:, timestamp:, secret:, tolerance: TOLERANCE_SECONDS) validate_inputs!(payload, signature, , secret) # Check timestamp to prevent replay attacks (, tolerance) # Calculate expected signature expected_signature = calculate_signature(payload, , secret) # Constant-time comparison to prevent timing attacks raise SignatureVerificationError, "Invalid signature" unless secure_compare(signature, expected_signature) rescue => e raise SignatureVerificationError, "Webhook signature verification failed: #{e.message}" end |