forked from fastify/fastify-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.js
502 lines (430 loc) · 15.3 KB
/
jwt.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
'use strict'
const fp = require('fastify-plugin')
const { createSigner, createDecoder, createVerifier, TokenError } = require('fast-jwt')
const assert = require('assert')
const steed = require('steed')
const { parse } = require('@lukeed/ms')
const {
BadRequest,
Unauthorized
} = require('http-errors')
const messages = {
badRequestErrorMessage: 'Format is Authorization: Bearer [token]',
badCookieRequestErrorMessage: 'Cookie could not be parsed in request',
noAuthorizationInHeaderMessage: 'No Authorization was found in request.headers',
noAuthorizationInCookieMessage: 'No Authorization was found in request.cookies',
authorizationTokenExpiredMessage: 'Authorization token expired',
authorizationTokenInvalid: (err) => `Authorization token is invalid: ${err.message}`,
authorizationTokenUntrusted: 'Untrusted authorization token'
}
function wrapStaticSecretInCallback (secret) {
return function (request, payload, cb) {
return cb(null, secret)
}
}
function convertToMs (time) {
// by default if time is number we assume that they are seconds - see README.md
if (typeof time === 'number') {
return time * 1000
}
return parse(time)
}
function convertTemporalProps (options, isVerifyOptions) {
if (options && typeof options !== 'function') {
if (isVerifyOptions && options.maxAge) {
options.maxAge = convertToMs(options.maxAge)
} else if (options.expiresIn || options.notBefore) {
if (options.expiresIn) {
options.expiresIn = convertToMs(options.expiresIn)
}
if (options.notBefore) {
options.notBefore = convertToMs(options.notBefore)
}
}
}
return options
}
function fastifyJwt (fastify, options, next) {
if (!options.secret) {
return next(new Error('missing secret'))
}
if (options.options) {
return next(new Error('options prefix is deprecated'))
}
const secret = options.secret
const trusted = options.trusted
let secretOrPrivateKey
let secretOrPublicKey
if (typeof secret === 'object' && !Buffer.isBuffer(secret)) {
if (!secret.private || !secret.public) {
return next(new Error('missing private key and/or public key'))
}
secretOrPrivateKey = secret.private
secretOrPublicKey = secret.public
} else {
secretOrPrivateKey = secretOrPublicKey = secret
}
let secretCallbackSign = secretOrPrivateKey
let secretCallbackVerify = secretOrPublicKey
if (typeof secretCallbackSign !== 'function') {
secretCallbackSign = wrapStaticSecretInCallback(secretCallbackSign)
}
if (typeof secretCallbackVerify !== 'function') {
secretCallbackVerify = wrapStaticSecretInCallback(secretCallbackVerify)
}
const cookie = options.cookie
const formatUser = options.formatUser
const decodeOptions = options.decode || {}
const signOptions = convertTemporalProps(options.sign) || {}
const verifyOptions = convertTemporalProps(options.verify, true) || {}
const messagesOptions = Object.assign({}, messages, options.messages)
const namespace = typeof options.namespace === 'string' ? options.namespace : undefined
if (
signOptions &&
signOptions.algorithm &&
signOptions.algorithm.includes('RS') &&
(typeof secret === 'string' ||
secret instanceof Buffer)
) {
return next(new Error('RSA Signatures set as Algorithm in the options require a private and public key to be set as the secret'))
}
if (
signOptions &&
signOptions.algorithm &&
signOptions.algorithm.includes('ES') &&
(typeof secret === 'string' ||
secret instanceof Buffer)
) {
return next(new Error('ECDSA Signatures set as Algorithm in the options require a private and public key to be set as the secret'))
}
const jwtConfig = {
decode: decode,
options: {
decode: decodeOptions,
sign: signOptions,
verify: verifyOptions,
messages: messagesOptions
},
cookie: cookie,
sign: sign,
verify: verify,
lookupToken: lookupToken
}
let jwtDecodeName = 'jwtDecode'
let jwtVerifyName = 'jwtVerify'
let jwtSignName = 'jwtSign'
if (namespace) {
if (!fastify.jwt) {
fastify.decorateRequest('user', null)
fastify.decorate('jwt', Object.create(null))
}
if (fastify.jwt[namespace]) {
return next(new Error(`JWT namespace already used "${namespace}"`))
}
fastify.jwt[namespace] = jwtConfig
jwtDecodeName = options.jwtDecode ? (typeof options.jwtDecode === 'string' ? options.jwtDecode : 'jwtDecode') : `${namespace}JwtDecode`
jwtVerifyName = options.jwtVerify || `${namespace}JwtVerify`
jwtSignName = options.jwtSign || `${namespace}JwtSign`
} else {
fastify.decorateRequest('user', null)
fastify.decorate('jwt', jwtConfig)
}
// Temporary conditional to prevent breaking changes by exposing `jwtDecode`,
// which already exists in fastify-auth0-verify.
// If jwtDecode has been requested, or plugin is configured to use a namespace.
// TODO Remove conditional when fastify-jwt >=4.x.x
if (options.jwtDecode || namespace) {
fastify.decorateRequest(jwtDecodeName, requestDecode)
}
fastify.decorateRequest(jwtVerifyName, requestVerify)
fastify.decorateReply(jwtSignName, replySign)
const signerConfig = checkAndMergeSignOptions()
const signer = createSigner(signerConfig.options)
const decoder = createDecoder(decodeOptions)
const verifierConfig = checkAndMergeVerifyOptions()
const verifier = createVerifier(verifierConfig.options)
next()
function decode (token, options) {
assert(token, 'missing token')
if (options && typeof options !== 'function') {
const localDecoder = createDecoder(options)
return localDecoder(token)
}
return decoder(token)
}
function lookupToken (request, options) {
assert(request, 'missing request')
options = Object.assign({}, verifyOptions, options)
let token
const extractToken = options.extractToken
if (extractToken) {
token = extractToken(request)
if (!token) {
throw new BadRequest(messagesOptions.badRequestErrorMessage)
}
} else if (request.headers && request.headers.authorization) {
const parts = request.headers.authorization.split(' ')
if (parts.length === 2) {
const scheme = parts[0]
token = parts[1]
if (!/^Bearer$/i.test(scheme)) {
throw new BadRequest(messagesOptions.badRequestErrorMessage)
}
} else {
throw new BadRequest(messagesOptions.badRequestErrorMessage)
}
} else if (cookie) {
if (request.cookies) {
if (request.cookies[cookie.cookieName]) {
const tokenValue = request.cookies[cookie.cookieName]
token = cookie.signed ? request.unsignCookie(tokenValue).value : tokenValue
} else {
throw new Unauthorized(messagesOptions.noAuthorizationInCookieMessage)
}
} else {
throw new BadRequest(messagesOptions.badCookieRequestErrorMessage)
}
} else {
throw new Unauthorized(messagesOptions.noAuthorizationInHeaderMessage)
}
return token
}
function mergeOptionsWithKey (options, useProvidedPrivateKey) {
if (useProvidedPrivateKey && (typeof useProvidedPrivateKey !== 'boolean')) {
return Object.assign({}, options, { key: useProvidedPrivateKey })
} else {
const key = useProvidedPrivateKey ? secretOrPrivateKey : secretOrPublicKey
return Object.assign(!options.key ? { key } : {}, options)
}
}
function checkAndMergeOptions (options, defaultOptions, usePrivateKey, callback) {
let mergedOptions
if (typeof options === 'function') {
callback = options
mergedOptions = mergeOptionsWithKey(defaultOptions, usePrivateKey)
} else {
if (!options) {
mergedOptions = mergeOptionsWithKey(defaultOptions, usePrivateKey)
} else {
mergedOptions = mergeOptionsWithKey(options, usePrivateKey)
}
}
return { options: mergedOptions, callback }
}
function checkAndMergeSignOptions (options, callback) {
return checkAndMergeOptions(options, signOptions, true, callback)
}
function checkAndMergeVerifyOptions (options, callback) {
return checkAndMergeOptions(options, verifyOptions, false, callback)
}
function sign (payload, options, callback) {
assert(payload, 'missing payload')
let localSigner = signer
convertTemporalProps(options)
const signerConfig = checkAndMergeSignOptions(options, callback)
if (options && typeof options !== 'function') {
localSigner = createSigner(signerConfig.options)
}
if (typeof signerConfig.callback === 'function') {
const token = localSigner(payload)
signerConfig.callback(null, token)
} else {
return localSigner(payload)
}
}
function verify (token, options, callback) {
assert(token, 'missing token')
assert(secretOrPublicKey, 'missing secret')
let localVerifier = verifier
convertTemporalProps(options, true)
const veriferConfig = checkAndMergeVerifyOptions(options, callback)
if (options && typeof options !== 'function') {
localVerifier = createVerifier(veriferConfig.options)
}
if (typeof veriferConfig.callback === 'function') {
const result = localVerifier(token)
veriferConfig.callback(null, result)
} else {
return localVerifier(token)
}
}
function replySign (payload, options, next) {
let useLocalSigner = true
if (typeof options === 'function') {
next = options
options = {}
useLocalSigner = false
} // support no options
if (!options) {
options = {}
useLocalSigner = false
}
const reply = this
if (next === undefined) {
return new Promise(function (resolve, reject) {
reply[jwtSignName](payload, options, function (err, val) {
err ? reject(err) : resolve(val)
})
})
}
if (options.sign) {
convertTemporalProps(options.sign)
// New supported contract, options supports sign and can expand
options = {
sign: mergeOptionsWithKey({ ...signOptions, ...options.sign }, true)
}
} else {
convertTemporalProps(options)
// Original contract, options supports only sign
options = mergeOptionsWithKey({ ...signOptions, ...options }, true)
}
if (!payload) {
return next(new Error('jwtSign requires a payload'))
}
steed.waterfall([
function getSecret (callback) {
const signResult = secretCallbackSign(reply.request, payload, callback)
if (signResult && typeof signResult.then === 'function') {
signResult.then(result => callback(null, result), callback)
}
},
function sign (secretOrPrivateKey, callback) {
if (useLocalSigner) {
const signerOptions = mergeOptionsWithKey(options.sign || options, secretOrPrivateKey)
const localSigner = createSigner(signerOptions)
const token = localSigner(payload)
callback(null, token)
} else {
const token = signer(payload)
callback(null, token)
}
}
], next)
}
function requestDecode (options, next) {
if (typeof options === 'function' && !next) {
next = options
options = {}
} // support no options
if (!options) {
options = {}
}
options = {
decode: Object.assign({}, decodeOptions, options.decode),
verify: Object.assign({}, verifyOptions, options.verify)
}
const request = this
if (next === undefined) {
return new Promise(function (resolve, reject) {
request[jwtDecodeName](options, function (err, val) {
err ? reject(err) : resolve(val)
})
})
}
try {
const token = lookupToken(request, options.verify)
const decodedToken = decode(token, options.decode)
return next(null, decodedToken)
} catch (err) {
return next(err)
}
}
function requestVerify (options, next) {
let useLocalVerifier = true
if (typeof options === 'function' && !next) {
next = options
options = {}
useLocalVerifier = false
} // support no options
if (!options) {
options = {}
useLocalVerifier = false
}
if (options.decode || options.verify) {
convertTemporalProps(options.verify, true)
// New supported contract, options supports both decode and verify
options = {
decode: Object.assign({}, decodeOptions, options.decode),
verify: Object.assign({}, verifyOptions, options.verify)
}
} else {
convertTemporalProps(options, true)
// Original contract, options supports only verify
options = Object.assign({}, verifyOptions, options)
}
const request = this
if (next === undefined) {
return new Promise(function (resolve, reject) {
request[jwtVerifyName](options, function (err, val) {
err ? reject(err) : resolve(val)
})
})
}
let token
try {
token = lookupToken(request, options.verify || options)
} catch (err) {
return next(err)
}
const decodedToken = decode(token, options.decode || decodeOptions)
steed.waterfall([
function getSecret (callback) {
const verifyResult = secretCallbackVerify(request, decodedToken, callback)
if (verifyResult && typeof verifyResult.then === 'function') {
verifyResult.then(result => callback(null, result), callback)
}
},
function verify (secretOrPublicKey, callback) {
try {
if (useLocalVerifier) {
const verifierOptions = mergeOptionsWithKey(options.verify || options, secretOrPublicKey)
const localVerifier = createVerifier(verifierOptions)
const verifyResult = localVerifier(token)
callback(null, verifyResult)
} else {
const verifyResult = verifier(token)
callback(null, verifyResult)
}
} catch (error) {
if (error.code === TokenError.codes.expired) {
return callback(new Unauthorized(messagesOptions.authorizationTokenExpiredMessage))
}
if (error.code === TokenError.codes.invalidKey ||
error.code === TokenError.codes.invalidSignature ||
error.code === TokenError.codes.invalidClaimValue
) {
return callback(new Unauthorized(typeof messagesOptions.authorizationTokenInvalid === 'function' ? messagesOptions.authorizationTokenInvalid(error) : messagesOptions.authorizationTokenInvalid))
}
return callback(error)
}
},
function checkIfIsTrusted (result, callback) {
if (!trusted) {
callback(null, result)
} else {
const maybePromise = trusted(request, result)
if (maybePromise && maybePromise.then) {
maybePromise
.then(trusted => trusted ? callback(null, result) : callback(new Unauthorized(messagesOptions.authorizationTokenUntrusted)))
} else if (maybePromise) {
callback(null, maybePromise)
} else {
callback(new Unauthorized(messagesOptions.authorizationTokenUntrusted))
}
}
}
], function (err, result) {
if (err) {
next(err)
} else {
const user = formatUser ? formatUser(result) : result
request.user = user
next(null, user)
}
})
}
}
module.exports = fp(fastifyJwt, {
fastify: '>=4.0.0-alpha.1',
name: 'fastify-jwt'
})