Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: token hasher option
Browse files Browse the repository at this point in the history
ilteoood committed Nov 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 72762fd commit aa23c18
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/verifier.js
Original file line number Diff line number Diff line change
@@ -80,7 +80,8 @@ function cacheSet(
maxAge,
clockTimestamp,
clockTolerance,
errorCacheTTL
errorCacheTTL,
tokenHasher
},
value
) {
@@ -93,7 +94,7 @@ function cacheSet(
if (value instanceof TokenError) {
const ttl = typeof errorCacheTTL === 'function' ? errorCacheTTL(value) : errorCacheTTL
cacheValue[2] = (clockTimestamp || Date.now()) + clockTolerance + ttl
cache.set(hashToken(token), cacheValue)
cache.set(tokenHasher(token), cacheValue)
return value
}

@@ -116,7 +117,7 @@ function cacheSet(
const maxTTL = (clockTimestamp || Date.now()) + clockTolerance + cacheTTL
cacheValue[2] = cacheValue[2] === 0 ? maxTTL : Math.min(cacheValue[2], maxTTL)

cache.set(hashToken(token), cacheValue)
cache.set(tokenHasher(token), cacheValue)

return value
}
@@ -258,7 +259,8 @@ function verify(
decode,
cache,
requiredClaims,
errorCacheTTL
errorCacheTTL,
tokenHasher
},
token,
cb
@@ -275,12 +277,13 @@ function verify(
ignoreNotBefore,
maxAge,
clockTimestamp,
clockTolerance
clockTolerance,
tokenHasher
}

// Check the cache
if (cache) {
const [value, min, max] = cache.get(hashToken(token)) || [undefined, 0, 0]
const [value, min, max] = cache.get(tokenHasher(token)) || [undefined, 0, 0]
const now = clockTimestamp || Date.now()

// Validate time range
@@ -392,8 +395,9 @@ module.exports = function createVerifier(options) {
allowedIss,
allowedSub,
allowedNonce,
requiredClaims
} = { cacheTTL: 600000, clockTolerance: 0, errorCacheTTL: -1, ...options }
requiredClaims,
tokenHasher
} = { cacheTTL: 600000, clockTolerance: 0, errorCacheTTL: -1, tokenHasher: hashToken, ...options }

// Validate options
if (!Array.isArray(allowedAlgorithms)) {
@@ -516,7 +520,8 @@ module.exports = function createVerifier(options) {
validators,
decode: createDecoder({ complete: true }),
cache: createCache(cacheSize),
requiredClaims
requiredClaims,
tokenHasher
}

// Return the verifier

0 comments on commit aa23c18

Please sign in to comment.