@auth0/auth0-server-js 1.8.0 → 1.10.0

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.
package/dist/index.cjs CHANGED
@@ -23,6 +23,7 @@ __export(index_exports, {
23
23
  AbstractStateStore: () => AbstractStateStore,
24
24
  AbstractTransactionStore: () => AbstractTransactionStore,
25
25
  BackchannelLogoutError: () => BackchannelLogoutError,
26
+ ChangePasswordError: () => import_auth0_auth_js4.ChangePasswordError,
26
27
  CookieTransactionStore: () => CookieTransactionStore,
27
28
  InvalidConfigurationError: () => InvalidConfigurationError,
28
29
  IssuerValidationError: () => IssuerValidationError,
@@ -30,22 +31,25 @@ __export(index_exports, {
30
31
  MfaEnrollmentError: () => import_auth0_auth_js2.MfaEnrollmentError,
31
32
  MfaListAuthenticatorsError: () => import_auth0_auth_js2.MfaListAuthenticatorsError,
32
33
  MfaVerifyError: () => import_auth0_auth_js2.MfaVerifyError,
33
- MissingClientAuthError: () => import_auth0_auth_js4.MissingClientAuthError,
34
+ MissingClientAuthError: () => import_auth0_auth_js5.MissingClientAuthError,
34
35
  MissingRequiredArgumentError: () => MissingRequiredArgumentError,
35
36
  MissingSessionError: () => MissingSessionError,
36
37
  MissingTransactionError: () => MissingTransactionError,
37
- OrganizationValidationError: () => import_auth0_auth_js3.OrganizationValidationError,
38
+ OrganizationValidationError: () => import_auth0_auth_js5.OrganizationValidationError,
38
39
  PasskeyChallengeError: () => import_auth0_auth_js3.PasskeyChallengeError,
39
40
  PasskeyGetTokenError: () => import_auth0_auth_js3.PasskeyGetTokenError,
40
41
  PasskeyRegisterError: () => import_auth0_auth_js3.PasskeyRegisterError,
41
42
  ServerClient: () => ServerClient,
43
+ ServerDatabaseClient: () => ServerDatabaseClient,
42
44
  ServerMfaClient: () => ServerMfaClient,
43
45
  ServerPasskeyClient: () => ServerPasskeyClient,
44
46
  SessionExpiredError: () => SessionExpiredError,
47
+ SignUpError: () => import_auth0_auth_js4.SignUpError,
45
48
  StartLinkUserError: () => StartLinkUserError,
46
49
  StatefulStateStore: () => StatefulStateStore,
47
50
  StatelessStateStore: () => StatelessStateStore,
48
- TokenExchangeError: () => import_auth0_auth_js4.TokenExchangeError,
51
+ TokenExchangeError: () => import_auth0_auth_js5.TokenExchangeError,
52
+ TokenRevocationError: () => import_auth0_auth_js5.TokenRevocationError,
49
53
  isMfaRequiredError: () => import_auth0_auth_js2.isMfaRequiredError
50
54
  });
51
55
  module.exports = __toCommonJS(index_exports);
@@ -250,7 +254,7 @@ function getTelemetryConfig(config) {
250
254
  return {
251
255
  enabled: true,
252
256
  name: config?.name ?? "@auth0/auth0-server-js",
253
- version: config?.version ?? "1.8.0"
257
+ version: config?.version ?? "1.10.0"
254
258
  };
255
259
  }
256
260
 
@@ -429,6 +433,53 @@ var ServerPasskeyClient = class {
429
433
  }
430
434
  };
431
435
 
436
+ // src/database/server-database-client.ts
437
+ var ServerDatabaseClient = class {
438
+ #options;
439
+ /**
440
+ * @internal
441
+ */
442
+ constructor(options) {
443
+ this.#options = options;
444
+ }
445
+ /**
446
+ * Registers a new user in a database connection.
447
+ *
448
+ * Delegates to the underlying `AuthClient.database.signUp` without any session
449
+ * state modification. The caller is responsible for handling the returned user
450
+ * data as needed.
451
+ *
452
+ * @param options The signup options (email, password, connection, etc.).
453
+ * @param storeOptions Optional options used to resolve the domain (resolver mode).
454
+ *
455
+ * @throws {SignUpError} If there was an issue signing the user up.
456
+ *
457
+ * @returns A promise resolving to the created user result with a normalized `id` field.
458
+ */
459
+ async signUp(options, storeOptions) {
460
+ const domain = await this.#options.resolveDomain(storeOptions);
461
+ return this.#options.getAuthClient(domain).database.signUp(options);
462
+ }
463
+ /**
464
+ * Requests a password-change email for a database connection user.
465
+ *
466
+ * Delegates to the underlying `AuthClient.database.changePassword` without any
467
+ * session state modification. The caller is responsible for informing the user
468
+ * of the sent email as needed.
469
+ *
470
+ * @param options The password change options (email, connection, organization, etc.).
471
+ * @param storeOptions Optional options used to resolve the domain (resolver mode).
472
+ *
473
+ * @throws {ChangePasswordError} If there was an issue requesting the password change.
474
+ *
475
+ * @returns A promise resolving to the server's plain-text confirmation message.
476
+ */
477
+ async changePassword(options, storeOptions) {
478
+ const domain = await this.#options.resolveDomain(storeOptions);
479
+ return this.#options.getAuthClient(domain).database.changePassword(options);
480
+ }
481
+ };
482
+
432
483
  // src/server-client.ts
433
484
  var normalizeDomain = (value) => {
434
485
  const trimmed = value.trim();
@@ -454,6 +505,7 @@ var ServerClient = class {
454
505
  #authClient;
455
506
  #mfaClient;
456
507
  #passkeyClient;
508
+ #databaseClient;
457
509
  /**
458
510
  * The underlying `authClient` instance that can be used to interact with the Auth0 Authentication API.
459
511
  * Generally, you should prefer to use the higher-level methods exposed on the `ServerClient` instance.
@@ -501,6 +553,21 @@ var ServerClient = class {
501
553
  get passkey() {
502
554
  return this.#passkeyClient;
503
555
  }
556
+ /**
557
+ * The database client for self-service sign-up and password-change requests
558
+ * against an Auth0 database connection.
559
+ *
560
+ * Provides `signUp()` to register a user and `changePassword()` to request a
561
+ * password-reset email. Both are pure passthrough operations to the Auth0
562
+ * Authentication API — they never read or write the session/state store.
563
+ *
564
+ * Like `passkey`, this property is available in both static and resolver
565
+ * (multi-tenant) domain modes. In resolver mode, pass `storeOptions` so the
566
+ * request resolves the intended tenant.
567
+ */
568
+ get database() {
569
+ return this.#databaseClient;
570
+ }
504
571
  constructor(options) {
505
572
  this.#options = options;
506
573
  this.#stateStoreIdentifier = this.#options.stateIdentifier || "__a0_session";
@@ -550,6 +617,10 @@ var ServerClient = class {
550
617
  defaultScope: this.#options.authorizationParams?.scope,
551
618
  defaultAudience: this.#options.authorizationParams?.audience
552
619
  });
620
+ this.#databaseClient = new ServerDatabaseClient({
621
+ resolveDomain: (storeOptions) => this.#resolveDomain(storeOptions),
622
+ getAuthClient: (domain) => this.#getAuthClient(domain)
623
+ });
553
624
  }
554
625
  async #resolveDomain(storeOptions) {
555
626
  if (typeof this.#options.domain === "function") {
@@ -601,9 +672,16 @@ var ServerClient = class {
601
672
  }
602
673
  /**
603
674
  * Starts the interactive login process, and returns a URL to redirect the user-agent to to request authorization at Auth0.
675
+ *
676
+ * When `organization` is provided (per-login option, client-level default, or via
677
+ * `authorizationParams.organization`), it is forwarded to `/authorize` and remembered so the
678
+ * returned ID token's organization claim can be validated in `completeInteractiveLogin`.
679
+ *
604
680
  * @param options Optional options used to configure the interactive login process.
605
681
  * @param storeOptions Optional options used to pass to the Transaction and State Store.
606
682
  *
683
+ * @throws {OrganizationValidationError} If the resolved `organization` is blank.
684
+ * @throws {InvalidConfigurationError} If `invitation` is provided without an `organization`.
607
685
  * @throws {BuildAuthorizationUrlError} If there was an issue when building the Authorization URL.
608
686
  *
609
687
  * @returns A promise resolving to a URL object, representing the URL to redirect the user-agent to to request authorization at Auth0.
@@ -614,6 +692,16 @@ var ServerClient = class {
614
692
  throw new MissingRequiredArgumentError("authorizationParams.redirect_uri");
615
693
  }
616
694
  const scope = ensureOpenIdScope(options?.authorizationParams?.scope ?? this.#options.authorizationParams?.scope);
695
+ const perLoginAuthParamsOrganization = typeof options?.authorizationParams?.organization === "string" ? options.authorizationParams.organization : void 0;
696
+ const clientAuthParamsOrganization = typeof this.#options.authorizationParams?.organization === "string" ? this.#options.authorizationParams.organization : void 0;
697
+ const resolvedOrganization = options?.organization ?? perLoginAuthParamsOrganization ?? this.#options.organization ?? clientAuthParamsOrganization;
698
+ if (resolvedOrganization !== void 0 && !resolvedOrganization.trim()) {
699
+ throw new import_auth0_auth_js.OrganizationValidationError("organization must not be blank");
700
+ }
701
+ const hasInvitation = !!(options?.invitation || options?.authorizationParams?.invitation);
702
+ if (hasInvitation && !resolvedOrganization) {
703
+ throw new InvalidConfigurationError("organization is required when invitation is provided.");
704
+ }
617
705
  const domain = await this.#resolveDomain(storeOptions);
618
706
  const authClient = this.#getAuthClient(domain);
619
707
  const { codeVerifier, authorizationUrl } = await authClient.buildAuthorizationUrl({
@@ -621,7 +709,9 @@ var ServerClient = class {
621
709
  authorizationParams: {
622
710
  ...options?.authorizationParams,
623
711
  redirect_uri: redirectUri,
624
- scope
712
+ scope,
713
+ ...resolvedOrganization ? { organization: resolvedOrganization } : {},
714
+ ...options?.invitation ? { invitation: options.invitation } : {}
625
715
  }
626
716
  });
627
717
  const transactionState = {
@@ -629,6 +719,9 @@ var ServerClient = class {
629
719
  codeVerifier,
630
720
  domain
631
721
  };
722
+ if (resolvedOrganization) {
723
+ transactionState.organization = resolvedOrganization;
724
+ }
632
725
  if (options?.appState) {
633
726
  transactionState.appState = options.appState;
634
727
  }
@@ -643,6 +736,7 @@ var ServerClient = class {
643
736
  *
644
737
  * @throws {MissingTransactionError} When no transaction was found.
645
738
  * @throws {TokenByCodeError} If there was an issue requesting the access token.
739
+ * @throws {OrganizationValidationError} When an organization was requested at login and the returned ID token's organization claim is missing or does not match; nothing is persisted.
646
740
  * @throws {SessionExpiredError} When the ID token's `session_expiry` is already in the past at login (the session is born expired); nothing is persisted.
647
741
  *
648
742
  * @returns A promise resolving to an object, containing the original appState (if present) and the authorizationDetails (when RAR was used).
@@ -656,7 +750,8 @@ var ServerClient = class {
656
750
  const authClient = this.#getAuthClient(domain);
657
751
  const tokenEndpointResponse = await authClient.getTokenByCode(url, {
658
752
  // TransactionData.codeVerifier is optional only to accommodate magic-link transactions.
659
- codeVerifier: transactionData.codeVerifier
753
+ codeVerifier: transactionData.codeVerifier,
754
+ organization: transactionData.organization
660
755
  });
661
756
  await this.#transactionStore.delete(this.#transactionStoreIdentifier, storeOptions);
662
757
  const existingStateData = await this.#stateStore.get(this.#stateStoreIdentifier, storeOptions);
@@ -1191,6 +1286,47 @@ var ServerClient = class {
1191
1286
  loginHint: options.loginHint
1192
1287
  };
1193
1288
  }
1289
+ /**
1290
+ * Revokes the refresh token stored in the current session, or an explicitly supplied token.
1291
+ *
1292
+ * In resolver mode, revocation only occurs when the session domain matches the domain resolved
1293
+ * for the current request. If the domains differ (or the session has no stored domain), the call
1294
+ * returns without revoking to avoid sending a token to the wrong tenant. This guard applies even
1295
+ * when a token is passed explicitly via `options.token`.
1296
+ *
1297
+ * @param options Optionally supply a token to revoke instead of reading from the session.
1298
+ * @param storeOptions Optional options passed to the StateStore.
1299
+ *
1300
+ * @throws {MissingRequiredArgumentError} If `options.token` is an empty string.
1301
+ * @throws {MissingSessionError} If no refresh token is found in the session and none was provided.
1302
+ * @throws {TokenRevocationError} If the revocation request fails.
1303
+ */
1304
+ async revokeRefreshToken(options = {}, storeOptions) {
1305
+ if (options.token !== void 0 && options.token.length === 0) {
1306
+ throw new MissingRequiredArgumentError("options.token must not be an empty string.");
1307
+ }
1308
+ let refreshToken = options.token;
1309
+ const needsStateData = !refreshToken || this.#isResolverMode();
1310
+ const stateData = needsStateData ? await this.#stateStore.get(this.#stateStoreIdentifier, storeOptions) : void 0;
1311
+ if (!refreshToken) {
1312
+ refreshToken = stateData?.refreshToken;
1313
+ }
1314
+ if (!refreshToken) {
1315
+ throw new MissingSessionError("Unable to revoke refresh token: no refresh token found in session.");
1316
+ }
1317
+ let authClient;
1318
+ if (this.#isResolverMode()) {
1319
+ const resolvedDomain = await this.#resolveDomain(storeOptions);
1320
+ const sessionDomain = stateData ? this.#getSessionDomain(stateData) : void 0;
1321
+ if (stateData && sessionDomain !== resolvedDomain) {
1322
+ return;
1323
+ }
1324
+ authClient = this.#getAuthClient(sessionDomain ?? resolvedDomain);
1325
+ } else {
1326
+ authClient = this.authClient;
1327
+ }
1328
+ await authClient.revokeToken({ token: refreshToken, tokenTypeHint: "refresh_token" });
1329
+ }
1194
1330
  /**
1195
1331
  * Logs the user out and returns a URL to redirect the user-agent to after they log out.
1196
1332
  * @param options Options used to configure the logout process.
@@ -1199,17 +1335,26 @@ var ServerClient = class {
1199
1335
  */
1200
1336
  async logout(options, storeOptions) {
1201
1337
  if (!this.#isResolverMode()) {
1338
+ try {
1339
+ await this.revokeRefreshToken({}, storeOptions);
1340
+ } catch {
1341
+ }
1202
1342
  await this.#stateStore.delete(this.#stateStoreIdentifier, storeOptions);
1203
1343
  return this.authClient.buildLogoutUrl(options);
1204
1344
  }
1205
1345
  const resolvedDomain = await this.#resolveDomain(storeOptions);
1206
1346
  const authClient = this.#getAuthClient(resolvedDomain);
1207
1347
  const stateData = await this.#stateStore.get(this.#stateStoreIdentifier, storeOptions);
1208
- const sessionDomain = stateData ? this.#getSessionDomain(stateData) : void 0;
1209
1348
  if (!stateData) {
1210
1349
  return authClient.buildLogoutUrl(options);
1211
1350
  }
1212
- if (sessionDomain && sessionDomain === resolvedDomain) {
1351
+ const sessionDomain = this.#getSessionDomain(stateData);
1352
+ const domainMatches = sessionDomain === resolvedDomain;
1353
+ if (domainMatches) {
1354
+ try {
1355
+ await this.revokeRefreshToken({}, storeOptions);
1356
+ } catch {
1357
+ }
1213
1358
  await this.#stateStore.delete(this.#stateStoreIdentifier, storeOptions);
1214
1359
  }
1215
1360
  return authClient.buildLogoutUrl(options);
@@ -1427,7 +1572,7 @@ var AbstractTransactionStore = class extends AbstractStore {
1427
1572
  };
1428
1573
 
1429
1574
  // src/index.ts
1430
- var import_auth0_auth_js4 = require("@auth0/auth0-auth-js");
1575
+ var import_auth0_auth_js5 = require("@auth0/auth0-auth-js");
1431
1576
 
1432
1577
  // src/store/cookie-transaction-store.ts
1433
1578
  var CookieTransactionStore = class extends AbstractTransactionStore {
@@ -1628,11 +1773,15 @@ var import_auth0_auth_js2 = require("@auth0/auth0-auth-js");
1628
1773
 
1629
1774
  // src/passkey/index.ts
1630
1775
  var import_auth0_auth_js3 = require("@auth0/auth0-auth-js");
1776
+
1777
+ // src/database/index.ts
1778
+ var import_auth0_auth_js4 = require("@auth0/auth0-auth-js");
1631
1779
  // Annotate the CommonJS export names for ESM import in node:
1632
1780
  0 && (module.exports = {
1633
1781
  AbstractStateStore,
1634
1782
  AbstractTransactionStore,
1635
1783
  BackchannelLogoutError,
1784
+ ChangePasswordError,
1636
1785
  CookieTransactionStore,
1637
1786
  InvalidConfigurationError,
1638
1787
  IssuerValidationError,
@@ -1649,13 +1798,16 @@ var import_auth0_auth_js3 = require("@auth0/auth0-auth-js");
1649
1798
  PasskeyGetTokenError,
1650
1799
  PasskeyRegisterError,
1651
1800
  ServerClient,
1801
+ ServerDatabaseClient,
1652
1802
  ServerMfaClient,
1653
1803
  ServerPasskeyClient,
1654
1804
  SessionExpiredError,
1805
+ SignUpError,
1655
1806
  StartLinkUserError,
1656
1807
  StatefulStateStore,
1657
1808
  StatelessStateStore,
1658
1809
  TokenExchangeError,
1810
+ TokenRevocationError,
1659
1811
  isMfaRequiredError
1660
1812
  });
1661
1813
  //# sourceMappingURL=index.cjs.map