Skip to content

Commit 765d4de

Browse files
committed
Fixups
1 parent fdb603e commit 765d4de

File tree

1 file changed

+36
-40
lines changed

1 file changed

+36
-40
lines changed

jwt.js

+36-40
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ const messages = {
1818
authorizationTokenUnsigned: 'Unsigned authorization token'
1919
}
2020

21-
const validatorCache = new Map()
22-
const maxCacheSize = 1000
23-
2421
function isString (x) {
2522
return Object.prototype.toString.call(x) === '[object String]'
2623
}
@@ -89,20 +86,6 @@ function validateOptions (options) {
8986
}
9087
}
9188

92-
function getVerifier (options) {
93-
const cacheKey = JSON.stringify(options)
94-
let verifier = validatorCache.get(cacheKey)
95-
if (!verifier) {
96-
verifier = createVerifier(options)
97-
validatorCache.set(cacheKey, verifier)
98-
99-
if (validatorCache.size > maxCacheSize) {
100-
validatorCache.delete(validatorCache.keys().next().value) // Remove the oldest cached verifier
101-
}
102-
}
103-
return verifier
104-
}
105-
10689
function fastifyJwt (fastify, options, next) {
10790
try {
10891
validateOptions(options)
@@ -127,6 +110,8 @@ function fastifyJwt (fastify, options, next) {
127110
...pluginOptions
128111
} = options
129112

113+
const validatorCache = new Map()
114+
130115
let secretOrPrivateKey
131116
let secretOrPublicKey
132117

@@ -140,13 +125,15 @@ function fastifyJwt (fastify, options, next) {
140125
secretOrPrivateKey = secretOrPublicKey = secret
141126
}
142127

128+
let hasStaticPublicKey = false
143129
let secretCallbackSign = secretOrPrivateKey
144130
let secretCallbackVerify = secretOrPublicKey
145131
if (typeof secretCallbackSign !== 'function') {
146132
secretCallbackSign = wrapStaticSecretInCallback(secretCallbackSign)
147133
}
148134
if (typeof secretCallbackVerify !== 'function') {
149135
secretCallbackVerify = wrapStaticSecretInCallback(secretCallbackVerify)
136+
hasStaticPublicKey = true
150137
}
151138

152139
const signOptions = convertTemporalProps(initialSignOptions)
@@ -219,12 +206,28 @@ function fastifyJwt (fastify, options, next) {
219206

220207
next()
221208

209+
function getVerifier (options, globalOptions) {
210+
const useGlobalOptions = globalOptions ?? options === verifierConfig.options
211+
// Use global verifier if using global options with static key
212+
if (useGlobalOptions && hasStaticPublicKey) return verifier
213+
// Only cache verifier when using default options (except for key)
214+
if (useGlobalOptions && options.key && typeof options.key === 'string') {
215+
let verifier = validatorCache.get(options.key)
216+
if (!verifier) {
217+
verifier = createVerifier(options)
218+
validatorCache.set(options.key, verifier)
219+
}
220+
return verifier
221+
}
222+
return createVerifier(options)
223+
}
224+
222225
function decode (token, options) {
223226
assert(token, 'missing token')
224227

225228
let selectedDecoder = decoder
226229

227-
if (options && typeof options !== 'function') {
230+
if (options && options !== decodeOptions && typeof options !== 'function') {
228231
selectedDecoder = createDecoder(options)
229232
}
230233

@@ -449,17 +452,25 @@ function fastifyJwt (fastify, options, next) {
449452
}
450453

451454
function requestVerify (options, next) {
452-
let useLocalVerifier = true
455+
const request = this
453456

454-
if (typeof options === 'function' && !next) {
457+
if (next === undefined) {
458+
return new Promise(function (resolve, reject) {
459+
request[jwtVerifyName](options, function (err, val) {
460+
err ? reject(err) : resolve(val)
461+
})
462+
})
463+
}
464+
465+
const useGlobalOptions = !options
466+
467+
if (typeof options === 'function') {
455468
next = options
456469
options = {}
457-
useLocalVerifier = false
458470
} // support no options
459471

460472
if (!options) {
461473
options = {}
462-
useLocalVerifier = false
463474
}
464475

465476
if (options.decode || options.verify) {
@@ -475,16 +486,6 @@ function fastifyJwt (fastify, options, next) {
475486
options = Object.assign({}, verifyOptions, localOptions)
476487
}
477488

478-
const request = this
479-
480-
if (next === undefined) {
481-
return new Promise(function (resolve, reject) {
482-
request[jwtVerifyName](options, function (err, val) {
483-
err ? reject(err) : resolve(val)
484-
})
485-
})
486-
}
487-
488489
let token
489490
try {
490491
token = lookupToken(request, options.verify || options)
@@ -503,14 +504,9 @@ function fastifyJwt (fastify, options, next) {
503504
},
504505
function verify (secretOrPublicKey, callback) {
505506
try {
506-
let verifyResult
507-
if (useLocalVerifier) {
508-
const verifierOptions = mergeOptionsWithKey(options.verify || options, secretOrPublicKey)
509-
const localVerifier = getVerifier(verifierOptions)
510-
verifyResult = localVerifier(token)
511-
} else {
512-
verifyResult = verifier(token)
513-
}
507+
const verifierOptions = mergeOptionsWithKey(options.verify || options, secretOrPublicKey)
508+
const localVerifier = getVerifier(verifierOptions, useGlobalOptions)
509+
const verifyResult = localVerifier(token)
514510
if (verifyResult && typeof verifyResult.then === 'function') {
515511
verifyResult.then(result => callback(null, result), error => wrapError(error, callback))
516512
} else {

0 commit comments

Comments
 (0)