@clerk/backend 3.16.8-canary.v20260818131337 → 3.16.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -284,7 +284,7 @@ var require_dist = __commonJS({
284
284
  // src/constants.ts
285
285
  var API_URL = "https://api.clerk.com";
286
286
  var API_VERSION = "v1";
287
- var USER_AGENT = `${"@clerk/backend"}@${"3.16.8-canary.v20260818131337"}`;
287
+ var USER_AGENT = `${"@clerk/backend"}@${"3.16.8"}`;
288
288
  var MAX_CACHE_LAST_UPDATED_AT_SECONDS = 5 * 60;
289
289
  var SUPPORTED_BAPI_VERSION = "2026-05-12";
290
290
  var Attributes = {
@@ -1663,17 +1663,19 @@ var M2MToken = class _M2MToken {
1663
1663
  };
1664
1664
 
1665
1665
  // src/tokens/keys.ts
1666
- var cache = {};
1667
- var lastUpdatedAt = 0;
1668
- function getFromCache(kid) {
1669
- return cache[kid];
1670
- }
1671
- function getCacheValues() {
1672
- return Object.values(cache);
1673
- }
1674
- function setInCache(cacheKey, jwk, shouldExpire = true) {
1675
- cache[cacheKey] = jwk;
1676
- lastUpdatedAt = shouldExpire ? Date.now() : -1;
1666
+ var remoteCaches = /* @__PURE__ */ new Map();
1667
+ function getRemoteCache(scope) {
1668
+ let cache = remoteCaches.get(scope);
1669
+ if (!cache) {
1670
+ for (const [key, entry] of remoteCaches) {
1671
+ if (cacheHasExpired(entry)) {
1672
+ remoteCaches.delete(key);
1673
+ }
1674
+ }
1675
+ cache = { keys: {}, lastUpdatedAt: 0 };
1676
+ remoteCaches.set(scope, cache);
1677
+ }
1678
+ return cache;
1677
1679
  }
1678
1680
  var PEM_HEADER = "-----BEGIN PUBLIC KEY-----";
1679
1681
  var PEM_TRAILER = "-----END PUBLIC KEY-----";
@@ -1681,11 +1683,6 @@ var RSA_PREFIX = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA";
1681
1683
  var RSA_SUFFIX = "IDAQAB";
1682
1684
  function loadClerkJwkFromPem(params) {
1683
1685
  const { kid, pem } = params;
1684
- const prefixedKid = `local-${kid}`;
1685
- const cachedJwk = getFromCache(prefixedKid);
1686
- if (cachedJwk) {
1687
- return cachedJwk;
1688
- }
1689
1686
  if (!pem) {
1690
1687
  throw new TokenVerificationError({
1691
1688
  action: TokenVerificationErrorAction.SetClerkJWTKey,
@@ -1694,13 +1691,13 @@ function loadClerkJwkFromPem(params) {
1694
1691
  });
1695
1692
  }
1696
1693
  const modulus = pem.replace(/\r\n|\n|\r/g, "").replace(PEM_HEADER, "").replace(PEM_TRAILER, "").replace(RSA_PREFIX, "").replace(RSA_SUFFIX, "").replace(/\+/g, "-").replace(/\//g, "_");
1697
- const jwk = { kid: prefixedKid, kty: "RSA", alg: "RS256", n: modulus, e: "AQAB" };
1698
- setInCache(prefixedKid, jwk, false);
1694
+ const jwk = { kid: `local-${kid}`, kty: "RSA", alg: "RS256", n: modulus, e: "AQAB" };
1699
1695
  return jwk;
1700
1696
  }
1701
1697
  async function loadClerkJWKFromRemote(params) {
1702
1698
  const { secretKey, apiUrl = API_URL, apiVersion = API_VERSION, kid, skipJwksCache } = params;
1703
- if (skipJwksCache || cacheHasExpired() || !getFromCache(kid)) {
1699
+ const cache = getRemoteCache(`${apiUrl}|${apiVersion}|${secretKey ?? ""}`);
1700
+ if (skipJwksCache || cacheHasExpired(cache) || !cache.keys[kid]) {
1704
1701
  if (!secretKey) {
1705
1702
  throw new TokenVerificationError({
1706
1703
  action: TokenVerificationErrorAction.ContactSupport,
@@ -1717,15 +1714,16 @@ async function loadClerkJWKFromRemote(params) {
1717
1714
  reason: TokenVerificationErrorReason.RemoteJWKFailedToLoad
1718
1715
  });
1719
1716
  }
1720
- keys.forEach((key) => setInCache(key.kid, key));
1717
+ keys.forEach((key) => {
1718
+ cache.keys[key.kid] = key;
1719
+ });
1720
+ cache.lastUpdatedAt = Date.now();
1721
1721
  }
1722
- const jwk = getFromCache(kid);
1722
+ const jwk = cache.keys[kid];
1723
1723
  if (!jwk) {
1724
- const cacheValues = getCacheValues();
1725
- const jwkKeys = cacheValues.map((jwk2) => jwk2.kid).sort().join(", ");
1726
1724
  throw new TokenVerificationError({
1727
1725
  action: `Go to your Dashboard and validate your secret and public keys are correct. ${TokenVerificationErrorAction.ContactSupport} if the issue persists.`,
1728
- message: `Unable to find a signing key in JWKS that matches the kid='${kid}' of the provided session token. Please make sure that the __session cookie or the HTTP authorization header contain a Clerk-generated session JWT. The following kid is available: ${jwkKeys}`,
1726
+ message: `Unable to find a signing key in JWKS that matches the kid='${kid}' of the provided session token. Please make sure that the __session cookie or the HTTP authorization header contain a Clerk-generated session JWT.`,
1729
1727
  reason: TokenVerificationErrorReason.JWKKidMismatch
1730
1728
  });
1731
1729
  }
@@ -1768,13 +1766,10 @@ async function fetchJWKSFromBAPI(apiUrl, key, apiVersion) {
1768
1766
  }
1769
1767
  return response.json();
1770
1768
  }
1771
- function cacheHasExpired() {
1772
- if (lastUpdatedAt === -1) {
1773
- return false;
1774
- }
1775
- const isExpired = Date.now() - lastUpdatedAt >= MAX_CACHE_LAST_UPDATED_AT_SECONDS * 1e3;
1769
+ function cacheHasExpired(cache) {
1770
+ const isExpired = Date.now() - cache.lastUpdatedAt >= MAX_CACHE_LAST_UPDATED_AT_SECONDS * 1e3;
1776
1771
  if (isExpired) {
1777
- cache = {};
1772
+ cache.keys = {};
1778
1773
  }
1779
1774
  return isExpired;
1780
1775
  }
@@ -7787,4 +7782,4 @@ export {
7787
7782
  debugRequestState,
7788
7783
  createAuthenticateRequest
7789
7784
  };
7790
- //# sourceMappingURL=chunk-WD3BW7IB.mjs.map
7785
+ //# sourceMappingURL=chunk-5QHQI6BM.mjs.map