/
encrypt.test.mjs
152 lines (135 loc) · 5.26 KB
/
encrypt.test.mjs
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
import test from 'ava'
import timekeeper from 'timekeeper'
const { EncryptJWT, compactDecrypt, jwtDecrypt } = await import('#dist')
const now = 1604416038
test.before(async (t) => {
t.context.secret = new Uint8Array(16)
t.context.initializationVector = new Uint8Array(12)
t.context.payload = { 'urn:example:claim': true }
timekeeper.freeze(new Date(now * 1000))
})
test.after(timekeeper.reset)
test('EncryptJWT', async (t) => {
const jwt = await new EncryptJWT(t.context.payload)
.setInitializationVector(t.context.initializationVector)
.setProtectedHeader({ alg: 'dir', enc: 'A128GCM' })
.encrypt(t.context.secret)
t.is(
jwt,
'eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0..AAAAAAAAAAAAAAAA.eKqvvA6MxuqSRbLVFIidFJb8x4lzPytWkoA.aglYAurAaFCoM8sCqaXSyw',
)
})
test('EncryptJWT w/crit', async (t) => {
const expected =
'eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIiwiY3JpdCI6WyJodHRwOi8vb3BlbmJhbmtpbmcub3JnLnVrL2lhdCJdLCJodHRwOi8vb3BlbmJhbmtpbmcub3JnLnVrL2lhdCI6MH0..AAAAAAAAAAAAAAAA.eKqvvA6MxuqSRbLVFIidFJb8x4lzPytWkoA.Kl-auiUImwUWk4X0xpxa8A'
await t.throwsAsync(
new EncryptJWT(t.context.payload)
.setInitializationVector(t.context.initializationVector)
.setProtectedHeader({
alg: 'dir',
enc: 'A128GCM',
crit: ['http://openbanking.org.uk/iat'],
'http://openbanking.org.uk/iat': 0,
})
.encrypt(t.context.secret),
{
code: 'ERR_JOSE_NOT_SUPPORTED',
message: 'Extension Header Parameter "http://openbanking.org.uk/iat" is not recognized',
},
)
await t.notThrowsAsync(async () => {
const jwt = await new EncryptJWT(t.context.payload)
.setInitializationVector(t.context.initializationVector)
.setProtectedHeader({
alg: 'dir',
enc: 'A128GCM',
crit: ['http://openbanking.org.uk/iat'],
'http://openbanking.org.uk/iat': 0,
})
.encrypt(t.context.secret, { crit: { 'http://openbanking.org.uk/iat': true } })
t.is(jwt, expected)
})
await t.throwsAsync(jwtDecrypt(expected, t.context.secret), {
code: 'ERR_JOSE_NOT_SUPPORTED',
message: 'Extension Header Parameter "http://openbanking.org.uk/iat" is not recognized',
})
await t.notThrowsAsync(
jwtDecrypt(expected, t.context.secret, { crit: { 'http://openbanking.org.uk/iat': true } }),
)
})
async function testJWTsetFunction(t, method, claim, value, duplicate = false, expected = value) {
let enc = new EncryptJWT().setProtectedHeader({ alg: 'dir', enc: 'A128GCM' })[method](value)
if (duplicate) {
enc = enc[`replicate${method.slice(3)}AsHeader`]()
}
const jwt = await enc.encrypt(t.context.secret)
const {
plaintext,
protectedHeader,
key: resolvedKey,
} = await compactDecrypt(jwt, async (header, token) => {
t.true('alg' in header)
t.true('enc' in header)
t.is(header.alg, 'dir')
t.is(header.enc, 'A128GCM')
t.true('ciphertext' in token)
t.true('iv' in token)
t.true('protected' in token)
t.true('tag' in token)
return t.context.secret
})
t.is(resolvedKey, t.context.secret)
const payload = JSON.parse(new TextDecoder().decode(plaintext))
t.is(payload[claim], expected)
if (duplicate) {
t.true(claim in protectedHeader)
t.is(protectedHeader[claim], expected)
} else {
t.false(claim in protectedHeader)
}
}
testJWTsetFunction.title = (title, method, claim, value) =>
`EncryptJWT.prototype.${method} called with ${value}${title ? ` (${title})` : ''}`
test(testJWTsetFunction, 'setIssuer', 'iss', 'urn:example:issuer')
test('duplicated', testJWTsetFunction, 'setIssuer', 'iss', 'urn:example:issuer', true)
test(testJWTsetFunction, 'setSubject', 'sub', 'urn:example:subject')
test('duplicated', testJWTsetFunction, 'setSubject', 'sub', 'urn:example:subject', true)
test(testJWTsetFunction, 'setAudience', 'aud', 'urn:example:audience')
test('duplicated', testJWTsetFunction, 'setAudience', 'aud', 'urn:example:audience', true)
test(testJWTsetFunction, 'setJti', 'jti', 'urn:example:jti')
test(testJWTsetFunction, 'setIssuedAt', 'iat', 0)
test(testJWTsetFunction, 'setIssuedAt', 'iat', undefined, undefined, now)
test(testJWTsetFunction, 'setExpirationTime', 'exp', 0)
test(testJWTsetFunction, 'setExpirationTime', 'exp', '10s', undefined, now + 10)
test(testJWTsetFunction, 'setNotBefore', 'nbf', 0)
test(testJWTsetFunction, 'setNotBefore', 'nbf', '10s', undefined, now + 10)
test('EncryptJWT.prototype.setProtectedHeader', (t) => {
t.throws(() => new EncryptJWT(t.context.payload).setProtectedHeader({}).setProtectedHeader({}), {
instanceOf: TypeError,
message: 'setProtectedHeader can only be called once',
})
})
test('EncryptJWT.prototype.setContentEncryptionKey', (t) => {
t.throws(
() =>
new EncryptJWT(t.context.payload)
.setContentEncryptionKey(t.context.secret)
.setContentEncryptionKey(t.context.secret),
{
instanceOf: TypeError,
message: 'setContentEncryptionKey can only be called once',
},
)
})
test('EncryptJWT.prototype.setInitializationVector', (t) => {
t.throws(
() =>
new EncryptJWT(t.context.payload)
.setInitializationVector(t.context.initializationVector)
.setInitializationVector(t.context.initializationVector),
{
instanceOf: TypeError,
message: 'setInitializationVector can only be called once',
},
)
})