@auth0/auth0-server-js 1.10.0 → 1.11.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/README.md +40 -0
- package/dist/index.cjs +195 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +184 -3
- package/dist/index.d.ts +184 -3
- package/dist/index.js +204 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
// src/errors.ts
|
|
2
|
+
var TokenExchangeErrorCode = {
|
|
3
|
+
ACTOR_UNAVAILABLE: "actor_unavailable",
|
|
4
|
+
SETACTOR_REQUIRED: "setactor_required",
|
|
5
|
+
SESSION_TRANSFER_DISABLED: "session_transfer_disabled"
|
|
6
|
+
};
|
|
2
7
|
var MissingTransactionError = class extends Error {
|
|
3
8
|
code = "missing_transaction_error";
|
|
4
9
|
constructor(message) {
|
|
@@ -166,7 +171,8 @@ import {
|
|
|
166
171
|
OrganizationValidationError,
|
|
167
172
|
PasswordlessStartError,
|
|
168
173
|
PasswordlessVerifyError,
|
|
169
|
-
TokenByRefreshTokenError
|
|
174
|
+
TokenByRefreshTokenError,
|
|
175
|
+
TokenExchangeError
|
|
170
176
|
} from "@auth0/auth0-auth-js";
|
|
171
177
|
|
|
172
178
|
// src/utils.ts
|
|
@@ -205,7 +211,7 @@ function getTelemetryConfig(config) {
|
|
|
205
211
|
return {
|
|
206
212
|
enabled: true,
|
|
207
213
|
name: config?.name ?? "@auth0/auth0-server-js",
|
|
208
|
-
version: config?.version ?? "1.
|
|
214
|
+
version: config?.version ?? "1.11.0"
|
|
209
215
|
};
|
|
210
216
|
}
|
|
211
217
|
|
|
@@ -445,6 +451,24 @@ var decodeIssuer = (token) => {
|
|
|
445
451
|
return void 0;
|
|
446
452
|
}
|
|
447
453
|
};
|
|
454
|
+
var ID_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:id_token";
|
|
455
|
+
var ID_TOKEN_EXPIRY_SKEW_SECONDS = 30;
|
|
456
|
+
var actorUnavailableError = (message) => {
|
|
457
|
+
const error = new TokenExchangeError(message);
|
|
458
|
+
error.code = TokenExchangeErrorCode.ACTOR_UNAVAILABLE;
|
|
459
|
+
return error;
|
|
460
|
+
};
|
|
461
|
+
var isTokenExpired = (token) => {
|
|
462
|
+
try {
|
|
463
|
+
const { exp } = decodeJwt(token);
|
|
464
|
+
if (typeof exp !== "number") {
|
|
465
|
+
return true;
|
|
466
|
+
}
|
|
467
|
+
return exp <= Date.now() / 1e3 + ID_TOKEN_EXPIRY_SKEW_SECONDS;
|
|
468
|
+
} catch {
|
|
469
|
+
return true;
|
|
470
|
+
}
|
|
471
|
+
};
|
|
448
472
|
var ServerClient = class {
|
|
449
473
|
#options;
|
|
450
474
|
#transactionStore;
|
|
@@ -1369,6 +1393,170 @@ var ServerClient = class {
|
|
|
1369
1393
|
const authClient = this.#getAuthClient(domain);
|
|
1370
1394
|
return authClient.exchangeToken(options);
|
|
1371
1395
|
}
|
|
1396
|
+
/**
|
|
1397
|
+
* Requests a Session Transfer Token (STT) for impersonation via session transfer (RFC 8693).
|
|
1398
|
+
*
|
|
1399
|
+
* Performs a Custom Token Exchange against the `urn:{domain}:session_transfer` audience and
|
|
1400
|
+
* returns the resulting STT. The audience is built from the SDK's resolved request domain, so
|
|
1401
|
+
* it is correct under multiple custom domains. The returned STT is opaque and single-use — hand
|
|
1402
|
+
* it to {@link ServerClient.buildSessionTransferRedirect} and do not decode, cache, or persist
|
|
1403
|
+
* it. This method writes nothing to the state store for the STT itself; the `act` claim is not
|
|
1404
|
+
* on the result — it only appears on the target session's tokens once the STT is redeemed.
|
|
1405
|
+
*
|
|
1406
|
+
* An actor is mandatory for an STT (this is what makes it auditable impersonation). It is
|
|
1407
|
+
* resolved in this order: an explicit `options.actor` wins; otherwise the current agent
|
|
1408
|
+
* session's ID token is used, refreshed when it has expired; if neither is available the method
|
|
1409
|
+
* throws before any network call.
|
|
1410
|
+
*
|
|
1411
|
+
* @param options Options including the developer-supplied `subjectToken`/`subjectTokenType` and an optional explicit `actor`.
|
|
1412
|
+
* @param storeOptions Optional options used to read the agent session (for the actor) and resolve the request domain.
|
|
1413
|
+
*
|
|
1414
|
+
* @throws {TokenExchangeError} With code `actor_unavailable` when no explicit actor is given and no usable session ID token can be resolved — no logged-in agent, a session that belongs to a different domain in resolver mode, or an expired ID token that cannot be refreshed (raised client-side, before any network call). With the default code when the exchange itself fails; a server-side `setactor_required` or `session_transfer_disabled` condition is surfaced via `cause.error` / `cause.error_description`.
|
|
1415
|
+
* @throws {MissingClientAuthError} When client credentials are not configured (STT requires a confidential client).
|
|
1416
|
+
* @throws {MissingRequiredArgumentError} When `subjectToken` or `subjectTokenType` is missing or blank (raised before any session read or network call).
|
|
1417
|
+
*
|
|
1418
|
+
* @returns A promise resolving to a {@link SessionTransferTokenResult} containing the STT and its metadata.
|
|
1419
|
+
*/
|
|
1420
|
+
async requestSessionTransferToken(options, storeOptions) {
|
|
1421
|
+
if (!options.subjectToken || !options.subjectToken.trim()) {
|
|
1422
|
+
throw new MissingRequiredArgumentError("subjectToken");
|
|
1423
|
+
}
|
|
1424
|
+
if (!options.subjectTokenType || !options.subjectTokenType.trim()) {
|
|
1425
|
+
throw new MissingRequiredArgumentError("subjectTokenType");
|
|
1426
|
+
}
|
|
1427
|
+
const domain = await this.#resolveDomain(storeOptions);
|
|
1428
|
+
const actor = await this.#resolveSessionTransferActor(options.actor, domain, storeOptions);
|
|
1429
|
+
const authClient = this.#getAuthClient(domain);
|
|
1430
|
+
const response = await authClient.exchangeToken({
|
|
1431
|
+
subjectToken: options.subjectToken,
|
|
1432
|
+
subjectTokenType: options.subjectTokenType,
|
|
1433
|
+
audience: `urn:${domain}:session_transfer`,
|
|
1434
|
+
scope: options.scope,
|
|
1435
|
+
actorToken: actor.token,
|
|
1436
|
+
actorTokenType: actor.type,
|
|
1437
|
+
extra: options.extra
|
|
1438
|
+
});
|
|
1439
|
+
return {
|
|
1440
|
+
sessionTransferToken: response.accessToken,
|
|
1441
|
+
// Surface exactly what the server returned — never fabricate the URN, so a non-STT
|
|
1442
|
+
// response is not mislabelled as an STT.
|
|
1443
|
+
issuedTokenType: response.issuedTokenType ?? "",
|
|
1444
|
+
// `expiresAt` is NaN when the server omitted `expires_in`; fall back to 0 rather than
|
|
1445
|
+
// surfacing NaN to callers.
|
|
1446
|
+
expiresIn: Number.isFinite(response.expiresAt) ? Math.max(0, Math.floor(response.expiresAt - Date.now() / 1e3)) : 0,
|
|
1447
|
+
tokenType: response.tokenType,
|
|
1448
|
+
scope: response.scope
|
|
1449
|
+
};
|
|
1450
|
+
}
|
|
1451
|
+
/**
|
|
1452
|
+
* Builds the redirect URL that hands a Session Transfer Token (STT) to the target app's login URL.
|
|
1453
|
+
*
|
|
1454
|
+
* Returns `targetLoginUrl` with `session_transfer_token` (and `organization`, when provided)
|
|
1455
|
+
* appended as query parameters, URL-encoded. This performs no network call and writes nothing
|
|
1456
|
+
* to the session — it only builds a string. The developer hands the returned URL to their
|
|
1457
|
+
* framework's redirect.
|
|
1458
|
+
*
|
|
1459
|
+
* `targetLoginUrl` attaches a single-use credential, so it must be a trusted, app-controlled
|
|
1460
|
+
* value — never derived from untrusted input (e.g. a `returnTo`), or the token could leak to an
|
|
1461
|
+
* attacker host. To harden against that, the URL must be absolute and use `https:` (an `http:`
|
|
1462
|
+
* URL is accepted only for `localhost` / loopback, to support local development).
|
|
1463
|
+
*
|
|
1464
|
+
* @param targetLoginUrl The target app's login URL (absolute, https).
|
|
1465
|
+
* @param result The {@link SessionTransferTokenResult} from {@link ServerClient.requestSessionTransferToken}.
|
|
1466
|
+
* @param options Optional options, e.g. the `organization` to forward when the STT is org-scoped.
|
|
1467
|
+
*
|
|
1468
|
+
* @throws {MissingRequiredArgumentError} When `targetLoginUrl` is missing or blank.
|
|
1469
|
+
* @throws {InvalidConfigurationError} When `targetLoginUrl` is not an absolute URL, or does not use `https:` (except for loopback hosts).
|
|
1470
|
+
*
|
|
1471
|
+
* @returns A {@link URL} with the STT (and optional organization) as query parameters.
|
|
1472
|
+
*/
|
|
1473
|
+
buildSessionTransferRedirect(targetLoginUrl, result, options) {
|
|
1474
|
+
if (!targetLoginUrl || !targetLoginUrl.trim()) {
|
|
1475
|
+
throw new MissingRequiredArgumentError("targetLoginUrl");
|
|
1476
|
+
}
|
|
1477
|
+
let url;
|
|
1478
|
+
try {
|
|
1479
|
+
url = new URL(targetLoginUrl);
|
|
1480
|
+
} catch {
|
|
1481
|
+
throw new InvalidConfigurationError(
|
|
1482
|
+
"targetLoginUrl must be an absolute URL (e.g. https://app.example.com/auth/login)."
|
|
1483
|
+
);
|
|
1484
|
+
}
|
|
1485
|
+
const isLoopback = url.hostname === "localhost" || url.hostname === "127.0.0.1" || url.hostname === "[::1]";
|
|
1486
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && isLoopback)) {
|
|
1487
|
+
throw new InvalidConfigurationError(
|
|
1488
|
+
"targetLoginUrl must use https (http is allowed only for localhost/loopback). The session transfer token is a single-use credential and must not be sent over an insecure or untrusted URL."
|
|
1489
|
+
);
|
|
1490
|
+
}
|
|
1491
|
+
url.searchParams.set("session_transfer_token", result.sessionTransferToken);
|
|
1492
|
+
if (options?.organization !== void 0) {
|
|
1493
|
+
if (!options.organization.trim()) {
|
|
1494
|
+
throw new OrganizationValidationError("organization must not be blank");
|
|
1495
|
+
}
|
|
1496
|
+
url.searchParams.set("organization", options.organization);
|
|
1497
|
+
}
|
|
1498
|
+
return url;
|
|
1499
|
+
}
|
|
1500
|
+
/**
|
|
1501
|
+
* Resolves the actor token for a Session Transfer Token request.
|
|
1502
|
+
*
|
|
1503
|
+
* An explicit actor wins. Otherwise the agent session's ID token is used, refreshed when it has
|
|
1504
|
+
* expired (and the refreshed session is persisted so the agent session stays coherent). If no
|
|
1505
|
+
* usable ID token can be obtained, throws a `TokenExchangeError` with code `actor_unavailable`
|
|
1506
|
+
* before any exchange is attempted.
|
|
1507
|
+
*/
|
|
1508
|
+
async #resolveSessionTransferActor(actor, domain, storeOptions) {
|
|
1509
|
+
if (actor !== void 0) {
|
|
1510
|
+
if (!actor.token || !actor.token.trim()) {
|
|
1511
|
+
throw actorUnavailableError(
|
|
1512
|
+
"Unable to resolve an actor for the session transfer token: an explicit actor was provided but its token is blank. Pass a non-blank actor token, or omit `actor` to source it from the agent session."
|
|
1513
|
+
);
|
|
1514
|
+
}
|
|
1515
|
+
return { token: actor.token, type: actor.type ?? ID_TOKEN_TYPE };
|
|
1516
|
+
}
|
|
1517
|
+
const stateData = await this.#stateStore.get(this.#stateStoreIdentifier, storeOptions);
|
|
1518
|
+
if (!stateData || !stateData.idToken) {
|
|
1519
|
+
throw actorUnavailableError(
|
|
1520
|
+
"Unable to resolve an actor for the session transfer token: no actor was provided and there is no logged-in agent session. Pass an explicit actor or ensure the agent is logged in."
|
|
1521
|
+
);
|
|
1522
|
+
}
|
|
1523
|
+
if (this.#isResolverMode() && !await this.#isSessionForCurrentDomain(stateData, storeOptions)) {
|
|
1524
|
+
throw actorUnavailableError(
|
|
1525
|
+
"Unable to resolve an actor for the session transfer token: the agent session belongs to a different domain than the one resolved for this request. Pass an explicit actor or ensure the agent is logged in on this domain."
|
|
1526
|
+
);
|
|
1527
|
+
}
|
|
1528
|
+
if (!isTokenExpired(stateData.idToken)) {
|
|
1529
|
+
return { token: stateData.idToken, type: ID_TOKEN_TYPE };
|
|
1530
|
+
}
|
|
1531
|
+
if (!stateData.refreshToken) {
|
|
1532
|
+
throw actorUnavailableError(
|
|
1533
|
+
"Unable to resolve an actor for the session transfer token: the agent session ID token has expired and no refresh token is available to refresh it. Pass an explicit actor or re-authenticate the agent."
|
|
1534
|
+
);
|
|
1535
|
+
}
|
|
1536
|
+
const sessionDomain = this.#getSessionDomain(stateData) ?? domain;
|
|
1537
|
+
let tokenEndpointResponse;
|
|
1538
|
+
try {
|
|
1539
|
+
tokenEndpointResponse = await this.#getAuthClient(sessionDomain).getTokenByRefreshToken({
|
|
1540
|
+
refreshToken: stateData.refreshToken
|
|
1541
|
+
});
|
|
1542
|
+
} catch {
|
|
1543
|
+
throw actorUnavailableError(
|
|
1544
|
+
"Unable to resolve an actor for the session transfer token: refreshing the agent session ID token failed. Pass an explicit actor or re-authenticate the agent."
|
|
1545
|
+
);
|
|
1546
|
+
}
|
|
1547
|
+
if (!tokenEndpointResponse.idToken) {
|
|
1548
|
+
throw actorUnavailableError(
|
|
1549
|
+
"Unable to resolve an actor for the session transfer token: refreshing the agent session did not return an ID token. Pass an explicit actor or re-authenticate the agent."
|
|
1550
|
+
);
|
|
1551
|
+
}
|
|
1552
|
+
const audience = this.#options.authorizationParams?.audience ?? "default";
|
|
1553
|
+
const existingStateData = await this.#stateStore.get(this.#stateStoreIdentifier, storeOptions);
|
|
1554
|
+
const updatedStateData = updateStateData(audience, existingStateData, tokenEndpointResponse, {
|
|
1555
|
+
domain: sessionDomain
|
|
1556
|
+
});
|
|
1557
|
+
await this.#stateStore.set(this.#stateStoreIdentifier, updatedStateData, false, storeOptions);
|
|
1558
|
+
return { token: tokenEndpointResponse.idToken, type: ID_TOKEN_TYPE };
|
|
1559
|
+
}
|
|
1372
1560
|
/**
|
|
1373
1561
|
* Handles the backchannel logout process by verifying the logout token and deleting the session from the store if the logout token was considered valid.
|
|
1374
1562
|
* @param logoutToken The logout token to verify and use to delete the session from the store.
|
|
@@ -1523,7 +1711,15 @@ var AbstractTransactionStore = class extends AbstractStore {
|
|
|
1523
1711
|
};
|
|
1524
1712
|
|
|
1525
1713
|
// src/index.ts
|
|
1526
|
-
import {
|
|
1714
|
+
import {
|
|
1715
|
+
TokenExchangeError as TokenExchangeError2,
|
|
1716
|
+
TokenRevocationError,
|
|
1717
|
+
MissingClientAuthError,
|
|
1718
|
+
OrganizationValidationError as OrganizationValidationError3,
|
|
1719
|
+
PasswordlessStartError as PasswordlessStartError2,
|
|
1720
|
+
PasswordlessVerifyError as PasswordlessVerifyError2,
|
|
1721
|
+
isMfaRequiredError as isMfaRequiredError2
|
|
1722
|
+
} from "@auth0/auth0-auth-js";
|
|
1527
1723
|
|
|
1528
1724
|
// src/store/cookie-transaction-store.ts
|
|
1529
1725
|
var CookieTransactionStore = class extends AbstractTransactionStore {
|
|
@@ -1758,6 +1954,8 @@ export {
|
|
|
1758
1954
|
PasskeyChallengeError,
|
|
1759
1955
|
PasskeyGetTokenError,
|
|
1760
1956
|
PasskeyRegisterError,
|
|
1957
|
+
PasswordlessStartError2 as PasswordlessStartError,
|
|
1958
|
+
PasswordlessVerifyError2 as PasswordlessVerifyError,
|
|
1761
1959
|
ServerClient,
|
|
1762
1960
|
ServerDatabaseClient,
|
|
1763
1961
|
ServerMfaClient,
|
|
@@ -1767,8 +1965,9 @@ export {
|
|
|
1767
1965
|
StartLinkUserError,
|
|
1768
1966
|
StatefulStateStore,
|
|
1769
1967
|
StatelessStateStore,
|
|
1770
|
-
TokenExchangeError,
|
|
1968
|
+
TokenExchangeError2 as TokenExchangeError,
|
|
1969
|
+
TokenExchangeErrorCode,
|
|
1771
1970
|
TokenRevocationError,
|
|
1772
|
-
isMfaRequiredError
|
|
1971
|
+
isMfaRequiredError2 as isMfaRequiredError
|
|
1773
1972
|
};
|
|
1774
1973
|
//# sourceMappingURL=index.js.map
|