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

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 (60) hide show
  1. package/dist/api/endpoints/APIKeysApi.d.ts +23 -2
  2. package/dist/api/endpoints/APIKeysApi.d.ts.map +1 -1
  3. package/dist/api/endpoints/InstanceApi.d.ts +3 -3
  4. package/dist/api/endpoints/OrganizationApi.d.ts +2 -2
  5. package/dist/api/endpoints/SamlConnectionApi.d.ts +3 -3
  6. package/dist/api/endpoints/UserApi.d.ts +2 -2
  7. package/dist/api/resources/CommercePlan.d.ts +27 -27
  8. package/dist/api/resources/CommerceSubscriptionItem.d.ts +6 -6
  9. package/dist/api/resources/Feature.d.ts +11 -11
  10. package/dist/api/resources/IdPOAuthAccessToken.d.ts +6 -0
  11. package/dist/api/resources/IdPOAuthAccessToken.d.ts.map +1 -1
  12. package/dist/api/resources/JSON.d.ts +4 -4
  13. package/dist/api/resources/Organization.d.ts +23 -23
  14. package/dist/api/resources/OrganizationInvitation.d.ts +7 -7
  15. package/dist/api/resources/OrganizationMembership.d.ts +7 -7
  16. package/dist/api/resources/SamlConnection.d.ts +4 -4
  17. package/dist/api/resources/Session.d.ts +2 -2
  18. package/dist/api/resources/User.d.ts +4 -4
  19. package/dist/{chunk-777XG3PJ.mjs → chunk-4FN7VCZQ.mjs} +165 -12
  20. package/dist/chunk-4FN7VCZQ.mjs.map +1 -0
  21. package/dist/{chunk-QYKVFAML.mjs → chunk-SNA7AD3D.mjs} +8 -7
  22. package/dist/chunk-SNA7AD3D.mjs.map +1 -0
  23. package/dist/{chunk-HNJNM32R.mjs → chunk-TCIXZLLW.mjs} +11 -4
  24. package/dist/{chunk-HNJNM32R.mjs.map → chunk-TCIXZLLW.mjs.map} +1 -1
  25. package/dist/errors.d.ts +6 -3
  26. package/dist/errors.d.ts.map +1 -1
  27. package/dist/errors.js +10 -3
  28. package/dist/errors.js.map +1 -1
  29. package/dist/errors.mjs +1 -1
  30. package/dist/fixtures/index.d.ts +12 -0
  31. package/dist/fixtures/index.d.ts.map +1 -1
  32. package/dist/fixtures/machine.d.ts +2 -0
  33. package/dist/fixtures/machine.d.ts.map +1 -1
  34. package/dist/index.d.ts +1 -0
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +178 -17
  37. package/dist/index.js.map +1 -1
  38. package/dist/index.mjs +3 -3
  39. package/dist/internal.js +178 -17
  40. package/dist/internal.js.map +1 -1
  41. package/dist/internal.mjs +3 -3
  42. package/dist/jwt/assertions.d.ts +1 -1
  43. package/dist/jwt/assertions.d.ts.map +1 -1
  44. package/dist/jwt/index.js +6 -5
  45. package/dist/jwt/index.js.map +1 -1
  46. package/dist/jwt/index.mjs +2 -2
  47. package/dist/jwt/verifyJwt.d.ts +5 -0
  48. package/dist/jwt/verifyJwt.d.ts.map +1 -1
  49. package/dist/tokens/authObjects.d.ts +1 -1
  50. package/dist/tokens/clerkRequest.d.ts.map +1 -1
  51. package/dist/tokens/handshake.d.ts.map +1 -1
  52. package/dist/tokens/machine.d.ts +23 -0
  53. package/dist/tokens/machine.d.ts.map +1 -1
  54. package/dist/tokens/types.d.ts +7 -7
  55. package/dist/tokens/verify.d.ts +1 -1
  56. package/dist/tokens/verify.d.ts.map +1 -1
  57. package/dist/util/shared.d.ts +1 -1
  58. package/package.json +2 -2
  59. package/dist/chunk-777XG3PJ.mjs.map +0 -1
  60. 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-4FN7VCZQ.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.v20251211120550"}`;
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);
@@ -1015,13 +1023,36 @@ var APIKeysAPI = class extends AbstractAPI {
1015
1023
  bodyParams: params
1016
1024
  });
1017
1025
  }
1018
- async revoke(params) {
1026
+ async get(apiKeyId) {
1027
+ this.requireId(apiKeyId);
1028
+ return this.request({
1029
+ method: "GET",
1030
+ path: joinPaths(basePath4, apiKeyId)
1031
+ });
1032
+ }
1033
+ async update(params) {
1019
1034
  const { apiKeyId, ...bodyParams } = params;
1020
1035
  this.requireId(apiKeyId);
1036
+ return this.request({
1037
+ method: "PATCH",
1038
+ path: joinPaths(basePath4, apiKeyId),
1039
+ bodyParams
1040
+ });
1041
+ }
1042
+ async delete(apiKeyId) {
1043
+ this.requireId(apiKeyId);
1044
+ return this.request({
1045
+ method: "DELETE",
1046
+ path: joinPaths(basePath4, apiKeyId)
1047
+ });
1048
+ }
1049
+ async revoke(params) {
1050
+ const { apiKeyId, revocationReason = null } = params;
1051
+ this.requireId(apiKeyId);
1021
1052
  return this.request({
1022
1053
  method: "POST",
1023
1054
  path: joinPaths(basePath4, apiKeyId, "revoke"),
1024
- bodyParams
1055
+ bodyParams: { revocationReason }
1025
1056
  });
1026
1057
  }
1027
1058
  async getSecret(apiKeyId) {
@@ -3098,6 +3129,26 @@ var IdPOAuthAccessToken = class _IdPOAuthAccessToken {
3098
3129
  data.updated_at
3099
3130
  );
3100
3131
  }
3132
+ /**
3133
+ * Creates an IdPOAuthAccessToken from a JWT payload.
3134
+ * Maps standard JWT claims and OAuth-specific fields to token properties.
3135
+ */
3136
+ static fromJwtPayload(payload, clockSkewInMs = 5e3) {
3137
+ const oauthPayload = payload;
3138
+ return new _IdPOAuthAccessToken(
3139
+ oauthPayload.jti ?? "",
3140
+ oauthPayload.client_id ?? "",
3141
+ "oauth_token",
3142
+ payload.sub,
3143
+ oauthPayload.scp ?? oauthPayload.scope?.split(" ") ?? [],
3144
+ false,
3145
+ null,
3146
+ payload.exp * 1e3 <= Date.now() - clockSkewInMs,
3147
+ payload.exp,
3148
+ payload.iat,
3149
+ payload.iat
3150
+ );
3151
+ }
3101
3152
  };
3102
3153
 
3103
3154
  // src/api/resources/Instance.ts
@@ -4330,14 +4381,33 @@ var M2M_TOKEN_PREFIX = "mt_";
4330
4381
  var OAUTH_TOKEN_PREFIX = "oat_";
4331
4382
  var API_KEY_PREFIX = "ak_";
4332
4383
  var MACHINE_TOKEN_PREFIXES = [M2M_TOKEN_PREFIX, OAUTH_TOKEN_PREFIX, API_KEY_PREFIX];
4384
+ var JwtFormatRegExp = /^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/;
4385
+ function isJwtFormat(token) {
4386
+ return JwtFormatRegExp.test(token);
4387
+ }
4388
+ var OAUTH_ACCESS_TOKEN_TYPES = ["at+jwt", "application/at+jwt"];
4389
+ function isOAuthJwt(token) {
4390
+ if (!isJwtFormat(token)) {
4391
+ return false;
4392
+ }
4393
+ try {
4394
+ const { data, errors } = decodeJwt(token);
4395
+ return !errors && !!data && OAUTH_ACCESS_TOKEN_TYPES.includes(data.header.typ);
4396
+ } catch {
4397
+ return false;
4398
+ }
4399
+ }
4333
4400
  function isMachineTokenByPrefix(token) {
4334
4401
  return MACHINE_TOKEN_PREFIXES.some((prefix) => token.startsWith(prefix));
4335
4402
  }
4403
+ function isMachineToken(token) {
4404
+ return isMachineTokenByPrefix(token) || isOAuthJwt(token);
4405
+ }
4336
4406
  function getMachineTokenType(token) {
4337
4407
  if (token.startsWith(M2M_TOKEN_PREFIX)) {
4338
4408
  return TokenType.M2MToken;
4339
4409
  }
4340
- if (token.startsWith(OAUTH_TOKEN_PREFIX)) {
4410
+ if (token.startsWith(OAUTH_TOKEN_PREFIX) || isOAuthJwt(token)) {
4341
4411
  return TokenType.OAuthToken;
4342
4412
  }
4343
4413
  if (token.startsWith(API_KEY_PREFIX)) {
@@ -4764,7 +4834,11 @@ var ClerkRequest = class extends Request {
4764
4834
  if (origin === initialUrl.origin) {
4765
4835
  return createClerkUrl(initialUrl);
4766
4836
  }
4767
- return createClerkUrl(initialUrl.pathname + initialUrl.search, origin);
4837
+ try {
4838
+ return createClerkUrl(initialUrl.pathname + initialUrl.search, origin);
4839
+ } catch {
4840
+ return createClerkUrl(initialUrl);
4841
+ }
4768
4842
  }
4769
4843
  getFirstValueFromHeader(value) {
4770
4844
  return value?.split(",")[0];
@@ -4993,7 +5067,91 @@ async function verifyM2MToken(token, options) {
4993
5067
  return handleClerkAPIError(TokenType.M2MToken, err, "Machine token not found");
4994
5068
  }
4995
5069
  }
5070
+ async function verifyJwtOAuthToken(accessToken, options) {
5071
+ let decoded;
5072
+ try {
5073
+ decoded = decodeJwt(accessToken);
5074
+ } catch (e) {
5075
+ return {
5076
+ data: void 0,
5077
+ tokenType: TokenType.OAuthToken,
5078
+ errors: [
5079
+ new MachineTokenVerificationError({
5080
+ code: MachineTokenVerificationErrorCode.TokenInvalid,
5081
+ message: e.message
5082
+ })
5083
+ ]
5084
+ };
5085
+ }
5086
+ const { data: decodedResult, errors } = decoded;
5087
+ if (errors) {
5088
+ return {
5089
+ data: void 0,
5090
+ tokenType: TokenType.OAuthToken,
5091
+ errors: [
5092
+ new MachineTokenVerificationError({
5093
+ code: MachineTokenVerificationErrorCode.TokenInvalid,
5094
+ message: errors[0].message
5095
+ })
5096
+ ]
5097
+ };
5098
+ }
5099
+ const { header } = decodedResult;
5100
+ const { kid } = header;
5101
+ let key;
5102
+ try {
5103
+ if (options.jwtKey) {
5104
+ key = loadClerkJwkFromPem({ kid, pem: options.jwtKey });
5105
+ } else if (options.secretKey) {
5106
+ key = await loadClerkJWKFromRemote({ ...options, kid });
5107
+ } else {
5108
+ return {
5109
+ data: void 0,
5110
+ tokenType: TokenType.OAuthToken,
5111
+ errors: [
5112
+ new MachineTokenVerificationError({
5113
+ action: TokenVerificationErrorAction.SetClerkJWTKey,
5114
+ message: "Failed to resolve JWK during verification.",
5115
+ code: MachineTokenVerificationErrorCode.TokenVerificationFailed
5116
+ })
5117
+ ]
5118
+ };
5119
+ }
5120
+ const { data: payload, errors: verifyErrors } = await verifyJwt(accessToken, {
5121
+ ...options,
5122
+ key,
5123
+ headerType: OAUTH_ACCESS_TOKEN_TYPES
5124
+ });
5125
+ if (verifyErrors) {
5126
+ return {
5127
+ data: void 0,
5128
+ tokenType: TokenType.OAuthToken,
5129
+ errors: [
5130
+ new MachineTokenVerificationError({
5131
+ code: MachineTokenVerificationErrorCode.TokenVerificationFailed,
5132
+ message: verifyErrors[0].message
5133
+ })
5134
+ ]
5135
+ };
5136
+ }
5137
+ const token = IdPOAuthAccessToken.fromJwtPayload(payload, options.clockSkewInMs);
5138
+ return { data: token, tokenType: TokenType.OAuthToken, errors: void 0 };
5139
+ } catch (error) {
5140
+ return {
5141
+ tokenType: TokenType.OAuthToken,
5142
+ errors: [
5143
+ new MachineTokenVerificationError({
5144
+ code: MachineTokenVerificationErrorCode.TokenVerificationFailed,
5145
+ message: error.message
5146
+ })
5147
+ ]
5148
+ };
5149
+ }
5150
+ }
4996
5151
  async function verifyOAuthToken(accessToken, options) {
5152
+ if (isJwtFormat(accessToken)) {
5153
+ return verifyJwtOAuthToken(accessToken, options);
5154
+ }
4997
5155
  try {
4998
5156
  const client = createBackendApiClient(options);
4999
5157
  const verifiedToken = await client.idPOAuthAccessToken.verify(accessToken);
@@ -5015,7 +5173,7 @@ async function verifyMachineAuthToken(token, options) {
5015
5173
  if (token.startsWith(M2M_TOKEN_PREFIX)) {
5016
5174
  return verifyM2MToken(token, options);
5017
5175
  }
5018
- if (token.startsWith(OAUTH_TOKEN_PREFIX)) {
5176
+ if (token.startsWith(OAUTH_TOKEN_PREFIX) || isJwtFormat(token)) {
5019
5177
  return verifyOAuthToken(token, options);
5020
5178
  }
5021
5179
  if (token.startsWith(API_KEY_PREFIX)) {
@@ -5116,6 +5274,9 @@ var HandshakeService = class {
5116
5274
  );
5117
5275
  url.searchParams.append(constants.QueryParameters.HandshakeReason, reason);
5118
5276
  url.searchParams.append(constants.QueryParameters.HandshakeFormat, "nonce");
5277
+ if (this.authenticateContext.sessionToken) {
5278
+ url.searchParams.append(constants.Cookies.Session, this.authenticateContext.sessionToken);
5279
+ }
5119
5280
  if (this.authenticateContext.instanceType === "development" && this.authenticateContext.devBrowserToken) {
5120
5281
  url.searchParams.append(constants.QueryParameters.DevBrowser, this.authenticateContext.devBrowserToken);
5121
5282
  }
@@ -5408,7 +5569,7 @@ function isTokenTypeInAcceptedArray(acceptsToken, authenticateContext) {
5408
5569
  let parsedTokenType = null;
5409
5570
  const { tokenInHeader } = authenticateContext;
5410
5571
  if (tokenInHeader) {
5411
- if (isMachineTokenByPrefix(tokenInHeader)) {
5572
+ if (isMachineToken(tokenInHeader)) {
5412
5573
  parsedTokenType = getMachineTokenType(tokenInHeader);
5413
5574
  } else {
5414
5575
  parsedTokenType = TokenType.SessionToken;
@@ -5818,7 +5979,7 @@ var authenticateRequest = (async (request, options) => {
5818
5979
  if (!tokenInHeader) {
5819
5980
  return handleSessionTokenError(new Error("Missing token in header"), "header");
5820
5981
  }
5821
- if (!isMachineTokenByPrefix(tokenInHeader)) {
5982
+ if (!isMachineToken(tokenInHeader)) {
5822
5983
  return signedOut({
5823
5984
  tokenType: acceptsToken,
5824
5985
  authenticateContext,
@@ -5847,7 +6008,7 @@ var authenticateRequest = (async (request, options) => {
5847
6008
  if (!tokenInHeader) {
5848
6009
  return handleSessionTokenError(new Error("Missing token in header"), "header");
5849
6010
  }
5850
- if (isMachineTokenByPrefix(tokenInHeader)) {
6011
+ if (isMachineToken(tokenInHeader)) {
5851
6012
  const parsedTokenType = getMachineTokenType(tokenInHeader);
5852
6013
  const mismatchState = checkTokenTypeMismatch(parsedTokenType, acceptsToken, authenticateContext);
5853
6014
  if (mismatchState) {