Module: Postscale::Webhook

Defined in:
lib/postscale/webhook_verification.rb

Constant Summary collapse

DEFAULT_TOLERANCE_SECONDS =
300

Class Method Summary collapse

Class Method Details

.sign_body(body, secret, timestamp: Time.now.to_i) ⇒ Object



53
54
55
56
# File 'lib/postscale/webhook_verification.rb', line 53

def sign_body(body, secret, timestamp: Time.now.to_i)
  signature = OpenSSL::HMAC.hexdigest("SHA256", secret.to_s, "#{timestamp}.#{body}")
  "t=#{timestamp},v1=#{signature}"
end

.verify_signature(body, header, secrets, now: Time.now.to_i, tolerance_seconds: DEFAULT_TOLERANCE_SECONDS) ⇒ Object



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, header, 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 = parse_signature_header(header)
  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