forked from MicahParks/keyfunc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwks_test.go
789 lines (700 loc) · 45.9 KB
/
jwks_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
package keyfunc_test
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
// _ "embed"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/golang-jwt/jwt"
keyfunc "github.com/pmishchenko-ua/jwt-keyfunc"
)
const (
// emptyJWKSJSON is a hard-coded empty JWKS in JSON format.
emptyJWKSJSON = `{"keys":[]}`
// logFmt is an error log formatting string.
logFmt = "%s\nError: %s"
// jwksFilePath is the full path of th JWKS file on the test HTTP server.
jwksFilePath = "/example_jwks.json"
// tokenUseEnc is a token encrypted with a key whose "use" parameter is "enc".
tokenUseEnc = "eyJhbGciOiJSUzI1NiIsImtpZCI6ImtpZFdpdGhCYWRVc2UiLCJ0eXAiOiJKV1QifQ.eyJmb28iOiJiYXIifQ.NKUjRgfqNZNCckL2yyiZc0ot_-BxtwYiknrILmsSnNapkOB32gMfRPTyc_j-UsIqw19FrDSBNk31blxSW40X3ubXp56hpwbcqE0nj9EvDyZoUWmtMl6pXIGPnTK5y-rNgS8i1IeeejNAQDYe8LOtCw_jE8CpOW5MBZzxdwjntPHGCWu4FCgrBu1ugth20B7WnuCHETa0xQ2NvXIX0W54JDbk_hdWTqjP4Bo7BvcGB6-5xZ1AaiiXjnOOuBrIMwTrZ-wtdTOjmSaWrcH94A8wDk263fSkhRLjM77d5IljIILT4a6nRHVSsgBfhblYevtX6NWBgllvQ_Hr_uuaT_b15A"
)
var (
// jwksJSON is a embedded JWKS in JSON format.
//go:embed example_jwks.json
jwksJSON string = `{
"keys": [
{
"kid": "zXew0UJ1h6Q4CCcd_9wxMzvcp5cEBifH0KWrCz2Kyxc",
"kty": "RSA",
"alg": "PS256",
"use": "sig",
"n": "wqS81x6fItPUdh1OWCT8p3AuLYgFlpmg61WXp6sp1pVijoyF29GOSaD9xE-vLtegX-5h0BnP7va0bwsOAPdh6SdeVslEifNGHCtID0xNFqHNWcXSt4eLfQKAPFUq0TsEO-8P1QHRq6yeG8JAFaxakkaagLFuV8Vd_21PGJFWhvJodJLhX_-Ym9L8XUpIPps_mQriMUOWDe-5DWjHnDtfV7mgaOxbBvVo3wj8V2Lmo5Li4HabT4MEzeJ6e9IdFo2kj_44Yy9osX-PMPtu8BQz_onPgf0wjrVWt349Rj6OkS8RxlNGYeuIxYZr0TOhP5F-yEPhSXDsKdVTwPf7zAAaKQ",
"e": "AQAB",
"x5c": [
"MIICmzCCAYMCBgF4HR7HNDANBgkqhkiG9w0BAQsFADARMQ8wDQYDVQQDDAZtYXN0ZXIwHhcNMjEwMzEwMTcwOTE5WhcNMzEwMzEwMTcxMDU5WjARMQ8wDQYDVQQDDAZtYXN0ZXIwggEiMA0GCSqGSIb3DQE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ss7Vm_ISWenNbdjKOsY1XvtiQz5scGzWDCEUoZzgV8Ve1mLOV0"
},
{
"kid": "TVAAet63O3xy_KK6_bxVIu7Ra3_z1wlB543Fbwi5VaU",
"kty": "EC",
"alg": "ES384",
"use": "sig",
"crv": "P-384",
"x": "Pik2o5as-evijFABH5p6YLXHnWw8iQ_N1ummPY1c_UgG6NO0za-gNOhTz2-tsd_w",
"y": "e98VSff71k19SY_mHgp3707lgQVrhfVpiGa-sGaKxOWVpxd2jWMhB0Q4RpSRuCp5"
},
{
"kid": "arlUxX4hh56rNO-XdIPhDT7bqBMqcBwNQuP_TnZJNGs",
"kty": "RSA",
"alg": "RS512",
"use": "sig",
"n": "hhtifu8LL3ICE3BAX5l1KZv6Lni0lhlhBusSfepnpxcb4C_z2U71cQTnLY27kt8WB4bNG6e5_KMx9K3xUdd3euj9MCq8vytwEPieeHE1KXQuhJfLv017lhpK_dRMOHyc-9-50YNdgs_8KWRkrzjjuYrCiO9Iu76n5319e-SC8OPvNUglqxp2N0Sp2ltne2ZrpN8T3OEEXT62TSGmLAVopRGw5gllNVrJfmEyZJCRrBM6s5CQcz8un0FjkAAC4DI6QD-eBL0qG3_NR0hQvR1he2o4BLwjOKH45Pk_jj-eArp-DD6Xq6ABQVb5SNOSdaxl5lnmuotRoY3G5d9YSl-K3w",
"e": "AQAB",
"x5c": [
"MIICmzCCAYMCBgF4HSCcDzANBgkqhkiG9w0BAQsFADARMQ8wDQYDVQQDDAZtYXN0ZXIwHhcNMjEwMzEwMTcxMTE5WhcNMzEwMzEwMTcxMjU5WjARMQ8wDQYDVQQDDAZtYXN0ZXIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCGG2J+7wsvcgITcEBfmXUpm/oueLSWGWEG6xJ96menFxvgL/PZTvVxBOctjbuS3xYHhs0bp7n8ozH0rfFR13d66P0wKry/K3AQ+J54cTUpdC6El8u/TXuWGkr91Ew4fJz737nRg12Cz/wpZGSvOOO5isKI70i7vqfnfX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ys6AUaeQ1T1yxzdnlqKEdzBGYL+hLs9n1Vz6XTEELIXdhEcj5qXymlIfAWt47B4Xf4k/emg0Bd33Tr+2NfYqUdImY2pxQ2JJ8SW7IKnTYAuVEUpdRDUeV9K6kdwfJIOVqd9TIjNmAz3KwK8LW9810C7BnjihQEy6X9c/HGHOV7AvZjw+1IMtQNYZMii7wzzTj0PxAgMBAAEwDQYJKoZIhvcNAQELBQADggEBABoThxhMd7Xiq4x0GJeoJFv2yDKXCL3dJEAEWtOr2+PqdeJl/ZfOxBXynIvrdtYnQdICztN5ydEgDsZ02piDsxZ+s/0SA0iqjw/MEoBYobmr8V+xwUv+WtRLpTBXqWGMuG7NEtrbjKid0iKLLAOAU4dcHQ49iOF9VLnbTkf1EXp4iphJreaubOXMwT6/JDzQPT1dRR34hlhYeKKzMSA0Cz5aYL1tI+eH12rar0MDczXykLChNS/8MlyTzreEf0siUiS9S1kj/lOZKQDg9E/z8fm5vmHEHzAVwf4ON5iO29tDsqLw7BeJqC4AESjliXIqMrdpFynfPnIsGgf3dnph5BM="
],
"x5t": "CmRnQVduZWtEsdOC4mauUUsSWxA",
"x5t#S256": "BvC0LmuM8ZIApN3TQQZWWbGO-d082Ah5d3D6vPvahGw"
},
{
"kid": "CGt0ZWS4Lc5faiKSdi0tU0fjCAdvGROQRGU9iR7tV0A",
"kty": "EC",
"alg": "ES256",
"use": "sig",
"crv": "P-256",
"x": "DPW7n9yjfE6Rt-VvVmEdeu4QdW44qifocAPPDxACDDY",
"y": "-ejsVw8222-hg2dJWx3QV0hE4-I0Ujp7ZsWebE68JE0"
},
{
"kid": "C65q0EKQyhpd1m4fr7SKO2He_nAxgCtAdws64d2BLt8",
"kty": "RSA",
"alg": "RS256",
"use": "sig",
"n": "ja99ybDrLvw11Z4CvNlDI-kkqJEBpSnvDf0pZF2DvBlvYmeVYL_ChqIe8E9GyHUmLMdtO_jifSgOqE5b8vILwi1kZnJR7N857uEnbWM9YTeevi_RZ-E_hr4frW2NKJ78YG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HJViXeiGoxhpKnc8ryISpRUBzsKngf5uZo3bnrD9PHLYBoGOHgzII1xw="
],
"x5t": "5GNr3LeRXHWI4YR8-QTSsF98oTI",
"x5t#S256": "Dgd0_wZZqvRuf4GEISPNHREX-1ixTMIsrPeGzk0bCxs"
},
{
"kty": "OKP",
"d": "TJ0UPkOZDPfneEDSH2ETbLQWjrALD-BPZQR-E7mgPvY",
"use": "sig",
"crv": "Ed25519",
"kid": "Q56A",
"x": "iZli54E2SkbrOvAThwrnxn1AMIOaazi_ckl6B-hbDK8"
},
{
"kty": "oct",
"use": "sig",
"kid": "hmac",
"k": "V_8Ob8dVs6JuZx6expyjShoUgFgxoaovGjmGhesL2jA"
},
{
"e": "AQAB",
"use": "enc",
"kid": "kidWithBadUse",
"kty": "RSA",
"n": "znO8fsURSvghcjbMu2nysqZhsreTkj-y46YL39kctmlj7-qqVLuvTUtw0XvsxwLi9WWczz_BsAm2Rn6LzyhvXUXjj6uMP8tk-HhWc4RMXP-esqB7y6WUmR8SioT94SykuVhWMDxwkg7kXTg_GWEYibEFJ7YM16vVZ2Na5z2vRfMRy7VARXRhDrinJmW0B-oY9FurPTyaZSDqOr-3Qkhk1jm9-6ygFsOkmnd4Ljnq28t8hq_4k3bdZSolZv11boQS8vDO-Fo_2YoQVxm4YMIjcr8bxZcali2slOEytEC5ItOKTPA_CydM62sJubw7MuTrOKh6GJrq0xnw6MtqR46-MQ"
}
]
}
`
)
// TestInvalidServer performs initialization + refresh initialization with a server providing invalid data.
// The test ensures that background refresh goroutine does not cause any trouble in case of init failure.
func TestInvalidServer(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write(nil)
if err != nil {
t.Fatalf(logFmt, "Failed to write empty response.", err)
}
}))
defer server.Close()
testingRefreshErrorHandler := func(err error) {
t.Fatalf(logFmt, "Unhandled JWKS error.", err)
}
refreshInterval := time.Second
options := keyfunc.Options{
RefreshInterval: refreshInterval,
RefreshErrorHandler: testingRefreshErrorHandler,
}
_, err := keyfunc.Get(server.URL, options)
if err == nil {
t.Fatalf("Creation of *keyfunc.JWKS with invalid server must fail.")
}
}
// TestJWKS performs a table test on the JWKS code.
func TestJWKS(t *testing.T) {
tempDir, err := ioutil.TempDir("", "*")
if err != nil {
t.Fatalf(logFmt, "Failed to create a temporary directory.", err)
}
defer func() {
err = os.RemoveAll(tempDir)
if err != nil {
t.Fatalf(logFmt, "Failed to remove temporary directory.", err)
}
}()
jwksFile := filepath.Join(tempDir, jwksFilePath)
err = ioutil.WriteFile(jwksFile, []byte(jwksJSON), 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
server := httptest.NewServer(http.FileServer(http.Dir(tempDir)))
defer server.Close()
testingRefreshInterval := time.Second
testingRateLimit := time.Millisecond * 500
testingRefreshTimeout := time.Second
testingRefreshErrorHandler := func(err error) {
panic(fmt.Sprintf(logFmt, "Unhandled JWKS error.", err))
}
jwksURL := server.URL + jwksFilePath
options := []keyfunc.Options{
{}, // Default options.
{
Client: http.DefaultClient, // Should be ineffectual. Just for code coverage.
},
{
Ctx: context.Background(), // Should be ineffectual. Just for code coverage.
},
{
RefreshErrorHandler: testingRefreshErrorHandler,
},
{
RefreshInterval: testingRefreshInterval,
},
{
RefreshRateLimit: testingRateLimit,
},
{
RefreshTimeout: testingRefreshTimeout,
},
}
for _, opts := range options {
jwks, err := keyfunc.Get(jwksURL, opts)
if err != nil {
t.Fatalf(logFmt, "Failed to get JWKS from testing URL.", err)
}
testCases := []struct {
token string
}{
{""}, // Empty JWT.
{"eyJhbGciOiJFUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJDR3QwWldTNExjNWZhaUtTZGkwdFUwZmpDQWR2R1JPUVJHVTlpUjd0VjBBIn0.eyJleHAiOjE2MTU0MDY4NjEsImlhdCI6MTYxNTQwNjgwMSwianRpIjoiYWVmOWQ5YjItN2EyYy00ZmQ4LTk4MzktODRiMzQ0Y2VmYz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JKV1QifQ.e30.vZ8H2-9j1pDXLNL2GFKbZOkC2qyA0dr7AiTJpNjgLcY"}, // HMAC
}
if opts.RefreshInterval != 0 {
time.Sleep(opts.RefreshInterval)
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("token: %s", tc.token), func(t *testing.T) {
// Use the JWKS jwt.Keyfunc to parse the token.
//
// Don't check for general errors. Unfortunately, an error occurs when a token is expired. All hard
// coded tokens are expired.
_, err = jwt.Parse(tc.token, jwks.Keyfunc)
if err != nil {
if errors.Is(err, jwt.ErrInvalidKeyType) {
t.Fatalf(logFmt, "Invaild key type selected.", err)
}
}
})
}
jwks.EndBackground()
}
}
// TestJWKS_Use tests that JWKs with a use value "enc" are not returned from jwt.Keyfunc.
func TestJWKS_Use(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(jwksJSON))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}))
defer server.Close()
jwksURL := server.URL
opts := keyfunc.Options{
JWKUseWhitelist: []keyfunc.JWKUse{keyfunc.UseOmitted, keyfunc.UseSignature},
}
jwks, err := keyfunc.Get(jwksURL, opts)
if err != nil {
t.Fatalf(logFmt, "Failed to get JWKS from testing URL.", err)
}
token, err := jwt.Parse(tokenUseEnc, jwks.Keyfunc)
if !errors.Is(err, keyfunc.ErrNoMatchingKey) {
t.Fatal(`Failed to return correct error for JWK with "use" parameter value of "enc".`, token.Raw)
}
}
// TestJWKS_UseNoWhitelistOverride tests that JWKUseNoWhitelist option overrides the JWKUseWhitelist option.
func TestJWKS_UseNoWhitelistOverride(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(jwksJSON))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}))
defer server.Close()
jwksURL := server.URL
opts := keyfunc.Options{
JWKUseWhitelist: []keyfunc.JWKUse{keyfunc.UseOmitted, keyfunc.UseSignature},
JWKUseNoWhitelist: true,
}
jwks, err := keyfunc.Get(jwksURL, opts)
if err != nil {
t.Fatalf(logFmt, "Failed to get JWKS from testing URL.", err)
}
_, err = jwt.Parse(tokenUseEnc, jwks.Keyfunc)
if err != nil {
t.Fatalf(logFmt, "The JWKUseNoWhitelist option should override the JWKUseWhitelist option.", err)
}
}
// TestKIDs confirms the JWKS.KIDs returns the key IDs (`kid`) stored in the JWKS.
func TestJWKS_KIDs(t *testing.T) {
jwks, err := keyfunc.NewJSON([]byte(jwksJSON))
if err != nil {
t.Fatalf(logFmt, "Failed to create a JWKS from JSON.", err)
}
expectedKIDs := []string{
"zXew0UJ1h6Q4CCcd_9wxMzvcp5cEBifH0KWrCz2Kyxc",
"ebJxnm9B3QDBljB5XJWEu72qx6BawDaMAhwz4aKPkQ0",
"TVAAet63O3xy_KK6_bxVIu7Ra3_z1wlB543Fbwi5VaU",
"arlUxX4hh56rNO-XdIPhDT7bqBMqcBwNQuP_TnZJNGs",
"tW6ae7TomE6_2jooM-sf9N_6lWg7HNtaQXrDsElBzM4",
"Lx1FmayP2YBtxaqS1SKJRJGiXRKnw2ov5WmYIMG-BLE",
"gnmAfvmlsi3kKH3VlM1AJ85P2hekQ8ON_XvJqs3xPD8",
"CGt0ZWS4Lc5faiKSdi0tU0fjCAdvGROQRGU9iR7tV0A",
"C65q0EKQyhpd1m4fr7SKO2He_nAxgCtAdws64d2BLt8",
"Q56A",
"hmac",
"kidWithBadUse",
}
actual := jwks.KIDs()
actualLen := len(actual)
expectedLen := len(expectedKIDs)
if actualLen != expectedLen {
t.Fatalf("The number of key IDs was not as expected.\n Expected length: %d\n Actual length: %d\n Actual key IDs: %v", expectedLen, actualLen, actual)
}
for _, expectedKID := range expectedKIDs {
found := false
for _, kid := range actual {
if kid == expectedKID {
found = true
break
}
}
if !found {
t.Errorf("Failed to find expected key ID in the slice of key IDs in the JWKS.\n Missing: %s", expectedKID)
}
}
}
// TestJWKS_Len confirms the JWKS.Len returns the number of keys in the JWKS.
func TestJWKS_Len(t *testing.T) {
jwks, err := keyfunc.NewJSON([]byte(jwksJSON))
if err != nil {
t.Fatalf(logFmt, "Failed to create a JWKS from JSON.", err)
}
expectedKIDs := []string{
"zXew0UJ1h6Q4CCcd_9wxMzvcp5cEBifH0KWrCz2Kyxc",
"ebJxnm9B3QDBljB5XJWEu72qx6BawDaMAhwz4aKPkQ0",
"TVAAet63O3xy_KK6_bxVIu7Ra3_z1wlB543Fbwi5VaU",
"arlUxX4hh56rNO-XdIPhDT7bqBMqcBwNQuP_TnZJNGs",
"tW6ae7TomE6_2jooM-sf9N_6lWg7HNtaQXrDsElBzM4",
"Lx1FmayP2YBtxaqS1SKJRJGiXRKnw2ov5WmYIMG-BLE",
"gnmAfvmlsi3kKH3VlM1AJ85P2hekQ8ON_XvJqs3xPD8",
"CGt0ZWS4Lc5faiKSdi0tU0fjCAdvGROQRGU9iR7tV0A",
"C65q0EKQyhpd1m4fr7SKO2He_nAxgCtAdws64d2BLt8",
"Q56A",
"hmac",
"WW91IGdldCBhIGdvbGQgc3RhciDwn4yfCg",
}
actualLen := jwks.Len()
expectedLen := len(expectedKIDs)
if actualLen != expectedLen {
t.Fatalf("The number of key IDs was not as expected.\n Expected length: %d\n Actual length: %d\n", expectedLen, actualLen)
}
}
// TestRateLimit performs a test to confirm the rate limiter works as expected.
func TestRateLimit(t *testing.T) {
tempDir, err := ioutil.TempDir("", "*")
if err != nil {
t.Fatalf(logFmt, "Failed to create a temporary directory.", err)
}
defer func() {
err = os.RemoveAll(tempDir)
if err != nil {
t.Fatalf(logFmt, "Failed to remove temporary directory.", err)
}
}()
refreshes := uint(0)
refreshMux := sync.Mutex{}
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
refreshMux.Lock()
refreshes++
refreshMux.Unlock()
writer.WriteHeader(200)
if _, serveErr := writer.Write([]byte(jwksJSON)); serveErr != nil {
t.Errorf(logFmt, "Failed to serve JWKS.", err)
}
}))
defer server.Close()
jwksURL := server.URL + jwksFilePath
refreshInterval := time.Second
refreshRateLimit := time.Millisecond * 500
refreshTimeout := time.Second
options := keyfunc.Options{
RefreshErrorHandler: func(err error) {
t.Errorf(logFmt, "The package itself had an error.", err)
},
RefreshInterval: refreshInterval,
RefreshRateLimit: refreshRateLimit,
RefreshTimeout: refreshTimeout,
RefreshUnknownKID: true,
}
jwks, err := keyfunc.Get(jwksURL, options)
if err != nil {
t.Fatalf(logFmt, "Failed to create *keyfunc.JWKS.", err)
}
defer jwks.EndBackground()
// Create four JWTs with unknown kids.
//
// These should prompt two refreshes.
// The first one will not be rate limited.
// The second will get a rate limit queue.
// The third will get no rate limit queue and will be ignored because there is already a one in the queue.
// The fourth will get no rate limit queue and will be ignored because there is already a one in the queue.
token1 := "eyJraWQiOiI0NWU3ZDcyMiIsInR5cCI6IkpXVCIsImFsZyI6IlJTNTEyIn0.eyJzdWIiOiJBbmRyZWEiLCJhdWQiOiJUYXNodWFuIiwiaXNzIjoiandrcy1zZXJ2aWNlLmFwcHNwb3QuY29tIiwiZXhwIjoxNjI0NzU2OTAwLCJpYXQiOjE2MjQ3NTY4OTUsImp0aSI6IjA5ZjkzZjljLTU0ZjMtNDM5Yi04Njg2LWZhMGYwMjlmYmIwZSJ9.g643vWnvDvR5u5TeCUaCblp-Ss8SPWoZrOxBo3y6WP9xQnRW63VSbacCirl-5nGRPoX6vostZAkRyUl62ICQHpTj3bRnDY4ZbkcQ42xtrWMBsI2Sw6dAmZtGsCR_tguQZmvdKE4gVNnFWLp0hBjCeLxPVbc59vC6njMdz7XHcOdW7RXN6iUYjLFoPAr4Qg93Vbrwfo9Qmkm8bDgbnuoJ3aQq0RFa02G1KC2-cx8SuUbxso_Uu7ddY6HDRL5OPF3xS9cKO5ty4zCfGYIVDhfH7V-zA2cJZyA2dlv3Ddd-ntU42aud0M4PcTTdjHf1CE29sCZHk5wTRgxsTjfWglYQQiVQJEkw6DD6kTlQ_MwN4p_OWNj06b55mXM6Bj9c9y8TfPL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sImFsZyI6IlBTMzg0In0.eyJzdWIiOiJGcmVkYSIsImF1ZCI6Ikx1Y2lhIiwiaXNzIjoiandrcy1zZXJ2aWNlLmFwcHNwb3QuY29tIiwiZXhwIjoxNjI0ODA0MTk0LCJpYXQiOjE2MjQ4MDQxODksImp0aSI6IjdjNTQ2Y2RmLTYwMTEtNDI3Ny04Y2Q0LTMwNjZmZTYwNTExZSJ9.hQm-OP_MMk8_S13-ohiINRuDP2IlCiB3yn8Ov6qTjeFbq4gZ6MegeJH_qiZOvXqlzOAwpwd5P4nm5JeS6LlNGdW6V_agwYwnAd08GI7APQNRib692_sEk1DKdSk-S-Y8V_ZAgeTT8asdaSDw4EBPxkDvROcuEqesZrfqnrOcpdqqa2BcmwX8q5sLtQ8TMp4cOvEZg-J8_0j2kdCUkv_n9ZdsRoA3EUT8M1bYqnGRRxIRqflsm-S_xq3HxMAnPF5hPlqIKFVKuRsU0SKgcHZGwXpuK2lJqPobl6MI987tGrc9sPPFzVkNYxeltcxu34-ZjzN6iCQN8r0w-mfqCZav7A"
// Use the JWKS jwk.Keyfunc to parse the tokens signed with unknown kids at nearly the same time.
waitGroup := sync.WaitGroup{}
waitGroup.Add(3)
go func() {
defer waitGroup.Done()
if _, parseErr := jwt.Parse(token1, jwks.Keyfunc); parseErr != nil {
if errors.Is(parseErr, jwt.ErrInvalidKeyType) {
t.Errorf(logFmt, "Invaild key type selected.", parseErr)
}
}
}()
go func() {
defer waitGroup.Done()
if _, parseErr := jwt.Parse(token2, jwks.Keyfunc); parseErr != nil {
if errors.Is(parseErr, jwt.ErrInvalidKeyType) {
t.Errorf(logFmt, "Invaild key type selected.", parseErr)
}
}
}()
go func() {
defer waitGroup.Done()
if _, parseErr := jwt.Parse(token3, jwks.Keyfunc); parseErr != nil {
if errors.Is(parseErr, jwt.ErrInvalidKeyType) {
t.Errorf(logFmt, "Invaild key type selected.", parseErr)
}
}
}()
if _, parseErr := jwt.Parse(token4, jwks.Keyfunc); parseErr != nil {
if errors.Is(parseErr, jwt.ErrInvalidKeyType) {
t.Fatalf(logFmt, "Invaild key type selected.", parseErr)
}
}
waitGroup.Wait()
// Confirm the JWKS was only refreshed once. (Refresh counter was first incremented on the creation of the JWKS.)
refreshMux.Lock()
expected := uint(2)
if refreshes != expected {
t.Fatalf("An incorrect number of refreshes occurred.\n Expected: %d\n Got: %d\n", expected, refreshes)
}
refreshMux.Unlock()
// Wait for the rate limiter to take the next queue.
time.Sleep(refreshRateLimit + time.Millisecond*100)
refreshMux.Lock()
expected = uint(3)
if refreshes != expected {
t.Fatalf("An incorrect number of refreshes occurred.\n Expected: %d\n Got: %d\n", expected, refreshes)
}
refreshMux.Unlock()
// Wait for the refresh interval to occur.
time.Sleep(refreshInterval + time.Millisecond*100)
refreshMux.Lock()
expected = uint(4)
if refreshes != expected {
t.Fatalf("An incorrect number of refreshes occurred.\n Expected: %d\n Got: %d\n", expected, refreshes)
}
refreshMux.Unlock()
}
// TestRawJWKS confirms a copy of the raw JWKS is returned from the method.
func TestRawJWKS(t *testing.T) {
tempDir, err := ioutil.TempDir("", "*")
if err != nil {
t.Fatalf(logFmt, "Failed to create a temporary directory.", err)
}
defer func() {
err = os.RemoveAll(tempDir)
if err != nil {
t.Fatalf(logFmt, "Failed to remove temporary directory.", err)
}
}()
jwksFile := filepath.Join(tempDir, jwksFilePath)
err = ioutil.WriteFile(jwksFile, []byte(jwksJSON), 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
server := httptest.NewServer(http.FileServer(http.Dir(tempDir)))
defer server.Close()
jwksURL := server.URL + jwksFilePath
jwks, err := keyfunc.Get(jwksURL, keyfunc.Options{})
if err != nil {
t.Fatalf(logFmt, "Failed to get JWKS from testing URL.", err)
}
raw := jwks.RawJWKS()
if !bytes.Equal(raw, []byte(jwksJSON)) {
t.Fatalf("Raw JWKS does not match remote JWKS resource.")
}
// Overwrite the slice returned, if it's a copy, it should ruin the original.
emptySlice := make([]byte, len(raw))
copy(raw, emptySlice)
nextRaw := jwks.RawJWKS()
if bytes.Equal(nextRaw, emptySlice) {
t.Fatalf("Raw JWKS is not a copy.")
}
}
// TestRequestFactory confirms the behavior of request factories.
func TestRequestFactory(t *testing.T) {
var fullJWKSHandler http.Handler
{
tempDir, err := ioutil.TempDir("", "*")
if err != nil {
t.Fatalf(logFmt, "Failed to create a temporary directory.", err)
}
defer func() {
err = os.RemoveAll(tempDir)
if err != nil {
t.Fatalf(logFmt, "Failed to remove temporary directory.", err)
}
}()
jwksFile := filepath.Join(tempDir, jwksFilePath)
err = ioutil.WriteFile(jwksFile, []byte(jwksJSON), 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
fullJWKSHandler = http.FileServer(http.Dir(tempDir))
}
var emptyJWKSHandler http.Handler
{
tempDir, err := ioutil.TempDir("", "*")
if err != nil {
t.Fatalf(logFmt, "Failed to create a temporary directory.", err)
}
defer func() {
err = os.RemoveAll(tempDir)
if err != nil {
t.Fatalf(logFmt, "Failed to remove temporary directory.", err)
}
}()
jwksFile := filepath.Join(tempDir, jwksFilePath)
err = ioutil.WriteFile(jwksFile, []byte(emptyJWKSJSON), 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
emptyJWKSHandler = http.FileServer(http.Dir(tempDir))
}
const (
fullJWKSUserAgent = "full-jwks-please"
userAgentHeader = "User-Agent"
)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Header.Get(userAgentHeader) {
case fullJWKSUserAgent:
fullJWKSHandler.ServeHTTP(w, r)
default:
emptyJWKSHandler.ServeHTTP(w, r)
}
}))
defer server.Close()
jwksURL := server.URL + jwksFilePath
optsFail := keyfunc.Options{
RequestFactory: func(ctx context.Context, url string) (*http.Request, error) {
badURL := fmt.Sprintf("%s/does/not/exist", url)
return http.NewRequestWithContext(ctx, http.MethodGet, badURL, bytes.NewReader(nil))
},
}
_, err := keyfunc.Get(jwksURL, optsFail)
if err == nil {
t.Fatalf("Creation of *keyfunc.JWKS reading from bad URL must fail.")
}
optsSuccess := keyfunc.Options{
RequestFactory: func(ctx context.Context, url string) (*http.Request, error) {
return http.NewRequestWithContext(ctx, http.MethodGet, url, bytes.NewReader(nil))
},
}
jwks, err := keyfunc.Get(jwksURL, optsSuccess)
if err != nil {
t.Fatalf(logFmt, "Failed to get JWKS from testing URL.", err)
}
if len(jwks.ReadOnlyKeys()) != 0 {
t.Fatalf("JWKS should be empty due to lack of custom HTTP header.")
}
optsCustomHeader := keyfunc.Options{
RequestFactory: func(ctx context.Context, url string) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, bytes.NewReader(nil))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set(userAgentHeader, fullJWKSUserAgent)
return req, nil
},
}
jwks, err = keyfunc.Get(jwksURL, optsCustomHeader)
if err != nil {
t.Fatalf(logFmt, "Failed to get JWKS from testing URL.", err)
}
if len(jwks.ReadOnlyKeys()) == 0 {
t.Fatalf("JWKS should not be empty due to custom HTTP header.")
}
}
// TestUnknownKIDRefresh performs a test to confirm that an Unknown kid with refresh the JWKS.
func TestUnknownKIDRefresh(t *testing.T) {
tempDir, err := ioutil.TempDir("", "*")
if err != nil {
t.Fatalf(logFmt, "Failed to create a temporary directory.", err)
}
defer func() {
err = os.RemoveAll(tempDir)
if err != nil {
t.Fatalf(logFmt, "Failed to remove temporary directory.", err)
}
}()
jwksFile := filepath.Join(tempDir, strings.TrimPrefix(jwksFilePath, "/"))
err = ioutil.WriteFile(jwksFile, []byte(emptyJWKSJSON), 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
server := httptest.NewServer(http.FileServer(http.Dir(tempDir)))
defer server.Close()
testingRefreshErrorHandler := func(err error) {
t.Fatalf(logFmt, "Unhandled JWKS error.", err)
}
jwksURL := server.URL + jwksFilePath
options := keyfunc.Options{
RefreshErrorHandler: testingRefreshErrorHandler,
RefreshUnknownKID: true,
}
jwks, err := keyfunc.Get(jwksURL, options)
if err != nil {
t.Fatalf(logFmt, "Failed to create *keyfunc.JWKS.", err)
}
defer jwks.EndBackground()
err = ioutil.WriteFile(jwksFile, []byte(jwksJSON), 0600)
if err != nil {
t.Fatalf(logFmt, "Failed to write JWKS file to temporary directory.", err)
}
// Use any JWT signed by a key in the non-empty JWKS.
token := "eyJhbGciOiJFUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJDR3QwWldTNExjNWZhaUtTZGkwdFUwZmpDQWR2R1JPUVJHVTlpUjd0VjBBIn0.eyJleHAiOjE2MTU0MDY4NjEsImlhdCI6MTYxNTQwNjgwMSwianRpIjoiYWVmOWQ5YjItN2EyYy00ZmQ4LTk4MzktODRiMzQ0Y2VmYzZhIiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo4MDgwL2F1dGgvcmVhbG1zL21hc3RlciIsImF1ZCI6ImFjY291bnQiLCJzdWIiOiJhZDEyOGRmMS0xMTQwLTRlNGMtYjA5Ny1hY2RjZTcwNWJkOWIiLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiJ0b2tlbmRlbG1lIiwiYWNyIjoiMSIsInJlYWxtX2FjY2VzcyI6eyJyb2xlcyI6WyJvZmZsaW5lX2FjY2VzcyIsInVtYV9hdXRob3JpemF0aW9uIl19LCJyZXNvdXJjZV9hY2Nlc3MiOnsiYWNjb3VudCI6eyJyb2xlcyI6WyJtYW5hZ2UtYWNjb3VudCIsIm1hbmFnZS1hY2NvdW50LWxpbmtzIiwidmlldy1wcm9maWxlIl19fSwic2NvcGUiOiJlbWFpbCBwcm9maWxlIiwiY2xpZW50SG9zdCI6IjE3Mi4yMC4wLjEiLCJjbGllbnRJZCI6InRva2VuZGVsbWUiLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsInByZWZlcnJlZF91c2VybmFtZSI6InNlcnZpY2UtYWNjb3VudC10b2tlbmRlbG1lIiwiY2xpZW50QWRkcmVzcyI6IjE3Mi4yMC4wLjEifQ.iQ77QGoPDNjR2oWLu3zT851mswP8J-h_nrGhs3fpa_tFB3FT1deKPGkjef9JOTYFI-CIVxdCFtW3KODOaw9Nrw"
// Use the JWKS jwk.Keyfunc to parse the token.
//
// Don't check for general errors. Unfortunately, an error occurs when a token is expired. All hard
// coded tokens are expired.
_, err = jwt.Parse(token, jwks.Keyfunc)
if err != nil {
if errors.Is(err, jwt.ErrInvalidKeyType) {
t.Fatalf(logFmt, "Invaild key type selected.", err)
}
}
}
// TestReadOnlyKeys verifies that the .ReadOnlyKeys() method returns a map with the correct types.
func TestReadOnlyKeys(t *testing.T) {
jwks, err := keyfunc.NewJSON([]byte(jwksJSON))
if err != nil {
t.Fatalf(logFmt, "Failed to create a JWKS from JSON.", err)
}
for _, key := range jwks.ReadOnlyKeys() {
switch key.(type) {
case *rsa.PublicKey:
// Do nothing.
case *ecdsa.PublicKey:
// Do nothing.
case ed25519.PublicKey:
// Do nothing.
case []byte:
// Do nothing.
default:
t.Errorf("Invalid type %T in .ReadOnlyKeys() method.", key)
}
}
}