@clerk/backend 3.0.0-snapshot.v20251204175016 → 3.0.0-snapshot.v20251208202852

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.
Files changed (40) hide show
  1. package/dist/api/resources/IdPOAuthAccessToken.d.ts +6 -0
  2. package/dist/api/resources/IdPOAuthAccessToken.d.ts.map +1 -1
  3. package/dist/{chunk-QYKVFAML.mjs → chunk-SNA7AD3D.mjs} +8 -7
  4. package/dist/chunk-SNA7AD3D.mjs.map +1 -0
  5. package/dist/{chunk-HNJNM32R.mjs → chunk-TCIXZLLW.mjs} +11 -4
  6. package/dist/{chunk-HNJNM32R.mjs.map → chunk-TCIXZLLW.mjs.map} +1 -1
  7. package/dist/{chunk-777XG3PJ.mjs → chunk-XZ7V2XHT.mjs} +132 -9
  8. package/dist/chunk-XZ7V2XHT.mjs.map +1 -0
  9. package/dist/errors.d.ts +6 -3
  10. package/dist/errors.d.ts.map +1 -1
  11. package/dist/errors.js +10 -3
  12. package/dist/errors.js.map +1 -1
  13. package/dist/errors.mjs +1 -1
  14. package/dist/fixtures/index.d.ts +12 -0
  15. package/dist/fixtures/index.d.ts.map +1 -1
  16. package/dist/fixtures/machine.d.ts +2 -0
  17. package/dist/fixtures/machine.d.ts.map +1 -1
  18. package/dist/index.d.ts +1 -0
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +145 -14
  21. package/dist/index.js.map +1 -1
  22. package/dist/index.mjs +3 -3
  23. package/dist/internal.js +145 -14
  24. package/dist/internal.js.map +1 -1
  25. package/dist/internal.mjs +3 -3
  26. package/dist/jwt/assertions.d.ts +1 -1
  27. package/dist/jwt/assertions.d.ts.map +1 -1
  28. package/dist/jwt/index.js +6 -5
  29. package/dist/jwt/index.js.map +1 -1
  30. package/dist/jwt/index.mjs +2 -2
  31. package/dist/jwt/verifyJwt.d.ts +5 -0
  32. package/dist/jwt/verifyJwt.d.ts.map +1 -1
  33. package/dist/tokens/machine.d.ts +23 -0
  34. package/dist/tokens/machine.d.ts.map +1 -1
  35. package/dist/tokens/verify.d.ts +1 -1
  36. package/dist/tokens/verify.d.ts.map +1 -1
  37. package/dist/util/shared.d.ts +1 -1
  38. package/package.json +2 -2
  39. package/dist/chunk-777XG3PJ.mjs.map +0 -1
  40. package/dist/chunk-QYKVFAML.mjs.map +0 -1
package/dist/index.mjs CHANGED
@@ -2,13 +2,13 @@ import {
2
2
  createAuthenticateRequest,
3
3
  createBackendApiClient,
4
4
  verifyToken
5
- } from "./chunk-777XG3PJ.mjs";
5
+ } from "./chunk-XZ7V2XHT.mjs";
6
6
  import "./chunk-YBVFDYDR.mjs";
7
7
  import {
8
8
  withLegacyReturn
9
9
  } from "./chunk-P263NW7Z.mjs";
10
- import "./chunk-QYKVFAML.mjs";
11
- import "./chunk-HNJNM32R.mjs";
10
+ import "./chunk-SNA7AD3D.mjs";
11
+ import "./chunk-TCIXZLLW.mjs";
12
12
  import "./chunk-RPS7XK5K.mjs";
13
13
 
14
14
  // src/index.ts
package/dist/internal.js CHANGED
@@ -56,7 +56,7 @@ module.exports = __toCommonJS(internal_exports);
56
56
  // src/constants.ts
57
57
  var API_URL = "https://api.clerk.com";
58
58
  var API_VERSION = "v1";
59
- var USER_AGENT = `${"@clerk/backend"}@${"3.0.0-snapshot.v20251204175016"}`;
59
+ var USER_AGENT = `${"@clerk/backend"}@${"3.0.0-snapshot.v20251208202852"}`;
60
60
  var MAX_CACHE_LAST_UPDATED_AT_SECONDS = 5 * 60;
61
61
  var SUPPORTED_BAPI_VERSION = "2025-11-10";
62
62
  var Attributes = {
@@ -281,17 +281,24 @@ var TokenVerificationError = class _TokenVerificationError extends Error {
281
281
  var MachineTokenVerificationErrorCode = {
282
282
  TokenInvalid: "token-invalid",
283
283
  InvalidSecretKey: "secret-key-invalid",
284
- UnexpectedError: "unexpected-error"
284
+ UnexpectedError: "unexpected-error",
285
+ TokenVerificationFailed: "token-verification-failed"
285
286
  };
286
287
  var MachineTokenVerificationError = class _MachineTokenVerificationError extends Error {
287
- constructor({ message, code, status }) {
288
+ constructor({
289
+ message,
290
+ code,
291
+ status,
292
+ action
293
+ }) {
288
294
  super(message);
289
295
  Object.setPrototypeOf(this, _MachineTokenVerificationError.prototype);
290
296
  this.code = code;
291
297
  this.status = status;
298
+ this.action = action;
292
299
  }
293
300
  getFullMessage() {
294
- return `${this.message} (code=${this.code}, status=${this.status})`;
301
+ return `${this.message} (code=${this.code}, status=${this.status || "n/a"})`;
295
302
  }
296
303
  };
297
304
 
@@ -445,15 +452,16 @@ var assertAudienceClaim = (aud, audience) => {
445
452
  }
446
453
  }
447
454
  };
448
- var assertHeaderType = (typ) => {
455
+ var assertHeaderType = (typ, allowedTypes = "JWT") => {
449
456
  if (typeof typ === "undefined") {
450
457
  return;
451
458
  }
452
- if (typ !== "JWT") {
459
+ const allowed = Array.isArray(allowedTypes) ? allowedTypes : [allowedTypes];
460
+ if (!allowed.includes(typ)) {
453
461
  throw new TokenVerificationError({
454
462
  action: TokenVerificationErrorAction.EnsureClerkJWT,
455
463
  reason: TokenVerificationErrorReason.TokenInvalid,
456
- message: `Invalid JWT type ${JSON.stringify(typ)}. Expected "JWT".`
464
+ message: `Invalid JWT type ${JSON.stringify(typ)}. Expected "${allowed.join(", ")}".`
457
465
  });
458
466
  }
459
467
  };
@@ -624,7 +632,7 @@ function decodeJwt(token) {
624
632
  return { data };
625
633
  }
626
634
  async function verifyJwt(token, options) {
627
- const { audience, authorizedParties, clockSkewInMs, key } = options;
635
+ const { audience, authorizedParties, clockSkewInMs, key, headerType } = options;
628
636
  const clockSkew = clockSkewInMs || DEFAULT_CLOCK_SKEW_IN_MS;
629
637
  const { data: decoded, errors } = decodeJwt(token);
630
638
  if (errors) {
@@ -633,7 +641,7 @@ async function verifyJwt(token, options) {
633
641
  const { header, payload } = decoded;
634
642
  try {
635
643
  const { typ, alg } = header;
636
- assertHeaderType(typ);
644
+ assertHeaderType(typ, headerType);
637
645
  assertHeaderAlgorithm(alg);
638
646
  const { azp, sub, aud, iat, exp, nbf } = payload;
639
647
  assertSubClaim(sub);
@@ -3098,6 +3106,26 @@ var IdPOAuthAccessToken = class _IdPOAuthAccessToken {
3098
3106
  data.updated_at
3099
3107
  );
3100
3108
  }
3109
+ /**
3110
+ * Creates an IdPOAuthAccessToken from a JWT payload.
3111
+ * Maps standard JWT claims and OAuth-specific fields to token properties.
3112
+ */
3113
+ static fromJwtPayload(payload, clockSkewInMs = 5e3) {
3114
+ const oauthPayload = payload;
3115
+ return new _IdPOAuthAccessToken(
3116
+ oauthPayload.jti ?? "",
3117
+ oauthPayload.client_id ?? "",
3118
+ "oauth_token",
3119
+ payload.sub,
3120
+ oauthPayload.scp ?? oauthPayload.scope?.split(" ") ?? [],
3121
+ false,
3122
+ null,
3123
+ payload.exp * 1e3 <= Date.now() - clockSkewInMs,
3124
+ payload.exp,
3125
+ payload.iat,
3126
+ payload.iat
3127
+ );
3128
+ }
3101
3129
  };
3102
3130
 
3103
3131
  // src/api/resources/Instance.ts
@@ -4330,14 +4358,33 @@ var M2M_TOKEN_PREFIX = "mt_";
4330
4358
  var OAUTH_TOKEN_PREFIX = "oat_";
4331
4359
  var API_KEY_PREFIX = "ak_";
4332
4360
  var MACHINE_TOKEN_PREFIXES = [M2M_TOKEN_PREFIX, OAUTH_TOKEN_PREFIX, API_KEY_PREFIX];
4361
+ var JwtFormatRegExp = /^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/;
4362
+ function isJwtFormat(token) {
4363
+ return JwtFormatRegExp.test(token);
4364
+ }
4365
+ var OAUTH_ACCESS_TOKEN_TYPES = ["at+jwt", "application/at+jwt"];
4366
+ function isOAuthJwt(token) {
4367
+ if (!isJwtFormat(token)) {
4368
+ return false;
4369
+ }
4370
+ try {
4371
+ const { data, errors } = decodeJwt(token);
4372
+ return !errors && !!data && OAUTH_ACCESS_TOKEN_TYPES.includes(data.header.typ);
4373
+ } catch {
4374
+ return false;
4375
+ }
4376
+ }
4333
4377
  function isMachineTokenByPrefix(token) {
4334
4378
  return MACHINE_TOKEN_PREFIXES.some((prefix) => token.startsWith(prefix));
4335
4379
  }
4380
+ function isMachineToken(token) {
4381
+ return isMachineTokenByPrefix(token) || isOAuthJwt(token);
4382
+ }
4336
4383
  function getMachineTokenType(token) {
4337
4384
  if (token.startsWith(M2M_TOKEN_PREFIX)) {
4338
4385
  return TokenType.M2MToken;
4339
4386
  }
4340
- if (token.startsWith(OAUTH_TOKEN_PREFIX)) {
4387
+ if (token.startsWith(OAUTH_TOKEN_PREFIX) || isOAuthJwt(token)) {
4341
4388
  return TokenType.OAuthToken;
4342
4389
  }
4343
4390
  if (token.startsWith(API_KEY_PREFIX)) {
@@ -4993,7 +5040,91 @@ async function verifyM2MToken(token, options) {
4993
5040
  return handleClerkAPIError(TokenType.M2MToken, err, "Machine token not found");
4994
5041
  }
4995
5042
  }
5043
+ async function verifyJwtOAuthToken(accessToken, options) {
5044
+ let decoded;
5045
+ try {
5046
+ decoded = decodeJwt(accessToken);
5047
+ } catch (e) {
5048
+ return {
5049
+ data: void 0,
5050
+ tokenType: TokenType.OAuthToken,
5051
+ errors: [
5052
+ new MachineTokenVerificationError({
5053
+ code: MachineTokenVerificationErrorCode.TokenInvalid,
5054
+ message: e.message
5055
+ })
5056
+ ]
5057
+ };
5058
+ }
5059
+ const { data: decodedResult, errors } = decoded;
5060
+ if (errors) {
5061
+ return {
5062
+ data: void 0,
5063
+ tokenType: TokenType.OAuthToken,
5064
+ errors: [
5065
+ new MachineTokenVerificationError({
5066
+ code: MachineTokenVerificationErrorCode.TokenInvalid,
5067
+ message: errors[0].message
5068
+ })
5069
+ ]
5070
+ };
5071
+ }
5072
+ const { header } = decodedResult;
5073
+ const { kid } = header;
5074
+ let key;
5075
+ try {
5076
+ if (options.jwtKey) {
5077
+ key = loadClerkJwkFromPem({ kid, pem: options.jwtKey });
5078
+ } else if (options.secretKey) {
5079
+ key = await loadClerkJWKFromRemote({ ...options, kid });
5080
+ } else {
5081
+ return {
5082
+ data: void 0,
5083
+ tokenType: TokenType.OAuthToken,
5084
+ errors: [
5085
+ new MachineTokenVerificationError({
5086
+ action: TokenVerificationErrorAction.SetClerkJWTKey,
5087
+ message: "Failed to resolve JWK during verification.",
5088
+ code: MachineTokenVerificationErrorCode.TokenVerificationFailed
5089
+ })
5090
+ ]
5091
+ };
5092
+ }
5093
+ const { data: payload, errors: verifyErrors } = await verifyJwt(accessToken, {
5094
+ ...options,
5095
+ key,
5096
+ headerType: OAUTH_ACCESS_TOKEN_TYPES
5097
+ });
5098
+ if (verifyErrors) {
5099
+ return {
5100
+ data: void 0,
5101
+ tokenType: TokenType.OAuthToken,
5102
+ errors: [
5103
+ new MachineTokenVerificationError({
5104
+ code: MachineTokenVerificationErrorCode.TokenVerificationFailed,
5105
+ message: verifyErrors[0].message
5106
+ })
5107
+ ]
5108
+ };
5109
+ }
5110
+ const token = IdPOAuthAccessToken.fromJwtPayload(payload, options.clockSkewInMs);
5111
+ return { data: token, tokenType: TokenType.OAuthToken, errors: void 0 };
5112
+ } catch (error) {
5113
+ return {
5114
+ tokenType: TokenType.OAuthToken,
5115
+ errors: [
5116
+ new MachineTokenVerificationError({
5117
+ code: MachineTokenVerificationErrorCode.TokenVerificationFailed,
5118
+ message: error.message
5119
+ })
5120
+ ]
5121
+ };
5122
+ }
5123
+ }
4996
5124
  async function verifyOAuthToken(accessToken, options) {
5125
+ if (isJwtFormat(accessToken)) {
5126
+ return verifyJwtOAuthToken(accessToken, options);
5127
+ }
4997
5128
  try {
4998
5129
  const client = createBackendApiClient(options);
4999
5130
  const verifiedToken = await client.idPOAuthAccessToken.verify(accessToken);
@@ -5015,7 +5146,7 @@ async function verifyMachineAuthToken(token, options) {
5015
5146
  if (token.startsWith(M2M_TOKEN_PREFIX)) {
5016
5147
  return verifyM2MToken(token, options);
5017
5148
  }
5018
- if (token.startsWith(OAUTH_TOKEN_PREFIX)) {
5149
+ if (token.startsWith(OAUTH_TOKEN_PREFIX) || isJwtFormat(token)) {
5019
5150
  return verifyOAuthToken(token, options);
5020
5151
  }
5021
5152
  if (token.startsWith(API_KEY_PREFIX)) {
@@ -5408,7 +5539,7 @@ function isTokenTypeInAcceptedArray(acceptsToken, authenticateContext) {
5408
5539
  let parsedTokenType = null;
5409
5540
  const { tokenInHeader } = authenticateContext;
5410
5541
  if (tokenInHeader) {
5411
- if (isMachineTokenByPrefix(tokenInHeader)) {
5542
+ if (isMachineToken(tokenInHeader)) {
5412
5543
  parsedTokenType = getMachineTokenType(tokenInHeader);
5413
5544
  } else {
5414
5545
  parsedTokenType = TokenType.SessionToken;
@@ -5818,7 +5949,7 @@ var authenticateRequest = (async (request, options) => {
5818
5949
  if (!tokenInHeader) {
5819
5950
  return handleSessionTokenError(new Error("Missing token in header"), "header");
5820
5951
  }
5821
- if (!isMachineTokenByPrefix(tokenInHeader)) {
5952
+ if (!isMachineToken(tokenInHeader)) {
5822
5953
  return signedOut({
5823
5954
  tokenType: acceptsToken,
5824
5955
  authenticateContext,
@@ -5847,7 +5978,7 @@ var authenticateRequest = (async (request, options) => {
5847
5978
  if (!tokenInHeader) {
5848
5979
  return handleSessionTokenError(new Error("Missing token in header"), "header");
5849
5980
  }
5850
- if (isMachineTokenByPrefix(tokenInHeader)) {
5981
+ if (isMachineToken(tokenInHeader)) {
5851
5982
  const parsedTokenType = getMachineTokenType(tokenInHeader);
5852
5983
  const mismatchState = checkTokenTypeMismatch(parsedTokenType, acceptsToken, authenticateContext);
5853
5984
  if (mismatchState) {