1 | const test = require('ava')
|
2 | const helpers = require('./_helpers')
|
3 | const fixtures = require('./fixtures')
|
4 | const utils = require('../src/utils')
|
5 |
|
6 | const valids = [
|
7 | {},
|
8 | { cache: {} },
|
9 | { cache: { segment: 'foobar' } },
|
10 | { cache: true },
|
11 | { cache: false },
|
12 | { userInfo: [] },
|
13 | { userInfo: ['string'] },
|
14 | { minTimeBetweenJwksRequests: 0 },
|
15 | { minTimeBetweenJwksRequests: 42 },
|
16 | { apiKey: {
|
17 | url: 'http://foobar.com/foo/bar'
|
18 | } },
|
19 | { apiKey: {
|
20 | url: 'http://foobar.com/foo/bar',
|
21 | in: 'headers'
|
22 | } },
|
23 | { apiKey: {
|
24 | url: 'http://foobar.com/foo/bar',
|
25 | in: 'query'
|
26 | } },
|
27 | { apiKey: {
|
28 | url: 'http://foobar.com/foo/bar',
|
29 | name: 'foobar'
|
30 | } },
|
31 | { apiKey: {
|
32 | url: 'http://foobar.com/foo/bar',
|
33 | prefix: 'barfoo '
|
34 | } },
|
35 | { apiKey: {
|
36 | url: 'http://foobar.com/foo/bar',
|
37 | request: {}
|
38 | } },
|
39 | { apiKey: {
|
40 | url: 'http://foobar.com/foo/bar',
|
41 | tokenPath: 'foo.bar'
|
42 | } },
|
43 | { apiKey: {
|
44 | url: 'http://foobar.com/{client}/bar'
|
45 | } }
|
46 | ]
|
47 |
|
48 | test('throw error if options are empty', (t) => {
|
49 | t.throws(() => utils.verify())
|
50 | t.throws(() => utils.verify({}))
|
51 | })
|
52 |
|
53 | test('throw error if options are invalid – schemeName', (t) => {
|
54 | const invalids = [
|
55 | null,
|
56 | NaN,
|
57 | 42,
|
58 | true,
|
59 | false,
|
60 | [],
|
61 | new RegExp(),
|
62 | {}
|
63 | ]
|
64 |
|
65 | t.plan(invalids.length)
|
66 |
|
67 | invalids.forEach((invalid) => {
|
68 | t.throws(() => utils.verify(helpers.getOptions({
|
69 | schemeName: invalid
|
70 | })))
|
71 | })
|
72 | })
|
73 |
|
74 | test('throw error if options are invalid – decoratorName', (t) => {
|
75 | const invalids = [
|
76 | null,
|
77 | NaN,
|
78 | 42,
|
79 | true,
|
80 | false,
|
81 | [],
|
82 | new RegExp(),
|
83 | {}
|
84 | ]
|
85 |
|
86 | t.plan(invalids.length)
|
87 |
|
88 | invalids.forEach((invalid) => {
|
89 | t.throws(() => utils.verify(helpers.getOptions({
|
90 | decoratorName: invalid
|
91 | })))
|
92 | })
|
93 | })
|
94 |
|
95 | test('throw error if options are invalid – realmUrl', (t) => {
|
96 | const invalids = [
|
97 | null,
|
98 | undefined,
|
99 | NaN,
|
100 | '',
|
101 | 'foobar',
|
102 | 42,
|
103 | true,
|
104 | false,
|
105 | [],
|
106 | new RegExp(),
|
107 | {}
|
108 | ]
|
109 |
|
110 | t.plan(invalids.length)
|
111 |
|
112 | invalids.forEach((invalid) => {
|
113 | t.throws(() => utils.verify(helpers.getOptions({
|
114 | realmUrl: invalid
|
115 | })))
|
116 | })
|
117 | })
|
118 |
|
119 | test('throw error if options are invalid – clientId', (t) => {
|
120 | const invalids = [
|
121 | null,
|
122 | undefined,
|
123 | NaN,
|
124 | '',
|
125 | 42,
|
126 | true,
|
127 | false,
|
128 | [],
|
129 | new RegExp(),
|
130 | {}
|
131 | ]
|
132 |
|
133 | t.plan(invalids.length)
|
134 |
|
135 | invalids.forEach((invalid) => {
|
136 | t.throws(() => utils.verify(helpers.getOptions({
|
137 | clientId: invalid
|
138 | })))
|
139 | })
|
140 | })
|
141 |
|
142 | test('throw error if options are invalid – secret', (t) => {
|
143 | const invalids = [
|
144 | null,
|
145 | NaN,
|
146 | '',
|
147 | 42,
|
148 | true,
|
149 | false,
|
150 | [],
|
151 | new RegExp(),
|
152 | {}
|
153 | ]
|
154 |
|
155 | t.plan(invalids.length)
|
156 |
|
157 | invalids.forEach((invalid) => {
|
158 | t.throws(() => utils.verify(helpers.getOptions({
|
159 | secret: invalid
|
160 | })))
|
161 | })
|
162 | })
|
163 |
|
164 | test('throw error if options are invalid – publicKey', (t) => {
|
165 | const invalids = [
|
166 | null,
|
167 | NaN,
|
168 | '',
|
169 | 'foobar',
|
170 | fixtures.common.baseUrl,
|
171 | 42,
|
172 | true,
|
173 | false,
|
174 | [],
|
175 | new RegExp(),
|
176 | {},
|
177 | {
|
178 | foobar: 42
|
179 | },
|
180 | fixtures.common.publicKey,
|
181 | fixtures.common.publicKeyCert
|
182 | ]
|
183 |
|
184 | t.plan(invalids.length)
|
185 |
|
186 | invalids.forEach((invalid) => {
|
187 | t.throws(() => utils.verify(helpers.getOptions({
|
188 | publicKey: invalid
|
189 | })))
|
190 | })
|
191 | })
|
192 |
|
193 | test('throw error if options are invalid – cache', (t) => {
|
194 | const invalids = [
|
195 | null,
|
196 | NaN,
|
197 | '',
|
198 | 'foobar',
|
199 | fixtures.common.baseUrl,
|
200 | 42,
|
201 | []
|
202 | ]
|
203 |
|
204 | t.plan(invalids.length)
|
205 |
|
206 | invalids.forEach((invalid) => {
|
207 | t.throws(() => utils.verify(helpers.getOptions({
|
208 | cache: invalid
|
209 | })))
|
210 | })
|
211 | })
|
212 |
|
213 | test('throw error if options are invalid – userInfo', (t) => {
|
214 | const invalids = [
|
215 | null,
|
216 | NaN,
|
217 | '',
|
218 | 'foobar',
|
219 | fixtures.common.baseUrl,
|
220 | 42,
|
221 | true,
|
222 | false,
|
223 | new RegExp(),
|
224 | {},
|
225 | [null],
|
226 | [undefined],
|
227 | [NaN],
|
228 | [''],
|
229 | [42],
|
230 | [true],
|
231 | [false],
|
232 | [[]],
|
233 | [new RegExp()],
|
234 | [{}]
|
235 | ]
|
236 |
|
237 | t.plan(invalids.length)
|
238 |
|
239 | invalids.forEach((invalid) => {
|
240 | t.throws(() => utils.verify(helpers.getOptions({
|
241 | userInfo: invalid
|
242 | })))
|
243 | })
|
244 | })
|
245 |
|
246 | test('throw error if options are invalid – minTimeBetweenJwksRequests', (t) => {
|
247 | const invalids = [
|
248 | null,
|
249 | NaN,
|
250 | '',
|
251 | 'foobar',
|
252 | fixtures.common.baseUrl,
|
253 | -42,
|
254 | 4.2,
|
255 | true,
|
256 | false,
|
257 | new RegExp(),
|
258 | {},
|
259 | []
|
260 | ]
|
261 |
|
262 | t.plan(invalids.length)
|
263 |
|
264 | invalids.forEach((invalid) => {
|
265 | t.throws(() => utils.verify(helpers.getOptions({
|
266 | minTimeBetweenJwksRequests: invalid
|
267 | })))
|
268 | })
|
269 | })
|
270 |
|
271 | test('throw error if options are invalid – entitlement', (t) => {
|
272 | const invalids = [
|
273 | null,
|
274 | NaN,
|
275 | '',
|
276 | 'foobar',
|
277 | fixtures.common.baseUrl,
|
278 | 42,
|
279 | false,
|
280 | new RegExp(),
|
281 | {},
|
282 | []
|
283 | ]
|
284 |
|
285 | t.plan(invalids.length)
|
286 |
|
287 | invalids.forEach((invalid) => {
|
288 | t.throws(() => utils.verify(helpers.getOptions({
|
289 | entitlement: invalid
|
290 | })))
|
291 | })
|
292 | })
|
293 |
|
294 | test('throw error if options are invalid – apiKey', (t) => {
|
295 | const invalids = [
|
296 | null,
|
297 | NaN,
|
298 | '',
|
299 | 'foobar',
|
300 | fixtures.common.baseUrl,
|
301 | 42,
|
302 | [],
|
303 | true,
|
304 | false,
|
305 | {}
|
306 | ]
|
307 |
|
308 | t.plan(invalids.length)
|
309 |
|
310 | invalids.forEach((invalid) => {
|
311 | t.throws(() => utils.verify(helpers.getOptions({
|
312 | apiKey: invalid
|
313 | })))
|
314 | })
|
315 | })
|
316 |
|
317 | test('throw error if options are invalid – apiKey.url', (t) => {
|
318 | const invalids = [
|
319 | null,
|
320 | NaN,
|
321 | '',
|
322 | 42,
|
323 | true,
|
324 | false,
|
325 | [],
|
326 | new RegExp(),
|
327 | {}
|
328 | ]
|
329 |
|
330 | t.plan(invalids.length)
|
331 |
|
332 | invalids.forEach((invalid) => {
|
333 | t.throws(() => utils.verify(helpers.getOptions({
|
334 | apiKey: {
|
335 | url: invalid
|
336 | }
|
337 | })))
|
338 | })
|
339 | })
|
340 |
|
341 | test('throw error if options are invalid – apiKey.name', (t) => {
|
342 | const invalids = [
|
343 | null,
|
344 | NaN,
|
345 | '',
|
346 | 42,
|
347 | true,
|
348 | false,
|
349 | [],
|
350 | new RegExp(),
|
351 | {}
|
352 | ]
|
353 |
|
354 | t.plan(invalids.length)
|
355 |
|
356 | invalids.forEach((invalid) => {
|
357 | t.throws(() => utils.verify(helpers.getOptions({
|
358 | apiKey: {
|
359 | url: 'http://foobar.com/foo/bar',
|
360 | name: invalid
|
361 | }
|
362 | })))
|
363 | })
|
364 | })
|
365 |
|
366 | test('throw error if options are invalid – apiKey.prefix', (t) => {
|
367 | const invalids = [
|
368 | null,
|
369 | NaN,
|
370 | '',
|
371 | 42,
|
372 | true,
|
373 | false,
|
374 | [],
|
375 | new RegExp(),
|
376 | {}
|
377 | ]
|
378 |
|
379 | t.plan(invalids.length)
|
380 |
|
381 | invalids.forEach((invalid) => {
|
382 | t.throws(() => utils.verify(helpers.getOptions({
|
383 | apiKey: {
|
384 | url: 'http://foobar.com/foo/bar',
|
385 | prefix: invalid
|
386 | }
|
387 | })))
|
388 | })
|
389 | })
|
390 |
|
391 | test('throw error if options are invalid – apiKey.tokenPath', (t) => {
|
392 | const invalids = [
|
393 | null,
|
394 | NaN,
|
395 | '',
|
396 | 42,
|
397 | true,
|
398 | false,
|
399 | [],
|
400 | new RegExp(),
|
401 | {}
|
402 | ]
|
403 |
|
404 | t.plan(invalids.length)
|
405 |
|
406 | invalids.forEach((invalid) => {
|
407 | t.throws(() => utils.verify(helpers.getOptions({
|
408 | apiKey: {
|
409 | url: 'http://foobar.com/foo/bar',
|
410 | tokenPath: invalid
|
411 | }
|
412 | })))
|
413 | })
|
414 | })
|
415 |
|
416 | test('throw error if options are invalid – apiKey.in', (t) => {
|
417 | const invalids = [
|
418 | null,
|
419 | NaN,
|
420 | '',
|
421 | 42,
|
422 | true,
|
423 | false,
|
424 | [],
|
425 | new RegExp(),
|
426 | {},
|
427 | 'foo'
|
428 | ]
|
429 |
|
430 | t.plan(invalids.length)
|
431 |
|
432 | invalids.forEach((invalid) => {
|
433 | t.throws(() => utils.verify(helpers.getOptions({
|
434 | apiKey: {
|
435 | url: 'http://foobar.com/foo/bar',
|
436 | in: invalid
|
437 | }
|
438 | })))
|
439 | })
|
440 | })
|
441 |
|
442 | test('throw error if options are invalid – apiKey.options', (t) => {
|
443 | const invalids = [
|
444 | null,
|
445 | NaN,
|
446 | '',
|
447 | 'foobar',
|
448 | fixtures.common.baseUrl,
|
449 | 42,
|
450 | [],
|
451 | {}
|
452 | ]
|
453 |
|
454 | t.plan(invalids.length)
|
455 |
|
456 | invalids.forEach((invalid) => {
|
457 | t.throws(() => utils.verify(helpers.getOptions({
|
458 | apiKey: {
|
459 | url: 'http://foobar.com/foo/bar',
|
460 | options: invalid
|
461 | }
|
462 | })))
|
463 | })
|
464 | })
|
465 |
|
466 | test('throw error if options are invalid – publicKey/secret/entitlement conflict', (t) => {
|
467 | t.throws(() => utils.verify(helpers.getOptions({
|
468 | publicKey: fixtures.common.publicKeyRsa,
|
469 | secret: fixtures.common.secret
|
470 | })))
|
471 |
|
472 | t.throws(() => utils.verify(helpers.getOptions({
|
473 | publicKey: fixtures.common.publicKeyRsa,
|
474 | entitlement: true
|
475 | })))
|
476 |
|
477 | t.throws(() => utils.verify(helpers.getOptions({
|
478 | secret: fixtures.common.secret,
|
479 | entitlement: true
|
480 | })))
|
481 | })
|
482 |
|
483 | test('throw no error if options are valid – schemeName', (t) => {
|
484 | const customValids = [
|
485 | ...valids,
|
486 | { schemeName: '' },
|
487 | { schemeName: 'foobar' },
|
488 | { schemeName: undefined }
|
489 | ]
|
490 |
|
491 | t.plan(customValids.length)
|
492 |
|
493 | customValids.forEach((valid) => {
|
494 | t.notThrows(
|
495 | () => utils.verify(helpers.getOptions(valid))
|
496 | )
|
497 | })
|
498 | })
|
499 |
|
500 | test('throw no error if options are valid – decoratorName', (t) => {
|
501 | const customValids = [
|
502 | ...valids,
|
503 | { decoratorName: '' },
|
504 | { decoratorName: 'foobar' },
|
505 | { decoratorName: undefined }
|
506 | ]
|
507 |
|
508 | t.plan(customValids.length)
|
509 |
|
510 | customValids.forEach((valid) => {
|
511 | t.notThrows(
|
512 | () => utils.verify(helpers.getOptions(valid))
|
513 | )
|
514 | })
|
515 | })
|
516 |
|
517 | test('throw no error if options are valid – secret', (t) => {
|
518 | t.plan(valids.length)
|
519 |
|
520 | valids.forEach((valid) => {
|
521 | t.notThrows(
|
522 | () => utils.verify(helpers.getOptions(Object.assign({
|
523 | secret: fixtures.common.secret
|
524 | }, valid)))
|
525 | )
|
526 | })
|
527 | })
|
528 |
|
529 | test('throw no error if options are valid – offline', (t) => {
|
530 | const customValids = [...valids, { entitlement: true }]
|
531 |
|
532 | t.plan(customValids.length)
|
533 |
|
534 | customValids.forEach((valid) => {
|
535 | t.notThrows(
|
536 | () => utils.verify(helpers.getOptions(valid))
|
537 | )
|
538 | })
|
539 | })
|
540 |
|
541 | test('throw no error if options are valid – publicKey/Rsa', (t) => {
|
542 | t.plan(valids.length)
|
543 |
|
544 | valids.forEach((valid) => {
|
545 | t.notThrows(
|
546 | () => utils.verify(helpers.getOptions(Object.assign({
|
547 | publicKey: fixtures.common.publicKeyRsa
|
548 | }, valid)))
|
549 | )
|
550 | })
|
551 | })
|
552 |
|
553 | test('throw no error if options are valid – publicKey/Buffer', (t) => {
|
554 | t.plan(valids.length)
|
555 |
|
556 | valids.forEach((valid) => {
|
557 | t.notThrows(
|
558 | () => utils.verify(helpers.getOptions(Object.assign({
|
559 | publicKey: fixtures.common.publicKeyBuffer
|
560 | }, valid)))
|
561 | )
|
562 | })
|
563 | })
|
564 |
|
565 | test('throw no error if options are valid – publicKey/JWK', (t) => {
|
566 | t.plan(valids.length)
|
567 |
|
568 | valids.forEach((valid) => {
|
569 | t.notThrows(
|
570 | () => utils.verify(helpers.getOptions(Object.assign({
|
571 | publicKey: fixtures.common.publicKeyJwk
|
572 | }, valid)))
|
573 | )
|
574 | })
|
575 | })
|
576 |
|
577 | test('sets default correctly – schemeName', (t) => {
|
578 | const opts = utils.verify(helpers.getOptions({ schemeName: undefined }))
|
579 | t.is(opts.schemeName, 'keycloak-jwt')
|
580 | })
|
581 |
|
582 | test('sets default correctly – decoratorName', (t) => {
|
583 | const opts = utils.verify(helpers.getOptions({ decoratorName: undefined }))
|
584 | t.is(opts.decoratorName, 'kjwt')
|
585 | })
|