@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.js CHANGED
@@ -144,13 +144,36 @@ var APIKeysAPI = class extends AbstractAPI {
144
144
  bodyParams: params
145
145
  });
146
146
  }
147
- async revoke(params) {
147
+ async get(apiKeyId) {
148
+ this.requireId(apiKeyId);
149
+ return this.request({
150
+ method: "GET",
151
+ path: joinPaths(basePath4, apiKeyId)
152
+ });
153
+ }
154
+ async update(params) {
148
155
  const { apiKeyId, ...bodyParams } = params;
149
156
  this.requireId(apiKeyId);
157
+ return this.request({
158
+ method: "PATCH",
159
+ path: joinPaths(basePath4, apiKeyId),
160
+ bodyParams
161
+ });
162
+ }
163
+ async delete(apiKeyId) {
164
+ this.requireId(apiKeyId);
165
+ return this.request({
166
+ method: "DELETE",
167
+ path: joinPaths(basePath4, apiKeyId)
168
+ });
169
+ }
170
+ async revoke(params) {
171
+ const { apiKeyId, revocationReason = null } = params;
172
+ this.requireId(apiKeyId);
150
173
  return this.request({
151
174
  method: "POST",
152
175
  path: joinPaths(basePath4, apiKeyId, "revoke"),
153
- bodyParams
176
+ bodyParams: { revocationReason }
154
177
  });
155
178
  }
156
179
  async getSecret(apiKeyId) {
@@ -1668,7 +1691,7 @@ var snakecase_keys_default = snakecaseKeys;
1668
1691
  // src/constants.ts
1669
1692
  var API_URL = "https://api.clerk.com";
1670
1693
  var API_VERSION = "v1";
1671
- var USER_AGENT = `${"@clerk/backend"}@${"3.0.0-snapshot.v20251204175016"}`;
1694
+ var USER_AGENT = `${"@clerk/backend"}@${"3.0.0-snapshot.v20251211120550"}`;
1672
1695
  var MAX_CACHE_LAST_UPDATED_AT_SECONDS = 5 * 60;
1673
1696
  var SUPPORTED_BAPI_VERSION = "2025-11-10";
1674
1697
  var Attributes = {
@@ -2330,6 +2353,26 @@ var IdPOAuthAccessToken = class _IdPOAuthAccessToken {
2330
2353
  data.updated_at
2331
2354
  );
2332
2355
  }
2356
+ /**
2357
+ * Creates an IdPOAuthAccessToken from a JWT payload.
2358
+ * Maps standard JWT claims and OAuth-specific fields to token properties.
2359
+ */
2360
+ static fromJwtPayload(payload, clockSkewInMs = 5e3) {
2361
+ const oauthPayload = payload;
2362
+ return new _IdPOAuthAccessToken(
2363
+ oauthPayload.jti ?? "",
2364
+ oauthPayload.client_id ?? "",
2365
+ "oauth_token",
2366
+ payload.sub,
2367
+ oauthPayload.scp ?? oauthPayload.scope?.split(" ") ?? [],
2368
+ false,
2369
+ null,
2370
+ payload.exp * 1e3 <= Date.now() - clockSkewInMs,
2371
+ payload.exp,
2372
+ payload.iat,
2373
+ payload.iat
2374
+ );
2375
+ }
2333
2376
  };
2334
2377
 
2335
2378
  // src/api/resources/Instance.ts
@@ -3625,17 +3668,24 @@ var TokenVerificationError = class _TokenVerificationError extends Error {
3625
3668
  var MachineTokenVerificationErrorCode = {
3626
3669
  TokenInvalid: "token-invalid",
3627
3670
  InvalidSecretKey: "secret-key-invalid",
3628
- UnexpectedError: "unexpected-error"
3671
+ UnexpectedError: "unexpected-error",
3672
+ TokenVerificationFailed: "token-verification-failed"
3629
3673
  };
3630
3674
  var MachineTokenVerificationError = class _MachineTokenVerificationError extends Error {
3631
- constructor({ message, code, status }) {
3675
+ constructor({
3676
+ message,
3677
+ code,
3678
+ status,
3679
+ action
3680
+ }) {
3632
3681
  super(message);
3633
3682
  Object.setPrototypeOf(this, _MachineTokenVerificationError.prototype);
3634
3683
  this.code = code;
3635
3684
  this.status = status;
3685
+ this.action = action;
3636
3686
  }
3637
3687
  getFullMessage() {
3638
- return `${this.message} (code=${this.code}, status=${this.status})`;
3688
+ return `${this.message} (code=${this.code}, status=${this.status || "n/a"})`;
3639
3689
  }
3640
3690
  };
3641
3691
 
@@ -3773,15 +3823,16 @@ var assertAudienceClaim = (aud, audience) => {
3773
3823
  }
3774
3824
  }
3775
3825
  };
3776
- var assertHeaderType = (typ) => {
3826
+ var assertHeaderType = (typ, allowedTypes = "JWT") => {
3777
3827
  if (typeof typ === "undefined") {
3778
3828
  return;
3779
3829
  }
3780
- if (typ !== "JWT") {
3830
+ const allowed = Array.isArray(allowedTypes) ? allowedTypes : [allowedTypes];
3831
+ if (!allowed.includes(typ)) {
3781
3832
  throw new TokenVerificationError({
3782
3833
  action: TokenVerificationErrorAction.EnsureClerkJWT,
3783
3834
  reason: TokenVerificationErrorReason.TokenInvalid,
3784
- message: `Invalid JWT type ${JSON.stringify(typ)}. Expected "JWT".`
3835
+ message: `Invalid JWT type ${JSON.stringify(typ)}. Expected "${allowed.join(", ")}".`
3785
3836
  });
3786
3837
  }
3787
3838
  };
@@ -3952,7 +4003,7 @@ function decodeJwt(token) {
3952
4003
  return { data };
3953
4004
  }
3954
4005
  async function verifyJwt(token, options) {
3955
- const { audience, authorizedParties, clockSkewInMs, key } = options;
4006
+ const { audience, authorizedParties, clockSkewInMs, key, headerType } = options;
3956
4007
  const clockSkew = clockSkewInMs || DEFAULT_CLOCK_SKEW_IN_MS;
3957
4008
  const { data: decoded, errors } = decodeJwt(token);
3958
4009
  if (errors) {
@@ -3961,7 +4012,7 @@ async function verifyJwt(token, options) {
3961
4012
  const { header, payload } = decoded;
3962
4013
  try {
3963
4014
  const { typ, alg } = header;
3964
- assertHeaderType(typ);
4015
+ assertHeaderType(typ, headerType);
3965
4016
  assertHeaderAlgorithm(alg);
3966
4017
  const { azp, sub, aud, iat, exp, nbf } = payload;
3967
4018
  assertSubClaim(sub);
@@ -4280,7 +4331,11 @@ var ClerkRequest = class extends Request {
4280
4331
  if (origin === initialUrl.origin) {
4281
4332
  return createClerkUrl(initialUrl);
4282
4333
  }
4283
- return createClerkUrl(initialUrl.pathname + initialUrl.search, origin);
4334
+ try {
4335
+ return createClerkUrl(initialUrl.pathname + initialUrl.search, origin);
4336
+ } catch {
4337
+ return createClerkUrl(initialUrl);
4338
+ }
4284
4339
  }
4285
4340
  getFirstValueFromHeader(value) {
4286
4341
  return value?.split(",")[0];
@@ -4431,14 +4486,33 @@ var M2M_TOKEN_PREFIX = "mt_";
4431
4486
  var OAUTH_TOKEN_PREFIX = "oat_";
4432
4487
  var API_KEY_PREFIX = "ak_";
4433
4488
  var MACHINE_TOKEN_PREFIXES = [M2M_TOKEN_PREFIX, OAUTH_TOKEN_PREFIX, API_KEY_PREFIX];
4489
+ var JwtFormatRegExp = /^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/;
4490
+ function isJwtFormat(token) {
4491
+ return JwtFormatRegExp.test(token);
4492
+ }
4493
+ var OAUTH_ACCESS_TOKEN_TYPES = ["at+jwt", "application/at+jwt"];
4494
+ function isOAuthJwt(token) {
4495
+ if (!isJwtFormat(token)) {
4496
+ return false;
4497
+ }
4498
+ try {
4499
+ const { data, errors } = decodeJwt(token);
4500
+ return !errors && !!data && OAUTH_ACCESS_TOKEN_TYPES.includes(data.header.typ);
4501
+ } catch {
4502
+ return false;
4503
+ }
4504
+ }
4434
4505
  function isMachineTokenByPrefix(token) {
4435
4506
  return MACHINE_TOKEN_PREFIXES.some((prefix) => token.startsWith(prefix));
4436
4507
  }
4508
+ function isMachineToken(token) {
4509
+ return isMachineTokenByPrefix(token) || isOAuthJwt(token);
4510
+ }
4437
4511
  function getMachineTokenType(token) {
4438
4512
  if (token.startsWith(M2M_TOKEN_PREFIX)) {
4439
4513
  return TokenType.M2MToken;
4440
4514
  }
4441
- if (token.startsWith(OAUTH_TOKEN_PREFIX)) {
4515
+ if (token.startsWith(OAUTH_TOKEN_PREFIX) || isOAuthJwt(token)) {
4442
4516
  return TokenType.OAuthToken;
4443
4517
  }
4444
4518
  if (token.startsWith(API_KEY_PREFIX)) {
@@ -4537,7 +4611,91 @@ async function verifyM2MToken(token, options) {
4537
4611
  return handleClerkAPIError(TokenType.M2MToken, err, "Machine token not found");
4538
4612
  }
4539
4613
  }
4614
+ async function verifyJwtOAuthToken(accessToken, options) {
4615
+ let decoded;
4616
+ try {
4617
+ decoded = decodeJwt(accessToken);
4618
+ } catch (e) {
4619
+ return {
4620
+ data: void 0,
4621
+ tokenType: TokenType.OAuthToken,
4622
+ errors: [
4623
+ new MachineTokenVerificationError({
4624
+ code: MachineTokenVerificationErrorCode.TokenInvalid,
4625
+ message: e.message
4626
+ })
4627
+ ]
4628
+ };
4629
+ }
4630
+ const { data: decodedResult, errors } = decoded;
4631
+ if (errors) {
4632
+ return {
4633
+ data: void 0,
4634
+ tokenType: TokenType.OAuthToken,
4635
+ errors: [
4636
+ new MachineTokenVerificationError({
4637
+ code: MachineTokenVerificationErrorCode.TokenInvalid,
4638
+ message: errors[0].message
4639
+ })
4640
+ ]
4641
+ };
4642
+ }
4643
+ const { header } = decodedResult;
4644
+ const { kid } = header;
4645
+ let key;
4646
+ try {
4647
+ if (options.jwtKey) {
4648
+ key = loadClerkJwkFromPem({ kid, pem: options.jwtKey });
4649
+ } else if (options.secretKey) {
4650
+ key = await loadClerkJWKFromRemote({ ...options, kid });
4651
+ } else {
4652
+ return {
4653
+ data: void 0,
4654
+ tokenType: TokenType.OAuthToken,
4655
+ errors: [
4656
+ new MachineTokenVerificationError({
4657
+ action: TokenVerificationErrorAction.SetClerkJWTKey,
4658
+ message: "Failed to resolve JWK during verification.",
4659
+ code: MachineTokenVerificationErrorCode.TokenVerificationFailed
4660
+ })
4661
+ ]
4662
+ };
4663
+ }
4664
+ const { data: payload, errors: verifyErrors } = await verifyJwt(accessToken, {
4665
+ ...options,
4666
+ key,
4667
+ headerType: OAUTH_ACCESS_TOKEN_TYPES
4668
+ });
4669
+ if (verifyErrors) {
4670
+ return {
4671
+ data: void 0,
4672
+ tokenType: TokenType.OAuthToken,
4673
+ errors: [
4674
+ new MachineTokenVerificationError({
4675
+ code: MachineTokenVerificationErrorCode.TokenVerificationFailed,
4676
+ message: verifyErrors[0].message
4677
+ })
4678
+ ]
4679
+ };
4680
+ }
4681
+ const token = IdPOAuthAccessToken.fromJwtPayload(payload, options.clockSkewInMs);
4682
+ return { data: token, tokenType: TokenType.OAuthToken, errors: void 0 };
4683
+ } catch (error) {
4684
+ return {
4685
+ tokenType: TokenType.OAuthToken,
4686
+ errors: [
4687
+ new MachineTokenVerificationError({
4688
+ code: MachineTokenVerificationErrorCode.TokenVerificationFailed,
4689
+ message: error.message
4690
+ })
4691
+ ]
4692
+ };
4693
+ }
4694
+ }
4540
4695
  async function verifyOAuthToken(accessToken, options) {
4696
+ if (isJwtFormat(accessToken)) {
4697
+ return verifyJwtOAuthToken(accessToken, options);
4698
+ }
4541
4699
  try {
4542
4700
  const client = createBackendApiClient(options);
4543
4701
  const verifiedToken = await client.idPOAuthAccessToken.verify(accessToken);
@@ -4559,7 +4717,7 @@ async function verifyMachineAuthToken(token, options) {
4559
4717
  if (token.startsWith(M2M_TOKEN_PREFIX)) {
4560
4718
  return verifyM2MToken(token, options);
4561
4719
  }
4562
- if (token.startsWith(OAUTH_TOKEN_PREFIX)) {
4720
+ if (token.startsWith(OAUTH_TOKEN_PREFIX) || isJwtFormat(token)) {
4563
4721
  return verifyOAuthToken(token, options);
4564
4722
  }
4565
4723
  if (token.startsWith(API_KEY_PREFIX)) {
@@ -4995,6 +5153,9 @@ var HandshakeService = class {
4995
5153
  );
4996
5154
  url.searchParams.append(constants.QueryParameters.HandshakeReason, reason);
4997
5155
  url.searchParams.append(constants.QueryParameters.HandshakeFormat, "nonce");
5156
+ if (this.authenticateContext.sessionToken) {
5157
+ url.searchParams.append(constants.Cookies.Session, this.authenticateContext.sessionToken);
5158
+ }
4998
5159
  if (this.authenticateContext.instanceType === "development" && this.authenticateContext.devBrowserToken) {
4999
5160
  url.searchParams.append(constants.QueryParameters.DevBrowser, this.authenticateContext.devBrowserToken);
5000
5161
  }
@@ -5287,7 +5448,7 @@ function isTokenTypeInAcceptedArray(acceptsToken, authenticateContext) {
5287
5448
  let parsedTokenType = null;
5288
5449
  const { tokenInHeader } = authenticateContext;
5289
5450
  if (tokenInHeader) {
5290
- if (isMachineTokenByPrefix(tokenInHeader)) {
5451
+ if (isMachineToken(tokenInHeader)) {
5291
5452
  parsedTokenType = getMachineTokenType(tokenInHeader);
5292
5453
  } else {
5293
5454
  parsedTokenType = TokenType.SessionToken;
@@ -5697,7 +5858,7 @@ var authenticateRequest = (async (request, options) => {
5697
5858
  if (!tokenInHeader) {
5698
5859
  return handleSessionTokenError(new Error("Missing token in header"), "header");
5699
5860
  }
5700
- if (!isMachineTokenByPrefix(tokenInHeader)) {
5861
+ if (!isMachineToken(tokenInHeader)) {
5701
5862
  return signedOut({
5702
5863
  tokenType: acceptsToken,
5703
5864
  authenticateContext,
@@ -5726,7 +5887,7 @@ var authenticateRequest = (async (request, options) => {
5726
5887
  if (!tokenInHeader) {
5727
5888
  return handleSessionTokenError(new Error("Missing token in header"), "header");
5728
5889
  }
5729
- if (isMachineTokenByPrefix(tokenInHeader)) {
5890
+ if (isMachineToken(tokenInHeader)) {
5730
5891
  const parsedTokenType = getMachineTokenType(tokenInHeader);
5731
5892
  const mismatchState = checkTokenTypeMismatch(parsedTokenType, acceptsToken, authenticateContext);
5732
5893
  if (mismatchState) {