@@ -16,7 +16,6 @@ func getTestWebhook() *Webhook {
1616 return NewWebhook (testSecretKey )
1717}
1818
19- // generateTestSignature creates a valid signature for testing purposes
2019func generateTestSignature (payload string , timestamp int64 , secret string ) string {
2120 hmacContent := fmt .Sprintf ("%d.%s" , timestamp , payload )
2221 mac := hmac .New (sha256 .New , []byte (secret ))
@@ -99,13 +98,10 @@ func TestInvalidComputedSignature(t *testing.T) {
9998func TestValidSignature (t * testing.T ) {
10099 webhook := getTestWebhook ()
101100
102- // Payload should be compact JSON (no whitespace)
103101 payload := `{"version":1,"id":"bc1598bc-e5d6-4c69-9afb-1a6fe3469d6e","source":"https://authsignal.com","time":"2025-02-20T01:51:56.070Z","tenantId":"7752d28e-e627-4b1b-bb81-b45d68d617bc","type":"email.created","data":{"to":"chris@authsignal.com","code":"157743","userId":"b9f74d36-fcfc-4efc-87f1-3664ab5a7fb0","actionCode":"accountRecovery","idempotencyKey":"ba8c1a7c-775d-4dff-9abe-be798b7b8bb9","verificationMethod":"EMAIL_OTP"}}`
104102
105- // Ignore tolerance window by passing -1
106103 tolerance := - 1
107104
108- // Generate a valid signature dynamically
109105 timestamp := time .Now ().Unix ()
110106 signature := generateTestSignature (payload , timestamp , testSecretKey )
111107
@@ -141,13 +137,10 @@ func TestValidSignatureWhenTwoApiKeysActive(t *testing.T) {
141137
142138 payload := `{"version":1,"id":"af7be03c-ea8f-4739-b18e-8b48fcbe4e38","source":"https://authsignal.com","time":"2025-02-20T01:47:17.248Z","tenantId":"7752d28e-e627-4b1b-bb81-b45d68d617bc","type":"email.created","data":{"to":"chris@authsignal.com","code":"718190","userId":"b9f74d36-fcfc-4efc-87f1-3664ab5a7fb0","actionCode":"accountRecovery","idempotencyKey":"68d68190-fac9-4e91-b277-c63d31d3c6b1","verificationMethod":"EMAIL_OTP"}}`
143139
144- // Ignore tolerance window
145140 tolerance := - 1
146141
147- // Generate a valid signature and add an old/invalid signature to simulate two API keys
148142 timestamp := time .Now ().Unix ()
149143 validSignature := generateTestSignature (payload , timestamp , testSecretKey )
150- // Add an invalid signature from an "old" key
151144 signature := validSignature + ",v2=oldKeyInvalidSignature123"
152145
153146 event , err := webhook .ConstructEvent (payload , signature , tolerance )
@@ -163,7 +156,6 @@ func TestValidSignatureWhenTwoApiKeysActive(t *testing.T) {
163156}
164157
165158func TestValidSignatureWithOldKeyFirst (t * testing.T ) {
166- // Test that validation works when the valid signature is NOT the first one
167159 webhook := getTestWebhook ()
168160
169161 payload := `{"version":1,"id":"test-id","source":"https://authsignal.com","time":"2025-02-20T01:47:17.248Z","tenantId":"test-tenant","type":"email.created","data":{}}`
@@ -176,7 +168,6 @@ func TestValidSignatureWithOldKeyFirst(t *testing.T) {
176168 mac .Write ([]byte (hmacContent ))
177169 validSig := strings .ReplaceAll (base64 .StdEncoding .EncodeToString (mac .Sum (nil )), "=" , "" )
178170
179- // Put the invalid signature FIRST, valid signature SECOND
180171 signature := fmt .Sprintf ("t=%d,v2=invalidOldKeySignature,v2=%s" , timestamp , validSig )
181172
182173 event , err := webhook .ConstructEvent (payload , signature , tolerance )
@@ -219,7 +210,6 @@ func TestConstructEventWithDefaultTolerance(t *testing.T) {
219210 payload := "{}"
220211 signature := "t=1630000000,v2=invalid_signature"
221212
222- // This should fail due to timestamp being outside tolerance
223213 _ , err := webhook .ConstructEventWithDefaultTolerance (payload , signature )
224214
225215 if err == nil {
@@ -242,7 +232,6 @@ func TestConstructEventWithDefaultToleranceValid(t *testing.T) {
242232 webhook := getTestWebhook ()
243233 payload := `{"version":1,"type":"test.event","id":"123","source":"test","time":"2025-01-01T00:00:00Z","tenantId":"tenant","data":{}}`
244234
245- // Generate a signature with current timestamp (within tolerance)
246235 timestamp := time .Now ().Unix ()
247236 signature := generateTestSignature (payload , timestamp , testSecretKey )
248237
@@ -266,7 +255,6 @@ func TestConstructEventWithDefaultToleranceValid(t *testing.T) {
266255func TestMissingTimestamp (t * testing.T ) {
267256 webhook := getTestWebhook ()
268257 payload := "{}"
269- // Signature without timestamp
270258 signature := "v2=someSignature"
271259
272260 _ , err := webhook .ConstructEvent (payload , signature , DefaultTolerance )
@@ -290,7 +278,6 @@ func TestMissingTimestamp(t *testing.T) {
290278func TestMissingSignature (t * testing.T ) {
291279 webhook := getTestWebhook ()
292280 payload := "{}"
293- // Signature with timestamp but no v2 signature
294281 signature := "t=1234567890"
295282
296283 _ , err := webhook .ConstructEvent (payload , signature , DefaultTolerance )
@@ -313,7 +300,6 @@ func TestMissingSignature(t *testing.T) {
313300
314301func TestInvalidJSON (t * testing.T ) {
315302 webhook := getTestWebhook ()
316- // Invalid JSON payload
317303 payload := "not valid json"
318304
319305 timestamp := time .Now ().Unix ()
@@ -326,7 +312,6 @@ func TestInvalidJSON(t *testing.T) {
326312 return
327313 }
328314
329- // Should be a JSON unmarshal error, not InvalidSignatureError
330315 _ , ok := err .(* InvalidSignatureError )
331316 if ok {
332317 t .Error ("Expected JSON error, not InvalidSignatureError" )
@@ -337,11 +322,9 @@ func TestTimestampAtExactTolerance(t *testing.T) {
337322 webhook := getTestWebhook ()
338323 payload := `{"version":1,"type":"test","id":"1","source":"test","time":"2025-01-01T00:00:00Z","tenantId":"t","data":{}}`
339324
340- // Timestamp exactly at the tolerance boundary (5 minutes ago)
341325 timestamp := time .Now ().Unix () - (DefaultTolerance * 60 )
342326 signature := generateTestSignature (payload , timestamp , testSecretKey )
343327
344- // Should still be valid (boundary is inclusive)
345328 event , err := webhook .ConstructEvent (payload , signature , DefaultTolerance )
346329
347330 if err != nil {
@@ -358,7 +341,6 @@ func TestTimestampJustOutsideTolerance(t *testing.T) {
358341 webhook := getTestWebhook ()
359342 payload := "{}"
360343
361- // Timestamp just outside the tolerance (5 minutes + 1 second ago)
362344 timestamp := time .Now ().Unix () - (DefaultTolerance * 60 + 1 )
363345 signature := generateTestSignature (payload , timestamp , testSecretKey )
364346
@@ -384,11 +366,9 @@ func TestZeroTolerance(t *testing.T) {
384366 webhook := getTestWebhook ()
385367 payload := `{"version":1,"type":"test","id":"1","source":"test","time":"2025-01-01T00:00:00Z","tenantId":"t","data":{}}`
386368
387- // Old timestamp
388369 timestamp := int64 (1630000000 )
389370 signature := generateTestSignature (payload , timestamp , testSecretKey )
390371
391- // With tolerance=0, timestamp check should be skipped (like tolerance=-1)
392372 event , err := webhook .ConstructEvent (payload , signature , 0 )
393373
394374 if err != nil {
@@ -402,7 +382,6 @@ func TestZeroTolerance(t *testing.T) {
402382}
403383
404384func TestSignatureWithEqualsInValue (t * testing.T ) {
405- // Test that signatures containing '=' in the value part are parsed correctly
406385 webhook := getTestWebhook ()
407386 payload := `{"version":1,"type":"test","id":"1","source":"test","time":"2025-01-01T00:00:00Z","tenantId":"t","data":{}}`
408387
0 commit comments