-
Notifications
You must be signed in to change notification settings - Fork 28
/
types.spec.ts
113 lines (98 loc) · 3.03 KB
/
types.spec.ts
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
/* eslint-disable no-unused-expressions */
/* eslint-disable @typescript-eslint/no-unused-expressions */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-empty-function */
import { createDecoder, createSigner, createVerifier, DecodedJwt, JwtHeader, TokenError, TokenValidationErrorCode } from '..'
import { expectAssignable, expectNotAssignable, expectType } from 'tsd'
// Signing
// Buffer key, both async/callback styles
const signerSync = createSigner({ key: Buffer.from('KEY'), algorithm: 'RS256' })
signerSync({ key: '1' })
const signerAsync = createSigner({ key: () => Buffer.from('KEY'), algorithm: 'RS256' })
signerAsync({ key: '1' }).then(console.log, console.log)
signerAsync({ key: '1' }, (_e: Error | null, _token?: string) => {})
// Dynamic key in callback style
createSigner({
clockTimestamp: 10,
key(_decodedJwt: DecodedJwt, cb: (e: Error | null, key: string) => void): void {
cb(null, 'KEY')
}
})({ key: 1 }).then(console.log, console.log)
// Dynamic key in async style
createSigner({
clockTimestamp: 10,
async key(_decodedJwt: DecodedJwt) {
return 'KEY'
}
})({ key: 1 }).then(console.log, console.log)
// expiresIn as a string
createSigner({
expiresIn: '10min',
key: Buffer.from('KEY'),
algorithm: 'RS256'
})
// Decoding
const decoder = createDecoder({ checkTyp: 'true' })
decoder('FOO')
decoder(Buffer.from('FOO'))
// Verifying
// String key, both async/callback styles
const verifierSync = createVerifier({ key: 'KEY', algorithms: ['RS256'], requiredClaims: ['aud'], checkTyp: 'JWT' })
verifierSync('2134')
const verifierAsync = createVerifier({ key: () => 'KEY', algorithms: ['RS256'] })
verifierAsync('123').then(console.log, console.log)
verifierAsync(Buffer.from('456'), (_e: Error | null, _token?: string) => {})
// Dynamic key in callback style
createVerifier({
clockTimestamp: 10,
key(_decodedJwt: DecodedJwt, cb: (e: Error | null, key: string) => void): void {
cb(null, 'KEY')
}
})('123').then(console.log, console.log)
// Dynamic key in async style
createVerifier({
clockTimestamp: 10,
async key(decodedJwt: DecodedJwt) {
if (decodedJwt.payload.iss) {
return 'ISS_KEY'
}
return 'KEY'
}
})('456').then(console.log, console.log)
// Errors
const wrapped = TokenError.wrap(new Error('ORIGINAL'), 'FAST_JWT_INVALID_TYPE', 'MESSAGE')
wrapped.code === 'FAST_JWT_INVALID_TYPE'
wrapped.message === 'MESSAGE'
Array.isArray(wrapped.stack)
wrapped.originalError.message === 'ORIGINAL'
const signerOptions = {
header: {
alg: 'RS256',
typ: '',
cty: '',
crit: [''],
kid: '',
jku: '',
x5u: '',
'x5t#S256': '',
x5t: '',
x5c: ''
}
}
expectAssignable<JwtHeader>(signerOptions.header)
const signerOptionsNoAlg = {
header: {
typ: '',
cty: '',
crit: [''],
kid: '',
jku: '',
x5u: '',
'x5t#S256': '',
x5t: '',
x5c: ''
}
}
expectNotAssignable<JwtHeader>(signerOptionsNoAlg.header)
// Check all errors are typed correctly
expectType<TokenValidationErrorCode[]>(Object.values(TokenError.codes))