@auth0/auth0-express 1.0.0-beta.1 → 1.0.0-beta.2
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 +25 -2
- package/dist/app-base-url.d.ts +31 -0
- package/dist/app-base-url.d.ts.map +1 -0
- package/dist/app-base-url.js +102 -0
- package/dist/app-base-url.js.map +1 -0
- package/dist/config.d.ts +15 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +102 -0
- package/dist/config.js.map +1 -0
- package/dist/errors/index.d.ts +6 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +9 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/handlers/backchannel-logout-handler.d.ts +3 -0
- package/dist/handlers/backchannel-logout-handler.d.ts.map +1 -0
- package/dist/handlers/backchannel-logout-handler.js +15 -0
- package/dist/handlers/backchannel-logout-handler.js.map +1 -0
- package/dist/handlers/callback-handler.d.ts +4 -0
- package/dist/handlers/callback-handler.d.ts.map +1 -0
- package/dist/handlers/callback-handler.js +18 -0
- package/dist/handlers/callback-handler.js.map +1 -0
- package/dist/handlers/login-handler.d.ts +4 -0
- package/dist/handlers/login-handler.d.ts.map +1 -0
- package/dist/handlers/login-handler.js +64 -0
- package/dist/handlers/login-handler.js.map +1 -0
- package/dist/handlers/logout-handler.d.ts +4 -0
- package/dist/handlers/logout-handler.d.ts.map +1 -0
- package/dist/handlers/logout-handler.js +15 -0
- package/dist/handlers/logout-handler.js.map +1 -0
- package/dist/index.d.ts +11 -343
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -521
- package/dist/index.js.map +1 -1
- package/dist/middleware/claim-auth.d.ts +28 -0
- package/dist/middleware/claim-auth.d.ts.map +1 -0
- package/dist/middleware/claim-auth.js +2 -0
- package/dist/middleware/claim-auth.js.map +1 -0
- package/dist/middleware/claim-check.d.ts +41 -0
- package/dist/middleware/claim-check.d.ts.map +1 -0
- package/dist/middleware/claim-check.js +62 -0
- package/dist/middleware/claim-check.js.map +1 -0
- package/dist/middleware/claim-equals.d.ts +27 -0
- package/dist/middleware/claim-equals.d.ts.map +1 -0
- package/dist/middleware/claim-equals.js +48 -0
- package/dist/middleware/claim-equals.js.map +1 -0
- package/dist/middleware/claim-includes.d.ts +37 -0
- package/dist/middleware/claim-includes.d.ts.map +1 -0
- package/dist/middleware/claim-includes.js +68 -0
- package/dist/middleware/claim-includes.js.map +1 -0
- package/dist/middleware/require-auth.d.ts +44 -0
- package/dist/middleware/require-auth.d.ts.map +1 -0
- package/dist/middleware/require-auth.js +57 -0
- package/dist/middleware/require-auth.js.map +1 -0
- package/dist/router.d.ts +34 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +76 -0
- package/dist/router.js.map +1 -0
- package/dist/store/express-cookie-handler.d.ts +17 -0
- package/dist/store/express-cookie-handler.d.ts.map +1 -0
- package/dist/store/express-cookie-handler.js +30 -0
- package/dist/store/express-cookie-handler.js.map +1 -0
- package/dist/store/request-context.d.ts +36 -0
- package/dist/store/request-context.d.ts.map +1 -0
- package/dist/store/request-context.js +49 -0
- package/dist/store/request-context.js.map +1 -0
- package/dist/types/express.d.ts +13 -0
- package/dist/types/express.d.ts.map +1 -0
- package/dist/types/express.js +2 -0
- package/dist/types/express.js.map +1 -0
- package/dist/types.d.ts +125 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +35 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +146 -0
- package/dist/utils.js.map +1 -0
- package/package.json +9 -8
- package/dist/index.cjs +0 -566
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -343
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
/**
|
|
3
|
+
* AsyncLocalStorage instance for storing request context.
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
const asyncLocalStorage = new AsyncLocalStorage();
|
|
7
|
+
/**
|
|
8
|
+
* Runs a callback within an AsyncLocalStorage context containing the request and response.
|
|
9
|
+
* This establishes the context for the entire request lifecycle.
|
|
10
|
+
*
|
|
11
|
+
* @param request - Express request object
|
|
12
|
+
* @param response - Express response object
|
|
13
|
+
* @param callback - Function to execute within the context
|
|
14
|
+
* @returns The result of the callback
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* app.use((req, res, next) => {
|
|
19
|
+
* runWithContext(req, res, () => next());
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function runWithContext(context, callback) {
|
|
24
|
+
return asyncLocalStorage.run(context, callback);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Retrieves the current request context from AsyncLocalStorage.
|
|
28
|
+
*
|
|
29
|
+
* @returns The current RequestContext
|
|
30
|
+
* @throws {Error} If called outside of a request context
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const { request, response } = getRequestContext();
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function getRequestContext() {
|
|
38
|
+
const context = asyncLocalStorage.getStore();
|
|
39
|
+
if (!context) {
|
|
40
|
+
throw new Error('Request context not available. This error typically occurs when:\n' +
|
|
41
|
+
'1. Client methods are called outside of a request handler\n' +
|
|
42
|
+
'2. The auth0 middleware has not been initialized\n' +
|
|
43
|
+
'3. AsyncLocalStorage context was lost in an async operation\n\n' +
|
|
44
|
+
'Ensure you are calling client methods within an Express request handler ' +
|
|
45
|
+
'and the auth0 middleware is properly configured.');
|
|
46
|
+
}
|
|
47
|
+
return context;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=request-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-context.js","sourceRoot":"","sources":["../../src/store/request-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AASrD;;;GAGG;AACH,MAAM,iBAAiB,GAAG,IAAI,iBAAiB,EAAkB,CAAC;AAElE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,cAAc,CAC5B,OAAuB,EACvB,QAAiB;IAEjB,OAAO,iBAAiB,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;AAClD,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,iBAAiB;IAC/B,MAAM,OAAO,GAAG,iBAAiB,CAAC,QAAQ,EAAE,CAAC;IAE7C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CACb,oEAAoE;YAClE,6DAA6D;YAC7D,oDAAoD;YACpD,iEAAiE;YACjE,0EAA0E;YAC1E,kDAAkD,CACrD,CAAC;IACJ,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ServerClient } from '@auth0/auth0-server-js';
|
|
2
|
+
import type { StoreOptions } from '../types.js';
|
|
3
|
+
declare global {
|
|
4
|
+
namespace Express {
|
|
5
|
+
interface Request {
|
|
6
|
+
auth0: {
|
|
7
|
+
client: ServerClient<StoreOptions>;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=express.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"express.d.ts","sourceRoot":"","sources":["../../src/types/express.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,OAAO,CAAC;QAChB,UAAU,OAAO;YACf,KAAK,EAAE;gBACL,MAAM,EAAE,YAAY,CAAC,YAAY,CAAC,CAAC;aACpC,CAAC;SACH;KACF;CACF;AAED,OAAO,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"express.js","sourceRoot":"","sources":["../../src/types/express.ts"],"names":[],"mappings":""}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { DiscoveryCacheOptions, DomainResolver, SessionConfiguration, SessionStore } from '@auth0/auth0-server-js';
|
|
2
|
+
import type { Request, Response } from 'express';
|
|
3
|
+
export type { DomainResolver, DiscoveryCacheOptions } from '@auth0/auth0-server-js';
|
|
4
|
+
/**
|
|
5
|
+
* Options passed to custom session store implementations.
|
|
6
|
+
*
|
|
7
|
+
* These options provide access to the Express request and response objects,
|
|
8
|
+
* which are needed for cookie and session management operations.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* class CustomSessionStore implements SessionStore<StoreOptions> {
|
|
13
|
+
* async get(id: string, options: StoreOptions) {
|
|
14
|
+
* const { request, response } = options;
|
|
15
|
+
* // Access cookies, headers, etc.
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export interface StoreOptions {
|
|
21
|
+
/** Express request object */
|
|
22
|
+
request: Request;
|
|
23
|
+
/** Express response object */
|
|
24
|
+
response: Response;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Configuration options for Auth0 authentication in Express applications.
|
|
28
|
+
*
|
|
29
|
+
* Environment variable support:
|
|
30
|
+
* - AUTH0_DOMAIN or ISSUER_BASE_URL: Auth0 domain
|
|
31
|
+
* - AUTH0_CLIENT_ID or CLIENT_ID: Client ID
|
|
32
|
+
* - AUTH0_CLIENT_SECRET or CLIENT_SECRET: Client secret
|
|
33
|
+
* - APP_BASE_URL or BASE_URL: Application base URL
|
|
34
|
+
* - AUTH0_SESSION_SECRET or SECRET: Session encryption secret
|
|
35
|
+
* - AUTH0_AUDIENCE: Optional API audience
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const options: Auth0Options = {
|
|
40
|
+
* domain: 'tenant.auth0.com',
|
|
41
|
+
* clientId: 'your_client_id',
|
|
42
|
+
* clientSecret: 'your_client_secret',
|
|
43
|
+
* appBaseUrl: 'http://localhost:3000',
|
|
44
|
+
* sessionSecret: process.env.SESSION_SECRET
|
|
45
|
+
* };
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export interface Auth0Options {
|
|
49
|
+
/**
|
|
50
|
+
* Auth0 domain (e.g., 'tenant.auth0.com') without protocol.
|
|
51
|
+
*
|
|
52
|
+
* Provide a `DomainResolver` function to resolve the Auth0 custom domain
|
|
53
|
+
* per request (Multiple Custom Domains). The resolver receives the Express
|
|
54
|
+
* request context (`{ request, response }`) and returns the domain string.
|
|
55
|
+
*/
|
|
56
|
+
domain: string | DomainResolver<StoreOptions>;
|
|
57
|
+
/** Auth0 application client ID */
|
|
58
|
+
clientId: string;
|
|
59
|
+
/** Auth0 application client secret (required for token exchange) */
|
|
60
|
+
clientSecret?: string;
|
|
61
|
+
/** Private key for client assertion authentication (alternative to clientSecret) */
|
|
62
|
+
clientAssertionSigningKey?: string | CryptoKey;
|
|
63
|
+
/** Algorithm for client assertion signing (e.g., 'RS256') */
|
|
64
|
+
clientAssertionSigningAlg?: string;
|
|
65
|
+
/** API audience for requesting access tokens */
|
|
66
|
+
audience?: string;
|
|
67
|
+
/**
|
|
68
|
+
* Base URL of your application (e.g., 'http://localhost:3000').
|
|
69
|
+
*
|
|
70
|
+
* - Provide a single URL string for a static base URL (default behavior).
|
|
71
|
+
* - Provide an array of allowed URLs to validate the incoming request origin
|
|
72
|
+
* against an allow-list (recommended for dynamic/preview deployments).
|
|
73
|
+
* - Omit it (or set `APP_BASE_URL`/`BASE_URL` empty) to infer the base URL
|
|
74
|
+
* from the incoming request host at runtime.
|
|
75
|
+
*/
|
|
76
|
+
appBaseUrl?: string | string[];
|
|
77
|
+
/** Enable Pushed Authorization Requests (PAR) for enhanced security */
|
|
78
|
+
pushedAuthorizationRequests?: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Secret for encrypting session cookies (minimum 32 characters recommended).
|
|
81
|
+
*
|
|
82
|
+
* Provide an array to support secret rotation: the first secret is used to encrypt new
|
|
83
|
+
* cookies, while all secrets are tried, in order, when decrypting. This lets you roll the
|
|
84
|
+
* secret without logging existing users out — deploy `[newSecret, oldSecret]`, then drop
|
|
85
|
+
* the old secret once every session has been re-encrypted under the new one.
|
|
86
|
+
*
|
|
87
|
+
* Via environment variables, a comma-separated `AUTH0_SESSION_SECRET` (or `SECRET`) is
|
|
88
|
+
* parsed into an array in the same order.
|
|
89
|
+
*/
|
|
90
|
+
sessionSecret: string | string[];
|
|
91
|
+
/** Custom session store implementation (defaults to cookie-based sessions) */
|
|
92
|
+
sessionStore?: SessionStore<StoreOptions>;
|
|
93
|
+
/** Advanced session configuration (cookie settings, timeouts, etc.) */
|
|
94
|
+
sessionConfiguration?: SessionConfiguration;
|
|
95
|
+
/**
|
|
96
|
+
* Whether to mount the default routes for login, logout, callback and backchannel logout.
|
|
97
|
+
* Routes: /auth/login, /auth/callback, /auth/logout, /auth/backchannel-logout
|
|
98
|
+
* @default true
|
|
99
|
+
*/
|
|
100
|
+
mountRoutes?: boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Custom fetch implementation for HTTP requests.
|
|
103
|
+
* Useful for proxies, custom headers, or testing.
|
|
104
|
+
*/
|
|
105
|
+
customFetch?: typeof fetch;
|
|
106
|
+
/**
|
|
107
|
+
* Optional discovery cache configuration for OIDC metadata and JWKS.
|
|
108
|
+
* `ttl` is in seconds. Cache entries are scoped per resolved domain, which
|
|
109
|
+
* matters for MCD deployments serving many domains. Defaults to the
|
|
110
|
+
* `ServerClient` defaults (TTL 600s, max 100 entries) when omitted.
|
|
111
|
+
*/
|
|
112
|
+
discoveryCache?: DiscoveryCacheOptions;
|
|
113
|
+
/** Custom paths for authentication routes (only used if mountRoutes is true) */
|
|
114
|
+
routes?: {
|
|
115
|
+
/** Login route path @default '/auth/login' */
|
|
116
|
+
login?: string;
|
|
117
|
+
/** OAuth callback route path @default '/auth/callback' */
|
|
118
|
+
callback?: string;
|
|
119
|
+
/** Logout route path @default '/auth/logout' */
|
|
120
|
+
logout?: string;
|
|
121
|
+
/** Backchannel logout route path @default '/auth/backchannel-logout' */
|
|
122
|
+
backchannelLogout?: string;
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,qBAAqB,EACrB,cAAc,EACd,oBAAoB,EACpB,YAAY,EACb,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEjD,YAAY,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAEpF;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,YAAY;IAC3B,6BAA6B;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,8BAA8B;IAC9B,QAAQ,EAAE,QAAQ,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,WAAW,YAAY;IAC3B;;;;;;OAMG;IACH,MAAM,EAAE,MAAM,GAAG,cAAc,CAAC,YAAY,CAAC,CAAC;IAC9C,kCAAkC;IAClC,QAAQ,EAAE,MAAM,CAAC;IACjB,oEAAoE;IACpE,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,oFAAoF;IACpF,yBAAyB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC/C,6DAA6D;IAC7D,yBAAyB,CAAC,EAAE,MAAM,CAAC;IACnC,gDAAgD;IAChD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;;;;;OAQG;IACH,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAE/B,uEAAuE;IACvE,2BAA2B,CAAC,EAAE,OAAO,CAAC;IAEtC;;;;;;;;;;OAUG;IACH,aAAa,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACjC,8EAA8E;IAC9E,YAAY,CAAC,EAAE,YAAY,CAAC,YAAY,CAAC,CAAC;IAC1C,uEAAuE;IACvE,oBAAoB,CAAC,EAAE,oBAAoB,CAAC;IAC5C;;;;OAIG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,KAAK,CAAC;IAE3B;;;;;OAKG;IACH,cAAc,CAAC,EAAE,qBAAqB,CAAC;IAEvC,gFAAgF;IAChF,MAAM,CAAC,EAAE;QACP,8CAA8C;QAC9C,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,0DAA0D;QAC1D,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,gDAAgD;QAChD,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,wEAAwE;QACxE,iBAAiB,CAAC,EAAE,MAAM,CAAC;KAC5B,CAAC;CACH"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ServerClient } from '@auth0/auth0-server-js';
|
|
2
|
+
import type { DomainResolver } from '@auth0/auth0-server-js';
|
|
3
|
+
import { Auth0Options, StoreOptions } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Wraps a user-provided domain resolver so it always receives the Express
|
|
6
|
+
* request context. `@auth0/auth0-server-js` invokes the resolver with the
|
|
7
|
+
* `storeOptions` passed to client methods; this SDK relies on
|
|
8
|
+
* `AsyncLocalStorage` instead of passing them explicitly, so when
|
|
9
|
+
* `storeOptions` is absent we recover it via `getRequestContext()` — the same
|
|
10
|
+
* fallback used by `ExpressCookieHandler`.
|
|
11
|
+
*
|
|
12
|
+
* A static string domain is returned unchanged.
|
|
13
|
+
*/
|
|
14
|
+
export declare function wrapDomainResolver(domain: Auth0Options['domain']): string | DomainResolver<StoreOptions>;
|
|
15
|
+
/**
|
|
16
|
+
* Utility function to ensure Route URLs are created correctly when using both the root and subpath as base URL.
|
|
17
|
+
* Accepts a relative path or an absolute same-origin URL, such as one forwarded by a reverse proxy.
|
|
18
|
+
* Validates that the constructed URL has the same origin as the base URL to prevent host override attacks.
|
|
19
|
+
* @param url Relative path (e.g. `/auth/callback`) or absolute same-origin URL.
|
|
20
|
+
* @param base The base URL to use, including any subpath.
|
|
21
|
+
* @returns A URL object, combining the base and url.
|
|
22
|
+
* @throws {Error} If the input contains multiple leading slashes/backslashes or the origin does not match the base URL.
|
|
23
|
+
*/
|
|
24
|
+
export declare function createRouteUrl(url: string, base: string): URL;
|
|
25
|
+
/**
|
|
26
|
+
* Validates a user-supplied redirect URL (e.g. a `returnTo` query parameter) against the application base URL.
|
|
27
|
+
* Accepts relative paths and absolute same-origin URLs. Rejects different-origin URLs and injection attempts.
|
|
28
|
+
* Never throws — returns undefined for any invalid or unsafe input.
|
|
29
|
+
* @param dangerousRedirect Untrusted redirect URL from user input.
|
|
30
|
+
* @param safeBaseUrl The base URL to check against.
|
|
31
|
+
* @returns A safe redirect URL, or undefined if the input is not safe.
|
|
32
|
+
*/
|
|
33
|
+
export declare function toSafeRedirect(dangerousRedirect: string, safeBaseUrl: string): string | undefined;
|
|
34
|
+
export declare function createServerClientInstance(options: Auth0Options): ServerClient<StoreOptions>;
|
|
35
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,YAAY,EAA2C,MAAM,wBAAwB,CAAC;AACvH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAsCxD;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,YAAY,CAAC,QAAQ,CAAC,GAC7B,MAAM,GAAG,cAAc,CAAC,YAAY,CAAC,CAMvC;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,OAsCvD;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,iBAAiB,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CASjG;AAED,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,YAAY,8BAyC/D"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { CookieTransactionStore, ServerClient, StatefulStateStore, StatelessStateStore } from '@auth0/auth0-server-js';
|
|
2
|
+
import { ExpressCookieHandler } from './store/express-cookie-handler.js';
|
|
3
|
+
import { getRequestContext } from './store/request-context.js';
|
|
4
|
+
/**
|
|
5
|
+
* Ensures the value has a trailing slash.
|
|
6
|
+
* If it does not, it will append one.
|
|
7
|
+
* @param value The value to ensure has a trailing slash.
|
|
8
|
+
* @returns The value with a trailing slash.
|
|
9
|
+
*/
|
|
10
|
+
function ensureTrailingSlash(value) {
|
|
11
|
+
return value && !value.endsWith('/') ? `${value}/` : value;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Ensures the value does not have any leading slashes or backslashes.
|
|
15
|
+
* If it does, it will trim all of them.
|
|
16
|
+
* @param value The value to ensure has no leading slashes or backslashes.
|
|
17
|
+
* @returns The value without leading slashes or backslashes.
|
|
18
|
+
*/
|
|
19
|
+
function ensureNoLeadingSlash(value) {
|
|
20
|
+
return value.replace(/^[/\\]+/, '');
|
|
21
|
+
}
|
|
22
|
+
// Matches only URLs with an authority component (e.g. https://, http://, file://).
|
|
23
|
+
// Bare-scheme inputs like `javascript:alert(1)` or `report:summary` do NOT match
|
|
24
|
+
// and are treated as relative paths — resolved safely against the base URL.
|
|
25
|
+
const AUTHORITY_URL_REGEX = /^[a-zA-Z][a-zA-Z0-9.+-]*:\/\//;
|
|
26
|
+
// Matches any scheme-like prefix (e.g. `javascript:`, `data:`, `report:`).
|
|
27
|
+
// Used to detect first-segment colons that would confuse the WHATWG URL parser
|
|
28
|
+
// when resolving relative paths, so we can prefix them with `./`.
|
|
29
|
+
const SCHEME_PREFIX_REGEX = /^[a-zA-Z][a-zA-Z0-9.+-]*:/;
|
|
30
|
+
// Schemes that must never be used as a redirect or route destination.
|
|
31
|
+
// These can execute code or render arbitrary content in a browser.
|
|
32
|
+
const UNSAFE_SCHEME_REGEX = /^(javascript|data|vbscript):/i;
|
|
33
|
+
/**
|
|
34
|
+
* Wraps a user-provided domain resolver so it always receives the Express
|
|
35
|
+
* request context. `@auth0/auth0-server-js` invokes the resolver with the
|
|
36
|
+
* `storeOptions` passed to client methods; this SDK relies on
|
|
37
|
+
* `AsyncLocalStorage` instead of passing them explicitly, so when
|
|
38
|
+
* `storeOptions` is absent we recover it via `getRequestContext()` — the same
|
|
39
|
+
* fallback used by `ExpressCookieHandler`.
|
|
40
|
+
*
|
|
41
|
+
* A static string domain is returned unchanged.
|
|
42
|
+
*/
|
|
43
|
+
export function wrapDomainResolver(domain) {
|
|
44
|
+
if (typeof domain === 'string') {
|
|
45
|
+
return domain;
|
|
46
|
+
}
|
|
47
|
+
return (storeOptions) => domain(storeOptions ?? getRequestContext());
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Utility function to ensure Route URLs are created correctly when using both the root and subpath as base URL.
|
|
51
|
+
* Accepts a relative path or an absolute same-origin URL, such as one forwarded by a reverse proxy.
|
|
52
|
+
* Validates that the constructed URL has the same origin as the base URL to prevent host override attacks.
|
|
53
|
+
* @param url Relative path (e.g. `/auth/callback`) or absolute same-origin URL.
|
|
54
|
+
* @param base The base URL to use, including any subpath.
|
|
55
|
+
* @returns A URL object, combining the base and url.
|
|
56
|
+
* @throws {Error} If the input contains multiple leading slashes/backslashes or the origin does not match the base URL.
|
|
57
|
+
*/
|
|
58
|
+
export function createRouteUrl(url, base) {
|
|
59
|
+
const baseUrl = new URL(ensureTrailingSlash(base));
|
|
60
|
+
if (AUTHORITY_URL_REGEX.test(url)) {
|
|
61
|
+
// Absolute URL with authority (https://, http://, file://, etc.).
|
|
62
|
+
// Resolve against the base so the subpath is validated, then check origin.
|
|
63
|
+
let absolute;
|
|
64
|
+
try {
|
|
65
|
+
absolute = new URL(url);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
throw new Error(`URL is not allowed: '${url}' is not a valid URL`);
|
|
69
|
+
}
|
|
70
|
+
if (absolute.origin !== baseUrl.origin) {
|
|
71
|
+
throw new Error('URL is not allowed: origin does not match base URL');
|
|
72
|
+
}
|
|
73
|
+
return absolute;
|
|
74
|
+
}
|
|
75
|
+
const normalized = ensureNoLeadingSlash(url);
|
|
76
|
+
if (normalized !== url.replace(/^[/\\]/, '')) {
|
|
77
|
+
throw new Error(`Invalid route configuration: '${url}' contains multiple leading slashes or backslashes`);
|
|
78
|
+
}
|
|
79
|
+
if (UNSAFE_SCHEME_REGEX.test(normalized)) {
|
|
80
|
+
throw new Error(`URL is not allowed: '${url}' uses an unsafe scheme`);
|
|
81
|
+
}
|
|
82
|
+
// Prepend `./` when the normalized path starts with a scheme-like prefix
|
|
83
|
+
// (e.g. `report:summary`) so the WHATWG URL parser treats it as a relative
|
|
84
|
+
// path rather than a scheme.
|
|
85
|
+
const resolvable = SCHEME_PREFIX_REGEX.test(normalized) ? `./${normalized}` : normalized;
|
|
86
|
+
const result = new URL(resolvable, baseUrl);
|
|
87
|
+
if (result.origin !== baseUrl.origin) {
|
|
88
|
+
throw new Error('URL is not allowed: origin does not match base URL');
|
|
89
|
+
}
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Validates a user-supplied redirect URL (e.g. a `returnTo` query parameter) against the application base URL.
|
|
94
|
+
* Accepts relative paths and absolute same-origin URLs. Rejects different-origin URLs and injection attempts.
|
|
95
|
+
* Never throws — returns undefined for any invalid or unsafe input.
|
|
96
|
+
* @param dangerousRedirect Untrusted redirect URL from user input.
|
|
97
|
+
* @param safeBaseUrl The base URL to check against.
|
|
98
|
+
* @returns A safe redirect URL, or undefined if the input is not safe.
|
|
99
|
+
*/
|
|
100
|
+
export function toSafeRedirect(dangerousRedirect, safeBaseUrl) {
|
|
101
|
+
try {
|
|
102
|
+
// createRouteUrl validates origin for both absolute and relative inputs.
|
|
103
|
+
// Using it for both branches avoids duplicating AUTHORITY_URL_REGEX logic here
|
|
104
|
+
// and ensures subpath preservation for relative paths in subpath deployments.
|
|
105
|
+
return createRouteUrl(dangerousRedirect, safeBaseUrl).toString();
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
export function createServerClientInstance(options) {
|
|
112
|
+
const callbackPath = options.routes?.callback ?? '/auth/callback';
|
|
113
|
+
// Only a static string base URL yields a startup redirect_uri. In dynamic
|
|
114
|
+
// (undefined) or allow-list (array) mode, the login handler sets redirect_uri
|
|
115
|
+
// per request from the resolved base URL.
|
|
116
|
+
const staticAppBaseUrl = typeof options.appBaseUrl === 'string' ? options.appBaseUrl : undefined;
|
|
117
|
+
const redirectUri = staticAppBaseUrl
|
|
118
|
+
? createRouteUrl(callbackPath, staticAppBaseUrl).toString()
|
|
119
|
+
: undefined;
|
|
120
|
+
return new ServerClient({
|
|
121
|
+
domain: wrapDomainResolver(options.domain),
|
|
122
|
+
clientId: options.clientId,
|
|
123
|
+
clientSecret: options.clientSecret,
|
|
124
|
+
clientAssertionSigningKey: options.clientAssertionSigningKey,
|
|
125
|
+
clientAssertionSigningAlg: options.clientAssertionSigningAlg,
|
|
126
|
+
authorizationParams: {
|
|
127
|
+
audience: options.audience,
|
|
128
|
+
redirect_uri: redirectUri,
|
|
129
|
+
},
|
|
130
|
+
transactionStore: new CookieTransactionStore({ secret: options.sessionSecret }, new ExpressCookieHandler()),
|
|
131
|
+
stateStore: options.sessionStore
|
|
132
|
+
? new StatefulStateStore({
|
|
133
|
+
...options.sessionConfiguration,
|
|
134
|
+
secret: options.sessionSecret,
|
|
135
|
+
store: options.sessionStore,
|
|
136
|
+
}, new ExpressCookieHandler())
|
|
137
|
+
: new StatelessStateStore({
|
|
138
|
+
...options.sessionConfiguration,
|
|
139
|
+
secret: options.sessionSecret,
|
|
140
|
+
}, new ExpressCookieHandler()),
|
|
141
|
+
stateIdentifier: options.sessionConfiguration?.cookie?.name,
|
|
142
|
+
customFetch: options.customFetch,
|
|
143
|
+
discoveryCache: options.discoveryCache,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,YAAY,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAGvH,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAE/D;;;;;GAKG;AACH,SAAS,mBAAmB,CAAC,KAAa;IACxC,OAAO,KAAK,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC;AAC7D,CAAC;AAED;;;;;GAKG;AACH,SAAS,oBAAoB,CAAC,KAAa;IACzC,OAAO,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE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0-express",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.2",
|
|
4
4
|
"description": "Auth0 Authentication SDK for Express Applications on JavaScript runtimes",
|
|
5
5
|
"author": "Auth0",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"type": "module",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": "^22.12.0 || >=24"
|
|
10
|
+
},
|
|
8
11
|
"scripts": {
|
|
9
|
-
"build": "
|
|
10
|
-
"build:watch": "
|
|
12
|
+
"build": "tsc --project tsconfig.build.json",
|
|
13
|
+
"build:watch": "tsc --project tsconfig.build.json --watch",
|
|
11
14
|
"clean": "rm -rf ./dist",
|
|
12
15
|
"lint": "eslint \"./**/*.ts*\"",
|
|
13
16
|
"test": "vitest run",
|
|
14
17
|
"test:ci": "vitest --watch false --coverage"
|
|
15
18
|
},
|
|
16
19
|
"types": "./dist/index.d.ts",
|
|
17
|
-
"main": "./dist/index.
|
|
18
|
-
"module": "./dist/index.js",
|
|
20
|
+
"main": "./dist/index.js",
|
|
19
21
|
"exports": {
|
|
20
22
|
".": {
|
|
21
23
|
"types": "./dist/index.d.ts",
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
+
"import": "./dist/index.js",
|
|
25
|
+
"default": "./dist/index.js"
|
|
24
26
|
}
|
|
25
27
|
},
|
|
26
28
|
"devDependencies": {
|
|
@@ -33,7 +35,6 @@
|
|
|
33
35
|
"express": "^5.2.1",
|
|
34
36
|
"msw": "^2.12.14",
|
|
35
37
|
"supertest": "^7.2.2",
|
|
36
|
-
"tsup": "^8.5.1",
|
|
37
38
|
"typescript": "~5.9.3",
|
|
38
39
|
"typescript-eslint": "^8.58.0",
|
|
39
40
|
"vitest": "^4.1.2"
|