@auth0/auth0-express 1.0.0-beta.1 → 1.0.0-beta.3
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 +26 -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 +106 -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 +106 -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 +30 -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 +83 -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 +22 -0
- package/dist/handlers/logout-handler.js.map +1 -0
- package/dist/handlers/start-enterprise-login.d.ts +21 -0
- package/dist/handlers/start-enterprise-login.d.ts.map +1 -0
- package/dist/handlers/start-enterprise-login.js +26 -0
- package/dist/handlers/start-enterprise-login.js.map +1 -0
- package/dist/index.d.ts +14 -343
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -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 +48 -0
- package/dist/middleware/require-auth.d.ts.map +1 -0
- package/dist/middleware/require-auth.js +61 -0
- package/dist/middleware/require-auth.js.map +1 -0
- package/dist/migration/index.d.ts +5 -0
- package/dist/migration/index.d.ts.map +1 -0
- package/dist/migration/index.js +3 -0
- package/dist/migration/index.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/express-oidc-hkdf.d.ts +2 -0
- package/dist/store/express-oidc-hkdf.d.ts.map +1 -0
- package/dist/store/express-oidc-hkdf.js +11 -0
- package/dist/store/express-oidc-hkdf.js.map +1 -0
- package/dist/store/legacy-compatible-stateful-state-store.d.ts +139 -0
- package/dist/store/legacy-compatible-stateful-state-store.d.ts.map +1 -0
- package/dist/store/legacy-compatible-stateful-state-store.js +234 -0
- package/dist/store/legacy-compatible-stateful-state-store.js.map +1 -0
- package/dist/store/legacy-compatible-stateless-state-store.d.ts +82 -0
- package/dist/store/legacy-compatible-stateless-state-store.d.ts.map +1 -0
- package/dist/store/legacy-compatible-stateless-state-store.js +135 -0
- package/dist/store/legacy-compatible-stateless-state-store.js.map +1 -0
- package/dist/store/legacy-session-transformer.d.ts +67 -0
- package/dist/store/legacy-session-transformer.d.ts.map +1 -0
- package/dist/store/legacy-session-transformer.js +118 -0
- package/dist/store/legacy-session-transformer.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 +190 -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 +186 -0
- package/dist/utils.js.map +1 -0
- package/package.json +22 -14
- package/dist/index.cjs +0 -566
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -343
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// HKDF parameters matching express-openid-connect's key derivation (lib/crypto.js).
|
|
2
|
+
// Both encryption ('JWE CEK') and signing ('JWS Cookie Signing') use the same approach.
|
|
3
|
+
const BYTE_LENGTH = 32;
|
|
4
|
+
const DIGEST = 'SHA-256';
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
export async function deriveHkdfKey(secret, info) {
|
|
7
|
+
const keyMaterial = await crypto.subtle.importKey('raw', encoder.encode(secret), 'HKDF', false, ['deriveBits']);
|
|
8
|
+
const derivedBits = await crypto.subtle.deriveBits({ name: 'HKDF', hash: DIGEST, info: encoder.encode(info), salt: new Uint8Array(0) }, keyMaterial, BYTE_LENGTH * 8);
|
|
9
|
+
return new Uint8Array(derivedBits);
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=express-oidc-hkdf.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"express-oidc-hkdf.js","sourceRoot":"","sources":["../../src/store/express-oidc-hkdf.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,wFAAwF;AACxF,MAAM,WAAW,GAAG,EAAE,CAAC;AACvB,MAAM,MAAM,GAAG,SAAS,CAAC;AACzB,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAElC,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,MAAc,EAAE,IAAY;IAC9D,MAAM,WAAW,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC;IAChH,MAAM,WAAW,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,UAAU,CAChD,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,EACnF,WAAW,EACX,WAAW,GAAG,CAAC,CAChB,CAAC;IACF,OAAO,IAAI,UAAU,CAAC,WAA0B,CAAC,CAAC;AACpD,CAAC"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { StatefulStateStore, SessionConfiguration, type AbstractDataStore } from '@auth0/auth0-server-js';
|
|
2
|
+
import type { CookieHandler, StateData } from '@auth0/auth0-server-js';
|
|
3
|
+
/**
|
|
4
|
+
* Options for {@link MigrationStatefulStateStore}.
|
|
5
|
+
*/
|
|
6
|
+
export interface MigrationStatefulStateStoreOptions {
|
|
7
|
+
/**
|
|
8
|
+
* The secret used by auth0-server-js for encryption.
|
|
9
|
+
*
|
|
10
|
+
* Provide an array to support secret rotation: the first secret encrypts new cookies, while
|
|
11
|
+
* all secrets are tried, in order, when decrypting.
|
|
12
|
+
*/
|
|
13
|
+
secret: string | string[];
|
|
14
|
+
/**
|
|
15
|
+
* The secret(s) that were used by express-openid-connect for signing cookies.
|
|
16
|
+
* Supports key rotation: provide an array to try multiple secrets in order.
|
|
17
|
+
* If not provided, uses the same secret as auth0-server-js.
|
|
18
|
+
*/
|
|
19
|
+
legacySecret?: string | string[];
|
|
20
|
+
/**
|
|
21
|
+
* Mirror of express-openid-connect's `requireSignedSessionStoreCookie`.
|
|
22
|
+
*
|
|
23
|
+
* When `true`, a legacy session-store cookie is only honored if it carries a valid
|
|
24
|
+
* JWS signature (verified against one of {@link legacySecret}); an unsigned cookie
|
|
25
|
+
* or one whose signature does not verify resolves to no session instead of being
|
|
26
|
+
* used verbatim as the store key. Set this when migrating a deployment that ran
|
|
27
|
+
* express-openid-connect with `requireSignedSessionStoreCookie: true`, so the
|
|
28
|
+
* signature remains the required integrity control over the store key.
|
|
29
|
+
*
|
|
30
|
+
* Defaults to `false`, matching express-openid-connect's default.
|
|
31
|
+
*
|
|
32
|
+
* @default false
|
|
33
|
+
*/
|
|
34
|
+
requireSignedLegacyCookie?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* The audience to assign to transformed token sets from express-openid-connect sessions.
|
|
37
|
+
*
|
|
38
|
+
* Token sets are looked up by audience, so this must equal the `audience` your app requests
|
|
39
|
+
* (e.g. via `getAccessToken`) or the migrated access token will not be found. The default
|
|
40
|
+
* `'default'` only matches callers that request no audience.
|
|
41
|
+
*
|
|
42
|
+
* Default: 'default'
|
|
43
|
+
*/
|
|
44
|
+
legacyAudience?: string;
|
|
45
|
+
/**
|
|
46
|
+
* The scope to assign to transformed token sets from express-openid-connect sessions.
|
|
47
|
+
* @default 'openid profile email offline_access'
|
|
48
|
+
*/
|
|
49
|
+
legacyScope?: string;
|
|
50
|
+
/**
|
|
51
|
+
* The custom session store (Redis, MongoDB, etc.)
|
|
52
|
+
*/
|
|
53
|
+
store: AbstractDataStore<unknown>;
|
|
54
|
+
/**
|
|
55
|
+
* Session configuration options
|
|
56
|
+
*/
|
|
57
|
+
sessionConfiguration?: SessionConfiguration;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* A server-side (stateful) state store for zero-downtime migration from `express-openid-connect`
|
|
61
|
+
* to `@auth0/auth0-express`.
|
|
62
|
+
*
|
|
63
|
+
* Extends {@link StatefulStateStore} and transparently reads existing
|
|
64
|
+
* `express-openid-connect` sessions from Redis, MongoDB, or any other custom store,
|
|
65
|
+
* converting them to the `auth0-server-js` format so users do not need to re-authenticate
|
|
66
|
+
* during the migration.
|
|
67
|
+
*
|
|
68
|
+
* **How it works:** `express-openid-connect` wraps session data in a `{ header, data, cookie }`
|
|
69
|
+
* envelope and stores a plain or JWS-signed session ID in the cookie. `auth0-server-js` stores
|
|
70
|
+
* {@link StateData} directly and encrypts the session ID in the cookie. This store overrides
|
|
71
|
+
* `decrypt` so the base machinery resolves the session ID from either cookie format, then reads
|
|
72
|
+
* the store by that ID. On `get`, a legacy envelope is transformed into {@link StateData} and
|
|
73
|
+
* immediately written back to the same store key via `set`, upgrading the session in place on
|
|
74
|
+
* first read rather than waiting for the caller's next write. This is what makes backchannel
|
|
75
|
+
* logout (`deleteByLogoutToken`) work for a migrated session right away: the app's store
|
|
76
|
+
* typically needs to index sessions by `sid` to resolve a logout token, and that index is
|
|
77
|
+
* naturally populated by `set`. A login (`removeIfExists=true`) still deletes the old key and
|
|
78
|
+
* rotates to a fresh ID.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* import { MigrationStatefulStateStore } from '@auth0/auth0-express/migration';
|
|
83
|
+
*
|
|
84
|
+
* const store = new MigrationStatefulStateStore(
|
|
85
|
+
* {
|
|
86
|
+
* secret: process.env.SESSION_SECRET,
|
|
87
|
+
* store: redisStore,
|
|
88
|
+
* legacySecret: process.env.SESSION_SECRET, // the secret used by express-openid-connect
|
|
89
|
+
* legacyAudience: 'https://api.example.com',
|
|
90
|
+
* legacyScope: 'openid profile email',
|
|
91
|
+
* },
|
|
92
|
+
* new ExpressCookieHandler()
|
|
93
|
+
* );
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export declare class MigrationStatefulStateStore<TStoreOptions> extends StatefulStateStore<TStoreOptions> {
|
|
97
|
+
#private;
|
|
98
|
+
constructor(options: MigrationStatefulStateStoreOptions, cookieHandler: CookieHandler<TStoreOptions>);
|
|
99
|
+
/**
|
|
100
|
+
* Overrides decrypt() so the base StatefulStateStore machinery can resolve a session ID
|
|
101
|
+
* from BOTH modern and legacy express-openid-connect cookies.
|
|
102
|
+
*
|
|
103
|
+
* The base `getSessionId()` calls `this.decrypt(identifier, cookieValue)` and reads `.id`
|
|
104
|
+
* from the result. A modern auth0-server-js cookie is a compact JWE (exactly 5 dot-separated
|
|
105
|
+
* segments); a legacy cookie is a plain session ID (no dots) or a JWS-signed `<id>.<sig>`
|
|
106
|
+
* (one dot). We route strictly by shape and never cross-resolve: feeding a modern JWE into the
|
|
107
|
+
* legacy resolver would return the raw ciphertext as a "session ID" and orphan the real session
|
|
108
|
+
* (e.g. when the base throws JWTExpired on an expired-but-valid modern cookie). Modern cookies
|
|
109
|
+
* are delegated entirely to `super.decrypt` (preserving the base's expiry/error semantics);
|
|
110
|
+
* legacy cookies are resolved to `{ id }` so read and write operate on the same store key —
|
|
111
|
+
* upgrading the session in place on the first write.
|
|
112
|
+
*
|
|
113
|
+
* Note: routing is by dot-count. A legacy session ID that itself contains dots (only possible
|
|
114
|
+
* with a custom express-openid-connect `genid`) whose signed form reaches exactly 5 segments
|
|
115
|
+
* would be routed to the modern path and fail to resolve, forcing a one-time re-login. The
|
|
116
|
+
* default `genid` (32 hex chars, no dots) is unaffected.
|
|
117
|
+
*/
|
|
118
|
+
protected decrypt<TData>(identifier: string, encryptedStateData: string): Promise<TData | undefined>;
|
|
119
|
+
/**
|
|
120
|
+
* Overrides get() to transform legacy express-openid-connect envelopes into StateData, and
|
|
121
|
+
* eagerly upgrades the session in place by writing the transformed data back immediately.
|
|
122
|
+
*
|
|
123
|
+
* ID resolution (modern or legacy) is handled by the decrypt() override, so this only needs
|
|
124
|
+
* to detect whether the value the base read from the store is a legacy `{ header, data, cookie }`
|
|
125
|
+
* envelope and, if so, transform it. Modern StateData passes through unchanged.
|
|
126
|
+
*
|
|
127
|
+
* Without the eager write, a migrated session would only be upgraded on the caller's next
|
|
128
|
+
* `set()` (e.g. token refresh, claim update) — until then, backchannel logout could not resolve
|
|
129
|
+
* it, since the app's store commonly builds its `sid` index inside `set()`. Writing here trades
|
|
130
|
+
* one extra store write on the session's first read after migration for backchannel logout
|
|
131
|
+
* working immediately, rather than only after some unrelated future write. The write reuses the
|
|
132
|
+
* same store key (`removeIfExists: false`) via the public `set()`, so it goes through the same
|
|
133
|
+
* cookie/session-id resolution as any other write and is a no-op cost-wise for legacy sessions
|
|
134
|
+
* that were about to be written anyway. A second `get()` within the same request re-reads the
|
|
135
|
+
* now-modern payload and skips the transform/write, so this only happens once per session.
|
|
136
|
+
*/
|
|
137
|
+
get(identifier: string, options?: TStoreOptions): Promise<StateData | undefined>;
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=legacy-compatible-stateful-state-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"legacy-compatible-stateful-state-store.d.ts","sourceRoot":"","sources":["../../src/store/legacy-compatible-stateful-state-store.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,KAAK,iBAAiB,EAEvB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAKvE;;GAEG;AACH,MAAM,WAAW,kCAAkC;IACjD;;;;;OAKG;IACH,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAE1B;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAEjC;;;;;;;;;;;;;OAaG;IACH,yBAAyB,CAAC,EAAE,OAAO,CAAC;IAEpC;;;;;;;;OAQG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;OAEG;IACH,KAAK,EAAE,iBAAiB,CAAC,OAAO,CAAC,CAAC;IAElC;;OAEG;IACH,oBAAoB,CAAC,EAAE,oBAAoB,CAAC;CAC7C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,qBAAa,2BAA2B,CAAC,aAAa,CAAE,SAAQ,kBAAkB,CAAC,aAAa,CAAC;;gBAKnF,OAAO,EAAE,kCAAkC,EAAE,aAAa,EAAE,aAAa,CAAC,aAAa,CAAC;IAwBpG;;;;;;;;;;;;;;;;;;OAkBG;cACsB,OAAO,CAAC,KAAK,EACpC,UAAU,EAAE,MAAM,EAClB,kBAAkB,EAAE,MAAM,GACzB,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC;IAc7B;;;;;;;;;;;;;;;;;OAiBG;IACY,GAAG,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC;CAuIhG"}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { StatefulStateStore, } from '@auth0/auth0-server-js';
|
|
2
|
+
import { LegacySessionTransformer, warnIfAbsoluteDurationUnset } from './legacy-session-transformer.js';
|
|
3
|
+
import { deriveHkdfKey } from './express-oidc-hkdf.js';
|
|
4
|
+
/**
|
|
5
|
+
* A server-side (stateful) state store for zero-downtime migration from `express-openid-connect`
|
|
6
|
+
* to `@auth0/auth0-express`.
|
|
7
|
+
*
|
|
8
|
+
* Extends {@link StatefulStateStore} and transparently reads existing
|
|
9
|
+
* `express-openid-connect` sessions from Redis, MongoDB, or any other custom store,
|
|
10
|
+
* converting them to the `auth0-server-js` format so users do not need to re-authenticate
|
|
11
|
+
* during the migration.
|
|
12
|
+
*
|
|
13
|
+
* **How it works:** `express-openid-connect` wraps session data in a `{ header, data, cookie }`
|
|
14
|
+
* envelope and stores a plain or JWS-signed session ID in the cookie. `auth0-server-js` stores
|
|
15
|
+
* {@link StateData} directly and encrypts the session ID in the cookie. This store overrides
|
|
16
|
+
* `decrypt` so the base machinery resolves the session ID from either cookie format, then reads
|
|
17
|
+
* the store by that ID. On `get`, a legacy envelope is transformed into {@link StateData} and
|
|
18
|
+
* immediately written back to the same store key via `set`, upgrading the session in place on
|
|
19
|
+
* first read rather than waiting for the caller's next write. This is what makes backchannel
|
|
20
|
+
* logout (`deleteByLogoutToken`) work for a migrated session right away: the app's store
|
|
21
|
+
* typically needs to index sessions by `sid` to resolve a logout token, and that index is
|
|
22
|
+
* naturally populated by `set`. A login (`removeIfExists=true`) still deletes the old key and
|
|
23
|
+
* rotates to a fresh ID.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { MigrationStatefulStateStore } from '@auth0/auth0-express/migration';
|
|
28
|
+
*
|
|
29
|
+
* const store = new MigrationStatefulStateStore(
|
|
30
|
+
* {
|
|
31
|
+
* secret: process.env.SESSION_SECRET,
|
|
32
|
+
* store: redisStore,
|
|
33
|
+
* legacySecret: process.env.SESSION_SECRET, // the secret used by express-openid-connect
|
|
34
|
+
* legacyAudience: 'https://api.example.com',
|
|
35
|
+
* legacyScope: 'openid profile email',
|
|
36
|
+
* },
|
|
37
|
+
* new ExpressCookieHandler()
|
|
38
|
+
* );
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export class MigrationStatefulStateStore extends StatefulStateStore {
|
|
42
|
+
#legacySecrets;
|
|
43
|
+
#requireSignedLegacyCookie;
|
|
44
|
+
#transformer;
|
|
45
|
+
constructor(options, cookieHandler) {
|
|
46
|
+
super({
|
|
47
|
+
...options.sessionConfiguration,
|
|
48
|
+
secret: options.secret,
|
|
49
|
+
store: options.store,
|
|
50
|
+
}, cookieHandler);
|
|
51
|
+
// Fall back to the app's session secret(s) when no explicit legacy secret is given. Either
|
|
52
|
+
// may be an array (rotation), so normalize both to a flat string[] tried in order.
|
|
53
|
+
const legacySecret = options.legacySecret ?? options.secret;
|
|
54
|
+
this.#legacySecrets = Array.isArray(legacySecret) ? legacySecret : [legacySecret];
|
|
55
|
+
this.#requireSignedLegacyCookie = options.requireSignedLegacyCookie ?? false;
|
|
56
|
+
const legacyAudience = options.legacyAudience ?? 'default';
|
|
57
|
+
const legacyScope = options.legacyScope ?? 'openid profile email offline_access';
|
|
58
|
+
this.#transformer = new LegacySessionTransformer(legacyAudience, legacyScope);
|
|
59
|
+
warnIfAbsoluteDurationUnset(options.sessionConfiguration);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Overrides decrypt() so the base StatefulStateStore machinery can resolve a session ID
|
|
63
|
+
* from BOTH modern and legacy express-openid-connect cookies.
|
|
64
|
+
*
|
|
65
|
+
* The base `getSessionId()` calls `this.decrypt(identifier, cookieValue)` and reads `.id`
|
|
66
|
+
* from the result. A modern auth0-server-js cookie is a compact JWE (exactly 5 dot-separated
|
|
67
|
+
* segments); a legacy cookie is a plain session ID (no dots) or a JWS-signed `<id>.<sig>`
|
|
68
|
+
* (one dot). We route strictly by shape and never cross-resolve: feeding a modern JWE into the
|
|
69
|
+
* legacy resolver would return the raw ciphertext as a "session ID" and orphan the real session
|
|
70
|
+
* (e.g. when the base throws JWTExpired on an expired-but-valid modern cookie). Modern cookies
|
|
71
|
+
* are delegated entirely to `super.decrypt` (preserving the base's expiry/error semantics);
|
|
72
|
+
* legacy cookies are resolved to `{ id }` so read and write operate on the same store key —
|
|
73
|
+
* upgrading the session in place on the first write.
|
|
74
|
+
*
|
|
75
|
+
* Note: routing is by dot-count. A legacy session ID that itself contains dots (only possible
|
|
76
|
+
* with a custom express-openid-connect `genid`) whose signed form reaches exactly 5 segments
|
|
77
|
+
* would be routed to the modern path and fail to resolve, forcing a one-time re-login. The
|
|
78
|
+
* default `genid` (32 hex chars, no dots) is unaffected.
|
|
79
|
+
*/
|
|
80
|
+
async decrypt(identifier, encryptedStateData) {
|
|
81
|
+
// Modern cookie: a compact JWE has exactly 5 dot-separated segments. Delegate to the base,
|
|
82
|
+
// preserving its decryption and expiry semantics. Never fall back to legacy resolution here —
|
|
83
|
+
// that would use the raw ciphertext as a store key.
|
|
84
|
+
if (encryptedStateData.split('.').length === 5) {
|
|
85
|
+
return super.decrypt(identifier, encryptedStateData);
|
|
86
|
+
}
|
|
87
|
+
// Legacy cookie: a plain session ID (no dots) or JWS-signed `<id>.<sig>` (one dot).
|
|
88
|
+
const sessionId = await this.#resolveLegacySessionId(identifier, encryptedStateData);
|
|
89
|
+
if (sessionId === undefined)
|
|
90
|
+
return undefined;
|
|
91
|
+
return { id: sessionId };
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Overrides get() to transform legacy express-openid-connect envelopes into StateData, and
|
|
95
|
+
* eagerly upgrades the session in place by writing the transformed data back immediately.
|
|
96
|
+
*
|
|
97
|
+
* ID resolution (modern or legacy) is handled by the decrypt() override, so this only needs
|
|
98
|
+
* to detect whether the value the base read from the store is a legacy `{ header, data, cookie }`
|
|
99
|
+
* envelope and, if so, transform it. Modern StateData passes through unchanged.
|
|
100
|
+
*
|
|
101
|
+
* Without the eager write, a migrated session would only be upgraded on the caller's next
|
|
102
|
+
* `set()` (e.g. token refresh, claim update) — until then, backchannel logout could not resolve
|
|
103
|
+
* it, since the app's store commonly builds its `sid` index inside `set()`. Writing here trades
|
|
104
|
+
* one extra store write on the session's first read after migration for backchannel logout
|
|
105
|
+
* working immediately, rather than only after some unrelated future write. The write reuses the
|
|
106
|
+
* same store key (`removeIfExists: false`) via the public `set()`, so it goes through the same
|
|
107
|
+
* cookie/session-id resolution as any other write and is a no-op cost-wise for legacy sessions
|
|
108
|
+
* that were about to be written anyway. A second `get()` within the same request re-reads the
|
|
109
|
+
* now-modern payload and skips the transform/write, so this only happens once per session.
|
|
110
|
+
*/
|
|
111
|
+
async get(identifier, options) {
|
|
112
|
+
const data = await super.get(identifier, options);
|
|
113
|
+
if (data && this.#isLegacyStorePayload(data)) {
|
|
114
|
+
const stateData = this.#transformLegacyStorePayload(data);
|
|
115
|
+
// When there is legacy state data found, we write-on-first-read to
|
|
116
|
+
// convert the legacy session data, to the session data used by this SDK.
|
|
117
|
+
if (stateData) {
|
|
118
|
+
await this.set(identifier, stateData, false, options);
|
|
119
|
+
}
|
|
120
|
+
return stateData;
|
|
121
|
+
}
|
|
122
|
+
return data;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Resolves a legacy session ID from a raw cookie value. If the value contains a dot,
|
|
126
|
+
* it may be JWS-signed — try to verify and extract the unsigned portion.
|
|
127
|
+
*
|
|
128
|
+
* With {@link MigrationStatefulStateStoreOptions.requireSignedLegacyCookie} enabled, only a
|
|
129
|
+
* valid JWS signature is accepted: an unsigned cookie or one whose signature does not verify
|
|
130
|
+
* resolves to `undefined` (no session). Otherwise (the default, matching express-openid-connect
|
|
131
|
+
* with `requireSignedSessionStoreCookie: false`) the raw value is used as the store key when it
|
|
132
|
+
* is unsigned or its signature cannot be verified.
|
|
133
|
+
*/
|
|
134
|
+
async #resolveLegacySessionId(cookieName, rawCookieValue) {
|
|
135
|
+
if (rawCookieValue.includes('.')) {
|
|
136
|
+
const stripped = await this.#resolveSignedCookie(cookieName, rawCookieValue);
|
|
137
|
+
if (stripped !== undefined)
|
|
138
|
+
return stripped;
|
|
139
|
+
}
|
|
140
|
+
// Signature required but the cookie was unsigned or failed verification: reject it rather than
|
|
141
|
+
// trusting the raw value as the store key.
|
|
142
|
+
if (this.#requireSignedLegacyCookie)
|
|
143
|
+
return undefined;
|
|
144
|
+
return rawCookieValue;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Type guard to check if data is in express-openid-connect SessionStorePayload format
|
|
148
|
+
*/
|
|
149
|
+
#isLegacyStorePayload(data) {
|
|
150
|
+
if (!data || typeof data !== 'object') {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
const payload = data;
|
|
154
|
+
if (!('header' in payload) || !('data' in payload) || !('cookie' in payload)) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
if (typeof payload.header !== 'object' || payload.header === null) {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
// The header timestamps are trusted downstream: `exp` gates expiry and `iat` becomes
|
|
161
|
+
// `createdAt`. Require them to be numbers so a malformed envelope with a non-numeric `exp`
|
|
162
|
+
// cannot slip past the `exp <= now` comparison (which would coerce and read as "not expired").
|
|
163
|
+
const header = payload.header;
|
|
164
|
+
return typeof header.iat === 'number' && typeof header.uat === 'number' && typeof header.exp === 'number';
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Transforms express-openid-connect SessionStorePayload to auth0-server-js StateData format.
|
|
168
|
+
* Rejects expired sessions.
|
|
169
|
+
*/
|
|
170
|
+
#transformLegacyStorePayload(payload) {
|
|
171
|
+
// Reject once exp has been reached, mirroring appSession's `exp > epoch()` assertion
|
|
172
|
+
// (i.e. invalid when `exp <= now`), not one second later.
|
|
173
|
+
if (payload.header.exp <= Math.floor(Date.now() / 1000)) {
|
|
174
|
+
return undefined;
|
|
175
|
+
}
|
|
176
|
+
// Enforce this SDK's absoluteDuration on read. The legacy envelope's own exp reflects the OLD
|
|
177
|
+
// deployment's window, so a session past `createdAt + absoluteDuration` (calculateMaxAge <= 0)
|
|
178
|
+
// must be treated as expired here rather than returned for this one request before the
|
|
179
|
+
// write-back drops the cookie.
|
|
180
|
+
if (this.calculateMaxAge(payload.header.iat) <= 0) {
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
const sessionData = this.#transformer.transformLegacySession(payload.data);
|
|
184
|
+
sessionData.internal.createdAt = payload.header.iat;
|
|
185
|
+
return sessionData;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Attempts to verify a JWS-signed cookie from express-openid-connect and return the
|
|
189
|
+
* unsigned session ID. Tries each legacy secret in order.
|
|
190
|
+
*/
|
|
191
|
+
async #resolveSignedCookie(cookieName, cookieValue) {
|
|
192
|
+
for (const secret of this.#legacySecrets) {
|
|
193
|
+
const result = await this.#verifySignedCookie(cookieName, cookieValue, secret);
|
|
194
|
+
if (result !== undefined) {
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
async #deriveLegacySigningKey(secret) {
|
|
201
|
+
const keyBytes = await deriveHkdfKey(secret, 'JWS Cookie Signing');
|
|
202
|
+
return crypto.subtle.importKey('raw', keyBytes, { name: 'HMAC', hash: 'SHA-256' }, false, ['verify']);
|
|
203
|
+
}
|
|
204
|
+
async #verifySignedCookie(cookieName, cookieValue, secret) {
|
|
205
|
+
try {
|
|
206
|
+
const dotIndex = cookieValue.lastIndexOf('.');
|
|
207
|
+
if (dotIndex === -1)
|
|
208
|
+
return undefined;
|
|
209
|
+
const sessionId = cookieValue.substring(0, dotIndex);
|
|
210
|
+
const signatureB64 = cookieValue.substring(dotIndex + 1);
|
|
211
|
+
// The fixed JWS header used by express-openid-connect
|
|
212
|
+
const HEADER_B64 = 'eyJhbGciOiJIUzI1NiIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19';
|
|
213
|
+
const encoder = new TextEncoder();
|
|
214
|
+
const rawPayload = encoder.encode(`${cookieName}=${sessionId}`);
|
|
215
|
+
const headerBytes = encoder.encode(HEADER_B64 + '.');
|
|
216
|
+
const signingInput = new Uint8Array(headerBytes.length + rawPayload.length);
|
|
217
|
+
signingInput.set(headerBytes, 0);
|
|
218
|
+
signingInput.set(rawPayload, headerBytes.length);
|
|
219
|
+
const cryptoKey = await this.#deriveLegacySigningKey(secret);
|
|
220
|
+
// Decode the base64url signature
|
|
221
|
+
const sigBase64 = signatureB64.replace(/-/g, '+').replace(/_/g, '/');
|
|
222
|
+
const sigRaw = atob(sigBase64);
|
|
223
|
+
const sigBytes = new Uint8Array(sigRaw.length);
|
|
224
|
+
for (let i = 0; i < sigRaw.length; i++)
|
|
225
|
+
sigBytes[i] = sigRaw.charCodeAt(i);
|
|
226
|
+
const valid = await crypto.subtle.verify('HMAC', cryptoKey, sigBytes, signingInput);
|
|
227
|
+
return valid ? sessionId : undefined;
|
|
228
|
+
}
|
|
229
|
+
catch {
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
//# sourceMappingURL=legacy-compatible-stateful-state-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"legacy-compatible-stateful-state-store.js","sourceRoot":"","sources":["../../src/store/legacy-compatible-stateful-state-store.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,GAInB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,wBAAwB,EAAE,2BAA2B,EAAE,MAAM,iCAAiC,CAAC;AAExG,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAiEvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,MAAM,OAAO,2BAA2C,SAAQ,kBAAiC;IACtF,cAAc,CAAW;IACzB,0BAA0B,CAAU;IACpC,YAAY,CAA2B;IAEhD,YAAY,OAA2C,EAAE,aAA2C;QAClG,KAAK,CACH;YACE,GAAG,OAAO,CAAC,oBAAoB;YAC/B,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,KAAK,EAAE,OAAO,CAAC,KAA+C;SAC/D,EACD,aAAa,CACd,CAAC;QAEF,2FAA2F;QAC3F,mFAAmF;QACnF,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC;QAC5D,IAAI,CAAC,cAAc,GAAG,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC;QAElF,IAAI,CAAC,0BA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AM,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC;YAC/B,MAAM,QAAQ,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YAC/C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE;gBAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;YAE3E,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,YAAY,CAAC,CAAC;YACpF,OAAO,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC;QACvC,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,SAAS,CAAC;QACnB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { StatelessStateStore, SessionConfiguration } from '@auth0/auth0-server-js';
|
|
2
|
+
import type { CookieHandler } from '@auth0/auth0-server-js';
|
|
3
|
+
/**
|
|
4
|
+
* Options for {@link MigrationStatelessStateStore}.
|
|
5
|
+
*/
|
|
6
|
+
export interface MigrationStatelessStateStoreOptions {
|
|
7
|
+
/**
|
|
8
|
+
* The secret used by auth0-server-js for encryption.
|
|
9
|
+
*
|
|
10
|
+
* Provide an array to support secret rotation: the first secret encrypts new cookies, while
|
|
11
|
+
* all secrets are tried, in order, when decrypting.
|
|
12
|
+
*/
|
|
13
|
+
secret: string | string[];
|
|
14
|
+
/**
|
|
15
|
+
* The secret(s) that were used by express-openid-connect for encryption.
|
|
16
|
+
* Supports key rotation: provide an array to try multiple secrets in order.
|
|
17
|
+
* If not provided, falls back to using the same secret as auth0-server-js.
|
|
18
|
+
*/
|
|
19
|
+
legacySecret?: string | string[];
|
|
20
|
+
/**
|
|
21
|
+
* The audience to assign to transformed token sets from express-openid-connect sessions.
|
|
22
|
+
*
|
|
23
|
+
* Token sets are looked up by audience, so this must equal the `audience` your app requests
|
|
24
|
+
* (e.g. via `getAccessToken`) or the migrated access token will not be found. The default
|
|
25
|
+
* `'default'` only matches callers that request no audience.
|
|
26
|
+
*
|
|
27
|
+
* Default: 'default'
|
|
28
|
+
*/
|
|
29
|
+
legacyAudience?: string;
|
|
30
|
+
/**
|
|
31
|
+
* The scope to assign to transformed token sets from express-openid-connect sessions.
|
|
32
|
+
* @default 'openid profile email offline_access'
|
|
33
|
+
*/
|
|
34
|
+
legacyScope?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Session configuration options
|
|
37
|
+
*/
|
|
38
|
+
sessionConfiguration?: SessionConfiguration;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A cookie-based (stateless) state store for zero-downtime migration from `express-openid-connect`
|
|
42
|
+
* to `@auth0/auth0-express`.
|
|
43
|
+
*
|
|
44
|
+
* Extends {@link StatelessStateStore} and transparently decrypts existing
|
|
45
|
+
* `express-openid-connect` session cookies on first access, converting them to the
|
|
46
|
+
* `auth0-server-js` format so users do not need to re-authenticate during the migration.
|
|
47
|
+
*
|
|
48
|
+
* **How it works:** `express-openid-connect` encrypts session cookies with `A256GCM` (HKDF
|
|
49
|
+
* derived, info `"JWE CEK"`). `auth0-server-js` uses `A256CBC-HS512` with a different
|
|
50
|
+
* derivation. This store tries the modern decryption first; if that fails it falls back to
|
|
51
|
+
* the `express-openid-connect` decryption and transforms the result into {@link StateData}.
|
|
52
|
+
*
|
|
53
|
+
* Once the user's next request writes back the session, the cookie is re-encrypted in the
|
|
54
|
+
* modern format and this fallback path is no longer exercised for that user.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* import { MigrationStatelessStateStore } from '@auth0/auth0-express/migration';
|
|
59
|
+
*
|
|
60
|
+
* const store = new MigrationStatelessStateStore(
|
|
61
|
+
* {
|
|
62
|
+
* secret: process.env.SESSION_SECRET,
|
|
63
|
+
* legacySecret: process.env.SESSION_SECRET, // the secret used by express-openid-connect
|
|
64
|
+
* legacyAudience: 'https://api.example.com',
|
|
65
|
+
* legacyScope: 'openid profile email',
|
|
66
|
+
* },
|
|
67
|
+
* new ExpressCookieHandler()
|
|
68
|
+
* );
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare class MigrationStatelessStateStore<TStoreOptions> extends StatelessStateStore<TStoreOptions> {
|
|
72
|
+
#private;
|
|
73
|
+
constructor(options: MigrationStatelessStateStoreOptions, cookieHandler: CookieHandler<TStoreOptions>);
|
|
74
|
+
/**
|
|
75
|
+
* Overrides the decrypt method to try modern decryption first, then fall back to legacy decryption.
|
|
76
|
+
*
|
|
77
|
+
* The base class decrypt throws on decryption failure. We catch that and attempt legacy
|
|
78
|
+
* express-openid-connect decryption. If both fail, we return undefined.
|
|
79
|
+
*/
|
|
80
|
+
protected decrypt<TData>(identifier: string, encryptedStateData: string): Promise<TData | undefined>;
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=legacy-compatible-stateless-state-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"legacy-compatible-stateless-state-store.d.ts","sourceRoot":"","sources":["../../src/store/legacy-compatible-stateless-state-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AACnF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAO5D;;GAEG;AACH,MAAM,WAAW,mCAAmC;IAClD;;;;;OAKG;IACH,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAE1B;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAEjC;;;;;;;;OAQG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;OAEG;IACH,oBAAoB,CAAC,EAAE,oBAAoB,CAAC;CAC7C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,qBAAa,4BAA4B,CAAC,aAAa,CAAE,SAAQ,mBAAmB,CAAC,aAAa,CAAC;;gBAIrF,OAAO,EAAE,mCAAmC,EAAE,aAAa,EAAE,aAAa,CAAC,aAAa,CAAC;IAqBrG;;;;;OAKG;cACsB,OAAO,CAAC,KAAK,EACpC,UAAU,EAAE,MAAM,EAClB,kBAAkB,EAAE,MAAM,GACzB,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC;CAgF9B"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { StatelessStateStore } from '@auth0/auth0-server-js';
|
|
2
|
+
import { jwtDecrypt, errors } from 'jose';
|
|
3
|
+
import { LegacySessionTransformer, warnIfAbsoluteDurationUnset } from './legacy-session-transformer.js';
|
|
4
|
+
import { deriveHkdfKey } from './express-oidc-hkdf.js';
|
|
5
|
+
/**
|
|
6
|
+
* A cookie-based (stateless) state store for zero-downtime migration from `express-openid-connect`
|
|
7
|
+
* to `@auth0/auth0-express`.
|
|
8
|
+
*
|
|
9
|
+
* Extends {@link StatelessStateStore} and transparently decrypts existing
|
|
10
|
+
* `express-openid-connect` session cookies on first access, converting them to the
|
|
11
|
+
* `auth0-server-js` format so users do not need to re-authenticate during the migration.
|
|
12
|
+
*
|
|
13
|
+
* **How it works:** `express-openid-connect` encrypts session cookies with `A256GCM` (HKDF
|
|
14
|
+
* derived, info `"JWE CEK"`). `auth0-server-js` uses `A256CBC-HS512` with a different
|
|
15
|
+
* derivation. This store tries the modern decryption first; if that fails it falls back to
|
|
16
|
+
* the `express-openid-connect` decryption and transforms the result into {@link StateData}.
|
|
17
|
+
*
|
|
18
|
+
* Once the user's next request writes back the session, the cookie is re-encrypted in the
|
|
19
|
+
* modern format and this fallback path is no longer exercised for that user.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { MigrationStatelessStateStore } from '@auth0/auth0-express/migration';
|
|
24
|
+
*
|
|
25
|
+
* const store = new MigrationStatelessStateStore(
|
|
26
|
+
* {
|
|
27
|
+
* secret: process.env.SESSION_SECRET,
|
|
28
|
+
* legacySecret: process.env.SESSION_SECRET, // the secret used by express-openid-connect
|
|
29
|
+
* legacyAudience: 'https://api.example.com',
|
|
30
|
+
* legacyScope: 'openid profile email',
|
|
31
|
+
* },
|
|
32
|
+
* new ExpressCookieHandler()
|
|
33
|
+
* );
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export class MigrationStatelessStateStore extends StatelessStateStore {
|
|
37
|
+
#legacySecrets;
|
|
38
|
+
#transformer;
|
|
39
|
+
constructor(options, cookieHandler) {
|
|
40
|
+
super({
|
|
41
|
+
...options.sessionConfiguration,
|
|
42
|
+
secret: options.secret,
|
|
43
|
+
}, cookieHandler);
|
|
44
|
+
// Fall back to the app's session secret(s) when no explicit legacy secret is given. Either
|
|
45
|
+
// may be an array (rotation), so normalize both to a flat string[] tried in order.
|
|
46
|
+
const legacySecret = options.legacySecret ?? options.secret;
|
|
47
|
+
this.#legacySecrets = Array.isArray(legacySecret) ? legacySecret : [legacySecret];
|
|
48
|
+
const legacyAudience = options.legacyAudience ?? 'default';
|
|
49
|
+
const legacyScope = options.legacyScope ?? 'openid profile email offline_access';
|
|
50
|
+
this.#transformer = new LegacySessionTransformer(legacyAudience, legacyScope);
|
|
51
|
+
warnIfAbsoluteDurationUnset(options.sessionConfiguration);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Overrides the decrypt method to try modern decryption first, then fall back to legacy decryption.
|
|
55
|
+
*
|
|
56
|
+
* The base class decrypt throws on decryption failure. We catch that and attempt legacy
|
|
57
|
+
* express-openid-connect decryption. If both fail, we return undefined.
|
|
58
|
+
*/
|
|
59
|
+
async decrypt(identifier, encryptedStateData) {
|
|
60
|
+
try {
|
|
61
|
+
const modernResult = await super.decrypt(identifier, encryptedStateData);
|
|
62
|
+
if (modernResult !== undefined)
|
|
63
|
+
return modernResult;
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
// Modern decryption threw — fall through to legacy attempt.
|
|
67
|
+
}
|
|
68
|
+
const legacyResult = await this.#decryptLegacy(encryptedStateData);
|
|
69
|
+
if (!legacyResult)
|
|
70
|
+
return undefined;
|
|
71
|
+
const { session: legacyData, iat } = legacyResult;
|
|
72
|
+
const stateData = this.#transformer.transformLegacySession(legacyData);
|
|
73
|
+
if (iat !== undefined) {
|
|
74
|
+
stateData.internal.createdAt = iat;
|
|
75
|
+
// Enforce this SDK's absoluteDuration on read. A migrated cookie still carries
|
|
76
|
+
// express-openid-connect's own Max-Age/exp (the OLD deployment's window), so unlike a modern
|
|
77
|
+
// cookie the browser does not stop sending it at this SDK's cap and there is no modern `exp`
|
|
78
|
+
// to reject it. `calculateMaxAge(iat) <= 0` means the session is already past
|
|
79
|
+
// `createdAt + absoluteDuration`: treat it as expired and return no session rather than
|
|
80
|
+
// honoring it until the next write.
|
|
81
|
+
if (this.calculateMaxAge(iat) <= 0) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return stateData;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Decrypts data using express-openid-connect's encryption method (A256GCM with HKDF).
|
|
89
|
+
* Tries each secret in order; the first successful decryption wins (key rotation support).
|
|
90
|
+
* Returns the session payload and the header `iat`, or undefined if all secrets fail.
|
|
91
|
+
*/
|
|
92
|
+
async #decryptLegacy(encryptedData) {
|
|
93
|
+
for (const secret of this.#legacySecrets) {
|
|
94
|
+
try {
|
|
95
|
+
const key = await this.#deriveLegacyKey(secret);
|
|
96
|
+
const { payload, protectedHeader } = await jwtDecrypt(encryptedData, key, {
|
|
97
|
+
contentEncryptionAlgorithms: ['A256GCM'],
|
|
98
|
+
keyManagementAlgorithms: ['dir'],
|
|
99
|
+
});
|
|
100
|
+
const header = protectedHeader;
|
|
101
|
+
// Check header-level exp (express-openid-connect stores exp in the JWE header, not the
|
|
102
|
+
// payload). A genuine express-openid-connect cookie always carries a numeric exp; reject a
|
|
103
|
+
// cookie that lacks one, rather than accepting an exp-less cookie indefinitely. Reject once
|
|
104
|
+
// exp has been reached, mirroring appSession's `exp > epoch()` assertion (i.e. invalid when
|
|
105
|
+
// `exp <= now`), not one second later.
|
|
106
|
+
const headerExp = header.exp;
|
|
107
|
+
if (typeof headerExp !== 'number' || headerExp <= Math.floor(Date.now() / 1000)) {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
const headerIat = header.iat;
|
|
111
|
+
return {
|
|
112
|
+
session: payload,
|
|
113
|
+
iat: typeof headerIat === 'number' ? headerIat : undefined,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
// Swallow any JOSE-level failure and try the next secret / fall through to "no session".
|
|
118
|
+
// Besides the expected JWEDecryptionFailed / JWEInvalid (wrong key or malformed JWE), a
|
|
119
|
+
// modern auth0-server-js cookie (A256CBC-HS512) fed into this A256GCM-only path throws
|
|
120
|
+
// JOSEAlgNotAllowed. All jose errors extend JOSEError, so this fails soft to `undefined`
|
|
121
|
+
// ("logged out"), matching the base store, while genuine programming errors (e.g.
|
|
122
|
+
// TypeError from a broken Web Crypto) are not JOSEError and still propagate.
|
|
123
|
+
if (!(err instanceof errors.JOSEError)) {
|
|
124
|
+
throw err;
|
|
125
|
+
}
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
async #deriveLegacyKey(secret) {
|
|
132
|
+
return deriveHkdfKey(secret, 'JWE CEK');
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=legacy-compatible-stateless-state-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"legacy-compatible-stateless-state-store.js","sourceRoot":"","sources":["../../src/store/legacy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