|
| 1 | +require 'json' |
| 2 | +require 'openssl' |
| 3 | +require 'base64' |
| 4 | + |
| 5 | +RSpec.describe Authsignal::Webhook do |
| 6 | + let(:api_url) { ENV['AUTHSIGNAL_API_URL'] } |
| 7 | + let(:api_secret_key) { ENV['AUTHSIGNAL_API_SECRET_KEY'] } |
| 8 | + |
| 9 | + before do |
| 10 | + raise "AUTHSIGNAL_API_URL is undefined in env" unless api_url |
| 11 | + raise "AUTHSIGNAL_API_SECRET_KEY is undefined in env" unless api_secret_key |
| 12 | + |
| 13 | + Authsignal.setup do |config| |
| 14 | + config.api_secret_key = api_secret_key |
| 15 | + config.api_url = api_url |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + describe "webhook verification" do |
| 20 | + it "raises error for invalid signature format" do |
| 21 | + payload = JSON.generate({}) |
| 22 | + signature = "123" |
| 23 | + |
| 24 | + expect { |
| 25 | + Authsignal.webhook.construct_event(payload, signature) |
| 26 | + }.to raise_error(Authsignal::InvalidSignatureError, "Signature format is invalid.") |
| 27 | + end |
| 28 | + |
| 29 | + it "raises error for timestamp outside tolerance zone" do |
| 30 | + payload = JSON.generate({}) |
| 31 | + signature = "t=1630000000,v2=invalid_signature" |
| 32 | + |
| 33 | + expect { |
| 34 | + Authsignal.webhook.construct_event(payload, signature) |
| 35 | + }.to raise_error(Authsignal::InvalidSignatureError, "Timestamp is outside the tolerance zone.") |
| 36 | + end |
| 37 | + |
| 38 | + it "raises error for invalid computed signature" do |
| 39 | + payload = JSON.generate({}) |
| 40 | + timestamp = Time.now.to_i |
| 41 | + signature = "t=#{timestamp},v2=invalid_signature" |
| 42 | + |
| 43 | + expect { |
| 44 | + Authsignal.webhook.construct_event(payload, signature) |
| 45 | + }.to raise_error(Authsignal::InvalidSignatureError, "Signature mismatch.") |
| 46 | + end |
| 47 | + |
| 48 | + it "validates a valid signature" do |
| 49 | + payload = JSON.generate({ |
| 50 | + version: 1, |
| 51 | + id: "bc1598bc-e5d6-4c69-9afb-1a6fe3469d6e", |
| 52 | + source: "https://authsignal.com", |
| 53 | + time: "2025-02-20T01:51:56.070Z", |
| 54 | + tenantId: "7752d28e-e627-4b1b-bb81-b45d68d617bc", |
| 55 | + type: "email.created", |
| 56 | + data: { |
| 57 | + |
| 58 | + code: "157743", |
| 59 | + userId: "b9f74d36-fcfc-4efc-87f1-3664ab5a7fb0", |
| 60 | + actionCode: "accountRecovery", |
| 61 | + idempotencyKey: "ba8c1a7c-775d-4dff-9abe-be798b7b8bb9", |
| 62 | + verificationMethod: "EMAIL_OTP" |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + tolerance = -1 |
| 67 | + timestamp = Time.now.to_i |
| 68 | + |
| 69 | + hmac_content = "#{timestamp}.#{payload}" |
| 70 | + computed_signature = OpenSSL::HMAC.digest( |
| 71 | + OpenSSL::Digest.new('sha256'), |
| 72 | + api_secret_key, |
| 73 | + hmac_content |
| 74 | + ) |
| 75 | + signature_b64 = Base64.strict_encode64(computed_signature).delete('=') |
| 76 | + signature = "t=#{timestamp},v2=#{signature_b64}" |
| 77 | + |
| 78 | + event = Authsignal.webhook.construct_event(payload, signature, tolerance) |
| 79 | + |
| 80 | + expect(event).not_to be_nil |
| 81 | + expect(event[:version]).to eq(1) |
| 82 | + expect(event[:data][:actionCode]).to eq("accountRecovery") |
| 83 | + end |
| 84 | + |
| 85 | + it "validates a signature when 2 API keys are active" do |
| 86 | + payload = JSON.generate({ |
| 87 | + version: 1, |
| 88 | + id: "af7be03c-ea8f-4739-b18e-8b48fcbe4e38", |
| 89 | + source: "https://authsignal.com", |
| 90 | + time: "2025-02-20T01:47:17.248Z", |
| 91 | + tenantId: "7752d28e-e627-4b1b-bb81-b45d68d617bc", |
| 92 | + type: "email.created", |
| 93 | + data: { |
| 94 | + |
| 95 | + code: "718190", |
| 96 | + userId: "b9f74d36-fcfc-4efc-87f1-3664ab5a7fb0", |
| 97 | + actionCode: "accountRecovery", |
| 98 | + idempotencyKey: "68d68190-fac9-4e91-b277-c63d31d3c6b1", |
| 99 | + verificationMethod: "EMAIL_OTP" |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + tolerance = -1 |
| 104 | + timestamp = Time.now.to_i |
| 105 | + |
| 106 | + hmac_content = "#{timestamp}.#{payload}" |
| 107 | + computed_signature = OpenSSL::HMAC.digest( |
| 108 | + OpenSSL::Digest.new('sha256'), |
| 109 | + api_secret_key, |
| 110 | + hmac_content |
| 111 | + ) |
| 112 | + signature_b64 = Base64.strict_encode64(computed_signature).delete('=') |
| 113 | + |
| 114 | + signature = "t=#{timestamp},v2=#{signature_b64},v2=oldKeySignature123" |
| 115 | + |
| 116 | + event = Authsignal.webhook.construct_event(payload, signature, tolerance) |
| 117 | + |
| 118 | + expect(event).not_to be_nil |
| 119 | + end |
| 120 | + end |
| 121 | +end |
0 commit comments