The jwksCache symbol is used to provide a persistent JSON Web Key Set (JWKS) cache in environments like cloud computing runtimes (e.g., AWS Lambda, Google Cloud Functions) where in-memory caching is not preserved between invocations.
When you pass an object using the [oauth.jwksCache] key into functions that accept JWKSCacheOptions, the module uses that object to:
- Serve as the initial JWKS value (avoiding an immediate HTTP request).
- Update the object's properties if the module triggers a new HTTP request to fetch the JWKS.
Security Warning
This option has security implications. You must ensure that the JWKS cache object is only writable by your own code to prevent unauthorized key injection.
Implementation Pattern
To use this correctly in a stateless environment:
- Retrieve: Before calling the OAuth function, fetch the previously cached object from a low-latency key-value store (like Redis or a cloud-native KV store). Default to an empty object
{} if no cache exists. - Execute: Pass the retrieved object into the function's
options using the [oauth.jwksCache] symbol. - Detect Changes: Check if the
uat (Update At) property of the object has changed compared to the version you initially retrieved. - Persist: If
uat has changed, save the updated object back to your key-value store.
let as!: oauth.AuthorizationServer
let request!: Request
let expectedAudience!: string
let getPreviouslyCachedJWKS!: () => Promise<oauth.ExportedJWKSCache>
let storeNewJWKScache!: (cache: oauth.ExportedJWKSCache) => Promise<void>
// 1. Load JSON Web Key Set cache from external storage
let jwksCache: oauth.JWKSCacheInput = (await getPreviouslyCachedJWKS()) || {}
let { uat } = jwksCache
// 2. Use JSON Web Key Set cache in an OAuth function
let accessTokenClaims = await oauth.validateJwtAccessToken(as, request, expectedAudience, {
[oauth.jwksCache]: jwksCache,
})
// 3. If the module updated the cache (uat changed), persist it
if (uat !== jwksCache.uat) {
await storeNewJWKScache(jwksCache)
}