Skip to content

Commit

Permalink
chore(backend): Extract jwks cache ttl into constant (#2110)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkl authored Nov 10, 2023
1 parent 19550ec commit 86ac27a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .changeset/late-cooks-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
1 change: 1 addition & 0 deletions packages/backend/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const API_VERSION = 'v1';
// TODO: Get information from package.json or define them from ESBuild
export const USER_AGENT = `@clerk/backend`;
export const MAX_CACHE_LAST_UPDATED_AT_SECONDS = 5 * 60;
export const JWKS_CACHE_TTL_MS = 1000 * 60 * 60;

const Attributes = {
AuthStatus: '__clerkAuthStatus',
Expand Down
9 changes: 3 additions & 6 deletions packages/backend/src/tokens/keys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { API_URL, API_VERSION, MAX_CACHE_LAST_UPDATED_AT_SECONDS } from '../constants';
import { API_URL, API_VERSION, JWKS_CACHE_TTL_MS, MAX_CACHE_LAST_UPDATED_AT_SECONDS } from '../constants';
// DO NOT CHANGE: Runtime needs to be imported as a default export so that we can stub its dependencies with Sinon.js
// For more information refer to https://sinonjs.org/how-to/stub-dependency/
import runtime from '../runtime';
Expand Down Expand Up @@ -27,10 +27,7 @@ function getCacheValues() {
return Object.values(cache);
}

function setInCache(
jwk: JsonWebKeyWithKid,
jwksCacheTtlInMs: number = 1000 * 60 * 60, // 1 hour
) {
function setInCache(jwk: JsonWebKeyWithKid, jwksCacheTtlInMs: number) {
cache[jwk.kid] = jwk;
lastUpdatedAt = Date.now();

Expand Down Expand Up @@ -128,7 +125,7 @@ export async function loadClerkJWKFromRemote({
apiVersion = API_VERSION,
issuer,
kid,
jwksCacheTtlInMs = 1000 * 60 * 60, // 1 hour,
jwksCacheTtlInMs = JWKS_CACHE_TTL_MS,
skipJwksCache,
}: LoadClerkJWKFromRemoteOptions): Promise<JsonWebKey> {
const shouldRefreshCache = !getFromCache(kid) && reachedMaxCacheUpdatedAt();
Expand Down

0 comments on commit 86ac27a

Please sign in to comment.