-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathjwt_test.go
817 lines (728 loc) · 25.4 KB
/
jwt_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
package caddyjwt
import (
"crypto/ed25519"
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jwt"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
type MapClaims map[string]interface{}
var (
testLogger, _ = zap.NewDevelopment()
// Symmetric
RawTestSignKey = []byte("NFL5*0Bc#9U6E@tnmC&E7SUN6GwHfLmY")
TestSignKey = base64.StdEncoding.EncodeToString(RawTestSignKey)
// Asymmetric
TestPubKey = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArzekF0pqttKNJMOiZeyt
RdYiabdyy/sdGQYWYJPGD2Q+QDU9ZqprDmKgFOTxUy/VUBnaYr7hOEMBe7I6dyaS
5G0EGr8UXAwgD5Uvhmz6gqvKTV+FyQfw0bupbcM4CdMD7wQ9uOxDdMYm7g7gdGd6
SSIVvmsGDibBI9S7nKlbcbmciCmxbAlwegTYSHHLjwWvDs2aAF8fxeRfphwQZKkd
HekSZ090/c2V4i0ju2M814QyGERMoq+cSlmikCgRWoSZeWOSTj+rAZJyEAzlVL4z
8ojzOpjmxw6pRYsS0vYIGEDuyiptf+ODC8smTbma/p3Vz+vzyLWPfReQY2RHtpUe
hwIDAQAB
-----END PUBLIC KEY-----`
// JWK URL
TestJWKURL = "http://127.0.0.1:2546/key"
TestJWKSetURL = "http://127.0.0.1:2546/keys"
TestJWKSetURLInapplicable = "http://127.0.0.1:2546/keys_inapplicable"
jwkKey jwk.Key // private key
jwkPubKey jwk.Key // public key
jwkPubKeySet jwk.Set // public key set
jwkPubKeySetInapplicable jwk.Set // public key set (inapplicable)
// EdDSA test
jwkKeyEd25519 jwk.Key // private key for EdDSA test
TestSignKeyEd25519 string // base64 encoded public key
)
func init() {
var err error
jwkKey = generateJWK()
jwkPubKey, err = jwkKey.PublicKey()
panicOnError(err)
anotherPubKeyI, err := generateJWK().PublicKey()
panicOnError(err)
anotherPubKeyII, err := generateJWK().PublicKey()
panicOnError(err)
jwkKeyEd25519 = generateEdDSAJWK()
jwkPubKeySet = jwk.NewSet()
jwkPubKeySet.AddKey(anotherPubKeyI)
jwkPubKeySet.AddKey(jwkPubKey)
jwkPubKeySetInapplicable = jwk.NewSet()
jwkPubKeySetInapplicable.AddKey(anotherPubKeyI)
jwkPubKeySetInapplicable.AddKey(anotherPubKeyII)
publishJWKsOnLocalServer()
}
func generateJWK() jwk.Key {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
panicOnError(err)
key, err := jwk.FromRaw(privateKey)
panicOnError(err)
jwk.AssignKeyID(key) // set "kid"
key.Set(jwk.AlgorithmKey, jwa.RS256) // set "alg"
key.Set(jwk.KeyUsageKey, jwk.ForSignature) // set "use"
return key
}
func generateEdDSAJWK() jwk.Key {
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
panicOnError(err)
TestSignKeyEd25519 = base64.StdEncoding.EncodeToString(publicKey)
key, err := jwk.FromRaw(privateKey)
panicOnError(err)
jwk.AssignKeyID(key)
key.Set(jwk.AlgorithmKey, jwa.EdDSA)
key.Set(jwk.KeyUsageKey, jwk.ForSignature)
return key
}
func publishJWKsOnLocalServer() {
go func() {
http.HandleFunc("/key", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(jwkPubKey)
})
http.HandleFunc("/keys", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(jwkPubKeySet)
})
http.HandleFunc("/keys_inapplicable", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(jwkPubKeySetInapplicable)
})
panicOnError(http.ListenAndServe("127.0.0.1:2546", nil))
}()
}
func panicOnError(err error) {
if err != nil {
panic(err)
}
}
func buildToken(claims MapClaims) jwt.Token {
tb := jwt.NewBuilder()
for k, v := range claims {
tb = tb.Claim(k, v)
}
token, err := tb.Build()
panicOnError(err)
return token
}
// issueTokenString issues a token string with the given claims,
// using HS256 signing algorithm.
func issueTokenString(claims MapClaims) string {
token := buildToken(claims)
tokenBytes, err := jwt.Sign(token, jwt.WithKey(jwa.HS256, RawTestSignKey))
panicOnError(err)
return string(tokenBytes)
}
// issueTokenString issues a token string with the given claims,
// using EdDSA signing algorithm.
func issueTokenStringEdDSA(claims MapClaims) string {
token := buildToken(claims)
tokenBytes, err := jwt.Sign(token, jwt.WithKey(jwa.EdDSA, jwkKeyEd25519))
panicOnError(err)
return string(tokenBytes)
}
func issueTokenStringJWK(claims MapClaims) string {
token := buildToken(claims)
tokenBytes, err := jwt.Sign(token, jwt.WithKey(jwa.RS256, jwkKey))
panicOnError(err)
return string(tokenBytes)
}
func TestValidate_SignKey(t *testing.T) {
// missing sign_key
ja := &JWTAuth{}
err := ja.Validate()
assert.NotNil(t, err)
assert.ErrorIs(t, err, ErrMissingKeys)
// having sign_key
ja = &JWTAuth{
SignKey: TestSignKey,
}
assert.Nil(t, ja.Validate())
}
func TestValidate_SignAlg(t *testing.T) {
// invalid sign_alg
ja := &JWTAuth{
SignKey: TestSignKey,
SignAlgorithm: "ABC",
}
assert.ErrorIs(t, ja.Validate(), ErrInvalidSignAlgorithm)
}
func TestValidate_usingJWK(t *testing.T) {
ja := &JWTAuth{JWKURL: TestJWKSetURL, logger: testLogger}
assert.True(t, ja.usingJWK())
err := ja.Validate()
assert.Nil(t, err)
}
// TestValidate_SkipVerification checks that validation does not fail when
// SkipVerification is enabled and no keys are provided. This ensures that
// enabling SkipVerification bypasses signature and keys validation without errors.
func TestValidate_SkipVerification(t *testing.T) {
// skipping verification
ja := &JWTAuth{
SkipVerification: true,
}
assert.NoError(t, ja.Validate())
}
func TestValidate_InvalidMetaClaims(t *testing.T) {
ja := &JWTAuth{
SignKey: TestSignKey,
MetaClaims: map[string]string{
"IsAdmin": "",
},
}
assert.Contains(t, ja.Validate().Error(), "invalid meta claim")
}
func TestAuthenticate_FromAuthorizationHeader(t *testing.T) {
claims := MapClaims{"sub": "ggicci"}
ja := &JWTAuth{SignKey: TestSignKey, logger: testLogger}
assert.Nil(t, ja.Validate())
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", "Bearer "+issueTokenString(claims))
gotUser, authenticated, err := ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "ggicci"}, gotUser)
}
func TestAuthenticate_EdDSA(t *testing.T) {
claims := MapClaims{"sub": "ggicci"}
ja := &JWTAuth{SignKey: TestSignKeyEd25519, SignAlgorithm: string(jwa.EdDSA), logger: testLogger}
assert.Nil(t, ja.Validate())
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", "Bearer "+issueTokenStringEdDSA(claims))
gotUser, authenticated, err := ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "ggicci"}, gotUser)
}
func TestAuthenticate_FromCustomHeader(t *testing.T) {
claims := MapClaims{"sub": "ggicci"}
ja := &JWTAuth{
SignKey: TestSignKey,
FromHeader: []string{"X-Api-Token"},
logger: testLogger,
}
assert.Nil(t, ja.Validate())
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("x-api-token", issueTokenString(claims))
gotUser, authenticated, err := ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "ggicci"}, gotUser)
}
func TestAuthenticate_FromQueryWithSkipVerification(t *testing.T) {
var (
claims = MapClaims{"sub": "ggicci"}
ja = &JWTAuth{
FromQuery: []string{"access_token", "token"},
SkipVerification: true,
logger: testLogger,
}
tokenString = issueTokenString(claims)
err error
rw *httptest.ResponseRecorder
r *http.Request
params url.Values
gotUser User
authenticated bool
)
assert.Nil(t, ja.Validate())
// trying "access_token" without signature key
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
params = make(url.Values)
params.Add("access_token", tokenString)
r.URL.RawQuery = params.Encode()
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "ggicci"}, gotUser)
// invalid "token" without signature key
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
params = make(url.Values)
params.Add("access_token", tokenString+"INVALID")
params.Add("token", tokenString)
r.URL.RawQuery = params.Encode()
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "ggicci"}, gotUser)
}
func TestAuthenticate_PopulateUserMetadataWithSkipVerification(t *testing.T) {
ja := &JWTAuth{
SkipVerification: true,
MetaClaims: map[string]string{
"jti": "jti",
"IsAdmin": "is_admin",
"settings.role": "role",
"settings.payout.paypal.enabled": "is_paypal_enabled",
},
logger: testLogger,
}
assert.Nil(t, ja.Validate())
claimsWithMetadata := MapClaims{
"jti": "a976475a-186a-4c1f-b182-95b3f886e2b4",
"sub": "ggicci",
"IsAdmin": true,
"settings": map[string]interface{}{
"role": "admin",
"payout": map[string]interface{}{
"paypal": map[string]interface{}{
"enabled": true,
},
},
},
}
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(claimsWithMetadata))
gotUser, authenticated, err := ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, "a976475a-186a-4c1f-b182-95b3f886e2b4", gotUser.Metadata["jti"])
assert.Equal(t, "true", gotUser.Metadata["is_admin"])
assert.Equal(t, "admin", gotUser.Metadata["role"])
assert.Equal(t, "true", gotUser.Metadata["is_paypal_enabled"])
}
func TestAuthenticate_FromQuery(t *testing.T) {
var (
claims = MapClaims{"sub": "ggicci"}
ja = &JWTAuth{
SignKey: TestSignKey,
FromQuery: []string{"access_token", "token"},
logger: testLogger,
}
tokenString = issueTokenString(claims)
err error
rw *httptest.ResponseRecorder
r *http.Request
params url.Values
gotUser User
authenticated bool
)
assert.Nil(t, ja.Validate())
// only "access_token"
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
params = make(url.Values)
params.Add("access_token", tokenString)
r.URL.RawQuery = params.Encode()
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "ggicci"}, gotUser)
// only "token"
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
params = make(url.Values)
params.Add("token", tokenString)
r.URL.RawQuery = params.Encode()
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "ggicci"}, gotUser)
// both valid "access_token", "token"
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
params = make(url.Values)
params.Add("access_token", tokenString)
params.Add("token", tokenString)
r.URL.RawQuery = params.Encode()
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "ggicci"}, gotUser)
// invalid "access_token", and valid "token"
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
params = make(url.Values)
params.Add("access_token", tokenString+"INVALID")
params.Add("token", tokenString)
r.URL.RawQuery = params.Encode()
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "ggicci"}, gotUser)
// both invalid "access_token", "token"
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
params = make(url.Values)
params.Add("access_token", tokenString+"INVALID")
params.Add("token", tokenString+"INVALID")
r.URL.RawQuery = params.Encode()
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.NotNil(t, err)
assert.False(t, authenticated)
assert.NotEqual(t, User{ID: "ggicci"}, gotUser)
}
func TestAuthenticate_FromCookies(t *testing.T) {
claims := MapClaims{"sub": "ggicci"}
ja := &JWTAuth{
SignKey: TestSignKey,
FromCookies: []string{"user_session", "sess"},
logger: testLogger,
}
assert.Nil(t, ja.Validate())
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
r.AddCookie(&http.Cookie{Name: "user_session", Value: issueTokenString(claims)})
gotUser, authenticated, err := ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "ggicci"}, gotUser)
}
func TestAuthenticate_CustomUserClaims(t *testing.T) {
claims := MapClaims{"sub": "182140474727", "username": "ggicci"}
ja := &JWTAuth{
SignKey: TestSignKey,
UserClaims: []string{"username"},
logger: testLogger,
}
assert.Nil(t, ja.Validate())
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(claims))
gotUser, authenticated, err := ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "ggicci"}, gotUser)
// custom user claims all empty should fail - having keys
claims = MapClaims{"sub": "ggicci", "username": ""}
ja = &JWTAuth{
SignKey: TestSignKey,
UserClaims: []string{"username"},
logger: testLogger,
}
assert.Nil(t, ja.Validate())
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(claims))
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.NotNil(t, err)
assert.False(t, authenticated)
assert.Empty(t, gotUser.ID)
// custom user claims all empty should fail - even no keys
claims = MapClaims{"username": "ggicci"}
ja = &JWTAuth{
SignKey: TestSignKey,
UserClaims: []string{"uid", "user_id"},
logger: testLogger,
}
assert.Nil(t, ja.Validate())
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(claims))
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.NotNil(t, err)
assert.False(t, authenticated)
assert.Empty(t, gotUser.ID)
// custom user claims at least one is non-empty can work
claims = MapClaims{"username": "ggicci", "user_id": nil, "uid": 19911110}
ja = &JWTAuth{
SignKey: TestSignKey,
UserClaims: []string{"user_id", "uid"},
logger: testLogger,
}
assert.Nil(t, ja.Validate())
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(claims))
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "19911110"}, gotUser)
}
func TestAuthenticate_ValidateStandardClaims(t *testing.T) {
ja := &JWTAuth{
SignKey: TestSignKey,
logger: testLogger,
}
assert.Nil(t, ja.Validate())
// invalid "exp" (Expiration Time)
expiredClaims := MapClaims{"sub": "ggicci", "exp": 689702400}
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(expiredClaims))
gotUser, authenticated, err := ja.Authenticate(rw, r)
assert.NotNil(t, err)
assert.False(t, authenticated)
assert.Empty(t, gotUser.ID)
// invalid "iat" (Issued At)
expiredClaims = MapClaims{"sub": "ggicci", "iat": 3845462400}
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(expiredClaims))
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.NotNil(t, err)
assert.False(t, authenticated)
assert.Empty(t, gotUser.ID)
// invalid "nbf" (Not Before)
expiredClaims = MapClaims{"sub": "ggicci", "nbf": 3845462400}
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(expiredClaims))
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.NotNil(t, err)
assert.False(t, authenticated)
assert.Empty(t, gotUser.ID)
}
func TestAuthenticate_VerifyIssuerWhitelist(t *testing.T) {
ja := &JWTAuth{
SignKey: TestSignKey,
logger: testLogger,
IssuerWhitelist: []string{"https://api.example.com", "https://api.github.com"},
}
assert.Nil(t, ja.Validate())
// valid "iss"
exampleClaims := MapClaims{"sub": "ggicci", "iss": "https://api.example.com"}
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(exampleClaims))
gotUser, authenticated, err := ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, gotUser.ID, "ggicci")
githubClaims := MapClaims{"sub": "ggicci", "iss": "https://api.github.com"}
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(githubClaims))
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, gotUser.ID, "ggicci")
// invalid "iss" (no iss)
noIssClaims := MapClaims{"sub": "ggicci"}
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(noIssClaims))
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.NotNil(t, err)
assert.False(t, authenticated)
assert.Empty(t, gotUser.ID)
// invalid "iss" (wrong value)
wrongIssClaims := MapClaims{"sub": "ggicci", "iss": "https://api.example.com/secure"}
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(wrongIssClaims))
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.NotNil(t, err)
assert.False(t, authenticated)
assert.Empty(t, gotUser.ID)
}
func TestAuthenticate_VerifyAudienceWhitelist(t *testing.T) {
ja := &JWTAuth{
SignKey: TestSignKey,
logger: testLogger,
IssuerWhitelist: []string{"https://api.github.com"},
AudienceWhitelist: []string{"https://api.codelet.io", "https://api.copilot.codelet.io"},
}
assert.Nil(t, ja.Validate())
// valid "aud" (of single string)
githubClaims := MapClaims{
"sub": "ggicci",
"iss": "https://api.github.com",
"aud": "https://api.codelet.io",
}
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(githubClaims))
gotUser, authenticated, err := ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, gotUser.ID, "ggicci")
// valid "aud" (multiple, as long as one of them is on the whitelist)
githubClaims = MapClaims{
"sub": "ggicci",
"iss": "https://api.github.com",
"aud": []string{"https://api.learn.codelet.io", "https://api.copilot.codelet.io"},
}
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(githubClaims))
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, gotUser.ID, "ggicci")
// invalid "aud" (no aud)
noIssClaims := MapClaims{"sub": "ggicci", "iss": "https://api.github.com"}
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(noIssClaims))
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.NotNil(t, err)
assert.False(t, authenticated)
assert.Empty(t, gotUser.ID)
// invalid "aud" (wrong value)
wrongIssClaims := MapClaims{
"sub": "ggicci",
"iss": "https://api.github.com",
"aud": []string{"https://api.example.com", "https://api.example.org"},
}
rw = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(wrongIssClaims))
gotUser, authenticated, err = ja.Authenticate(rw, r)
assert.NotNil(t, err)
assert.False(t, authenticated)
assert.Empty(t, gotUser.ID)
}
func TestAuthenticate_PopulateUserMetadata(t *testing.T) {
ja := &JWTAuth{
SignKey: TestSignKey,
MetaClaims: map[string]string{
"jti": "jti",
"IsAdmin": "is_admin",
"registerTime": "registered_at",
"absent": "absent", // not found in JWT payload, final ""
"groups": "groups", // supported array type, final "csgo,dota2"
"settings.role": "role", // supported nested claim, final "admin"
"settings.payout.paypal.enabled": "is_paypal_enabled",
"settings.payout.alipay.enabled": "is_alipay_enabled",
},
logger: testLogger,
}
assert.Nil(t, ja.Validate())
claimsWithMetadata := MapClaims{
"jti": "a976475a-186a-4c1f-b182-95b3f886e2b4",
"sub": "ggicci",
"IsAdmin": true,
"registerTime": time.Date(2000, 1, 2, 15, 23, 18, 0, time.UTC),
"groups": []string{"csgo", "dota2"},
"settings": map[string]interface{}{
"role": "admin",
"payout": map[string]interface{}{
"paypal": map[string]interface{}{
"enabled": true,
},
},
},
}
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", issueTokenString(claimsWithMetadata))
gotUser, authenticated, err := ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, "ggicci", gotUser.ID)
assert.Equal(t, "a976475a-186a-4c1f-b182-95b3f886e2b4", gotUser.Metadata["jti"])
assert.Equal(t, "true", gotUser.Metadata["is_admin"])
assert.Equal(t, "2000-01-02T15:23:18Z", gotUser.Metadata["registered_at"])
assert.Equal(t, "", gotUser.Metadata["absent"])
assert.Equal(t, "csgo,dota2", gotUser.Metadata["groups"])
assert.Equal(t, "admin", gotUser.Metadata["role"])
assert.Equal(t, "true", gotUser.Metadata["is_paypal_enabled"])
assert.Equal(t, "", gotUser.Metadata["is_alipay_enabled"])
}
type ThingNotStringer struct{}
type ThingIsStringer struct{}
func (t ThingIsStringer) String() string { return "i'm stringer" }
func Test_stringify(t *testing.T) {
now := time.Now()
for _, c := range []struct {
Input interface{}
Expected string
}{
{nil, ""},
{"abc", "abc"},
{true, "true"},
{false, "false"},
{json.Number("1991"), "1991"},
{now, now.UTC().Format(time.RFC3339Nano)},
{[]int{1, 2, 3}, ""}, // unsupported array type
{ThingNotStringer{}, ""}, // unsupported custom type
{ThingIsStringer{}, "i'm stringer"}, // support fmt.Stringer interface
} {
assert.Equal(t, stringify(c.Input), c.Expected)
}
}
func Test_desensitizedTokenString(t *testing.T) {
for _, c := range []struct {
Input string
Expected string
}{
{"", ""},
{"abc", "abc"},
{"abcdef", "abcdef"},
{"abcdefg", "ab…fg"},
{"abcdefeijk", "abc…ijk"},
{"abcdefghijklmnopqrstuvwxyz", "abcdefgh…stuvwxyz"},
{"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv", "abcdefghijklmnop…ghijklmnopqrstuv"},
{
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
"abcdefghijklmnop…klmnopqrstuvwxyz",
},
{
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
"abcdefghijklmnop…klmnopqrstuvwxyz",
},
} {
assert.Equal(t, desensitizedTokenString(c.Input), c.Expected)
}
}
func Test_AsymmetricAlgorithm(t *testing.T) {
ja := &JWTAuth{SignKey: TestPubKey, UserClaims: []string{"login"}, logger: testLogger}
assert.Nil(t, ja.Validate())
token := "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIzMDc3NTU1IiwibG9naW4iOiJnZ2ljY2kiLCJkaXNwbGF5IjoiR2dpY2NpIiwiYWRtaW4iOmZhbHNlfQ.eOXRUSS-WSebEobZgqmui9VlKentHW5IxQpWR5xGu-u9svzdWJnGqLbnKBeIy42tQkFHNDWUx4R2z8Jv3ZPByN1qvWYIloJ8vLQsb0GsfXoqOPkhsfAzkOEp0m5Ws83ar9TT83MLQrUisKU-WjRZTOid9Hfe2atKN4h74vqpNMUfdRZ4NOZtBTmKjoRdWwNBmM5kg59b_cUKNR9Ruab0dwI72_svFZaNiRzBXLTTOVP2Xn0wk_mavyo4dhP83P66mefSYNkoA4_xft3iG43Zkta5lnjV-EF9fACG8g4pugytDGAgGBsOoKZagIqDdNqQWo1e4CLP4G2kMTfGqlosLQ"
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", "Bearer "+token)
gotUser, authenticated, err := ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "ggicci"}, gotUser)
}
func Test_AsymmetricAlgorithm_InvalidPubKey(t *testing.T) {
ja := &JWTAuth{SignKey: `-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAA ... invalid\n-----END PUBLIC KEY-----`, UserClaims: []string{"login"}, logger: testLogger}
assert.ErrorIs(t, ja.Validate(), ErrInvalidPublicKey)
}
func TestJWK(t *testing.T) {
time.Sleep(3 * time.Second)
ja := &JWTAuth{JWKURL: TestJWKURL, logger: testLogger}
assert.Nil(t, ja.Validate())
assert.Equal(t, 1, ja.jwkCachedSet.Len())
token := issueTokenStringJWK(MapClaims{"sub": "ggicci"})
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", "Bearer "+token)
gotUser, authenticated, err := ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "ggicci"}, gotUser)
}
func TestJWKSet(t *testing.T) {
time.Sleep(3 * time.Second)
ja := &JWTAuth{JWKURL: TestJWKSetURL, logger: testLogger}
assert.Nil(t, ja.Validate())
assert.Equal(t, 2, ja.jwkCachedSet.Len())
token := issueTokenStringJWK(MapClaims{"sub": "ggicci"})
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", "Bearer "+token)
gotUser, authenticated, err := ja.Authenticate(rw, r)
assert.Nil(t, err)
assert.True(t, authenticated)
assert.Equal(t, User{ID: "ggicci"}, gotUser)
}
func TestJWKSet_KeyNotFound(t *testing.T) {
time.Sleep(3 * time.Second)
ja := &JWTAuth{JWKURL: TestJWKSetURLInapplicable, logger: testLogger}
assert.Nil(t, ja.Validate())
assert.Equal(t, 2, ja.jwkCachedSet.Len())
token := issueTokenStringJWK(MapClaims{"sub": "ggicci"})
rw := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("Authorization", "Bearer "+token)
gotUser, authenticated, err := ja.Authenticate(rw, r)
assert.Error(t, err)
assert.False(t, authenticated)
assert.Empty(t, gotUser.ID)
}