-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathnamespace.test.js
More file actions
132 lines (115 loc) · 3.85 KB
/
Copy pathnamespace.test.js
File metadata and controls
132 lines (115 loc) · 3.85 KB
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
'use strict'
const { test } = require('node:test')
const Fastify = require('fastify')
const jwt = require('..')
test('Unable to add the namespace twice', async function (t) {
t.plan(1)
const fastify = Fastify()
fastify.register(jwt, { secret: 'test', namespace: 'security', jwtVerify: 'securityVerify', jwtSign: 'securitySign' })
await t.assert.rejects(() => fastify.register(jwt, { secret: 'hello', namespace: 'security', jwtVerify: 'secureVerify', jwtSign: 'secureSign' })
.ready(), new Error('JWT namespace already used "security"'))
})
test('multiple namespace', async function (t) {
const fastify = Fastify()
fastify.register(jwt, { namespace: 'aaa', secret: 'test', verify: { extractToken: (request) => request.headers.customauthheader } })
fastify.register(jwt, { namespace: 'bbb', secret: 'sea', verify: { extractToken: (request) => request.headers.customauthheader }, jwtVerify: 'verifyCustom', jwtSign: 'signCustom', jwtDecode: 'decodeCustom' })
fastify.post('/sign/:namespace', async function (request, reply) {
switch (request.params.namespace) {
case 'aaa':
return reply.aaaJwtSign(request.body)
case 'bbb':
return reply.signCustom(request.body)
default:
reply.code(501).send({ message: `Namespace ${request.params.namespace} is not implemented correctly` })
}
})
fastify.get('/verify/:namespace', async function (request, reply) {
switch (request.params.namespace) {
case 'aaa':
return request.aaaJwtVerify()
case 'bbb':
return request.verifyCustom()
default:
reply.code(501).send({ message: `Namespace ${request.params.namespace} is not implemented correctly` })
}
})
fastify.get('/decode/:namespace', async function (request, reply) {
switch (request.params.namespace) {
case 'aaa':
return request.aaaJwtDecode()
case 'bbb':
return request.decodeCustom()
default:
reply.code(501).send({ message: `Namespace ${request.params.namespace} is not implemented correctly` })
}
})
await fastify.ready()
let signResponse
let verifyResponse
signResponse = await fastify.inject({
method: 'post',
url: '/sign/aaa',
payload: { foo: 'bar' }
})
const tokenA = signResponse.payload
t.assert.ok(tokenA)
verifyResponse = await fastify.inject({
method: 'get',
url: '/verify/aaa',
headers: {
customauthheader: tokenA
}
})
t.assert.strictEqual(verifyResponse.statusCode, 200)
t.assert.strictEqual(verifyResponse.json().foo, 'bar')
verifyResponse = await fastify.inject({
method: 'get',
url: '/verify/bbb',
headers: {
customauthheader: tokenA
}
})
t.assert.strictEqual(verifyResponse.statusCode, 401)
signResponse = await fastify.inject({
method: 'post',
url: '/sign/bbb',
payload: { foo: 'sky' }
})
const tokenB = signResponse.payload
t.assert.ok(tokenB)
verifyResponse = await fastify.inject({
method: 'get',
url: '/verify/bbb',
headers: {
customauthheader: tokenB
}
})
t.assert.strictEqual(verifyResponse.statusCode, 200)
t.assert.strictEqual(verifyResponse.json().foo, 'sky')
verifyResponse = await fastify.inject({
method: 'get',
url: '/verify/aaa',
headers: {
customauthheader: tokenB
}
})
t.assert.strictEqual(verifyResponse.statusCode, 401)
const decodeResponseAAA = await fastify.inject({
method: 'get',
url: '/decode/aaa',
headers: {
customauthheader: tokenA
}
})
t.assert.strictEqual(decodeResponseAAA.statusCode, 200)
t.assert.strictEqual(decodeResponseAAA.json().foo, 'bar')
const verifyResponseBBB = await fastify.inject({
method: 'get',
url: '/decode/bbb',
headers: {
customauthheader: tokenB
}
})
t.assert.strictEqual(verifyResponseBBB.statusCode, 200)
t.assert.strictEqual(verifyResponseBBB.json().foo, 'sky')
})