forked from MicahParks/keyfunc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwks_test.go
391 lines (338 loc) · 36.6 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
package keyfunc_test
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"sync"
"testing"
"time"
"github.com/golang-jwt/jwt/v4"
"github.com/MicahParks/keyfunc"
)
const (
// emptyJWKsJSON is a hard-coded empty JWKs in JSON format.
emptyJWKsJS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sWtQtvPxl3JNcBQgDR5/IMgl5VndRZ9OT56KUqrR5xRsWiCvh5Lgv4fUEzAAo9ToiPLub1SKP063zWrvfgi3YZ19bty0iXFm7l2cpQ3ejFV7WpcdLJE0lapFdPLo6QaRdgNu/1p4vbYg7zSK1fQ0OY5b3ajhAx/bhWlrN685owRbO5/r4rUOa6oo9l4Qn7jUxKUx4rcoe7zUM7qrpOPqKvn0DBp3n1/+9pOZXCjIfZGvYwP5NhzBDCkRzaXcJHlOqWzMBzyovVrzVmUilBcj+EsTYJs0gVXKzduX5zO6YWhFs23lu7AijdkxTY65YM0="],"x5t":"IYIeevIT57t8ppUejM42Bqx6f3I","x5t#S256":"TuOrBy2NcTlFSWuZ8Kh8W8AjQagb4fnfP1SlKMO8-So"},{"kid":"ebJxnm9B3QDBljB5XJWEu72qx6BawDaMAhwz4aKPkQ0","kty":"EC","alg":"ES512","use":"sig","crv":"P-521","x":"YQ95Xj8MTzcHytbU1h8YkCN2kdEQA7ThuZ1ctB9Ekiw6tlM9RwL62eQvzEt4Rz8qN69uRqgU9RzxQOkSU5xVvyo","y":"SMMuP3QnAPHtx7Go2ARsG3NBaySWBLmVvS8s2Ss7Vm_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_KMx9K3xUdd3euj9MCq8vytwEPie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4AESjliXIqMrdpFynfPnIsGgf3dnph5BM="],"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_hr4frW2NKJ78YGvCzwLKG2GgtSjj0zuTLnSaK8fCGzqXgy6paXNhgHUSZgGwvO0YItpMlyJeqEj1wGTWz1IyA1sguF1cC7K0fojPbPoBwrhvaAeoGRPLraE0rrBsQv8iiLwnRBIez9B1j0NiUG8Iad953Y7UzaKOAw8crIEK45NIK_yxHUpxqcHLjPIcRyIyJGioRyGK7cp-_7iPLOCutQc-u46mom1_ZQ","e":"AQAB","x5c":["MIICmzCCAYMCBgF4BJRpbzANBgkqhkiG9w0BAQsFADARMQ8wDQYDVQQDDAZtYXN0ZXIwHhcNMjEwMzA1MjI0NzE4WhcNMzEwMzA1MjI0ODU4WjARMQ8wDQYDVQQDDAZtYXN0ZXIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCNr33JsOsu/DXVngK82UMj6SSokQGlKe8N/SlkXYO8GW9iZ5Vgv8KGoh7wT0bIdSYsx207+OJ9KA6oTlvy8gvCLWRmclHs3znu4SdtYz1hN56+L9Fn4T+Gvh+tbY0onvxga8LPAsobYaC1KOPTO5MudJorx8IbOpeDLqlpc2GAdRJmAbC87Rgi2kyXIl6oSPXAZNbPUjIDWyC4XVwLsrR+iM9s+gHCuG9oB6gZE8utoTSusGxC/yKIvCdEEh7P0HWPQ2JQbwhp33ndjtTNoo4DDxysgQrjk0gr/LEdSnGpwcuM8hxHIjIkaKhHIYrtyn7/uI8s4K61Bz67jqaibX9lAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAHrGJFhVNiQupIwkn2jiW/jBobm9CHUxOwQL5E7WdRz5uaOJ0v62PrynOQE9xim9Qk8bT3q7DThZs66U9bpIk3msKVRgXRfn5FZy1H5RKOlEEFZhGakPqSlC1yPbhUNhHXMs3GTzdGMLtYaGvSy6XM/8/zqVqVwgh6BpbAR9RfiSdyaiNTSBriu+n/tHW934G9J8UIzdfpVcb0Yt9y4o0UgIXt64NtGFq7zmNJijH88AxBZFB6eUUmQQCczebzoAjyYbVOes5gGFzboVWcyLe3iyD0vvsAVHJViXeiGoxhpKnc8ryISpRUBzsKngf5uZo3bnrD9PHLYBoGOHgzII1xw="],"x5t":"5GNr3LeRXHWI4YR8-QTSsF98oTI","x5t#S256":"Dgd0_wZZqvRuf4GEISPNHREX-1ixTMIsrPeGzk0bCxs"}]}`
)
// TestInvalidServer performs initialization + refresh initialization with a server providing invalid data.
// The test ensures that background refresh goroutine does not causes any troubles in case of init failure.
func TestInvalidServer(t *testing.T) {
// Create the HTTP test server.
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
if _, err := w.Write(nil); err != nil {
t.Errorf("Failed to write empty response.\nError: %s\n", err.Error())
t.FailNow()
}
}))
defer server.Close()
// Create testing options.
testingRefreshErrorHandler := func(err error) {
t.Errorf("Unhandled JWKs error: %s", err.Error())
t.FailNow()
}
// Set the options to refresh KID when unknown.
refreshInterval := time.Second
options := keyfunc.Options{
RefreshInterval: &refreshInterval,
RefreshErrorHandler: testingRefreshErrorHandler,
}
// Create the JWKs.
if _, err := keyfunc.Get(server.URL, options); err == nil {
t.Errorf("Creation of *keyfunc.JWKs with invalid server must fail.")
t.FailNow()
}
}
// TestJWKs performs a table test on the JWKs code.
func TestJWKs(t *testing.T) {
// Could add a test with an invalid JWKs endpoint.
// Create a temporary directory to serve the JWKs from.
tempDir, err := ioutil.TempDir("", "*")
if err != nil {
t.Errorf("Failed to create a temporary directory.\nError:%s\n", err.Error())
t.FailNow()
}
defer func() {
if err = os.RemoveAll(tempDir); err != nil {
t.Errorf("Failed to remove temporary directory.\nError:%s\n", err.Error())
t.FailNow()
}
}()
// Create the JWKs file path.
jwksFile := filepath.Join(tempDir, "example_jwks.json")
// Write the empty JWKs.
if err = ioutil.WriteFile(jwksFile, []byte(jwksJSON), 0600); err != nil {
t.Errorf("Failed to write JWKs file to temporary directory.\nError:%s\n", err.Error())
t.FailNow()
}
// Create the HTTP test server.
server := httptest.NewServer(http.FileServer(http.FS(os.DirFS(tempDir))))
defer server.Close()
// Create testing options.
testingRefreshInterval := time.Second
testingRateLimit := time.Millisecond * 500
testingRefreshTimeout := time.Second
testingRefreshErrorHandler := func(err error) {
t.Errorf("Unhandled JWKs error: %s", err.Error())
t.FailNow()
}
// Set the JWKs URL.
jwksFilePath := "/example_jwks.json"
jwksURL := server.URL + jwksFilePath
// Create a table of options to test.
options := []keyfunc.Options{
{}, // Default options.
{
Client: http.DefaultClient, // Should be ineffectual. Just for code coverage.
},
{
RefreshInterval: &testingRefreshInterval,
},
{
RefreshTimeout: &testingRefreshTimeout,
},
{
RefreshErrorHandler: testingRefreshErrorHandler,
},
{
RefreshRateLimit: &testingRateLimit,
},
}
// Iterate through all options.
for _, opts := range options {
// Create the JWKs from the resource at the testing URL.
jwks, err := keyfunc.Get(jwksURL, opts)
if err != nil {
t.Errorf("Failed to get JWKs from testing URL.\nError:%s\n", err.Error())
t.FailNow()
}
// Create the test cases.
testCases := []struct {
token string
}{
{""}, // Empty JWT.
{"eyJhbGciOiJFUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJDR3QwWldTNExjNWZhaUtTZ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nQiLCJzdWIiOiJhZDEyOGRmMS0xMTQwLTRlNGMtYjA5Ny1hY2RjZTcwNWJkOWIiLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiJ0b2tlbmRlbG1lIiwiYWNyIjoiMSIsInJlYWxtX2FjY2VzcyI6eyJyb2xlcyI6WyJvZmZsaW5lX2FjY2VzcyIsInVtYV9hdXRob3JpemF0aW9uIl19LCJyZXNvdXJjZV9hY2Nlc3MiOnsiYWNjb3VudCI6eyJyb2xlcyI6WyJtYW5hZ2UtYWNjb3VudCIsIm1hbmFnZS1hY2NvdW50LWxpbmtzIiwidmlldy1wcm9maWxlIl19fSwic2NvcGUiOiJlbWFpbCBwcm9maWxlIiwiY2xpZW50SG9zdCI6IjE3Mi4yMC4wLjEiLCJjbGllbnRJZCI6InRva2VuZGVsbWUiLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsInByZWZlcnJlZF91c2VybmFtZSI6InNlcnZpY2UtYWNjb3VudC10b2tlbmRlbG1lIiwiY2xpZW50QWRkcmVzcyI6IjE3Mi4yMC4wLjEifQ.BHygL0iGWEL46QdcnInqgjhgtlfBN8H2BMhFAK1sZuGB6rX-FGHFav0NgnWzT5Ae6wM3KqJY30aME5OOvycV--5w7ZX8uqnYjXYdLbJ-azLtP3Hw8vwY9u6GC81ZvWZdKvQNpbcuvtJYL2uhrbv0GdXcClTHmA-NiReGFuBFgo0fBX_ipjNx_q94OnaDxSHUSGeKqNFoNOttXBV7Xqa_K9j60zfoO9E2OV0jkYI5_8MPPZI85Y8XG7PUK2opg7LHNrFbB67C_RxJ7ZDKt0jBApzJyZ96_8UBSvNtBnytQO-CexOG-5y-nN3mcw7NU7g7dFxlb18Yur194h7VTT9tHQ"}, // RSA precomputed.
}
// Wait for the interval to pass, if required.
if opts.RefreshInterval != nil {
time.Sleep(*opts.RefreshInterval)
}
// Iterate through the test cases.
for _, tc := range testCases {
t.Run(fmt.Sprintf("token: %s", tc.token), func(t *testing.T) {
// 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.
if _, err = jwt.Parse(tc.token, jwks.KeyFunc); err != nil {
if errors.Is(err, jwt.ErrInvalidKeyType) {
t.Errorf("Invaild key type selected.\nError:%s\n", err.Error())
t.FailNow()
}
}
})
}
// End the background goroutine. Ineffectual without refresh interval.
jwks.EndBackground()
}
}
// TestRateLimit performs a test to confirm the rate limiter works as expected.
func TestRateLimit(t *testing.T) {
// Create a temporary directory to serve the JWKs from.
tempDir, err := ioutil.TempDir("", "*")
if err != nil {
t.Errorf("Failed to create a temporary directory.\nError:%s\n", err.Error())
t.FailNow()
}
defer func() {
if err = os.RemoveAll(tempDir); err != nil {
t.Errorf("Failed to remove temporary directory.\nError:%s\n", err.Error())
t.FailNow()
}
}()
// Create an integer to keep track of how many times the JWKs has been refreshed.
refreshes := uint(0)
refreshMux := sync.Mutex{}
// Create the HTTP test server.
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
// Increment the number of refreshes that have occurred.
refreshMux.Lock()
refreshes++
refreshMux.Unlock()
// Write the JWKs to the response, regardless of the request.
writer.WriteHeader(200)
if _, serveErr := writer.Write([]byte(jwksJSON)); serveErr != nil {
t.Errorf("Failed to serve JWKs.\nError: %s\n", err.Error())
}
}))
defer server.Close()
// Set the JWKs URL.
jwksFilePath := "/example_jwks.json"
jwksURL := server.URL + jwksFilePath
// Create the testing options.
refreshInterval := time.Second
refreshRateLimit := time.Millisecond * 500
refreshTimeout := time.Second
refreshUnknownKID := true
options := keyfunc.Options{
RefreshErrorHandler: func(err error) {
t.Errorf("The package itself had an error.\nError: %s\n", err.Error())
},
RefreshInterval: &refreshInterval,
RefreshRateLimit: &refreshRateLimit,
RefreshTimeout: &refreshTimeout,
RefreshUnknownKID: &refreshUnknownKID,
}
// Create the JWKs.
var jwks *keyfunc.JWKs
if jwks, err = keyfunc.Get(jwksURL, options); err != nil {
t.Errorf("Failed to create *keyfunc.JWKs.\nError:%s\n", err.Error())
t.FailNow()
}
// 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_OWNj06b55mXM6Bj9c9y8TfPLETDy_PRc1lHu1PuiizLg019JaGidpTLF8IdKTa9emkEnf2n8xWi-YMkkRk57hpuc56GmnBR0d8ODfuL0XILlQp2guFsVRo9A4Sdqy7fGdZGoSS4XzSR-TIEw7W_KSqlYCtWC0xNk1Kze3xSY2mDqrn1YFFlvXgXQlgzU8GN1eL7QRRQlxaPGti2wEH6OYH4A160nR_OM-zFBobpQn79g8HsK8yZgPiY0p94F6pvKBQtSHDBvAe3W0-UHYfspwT9c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yn8Ov6qTjeFbq4gZ6MegeJH_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("Invaild key type selected.\nError:%s\n", parseErr.Error())
}
}
}()
go func() {
defer waitGroup.Done()
if _, parseErr := jwt.Parse(token2, jwks.KeyFunc); parseErr != nil {
if errors.Is(parseErr, jwt.ErrInvalidKeyType) {
t.Errorf("Invaild key type selected.\nError:%s\n", parseErr.Error())
}
}
}()
go func() {
defer waitGroup.Done()
if _, parseErr := jwt.Parse(token3, jwks.KeyFunc); parseErr != nil {
if errors.Is(parseErr, jwt.ErrInvalidKeyType) {
t.Errorf("Invaild key type selected.\nError:%s\n", parseErr.Error())
}
}
}()
if _, parseErr := jwt.Parse(token4, jwks.KeyFunc); parseErr != nil {
if errors.Is(parseErr, jwt.ErrInvalidKeyType) {
t.Errorf("Invaild key type selected.\nError:%s\n", parseErr.Error())
t.FailNow()
}
}
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.Errorf("An incorrect number of refreshes occurred.\n Expected: %d\n Got: %d\n", expected, refreshes)
t.FailNow()
}
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.Errorf("An incorrect number of refreshes occurred.\n Expected: %d\n Got: %d\n", expected, refreshes)
t.FailNow()
}
refreshMux.Unlock()
// Wait for the refresh interval to occur.
time.Sleep(refreshInterval + time.Millisecond*100)
refreshMux.Lock()
expected = uint(4)
if refreshes != expected {
t.Errorf("An incorrect number of refreshes occurred.\n Expected: %d\n Got: %d\n", expected, refreshes)
t.FailNow()
}
refreshMux.Unlock()
}
// TestUnknownKIDRefresh performs a test to confirm that an Unknown kid with refresh the JWKs.
func TestUnknownKIDRefresh(t *testing.T) {
// Create a temporary directory to serve the JWKs from.
tempDir, err := ioutil.TempDir("", "*")
if err != nil {
t.Errorf("Failed to create a temporary directory.\nError:%s\n", err.Error())
t.FailNow()
}
defer func() {
if err = os.RemoveAll(tempDir); err != nil {
t.Errorf("Failed to remove temporary directory.\nError:%s\n", err.Error())
t.FailNow()
}
}()
// Create the JWKs file path.
jwksFile := filepath.Join(tempDir, "example_jwks.json")
// Write the empty JWKs.
if err = ioutil.WriteFile(jwksFile, []byte(emptyJWKsJSON), 0600); err != nil {
t.Errorf("Failed to write JWKs file to temporary directory.\nError:%s\n", err.Error())
t.FailNow()
}
// Create the HTTP test server.
server := httptest.NewServer(http.FileServer(http.FS(os.DirFS(tempDir))))
defer server.Close()
// Create testing options.
testingRefreshErrorHandler := func(err error) {
t.Errorf("Unhandled JWKs error: %s", err.Error())
t.FailNow()
}
// Set the JWKs URL.
jwksFilePath := "/example_jwks.json"
jwksURL := server.URL + jwksFilePath
// Set the options to refresh KID when unknown.
options := keyfunc.Options{
RefreshErrorHandler: testingRefreshErrorHandler,
RefreshUnknownKID: &[]bool{true}[0],
}
// Create the JWKs.
var jwks *keyfunc.JWKs
if jwks, err = keyfunc.Get(jwksURL, options); err != nil {
t.Errorf("Failed to create *keyfunc.JWKs.\nError:%s\n", err.Error())
t.FailNow()
}
// Write the empty JWKs.
if err = ioutil.WriteFile(jwksFile, []byte(jwksJSON), 0600); err != nil {
t.Errorf("Failed to write JWKs file to temporary directory.\nError:%s\n", err.Error())
t.FailNow()
}
// 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.
if _, err = jwt.Parse(token, jwks.KeyFunc); err != nil {
if errors.Is(err, jwt.ErrInvalidKeyType) {
t.Errorf("Invaild key type selected.\nError:%s\n", err.Error())
t.FailNow()
}
}
}