Skip to content

Commit

Permalink
feat: LRU cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed Nov 10, 2024
1 parent 72762fd commit dae254f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 65 deletions.
17 changes: 0 additions & 17 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,7 @@ function ensurePromiseCallback(callback) {
]
}

function hashToken(token) {
const rawHeader = token.split('.', 1)[0]
const header = Buffer.from(rawHeader, 'base64').toString('utf-8')
let hasher = null

/* istanbul ignore next */
if (header.match(edAlgorithmMatcher) && header.match(ed448CurveMatcher)) {
hasher = createHash('shake256', { outputLength: 114 })
} else {
const mo = header.match(algorithmMatcher)
hasher = createHash(`sha${mo ? mo[1] : '512'}`)
}

return hasher.update(token).digest('hex')
}

module.exports = {
getAsyncKey,
ensurePromiseCallback,

Check failure on line 47 in src/utils.js

View workflow job for this annotation

GitHub Actions / build / test (22)

Unexpected trailing comma

Check failure on line 47 in src/utils.js

View workflow job for this annotation

GitHub Actions / build / test (20)

Unexpected trailing comma

Check failure on line 47 in src/utils.js

View workflow job for this annotation

GitHub Actions / build / test (23, experimental)

Unexpected trailing comma
hashToken
}
8 changes: 4 additions & 4 deletions src/verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Cache = require('mnemonist/lru-cache')
const { useNewCrypto, hsAlgorithms, verifySignature, detectPublicKeyAlgorithms } = require('./crypto')
const createDecoder = require('./decoder')
const { TokenError } = require('./error')
const { getAsyncKey, ensurePromiseCallback, hashToken } = require('./utils')
const { getAsyncKey, ensurePromiseCallback } = require('./utils')

const defaultCacheSize = 1000

Expand Down Expand Up @@ -93,7 +93,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(token, cacheValue)
return value
}

Expand All @@ -116,7 +116,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(token, cacheValue)

return value
}
Expand Down Expand Up @@ -280,7 +280,7 @@ function verify(

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

// Validate time range
Expand Down
Loading

0 comments on commit dae254f

Please sign in to comment.