Debian Codegraph @

pkg/oidc/keyset_test.go

raw
1 package oidc
2
3 import (
4 "crypto/ecdsa"
5 "crypto/rsa"
6 "errors"
7 "reflect"
8 "testing"
9
10 jose "github.com/go-jose/go-jose/v4"
11 )
12
13 func TestFindKey(t *testing.T) {
14 type args struct {
15 keyID string
16 use string
17 expectedAlg string
18 keys []jose.JSONWebKey
19 }
20 type res struct {
21 key jose.JSONWebKey
22 err error
23 }
24 tests := []struct {
25 name string
26 args args
27 res res
28 }{
29 {
30 "no keys, ErrKeyNone",
31 args{
32 keyID: "",
33 use: KeyUseSignature,
34 expectedAlg: "RS256",
35 keys: nil,
36 },
37 res{
38 key: jose.JSONWebKey{},
39 err: ErrKeyNone,
40 },
41 },
42 {
43 "single key enc, ErrKeyNone",
44 args{
45 keyID: "",
46 use: KeyUseSignature,
47 expectedAlg: "RS256",
48 keys: []jose.JSONWebKey{
49 {
50 Use: "enc",
51 Key: &rsa.PublicKey{},
52 },
53 },
54 },
55 res{
56 key: jose.JSONWebKey{},
57 err: ErrKeyNone,
58 },
59 },
60 {
61 "single key wrong algorithm, ErrKeyNone",
62 args{
63 keyID: "",
64 use: KeyUseSignature,
65 expectedAlg: "RS256",
66 keys: []jose.JSONWebKey{
67 {
68 Use: "sig",
69 Key: &rsa.PrivateKey{},
70 },
71 },
72 },
73 res{
74 key: jose.JSONWebKey{},
75 err: ErrKeyNone,
76 },
77 },
78 {
79 "single key no kid, no jwt kid, match",
80 args{
81 keyID: "",
82 use: KeyUseSignature,
83 expectedAlg: "RS256",
84 keys: []jose.JSONWebKey{
85 {
86 Use: "sig",
87 Key: &rsa.PublicKey{},
88 },
89 },
90 },
91 res{
92 key: jose.JSONWebKey{
93 Use: "sig",
94 Key: &rsa.PublicKey{},
95 },
96 err: nil,
97 },
98 },
99 {
100 "single key kid, jwt no kid, match",
101 args{
102 keyID: "",
103 use: KeyUseSignature,
104 expectedAlg: "RS256",
105 keys: []jose.JSONWebKey{
106 {
107 Use: "sig",
108 KeyID: "id",
109 Key: &rsa.PublicKey{},
110 },
111 },
112 },
113 res{
114 key: jose.JSONWebKey{
115 Use: "sig",
116 KeyID: "id",
117 Key: &rsa.PublicKey{},
118 },
119 err: nil,
120 },
121 },
122 {
123 "single key no kid, jwt with kid, match",
124 args{
125 keyID: "id",
126 use: KeyUseSignature,
127 expectedAlg: "RS256",
128 keys: []jose.JSONWebKey{
129 {
130 Use: "sig",
131 Key: &rsa.PublicKey{},
132 },
133 },
134 },
135 res{
136 key: jose.JSONWebKey{
137 Use: "sig",
138 Key: &rsa.PublicKey{},
139 },
140 err: nil,
141 },
142 },
143 {
144 "single key no use, jwt with kid, match",
145 args{
146 keyID: "id",
147 use: KeyUseSignature,
148 expectedAlg: "RS256",
149 keys: []jose.JSONWebKey{
150 {
151 KeyID: "id",
152 Key: &rsa.PublicKey{},
153 },
154 },
155 },
156 res{
157 key: jose.JSONWebKey{
158 KeyID: "id",
159 Key: &rsa.PublicKey{},
160 },
161 err: nil,
162 },
163 },
164 {
165 "single key wrong kid, ErrKeyNone",
166 args{
167 keyID: "id",
168 use: KeyUseSignature,
169 expectedAlg: "RS256",
170 keys: []jose.JSONWebKey{
171 {
172 Use: "sig",
173 KeyID: "id2",
174 Key: &rsa.PublicKey{},
175 },
176 },
177 },
178 res{
179 key: jose.JSONWebKey{},
180 err: ErrKeyNone,
181 },
182 },
183 {
184 "multiple keys no kid, jwt no kid, ErrKeyMultiple",
185 args{
186 keyID: "",
187 use: KeyUseSignature,
188 expectedAlg: "RS256",
189 keys: []jose.JSONWebKey{
190 {
191 Use: "sig",
192 Key: &rsa.PublicKey{},
193 },
194 {
195 Use: "sig",
196 Key: &rsa.PublicKey{},
197 },
198 },
199 },
200 res{
201 key: jose.JSONWebKey{},
202 err: ErrKeyMultiple,
203 },
204 },
205 {
206 "multiple keys with kid, jwt no kid, ErrKeyMultiple",
207 args{
208 keyID: "",
209 use: KeyUseSignature,
210 expectedAlg: "RS256",
211 keys: []jose.JSONWebKey{
212 {
213 Use: "sig",
214 KeyID: "id1",
215 Key: &rsa.PublicKey{},
216 },
217 {
218 Use: "sig",
219 KeyID: "id2",
220 Key: &rsa.PublicKey{},
221 },
222 },
223 },
224 res{
225 key: jose.JSONWebKey{},
226 err: ErrKeyMultiple,
227 },
228 },
229 {
230 "multiple keys, single sig key, jwt no kid, match",
231 args{
232 keyID: "",
233 use: KeyUseSignature,
234 expectedAlg: "RS256",
235 keys: []jose.JSONWebKey{
236 {
237 Use: "sig",
238 Key: &rsa.PublicKey{},
239 },
240 {
241 Use: "enc",
242 Key: &rsa.PublicKey{},
243 },
244 },
245 },
246 res{
247 key: jose.JSONWebKey{
248 Use: "sig",
249 Key: &rsa.PublicKey{},
250 },
251 err: nil,
252 },
253 },
254 {
255 "multiple keys no kid, jwt with kid, ErrKeyMultiple",
256 args{
257 keyID: "id",
258 use: KeyUseSignature,
259 expectedAlg: "RS256",
260 keys: []jose.JSONWebKey{
261 {
262 Use: "sig",
263 Key: &rsa.PublicKey{},
264 },
265 {
266 Use: "sig",
267 Key: &rsa.PublicKey{},
268 },
269 },
270 },
271 res{
272 key: jose.JSONWebKey{},
273 err: ErrKeyMultiple,
274 },
275 },
276 {
277 "multiple keys with kid, jwt with kid, match",
278 args{
279 keyID: "id1",
280 use: KeyUseSignature,
281 expectedAlg: "RS256",
282 keys: []jose.JSONWebKey{
283 {
284 Use: "sig",
285 KeyID: "id1",
286 Key: &rsa.PublicKey{},
287 },
288 {
289 Use: "sig",
290 KeyID: "id2",
291 Key: &rsa.PublicKey{},
292 },
293 },
294 },
295 res{
296 key: jose.JSONWebKey{
297 Use: "sig",
298 KeyID: "id1",
299 Key: &rsa.PublicKey{},
300 },
301 err: nil,
302 },
303 },
304 {
305 "multiple keys, single sig key, jwt with kid, match",
306 args{
307 keyID: "id1",
308 use: KeyUseSignature,
309 expectedAlg: "RS256",
310 keys: []jose.JSONWebKey{
311 {
312 Use: "sig",
313 Key: &rsa.PublicKey{},
314 },
315 {
316 Use: "enc",
317 Key: &rsa.PublicKey{},
318 },
319 },
320 },
321 res{
322 key: jose.JSONWebKey{
323 Use: "sig",
324 Key: &rsa.PublicKey{},
325 },
326 err: nil,
327 },
328 },
329 {
330 "multiple keys, no use, jwt with kid, match",
331 args{
332 keyID: "id1",
333 use: KeyUseSignature,
334 expectedAlg: "RS256",
335 keys: []jose.JSONWebKey{
336 {
337 KeyID: "id1",
338 Key: &rsa.PublicKey{},
339 },
340 {
341 KeyID: "id2",
342 Key: &rsa.PublicKey{},
343 },
344 },
345 },
346 res{
347 key: jose.JSONWebKey{
348 KeyID: "id1",
349 Key: &rsa.PublicKey{},
350 },
351 err: nil,
352 },
353 },
354 {
355 "multiple keys, no use, jwt without kid, ErrKeyMultiple",
356 args{
357 use: KeyUseSignature,
358 expectedAlg: "RS256",
359 keys: []jose.JSONWebKey{
360 {
361 KeyID: "id1",
362 Key: &rsa.PublicKey{},
363 },
364 {
365 KeyID: "id2",
366 Key: &rsa.PublicKey{},
367 },
368 },
369 },
370 res{
371 key: jose.JSONWebKey{},
372 err: ErrKeyMultiple,
373 },
374 },
375 {
376 "multiple keys, no use or id, jwt with kid, ErrKeyMultiple",
377 args{
378 use: KeyUseSignature,
379 expectedAlg: "RS256",
380 keyID: "id1",
381 keys: []jose.JSONWebKey{
382 {
383 Key: &rsa.PublicKey{},
384 },
385 {
386 Key: &rsa.PublicKey{},
387 },
388 },
389 },
390 res{
391 key: jose.JSONWebKey{},
392 err: ErrKeyMultiple,
393 },
394 },
395 {
396 "multiple keys (only one matching alg), jwt with kid, match",
397 args{
398 use: KeyUseSignature,
399 expectedAlg: "RS256",
400 keyID: "id1",
401 keys: []jose.JSONWebKey{
402 {
403 Key: &rsa.PublicKey{},
404 },
405 {
406 Key: &ecdsa.PublicKey{},
407 },
408 },
409 },
410 res{
411 key: jose.JSONWebKey{
412 Key: &rsa.PublicKey{},
413 },
414 err: nil,
415 },
416 },
417 }
418 for _, tt := range tests {
419 t.Run(tt.name, func(t *testing.T) {
420 got, err := FindMatchingKey(tt.args.keyID, tt.args.use, tt.args.expectedAlg, tt.args.keys...)
421 if (tt.res.err != nil && !errors.Is(err, tt.res.err)) || (tt.res.err == nil && err != nil) {
422 t.Errorf("FindKey() error, got = %v, want = %v", err, tt.res.err)
423 }
424 if !reflect.DeepEqual(got, tt.res.key) {
425 t.Errorf("FindKey() got = %v, want %v", got, tt.res.key)
426 }
427 })
428 }
429 }