@@ -18,9 +18,6 @@ const messages = {
18
18
authorizationTokenUnsigned : 'Unsigned authorization token'
19
19
}
20
20
21
- const validatorCache = new Map ( )
22
- const maxCacheSize = 1000
23
-
24
21
function isString ( x ) {
25
22
return Object . prototype . toString . call ( x ) === '[object String]'
26
23
}
@@ -89,20 +86,6 @@ function validateOptions (options) {
89
86
}
90
87
}
91
88
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
-
106
89
function fastifyJwt ( fastify , options , next ) {
107
90
try {
108
91
validateOptions ( options )
@@ -127,6 +110,8 @@ function fastifyJwt (fastify, options, next) {
127
110
...pluginOptions
128
111
} = options
129
112
113
+ const validatorCache = new Map ( )
114
+
130
115
let secretOrPrivateKey
131
116
let secretOrPublicKey
132
117
@@ -140,13 +125,15 @@ function fastifyJwt (fastify, options, next) {
140
125
secretOrPrivateKey = secretOrPublicKey = secret
141
126
}
142
127
128
+ let hasStaticPublicKey = false
143
129
let secretCallbackSign = secretOrPrivateKey
144
130
let secretCallbackVerify = secretOrPublicKey
145
131
if ( typeof secretCallbackSign !== 'function' ) {
146
132
secretCallbackSign = wrapStaticSecretInCallback ( secretCallbackSign )
147
133
}
148
134
if ( typeof secretCallbackVerify !== 'function' ) {
149
135
secretCallbackVerify = wrapStaticSecretInCallback ( secretCallbackVerify )
136
+ hasStaticPublicKey = true
150
137
}
151
138
152
139
const signOptions = convertTemporalProps ( initialSignOptions )
@@ -219,12 +206,28 @@ function fastifyJwt (fastify, options, next) {
219
206
220
207
next ( )
221
208
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
+
222
225
function decode ( token , options ) {
223
226
assert ( token , 'missing token' )
224
227
225
228
let selectedDecoder = decoder
226
229
227
- if ( options && typeof options !== 'function' ) {
230
+ if ( options && options !== decodeOptions && typeof options !== 'function' ) {
228
231
selectedDecoder = createDecoder ( options )
229
232
}
230
233
@@ -449,17 +452,25 @@ function fastifyJwt (fastify, options, next) {
449
452
}
450
453
451
454
function requestVerify ( options , next ) {
452
- let useLocalVerifier = true
455
+ const request = this
453
456
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' ) {
455
468
next = options
456
469
options = { }
457
- useLocalVerifier = false
458
470
} // support no options
459
471
460
472
if ( ! options ) {
461
473
options = { }
462
- useLocalVerifier = false
463
474
}
464
475
465
476
if ( options . decode || options . verify ) {
@@ -475,16 +486,6 @@ function fastifyJwt (fastify, options, next) {
475
486
options = Object . assign ( { } , verifyOptions , localOptions )
476
487
}
477
488
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
-
488
489
let token
489
490
try {
490
491
token = lookupToken ( request , options . verify || options )
@@ -503,14 +504,9 @@ function fastifyJwt (fastify, options, next) {
503
504
} ,
504
505
function verify ( secretOrPublicKey , callback ) {
505
506
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 )
514
510
if ( verifyResult && typeof verifyResult . then === 'function' ) {
515
511
verifyResult . then ( result => callback ( null , result ) , error => wrapError ( error , callback ) )
516
512
} else {
0 commit comments