@auth0/auth0-server-js 1.3.0 → 1.5.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.d.ts CHANGED
@@ -1,14 +1,32 @@
1
- import { AuthorizationDetails, TelemetryConfig, AuthClient } from '@auth0/auth0-auth-js';
2
- export { TelemetryConfig } from '@auth0/auth0-auth-js';
1
+ import { AuthorizationDetails, DiscoveryCacheOptions, TelemetryConfig, AuthClient, ListAuthenticatorsOptions, AuthenticatorResponse, EnrollAuthenticatorOptions, EnrollmentResponse, ChallengeOptions, ChallengeResponse, MfaVerifyOptions } from '@auth0/auth0-auth-js';
2
+ export { AuthenticatorResponse, AuthenticatorType, ChallengeOptions, ChallengeResponse, DiscoveryCacheOptions, EnrollAuthenticatorOptions, EnrollEmailOptions, EnrollOobOptions, EnrollOtpOptions, EnrollmentResponse, ListAuthenticatorsOptions, MfaChallengeError, MfaEnrollmentError, MfaFactorType, MfaListAuthenticatorsError, MfaRequirements, MfaVerifyError, MfaVerifyOobOptions, MfaVerifyOptions, MfaVerifyOtpOptions, MfaVerifyRecoveryCodeOptions, OobChannel, OobEnrollmentResponse, OtpEnrollmentResponse, TelemetryConfig, isMfaRequiredError } from '@auth0/auth0-auth-js';
3
3
  import { JWTPayload } from 'jose';
4
4
 
5
+ /**
6
+ * Resolves the Auth0 custom domain at runtime using request-specific context.
7
+ *
8
+ * Should return a custom domain hostname (for example,
9
+ * `brand-1.custom-domain.com`) without protocol.
10
+ *
11
+ * The resolver receives a context object from SDK method calls (typically
12
+ * the same `storeOptions` object passed by the application).
13
+ * Resolved custom domains must be trusted and must belong to the same Auth0 tenant.
14
+ * Do not derive the returned domain directly from untrusted request input.
15
+ *
16
+ * The resolver must return a non-empty domain string. If it returns `null`,
17
+ * `undefined`, or an empty string at runtime, the SDK throws
18
+ * `InvalidConfigurationError`.
19
+ */
20
+ type DomainResolver<TStoreOptions> = (context?: TStoreOptions) => Promise<string> | string;
21
+
5
22
  interface ServerClientOptions<TStoreOptions = unknown> {
6
- domain: string;
23
+ domain: string | DomainResolver<TStoreOptions>;
7
24
  clientId: string;
8
25
  clientSecret?: string;
9
26
  clientAssertionSigningKey?: string | CryptoKey;
10
27
  clientAssertionSigningAlg?: string;
11
28
  authorizationParams?: AuthorizationParameters;
29
+ discoveryCache?: DiscoveryCacheOptions;
12
30
  transactionIdentifier?: string;
13
31
  stateIdentifier?: string;
14
32
  /**
@@ -73,11 +91,13 @@ interface SessionData {
73
91
  refreshToken: string | undefined;
74
92
  tokenSets: TokenSet[];
75
93
  connectionTokenSets?: ConnectionTokenSet[];
94
+ domain?: string;
76
95
  [key: string]: unknown;
77
96
  }
78
97
  interface TransactionData {
79
98
  audience?: string;
80
99
  codeVerifier: string;
100
+ domain?: string;
81
101
  [key: string]: unknown;
82
102
  }
83
103
  interface AbstractDataStore<TData, TStoreOptions = unknown> {
@@ -85,9 +105,16 @@ interface AbstractDataStore<TData, TStoreOptions = unknown> {
85
105
  get(identifier: string, options?: TStoreOptions): Promise<TData | undefined>;
86
106
  delete(identifier: string, options?: TStoreOptions): Promise<void>;
87
107
  }
108
+ /**
109
+ * Claims used to identify sessions for Backchannel Logout.
110
+ *
111
+ * `iss` is optional for backward compatibility, but is included by resolver-mode
112
+ * implementations to disambiguate sessions across multiple issuers/domains.
113
+ */
88
114
  type LogoutTokenClaims = {
89
115
  sub?: string;
90
116
  sid?: string;
117
+ iss?: string;
91
118
  };
92
119
  interface StateStore<TStoreOptions = unknown> extends AbstractDataStore<StateData, TStoreOptions> {
93
120
  deleteByLogoutToken(claims: LogoutTokenClaims, options?: TStoreOptions): Promise<void>;
@@ -202,7 +229,7 @@ interface SessionCookieOptions {
202
229
  *
203
230
  * Default: `lax`.
204
231
  */
205
- sameSite?: "strict" | "lax" | "none";
232
+ sameSite?: 'strict' | 'lax' | 'none';
206
233
  /**
207
234
  * The secure attribute of the session cookie.
208
235
  *
@@ -225,15 +252,108 @@ interface SessionCookieOptions {
225
252
  path?: string;
226
253
  }
227
254
 
255
+ /**
256
+ * Response from a successful MFA verification.
257
+ */
258
+ interface MfaVerifyResponse {
259
+ /** The access token */
260
+ accessToken: string;
261
+ /** The ID token (if openid scope was requested) */
262
+ idToken?: string;
263
+ /** The refresh token (if offline_access scope was requested) */
264
+ refreshToken?: string;
265
+ /** The token type (typically "bearer") */
266
+ tokenType: string;
267
+ /** Unix timestamp (seconds) at which the access token expires */
268
+ expiresAt: number;
269
+ /** The granted scopes */
270
+ scope?: string;
271
+ /** A new recovery code (only returned when verifying with a recovery code) */
272
+ recoveryCode?: string;
273
+ }
274
+ /**
275
+ * @internal
276
+ * Options for constructing a ServerMfaClient.
277
+ */
278
+ interface ServerMfaClientOptions<TStoreOptions = unknown> {
279
+ authClient: AuthClient;
280
+ domain: string;
281
+ stateStore: StateStore<TStoreOptions>;
282
+ stateStoreIdentifier: string;
283
+ defaultAudience: string;
284
+ }
285
+
286
+ declare class ServerMfaClient<TStoreOptions = unknown> {
287
+ #private;
288
+ /**
289
+ * @internal
290
+ */
291
+ constructor(options: ServerMfaClientOptions<TStoreOptions>);
292
+ /**
293
+ * Lists all MFA authenticators enrolled by the user.
294
+ *
295
+ * @param options - Options for listing authenticators
296
+ * @returns Promise resolving to an array of enrolled authenticators
297
+ * @throws {MfaListAuthenticatorsError} When the request fails
298
+ */
299
+ listAuthenticators(options: ListAuthenticatorsOptions): Promise<AuthenticatorResponse[]>;
300
+ /**
301
+ * Enrolls a new MFA authenticator for the user.
302
+ *
303
+ * @param options - Enrollment options
304
+ * @returns Promise resolving to enrollment response with authenticator details
305
+ * @throws {MfaEnrollmentError} When enrollment fails
306
+ */
307
+ enrollAuthenticator(options: EnrollAuthenticatorOptions): Promise<EnrollmentResponse>;
308
+ /**
309
+ * Initiates an MFA challenge for user verification.
310
+ *
311
+ * @param options - Challenge options
312
+ * @returns Promise resolving to challenge response with challenge details
313
+ * @throws {MfaChallengeError} When the challenge fails
314
+ */
315
+ challengeAuthenticator(options: ChallengeOptions): Promise<ChallengeResponse>;
316
+ /**
317
+ * Verifies an MFA challenge and completes the authentication flow.
318
+ *
319
+ * Exchanges the MFA token and verification code for access, ID, and refresh tokens,
320
+ * then saves them into the user's session automatically.
321
+ *
322
+ * @param options - The MFA token, factor type (otp / oob / recovery-code), and the code to verify
323
+ * @param storeOptions - Optional options forwarded to the session store. Can be omitted when
324
+ * using the built-in stores; required if your custom store needs extra context (e.g. a request object).
325
+ * @returns The tokens returned by Auth0 after successful verification
326
+ * @throws {MfaVerifyError} When verification fails (e.g. invalid token, wrong code)
327
+ */
328
+ verify(options: MfaVerifyOptions, storeOptions?: TStoreOptions): Promise<MfaVerifyResponse>;
329
+ }
330
+
228
331
  declare class ServerClient<TStoreOptions = unknown> {
229
332
  #private;
230
333
  /**
231
334
  * The underlying `authClient` instance that can be used to interact with the Auth0 Authentication API.
232
335
  * Generally, you should prefer to use the higher-level methods exposed on the `ServerClient` instance.
233
336
  *
337
+ * This property can only be used when `domain` is configured as a static string.
338
+ * In resolver mode (`domain` as a function), the SDK resolves the domain per request,
339
+ * so use `ServerClient` methods instead.
340
+ *
234
341
  * Important: the methods exposed on the `authClient` instance do not handle any session or state management.
235
342
  */
236
- readonly authClient: AuthClient;
343
+ get authClient(): AuthClient;
344
+ /**
345
+ * The MFA client for managing multi-factor authentication operations.
346
+ *
347
+ * Provides methods to list, enroll, and challenge MFA authenticators,
348
+ * as well as verify MFA challenges to complete authentication.
349
+ *
350
+ * The `verify` method integrates with the session state store, persisting tokens
351
+ * and user data after successful MFA verification.
352
+ *
353
+ * This property can only be used when `domain` is configured as a static string.
354
+ * In resolver mode (`domain` as a function), MFA is not supported.
355
+ */
356
+ get mfa(): ServerMfaClient<TStoreOptions>;
237
357
  constructor(options: ServerClientOptions<TStoreOptions>);
238
358
  /**
239
359
  * Starts the interactive login process, and returns a URL to redirect the user-agent to to request authorization at Auth0.
@@ -332,11 +452,19 @@ declare class ServerClient<TStoreOptions = unknown> {
332
452
  /**
333
453
  * Retrieve the user session from the store, or undefined if no session found.
334
454
  * @param storeOptions Optional options used to pass to the Transaction and State Store.
335
- * @returns The sessionm or undefined if no session found in the store.
455
+ * @returns The session or undefined if no session found in the store.
336
456
  */
337
457
  getSession(storeOptions?: TStoreOptions): Promise<SessionData | undefined>;
458
+ /**
459
+ * Retrieves the access token from the store, or calls Auth0 when the access token is expired and a refresh token is available in the store.
460
+ * Also updates the store when a new token was retrieved from Auth0.
461
+ * @param storeOptions Optional options used to pass to the Transaction and State Store.
462
+ *
463
+ * @throws {TokenByRefreshTokenError} If the refresh token was not found or there was an issue requesting the access token. When the cause is `mfa_required`, use `isMfaRequiredError(error)` to narrow the error and read `cause.mfa_token`.
464
+ *
465
+ * @returns The Token Set, containing the access token, as well as additional information.
466
+ */
338
467
  getAccessToken(storeOptions?: TStoreOptions): Promise<TokenSet>;
339
- getAccessToken(options: GetAccessTokenOptions, storeOptions?: TStoreOptions): Promise<TokenSet>;
340
468
  /**
341
469
  * Retrieves an access token for a connection.
342
470
  *
@@ -538,5 +666,12 @@ declare class InvalidConfigurationError extends Error {
538
666
  code: string;
539
667
  constructor(message: string);
540
668
  }
669
+ /**
670
+ * Error thrown when the issuer validation fails.
671
+ */
672
+ declare class IssuerValidationError extends Error {
673
+ code: string;
674
+ constructor(message: string);
675
+ }
541
676
 
542
- export { type AbstractDataStore, AbstractStateStore, AbstractTransactionStore, type AccessTokenForConnectionOptions, type AuthorizationParameters, BackchannelLogoutError, type ConnectionTokenSet, type CookieHandler, type CookieSerializeOptions, CookieTransactionStore, type EncryptedStoreOptions, type GetAccessTokenOptions, type InternalStateData, InvalidConfigurationError, type LoginBackchannelOptions, type LoginBackchannelResult, type LogoutOptions, type LogoutTokenClaims, MissingRequiredArgumentError, MissingSessionError, MissingTransactionError, ServerClient, type ServerClientOptions, type SessionConfiguration, type SessionCookieOptions, type SessionData, type SessionStore, type StartInteractiveLoginOptions, StartLinkUserError, type StartLinkUserOptions, type StartUnlinkUserOptions, type StateData, type StateStore, StatefulStateStore, type StatefulStateStoreOptions, StatelessStateStore, type TokenSet, type TransactionData, type TransactionStore, type UserClaims };
677
+ export { type AbstractDataStore, AbstractStateStore, AbstractTransactionStore, type AccessTokenForConnectionOptions, type AuthorizationParameters, BackchannelLogoutError, type ConnectionTokenSet, type CookieHandler, type CookieSerializeOptions, CookieTransactionStore, type DomainResolver, type EncryptedStoreOptions, type GetAccessTokenOptions, type InternalStateData, InvalidConfigurationError, IssuerValidationError, type LoginBackchannelOptions, type LoginBackchannelResult, type LogoutOptions, type LogoutTokenClaims, type MfaVerifyResponse, MissingRequiredArgumentError, MissingSessionError, MissingTransactionError, ServerClient, type ServerClientOptions, ServerMfaClient, type SessionConfiguration, type SessionCookieOptions, type SessionData, type SessionStore, type StartInteractiveLoginOptions, StartLinkUserError, type StartLinkUserOptions, type StartUnlinkUserOptions, type StateData, type StateStore, StatefulStateStore, type StatefulStateStoreOptions, StatelessStateStore, type TokenSet, type TransactionData, type TransactionStore, type UserClaims };