@workos/authkit-session 0.3.4 → 0.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/README.md +111 -27
- package/dist/core/AuthKitCore.d.ts +20 -0
- package/dist/core/AuthKitCore.d.ts.map +1 -1
- package/dist/core/AuthKitCore.js +36 -4
- package/dist/core/AuthKitCore.js.map +1 -1
- package/dist/core/encryption/ironWebcryptoEncryption.d.ts +2 -1
- package/dist/core/encryption/ironWebcryptoEncryption.d.ts.map +1 -1
- package/dist/core/encryption/ironWebcryptoEncryption.js +2 -2
- package/dist/core/encryption/ironWebcryptoEncryption.js.map +1 -1
- package/dist/core/errors.d.ts +9 -0
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +18 -0
- package/dist/core/errors.js.map +1 -1
- package/dist/core/pkce/cookieName.d.ts +17 -0
- package/dist/core/pkce/cookieName.d.ts.map +1 -0
- package/dist/core/pkce/cookieName.js +26 -0
- package/dist/core/pkce/cookieName.js.map +1 -0
- package/dist/core/pkce/cookieOptions.d.ts +34 -0
- package/dist/core/pkce/cookieOptions.d.ts.map +1 -0
- package/dist/core/pkce/cookieOptions.js +53 -0
- package/dist/core/pkce/cookieOptions.js.map +1 -0
- package/dist/core/pkce/generateAuthorizationUrl.d.ts +46 -0
- package/dist/core/pkce/generateAuthorizationUrl.d.ts.map +1 -0
- package/dist/core/pkce/generateAuthorizationUrl.js +85 -0
- package/dist/core/pkce/generateAuthorizationUrl.js.map +1 -0
- package/dist/core/pkce/state.d.ts +53 -0
- package/dist/core/pkce/state.d.ts.map +1 -0
- package/dist/core/pkce/state.js +91 -0
- package/dist/core/pkce/state.js.map +1 -0
- package/dist/core/session/CookieSessionStorage.d.ts +25 -15
- package/dist/core/session/CookieSessionStorage.d.ts.map +1 -1
- package/dist/core/session/CookieSessionStorage.js +20 -27
- package/dist/core/session/CookieSessionStorage.js.map +1 -1
- package/dist/core/session/TokenManager.js +2 -2
- package/dist/core/session/TokenManager.js.map +1 -1
- package/dist/core/session/serializeCookie.d.ts +13 -0
- package/dist/core/session/serializeCookie.d.ts.map +1 -0
- package/dist/core/session/serializeCookie.js +32 -0
- package/dist/core/session/serializeCookie.js.map +1 -0
- package/dist/core/session/types.d.ts +76 -2
- package/dist/core/session/types.d.ts.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/operations/AuthOperations.d.ts +15 -13
- package/dist/operations/AuthOperations.d.ts.map +1 -1
- package/dist/operations/AuthOperations.js +22 -34
- package/dist/operations/AuthOperations.js.map +1 -1
- package/dist/service/AuthService.d.ts +64 -15
- package/dist/service/AuthService.d.ts.map +1 -1
- package/dist/service/AuthService.js +171 -70
- package/dist/service/AuthService.js.map +1 -1
- package/dist/service/factory.d.ts.map +1 -1
- package/dist/service/factory.js +4 -3
- package/dist/service/factory.js.map +1 -1
- package/dist/utils.d.ts +16 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +23 -0
- package/dist/utils.js.map +1 -1
- package/package.json +23 -20
- package/src/core/AuthKitCore.spec.ts +109 -0
- package/src/core/AuthKitCore.ts +55 -4
- package/src/core/encryption/ironWebcryptoEncryption.spec.ts +62 -0
- package/src/core/encryption/ironWebcryptoEncryption.ts +2 -2
- package/src/core/errors.spec.ts +33 -142
- package/src/core/errors.ts +21 -0
- package/src/core/pkce/cookieName.spec.ts +56 -0
- package/src/core/pkce/cookieName.ts +27 -0
- package/src/core/pkce/cookieOptions.spec.ts +144 -0
- package/src/core/pkce/cookieOptions.ts +61 -0
- package/src/core/pkce/generateAuthorizationUrl.ts +123 -0
- package/src/core/pkce/pkce.spec.ts +175 -0
- package/src/core/pkce/state.spec.ts +216 -0
- package/src/core/pkce/state.ts +120 -0
- package/src/core/session/CookieSessionStorage.spec.ts +79 -3
- package/src/core/session/CookieSessionStorage.ts +56 -36
- package/src/core/session/TokenManager.ts +2 -2
- package/src/core/session/serializeCookie.ts +33 -0
- package/src/core/session/types.ts +83 -2
- package/src/index.ts +11 -0
- package/src/operations/AuthOperations.spec.ts +107 -62
- package/src/operations/AuthOperations.ts +34 -40
- package/src/service/AuthService.spec.ts +750 -97
- package/src/service/AuthService.ts +210 -72
- package/src/service/factory.spec.ts +4 -3
- package/src/service/factory.ts +6 -3
- package/src/utils.ts +22 -0
package/README.md
CHANGED
|
@@ -49,25 +49,26 @@ configure({
|
|
|
49
49
|
### 2. Create Storage Adapter
|
|
50
50
|
|
|
51
51
|
```typescript
|
|
52
|
-
import {
|
|
53
|
-
CookieSessionStorage,
|
|
54
|
-
parseCookieHeader,
|
|
55
|
-
} from '@workos/authkit-session';
|
|
52
|
+
import { CookieSessionStorage } from '@workos/authkit-session';
|
|
56
53
|
|
|
57
54
|
export class MyFrameworkStorage extends CookieSessionStorage<
|
|
58
55
|
Request,
|
|
59
56
|
Response
|
|
60
57
|
> {
|
|
61
|
-
async
|
|
62
|
-
const
|
|
63
|
-
if (!
|
|
64
|
-
|
|
58
|
+
async getCookie(request: Request, name: string): Promise<string | null> {
|
|
59
|
+
const header = request.headers.get('cookie');
|
|
60
|
+
if (!header) return null;
|
|
61
|
+
for (const part of header.split(';')) {
|
|
62
|
+
const [k, ...rest] = part.trim().split('=');
|
|
63
|
+
if (k === name) return decodeURIComponent(rest.join('='));
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
65
66
|
}
|
|
66
67
|
|
|
67
68
|
// Optional: override if your framework can mutate responses
|
|
68
69
|
protected async applyHeaders(
|
|
69
70
|
response: Response | undefined,
|
|
70
|
-
headers: Record<string, string>,
|
|
71
|
+
headers: Record<string, string | string[]>,
|
|
71
72
|
): Promise<{ response: Response }> {
|
|
72
73
|
const newResponse = response
|
|
73
74
|
? new Response(response.body, {
|
|
@@ -77,16 +78,20 @@ export class MyFrameworkStorage extends CookieSessionStorage<
|
|
|
77
78
|
})
|
|
78
79
|
: new Response();
|
|
79
80
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
81
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
82
|
+
if (Array.isArray(value)) {
|
|
83
|
+
for (const v of value) newResponse.headers.append(key, v);
|
|
84
|
+
} else {
|
|
85
|
+
newResponse.headers.append(key, value);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
83
88
|
|
|
84
89
|
return { response: newResponse };
|
|
85
90
|
}
|
|
86
91
|
}
|
|
87
92
|
```
|
|
88
93
|
|
|
89
|
-
`CookieSessionStorage` provides `this.cookieName`, `this.cookieOptions`,
|
|
94
|
+
`CookieSessionStorage` provides `this.cookieName`, `this.cookieOptions`, and generic `setCookie`/`clearCookie`/`serializeCookie` primitives. `getSession`/`saveSession`/`clearSession` are one-line wrappers — you only implement `getCookie`.
|
|
90
95
|
|
|
91
96
|
### 3. Create Service
|
|
92
97
|
|
|
@@ -117,13 +122,18 @@ export const authMiddleware = () => {
|
|
|
117
122
|
undefined,
|
|
118
123
|
refreshedSessionData,
|
|
119
124
|
);
|
|
120
|
-
|
|
125
|
+
const setCookie = headers?.['Set-Cookie'];
|
|
126
|
+
if (setCookie) {
|
|
121
127
|
const newResponse = new Response(result.response.body, {
|
|
122
128
|
status: result.response.status,
|
|
123
129
|
statusText: result.response.statusText,
|
|
124
130
|
headers: result.response.headers,
|
|
125
131
|
});
|
|
126
|
-
|
|
132
|
+
// Append each entry — never `.set()` with an array (comma-joined
|
|
133
|
+
// Set-Cookie is not a valid single HTTP header).
|
|
134
|
+
for (const v of Array.isArray(setCookie) ? setCookie : [setCookie]) {
|
|
135
|
+
newResponse.headers.append('Set-Cookie', v);
|
|
136
|
+
}
|
|
127
137
|
return { ...result, response: newResponse };
|
|
128
138
|
}
|
|
129
139
|
}
|
|
@@ -160,7 +170,7 @@ auth.claims.sid; // string
|
|
|
160
170
|
| `WORKOS_API_KEY` | `apiKey` | WorkOS API key |
|
|
161
171
|
| `WORKOS_REDIRECT_URI` | `redirectUri` | OAuth callback URL |
|
|
162
172
|
| `WORKOS_COOKIE_PASSWORD` | `cookiePassword` | 32+ char encryption key |
|
|
163
|
-
| `WORKOS_COOKIE_NAME` | `cookieName` | Cookie name (default: `
|
|
173
|
+
| `WORKOS_COOKIE_NAME` | `cookieName` | Cookie name (default: `wos-session`) |
|
|
164
174
|
| `WORKOS_COOKIE_MAX_AGE` | `cookieMaxAge` | Cookie lifetime in seconds |
|
|
165
175
|
| `WORKOS_COOKIE_DOMAIN` | `cookieDomain` | Cookie domain |
|
|
166
176
|
| `WORKOS_COOKIE_SAME_SITE` | `cookieSameSite` | `lax`, `strict`, or `none` |
|
|
@@ -180,16 +190,89 @@ authService.saveSession(response, sessionData) // → { response?, headers? }
|
|
|
180
190
|
authService.clearSession(response)
|
|
181
191
|
|
|
182
192
|
// WorkOS Operations
|
|
183
|
-
authService.signOut(sessionId, { returnTo }) // → { logoutUrl,
|
|
193
|
+
authService.signOut(sessionId, { returnTo }) // → { logoutUrl, response?, headers? }
|
|
184
194
|
authService.refreshSession(session, organizationId?)
|
|
185
195
|
authService.switchOrganization(session, organizationId)
|
|
186
196
|
|
|
187
|
-
// URL Generation
|
|
188
|
-
authService.
|
|
189
|
-
authService.
|
|
190
|
-
authService.
|
|
197
|
+
// URL Generation — write verifier cookie, return { url, cookieName, response?, headers? }
|
|
198
|
+
authService.createAuthorization(response, options)
|
|
199
|
+
authService.createSignIn(response, options)
|
|
200
|
+
authService.createSignUp(response, options)
|
|
201
|
+
|
|
202
|
+
// Error-path cleanup for the PKCE verifier cookie
|
|
203
|
+
// `state` is required (from the callback URL) — it identifies which per-flow
|
|
204
|
+
// verifier cookie to clear. Skip this call when `state` is absent from the
|
|
205
|
+
// callback URL (malformed callback); the 10-minute PKCE TTL handles the orphan.
|
|
206
|
+
// (response may be `undefined` for headers-only adapters)
|
|
207
|
+
authService.clearPendingVerifier(response, { state, redirectUri? })
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### PKCE verifier cookie (`wos-auth-verifier-<fnv1a>`)
|
|
211
|
+
|
|
212
|
+
This library binds every OAuth sign-in to a PKCE code verifier, so a leaked
|
|
213
|
+
`state` value on its own cannot be used to complete a session hijack.
|
|
214
|
+
|
|
215
|
+
Each in-flight sign-in gets its own per-flow verifier cookie with a
|
|
216
|
+
deterministic suffix derived from the sealed blob, so concurrent
|
|
217
|
+
sign-ins from multiple tabs no longer clobber each other.
|
|
218
|
+
|
|
219
|
+
The verifier is sealed into a single blob that serves two roles:
|
|
220
|
+
|
|
221
|
+
1. It is sent to WorkOS as the OAuth `state` query parameter.
|
|
222
|
+
2. It is set as a short-lived HTTP-only cookie (`wos-auth-verifier-<fnv1a>`, 10 min).
|
|
223
|
+
|
|
224
|
+
The cookie is written and read through `SessionStorage`. Callers don't see
|
|
225
|
+
sealed blobs or cookie options:
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
// Sign in: library writes the verifier cookie via storage, returns the URL + headers
|
|
229
|
+
const { url, headers } = await authService.createSignIn(response, {
|
|
230
|
+
returnPathname: '/dashboard',
|
|
231
|
+
});
|
|
232
|
+
return new Response(null, {
|
|
233
|
+
status: 302,
|
|
234
|
+
headers: { ...headers, Location: url },
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// Callback: library reads the verifier via storage, byte-compares, then exchanges
|
|
238
|
+
await authService.handleCallback(request, response, {
|
|
239
|
+
code,
|
|
240
|
+
state, // from URL
|
|
241
|
+
});
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
On success, `handleCallback` returns a `Set-Cookie` entry in `headers` as a
|
|
245
|
+
`string[]` with two values — the session cookie AND a clear for the verifier
|
|
246
|
+
cookie. Adapters must append each entry as its own `Set-Cookie` HTTP header
|
|
247
|
+
(never comma-join). The bag key is case-insensitive — `mergeHeaderBags`
|
|
248
|
+
preserves the adapter's casing — so look it up that way:
|
|
249
|
+
|
|
250
|
+
```ts
|
|
251
|
+
const setCookie =
|
|
252
|
+
result.headers?.['Set-Cookie'] ?? result.headers?.['set-cookie'];
|
|
253
|
+
if (setCookie) {
|
|
254
|
+
for (const v of Array.isArray(setCookie) ? setCookie : [setCookie]) {
|
|
255
|
+
response.headers.append('Set-Cookie', v);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
191
258
|
```
|
|
192
259
|
|
|
260
|
+
Mismatched state and cookie raise `OAuthStateMismatchError`. A missing cookie
|
|
261
|
+
(typical cause: Set-Cookie stripped by a proxy) raises
|
|
262
|
+
`PKCECookieMissingError`. On either error path — or any early bail-out before
|
|
263
|
+
`handleCallback` runs — call
|
|
264
|
+
`authService.clearPendingVerifier(response, { state })` with the `state` from
|
|
265
|
+
the callback URL to emit a delete header for the correct per-flow cookie:
|
|
266
|
+
|
|
267
|
+
```ts
|
|
268
|
+
if (state) {
|
|
269
|
+
await authService.clearPendingVerifier(response, { state });
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
If the callback URL has no `state` (malformed callback), skip this call — the
|
|
274
|
+
10-minute PKCE TTL handles the orphan.
|
|
275
|
+
|
|
193
276
|
### Direct Access (Advanced)
|
|
194
277
|
|
|
195
278
|
For maximum control, use the primitives directly:
|
|
@@ -198,14 +281,15 @@ For maximum control, use the primitives directly:
|
|
|
198
281
|
import {
|
|
199
282
|
AuthKitCore,
|
|
200
283
|
AuthOperations,
|
|
201
|
-
getWorkOS,
|
|
202
284
|
getConfigurationProvider,
|
|
285
|
+
getWorkOS,
|
|
286
|
+
sessionEncryption,
|
|
203
287
|
} from '@workos/authkit-session';
|
|
204
288
|
|
|
205
|
-
const config = getConfigurationProvider();
|
|
206
|
-
const client = getWorkOS(
|
|
207
|
-
const core = new AuthKitCore(config, client,
|
|
208
|
-
const operations = new AuthOperations(core, client, config);
|
|
289
|
+
const config = getConfigurationProvider().getConfig();
|
|
290
|
+
const client = getWorkOS();
|
|
291
|
+
const core = new AuthKitCore(config, client, sessionEncryption);
|
|
292
|
+
const operations = new AuthOperations(core, client, config, sessionEncryption);
|
|
209
293
|
|
|
210
294
|
// Use core.validateAndRefresh(), core.encryptSession(), etc.
|
|
211
295
|
```
|
|
@@ -213,7 +297,7 @@ const operations = new AuthOperations(core, client, config);
|
|
|
213
297
|
## Technical Details
|
|
214
298
|
|
|
215
299
|
- **JWKS Caching**: Keys fetched on-demand, cached for process lifetime. `jose` handles key rotation automatically.
|
|
216
|
-
- **Token
|
|
300
|
+
- **Token Refresh**: `validateAndRefresh` refreshes when `verifyToken` fails (i.e. when the access token is expired or invalid). `isTokenExpiring(token, buffer)` is available as a separate helper for callers that want to proactively refresh before expiry.
|
|
217
301
|
- **Session Encryption**: AES-256-CBC + SHA-256 HMAC via `iron-webcrypto`.
|
|
218
302
|
- **Lazy Initialization**: `createAuthService()` defers initialization until first use, allowing `configure()` to be called later.
|
|
219
303
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Impersonator, User, WorkOS } from '@workos-inc/node';
|
|
2
2
|
import type { AuthKitConfig } from './config/types.js';
|
|
3
|
+
import { type PKCEState } from './pkce/state.js';
|
|
3
4
|
import type { BaseTokenClaims, CustomClaims, Session, SessionEncryption } from './session/types.js';
|
|
4
5
|
/**
|
|
5
6
|
* AuthKitCore provides pure business logic for authentication operations.
|
|
@@ -70,6 +71,25 @@ export declare class AuthKitCore {
|
|
|
70
71
|
* @throws SessionEncryptionError if decryption fails
|
|
71
72
|
*/
|
|
72
73
|
decryptSession(encryptedSession: string): Promise<Session>;
|
|
74
|
+
/**
|
|
75
|
+
* Verify the OAuth callback state against the PKCE verifier cookie and
|
|
76
|
+
* return the unsealed state blob.
|
|
77
|
+
*
|
|
78
|
+
* Core owns encryption, so verification lives here rather than on
|
|
79
|
+
* AuthService. Byte-compares the query-string `state` against the cookie
|
|
80
|
+
* value BEFORE attempting to decrypt — smaller crypto attack surface and
|
|
81
|
+
* no decryption cost on obvious mismatches.
|
|
82
|
+
*
|
|
83
|
+
* @throws {OAuthStateMismatchError} state missing or does not match cookie
|
|
84
|
+
* @throws {PKCECookieMissingError} cookie not sent — typically a proxy or
|
|
85
|
+
* Set-Cookie propagation issue on the adapter's callback path
|
|
86
|
+
* @throws {SessionEncryptionError} seal tampered, wrong password, TTL expired,
|
|
87
|
+
* or schema mismatch on the unsealed payload
|
|
88
|
+
*/
|
|
89
|
+
verifyCallbackState(params: {
|
|
90
|
+
stateFromUrl: string | undefined;
|
|
91
|
+
cookieValue: string | undefined;
|
|
92
|
+
}): Promise<PKCEState>;
|
|
73
93
|
/**
|
|
74
94
|
* Refresh tokens using WorkOS API.
|
|
75
95
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthKitCore.d.ts","sourceRoot":"","sources":["../../src/core/AuthKitCore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAGnE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"AuthKitCore.d.ts","sourceRoot":"","sources":["../../src/core/AuthKitCore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAGnE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAOvD,OAAO,EAAE,KAAK,SAAS,EAAe,MAAM,iBAAiB,CAAC;AAC9D,OAAO,KAAK,EACV,eAAe,EACf,YAAY,EACZ,OAAO,EACP,iBAAiB,EAClB,MAAM,oBAAoB,CAAC;AAE5B;;;;;;;;;;;;GAYG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,UAAU,CAAoB;IACtC,OAAO,CAAC,QAAQ,CAAS;gBAGvB,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,iBAAiB;IAQ/B;;OAEG;IACH,OAAO,CAAC,QAAQ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ACrD,OAAO,CAAC;QACT,KAAK,EAAE,OAAO,CAAC;QACf,SAAS,EAAE,OAAO,CAAC;QACnB,OAAO,EAAE,OAAO,CAAC;QACjB,MAAM,EAAE,eAAe,GAAG,aAAa,CAAC;KACzC,CAAC;CA8CH"}
|
package/dist/core/AuthKitCore.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createRemoteJWKSet, decodeJwt, jwtVerify } from 'jose';
|
|
2
|
-
import { once } from '../utils.js';
|
|
3
|
-
import { SessionEncryptionError, TokenRefreshError } from './errors.js';
|
|
2
|
+
import { constantTimeEqual, once } from '../utils.js';
|
|
3
|
+
import { OAuthStateMismatchError, PKCECookieMissingError, SessionEncryptionError, TokenRefreshError, } from './errors.js';
|
|
4
|
+
import { unsealState } from './pkce/state.js';
|
|
4
5
|
/**
|
|
5
6
|
* AuthKitCore provides pure business logic for authentication operations.
|
|
6
7
|
*
|
|
@@ -80,8 +81,8 @@ export class AuthKitCore {
|
|
|
80
81
|
try {
|
|
81
82
|
return decodeJwt(token);
|
|
82
83
|
}
|
|
83
|
-
catch (
|
|
84
|
-
throw new Error('Invalid token');
|
|
84
|
+
catch (cause) {
|
|
85
|
+
throw new Error('Invalid token', { cause });
|
|
85
86
|
}
|
|
86
87
|
}
|
|
87
88
|
/**
|
|
@@ -119,6 +120,37 @@ export class AuthKitCore {
|
|
|
119
120
|
throw new SessionEncryptionError('Failed to decrypt session', error);
|
|
120
121
|
}
|
|
121
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Verify the OAuth callback state against the PKCE verifier cookie and
|
|
125
|
+
* return the unsealed state blob.
|
|
126
|
+
*
|
|
127
|
+
* Core owns encryption, so verification lives here rather than on
|
|
128
|
+
* AuthService. Byte-compares the query-string `state` against the cookie
|
|
129
|
+
* value BEFORE attempting to decrypt — smaller crypto attack surface and
|
|
130
|
+
* no decryption cost on obvious mismatches.
|
|
131
|
+
*
|
|
132
|
+
* @throws {OAuthStateMismatchError} state missing or does not match cookie
|
|
133
|
+
* @throws {PKCECookieMissingError} cookie not sent — typically a proxy or
|
|
134
|
+
* Set-Cookie propagation issue on the adapter's callback path
|
|
135
|
+
* @throws {SessionEncryptionError} seal tampered, wrong password, TTL expired,
|
|
136
|
+
* or schema mismatch on the unsealed payload
|
|
137
|
+
*/
|
|
138
|
+
async verifyCallbackState(params) {
|
|
139
|
+
const { stateFromUrl, cookieValue } = params;
|
|
140
|
+
if (!stateFromUrl) {
|
|
141
|
+
throw new OAuthStateMismatchError('Missing state parameter from callback URL');
|
|
142
|
+
}
|
|
143
|
+
if (!cookieValue) {
|
|
144
|
+
throw new PKCECookieMissingError('PKCE verifier cookie missing — cannot verify OAuth state. Ensure Set-Cookie headers are propagated on redirects.');
|
|
145
|
+
}
|
|
146
|
+
const encoder = new TextEncoder();
|
|
147
|
+
const urlBytes = encoder.encode(stateFromUrl);
|
|
148
|
+
const cookieBytes = encoder.encode(cookieValue);
|
|
149
|
+
if (!constantTimeEqual(urlBytes, cookieBytes)) {
|
|
150
|
+
throw new OAuthStateMismatchError('OAuth state mismatch');
|
|
151
|
+
}
|
|
152
|
+
return unsealState(this.encryption, this.config.cookiePassword, cookieValue);
|
|
153
|
+
}
|
|
122
154
|
/**
|
|
123
155
|
* Refresh tokens using WorkOS API.
|
|
124
156
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthKitCore.js","sourceRoot":"","sources":["../../src/core/AuthKitCore.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAChE,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"AuthKitCore.js","sourceRoot":"","sources":["../../src/core/AuthKitCore.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,I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EACpB,cAAc,EACd,EAAE,MAAM,EAAE,OAAO,CAAC,IAAI,EAAE,EAAE,EAAE,SAAS,EAAE,CACxC,CAAC;QACF,MAAM,SAAS,GAAG,IAAI,CAAC,gBAAgB,CACrC,UAAU,CAAC,WAAW,CACvB,CAAC;QACF,OAAO;YACL,KAAK,EAAE,IAAI;YACX,SAAS,EAAE,IAAI;YACf,OAAO,EAAE,UAAU;YACnB,MAAM,EAAE,SAAS;SAClB,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -10,8 +10,9 @@ export declare class SessionEncryption implements SessionEncryptionInterface {
|
|
|
10
10
|
password: string;
|
|
11
11
|
ttl?: number | undefined;
|
|
12
12
|
}): Promise<string>;
|
|
13
|
-
unsealData<T = unknown>(encryptedData: string, { password }: {
|
|
13
|
+
unsealData<T = unknown>(encryptedData: string, { password, ttl }: {
|
|
14
14
|
password: string;
|
|
15
|
+
ttl?: number | undefined;
|
|
15
16
|
}): Promise<T>;
|
|
16
17
|
}
|
|
17
18
|
declare const ironWebcryptoEncryption: SessionEncryption;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ironWebcryptoEncryption.d.ts","sourceRoot":"","sources":["../../../src/core/encryption/ironWebcryptoEncryption.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,IAAI,0BAA0B,EAAE,MAAM,qBAAqB,CAAC;AAE3F;;GAEG;AACH,qBAAa,iBAAkB,YAAW,0BAA0B;IAClE,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAO;IACxC,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAK;IAGzC,OAAO,CAAC,SAAS;IAaX,QAAQ,CACZ,IAAI,EAAE,OAAO,EACb,EAAE,QAAQ,EAAE,GAAO,EAAE,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE;IAgCjE,UAAU,CAAC,CAAC,GAAG,OAAO,EAC1B,aAAa,EAAE,MAAM,EACrB,EAAE,QAAQ,EAAE,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,
|
|
1
|
+
{"version":3,"file":"ironWebcryptoEncryption.d.ts","sourceRoot":"","sources":["../../../src/core/encryption/ironWebcryptoEncryption.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,IAAI,0BAA0B,EAAE,MAAM,qBAAqB,CAAC;AAE3F;;GAEG;AACH,qBAAa,iBAAkB,YAAW,0BAA0B;IAClE,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAO;IACxC,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAK;IAGzC,OAAO,CAAC,SAAS;IAaX,QAAQ,CACZ,IAAI,EAAE,OAAO,EACb,EAAE,QAAQ,EAAE,GAAO,EAAE,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE;IAgCjE,UAAU,CAAC,CAAC,GAAG,OAAO,EAC1B,aAAa,EAAE,MAAM,EACrB,EAAE,QAAQ,EAAE,GAAO,EAAE,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,GACpE,OAAO,CAAC,CAAC,CAAC;CAoCd;AAED,QAAA,MAAM,uBAAuB,mBAA0B,CAAC;AAExD,eAAe,uBAAuB,CAAC"}
|
|
@@ -40,7 +40,7 @@ export class SessionEncryption {
|
|
|
40
40
|
return `${seal}${this.versionDelimiter}${this.currentMajorVersion}`;
|
|
41
41
|
}
|
|
42
42
|
// Decrypt data from iron-session with HMAC verification
|
|
43
|
-
async unsealData(encryptedData, { password }) {
|
|
43
|
+
async unsealData(encryptedData, { password, ttl = 0 }) {
|
|
44
44
|
// First, parse the seal to extract the version
|
|
45
45
|
const { sealWithoutVersion, tokenVersion } = this.parseSeal(encryptedData);
|
|
46
46
|
// Format password as a map like iron-session expects
|
|
@@ -59,7 +59,7 @@ export class SessionEncryption {
|
|
|
59
59
|
iterations: 1,
|
|
60
60
|
minPasswordlength: 32,
|
|
61
61
|
},
|
|
62
|
-
ttl: 0
|
|
62
|
+
ttl: ttl * 1000, // Convert seconds to milliseconds. 0 = no TTL enforcement.
|
|
63
63
|
timestampSkewSec: 60,
|
|
64
64
|
localtimeOffsetMsec: 0,
|
|
65
65
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ironWebcryptoEncryption.js","sourceRoot":"","sources":["../../../src/core/encryption/ironWebcryptoEncryption.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,IAAI,QAAQ,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAGxE;;GAEG;AACH,MAAM,OAAO,iBAAiB;IACX,gBAAgB,GAAG,GAAG,CAAC;IACvB,mBAAmB,GAAG,CAAC,CAAC;IAEzC,oDAAoD;IAC5C,SAAS,CAAC,IAAY;QAI5B,MAAM,CAAC,kBAAkB,GAAG,EAAE,EAAE,oBAAoB,CAAC,GAAG,IAAI,CAAC,KAAK,CAChE,IAAI,CAAC,gBAAgB,CACtB,CAAC;QACF,MAAM,YAAY,GAChB,oBAAoB,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,oBAAoB,EAAE,EAAE,CAAC,CAAC;QAC3E,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,CAAC;IAC9C,CAAC;IAED,4DAA4D;IAC5D,KAAK,CAAC,QAAQ,CACZ,IAAa,EACb,EAAE,QAAQ,EAAE,GAAG,GAAG,CAAC,EAAkD;QAErE,0CAA0C;QAC1C,MAAM,WAAW,GAAG;YAClB,EAAE,EAAE,GAAG;YACP,MAAM,EAAE,QAAQ;SACjB,CAAC;QAEF,sEAAsE;QACtE,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,WAAW,EAAE;YAC7C,UAAU,EAAE;g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,SAAS,EAAE;gBACT,QAAQ,EAAE,GAAG;gBACb,SAAS,EAAE,QAAQ;gBACnB,UAAU,EAAE,CAAC;gBACb,iBAAiB,EAAE,EAAE;aACtB;YACD,GAAG,EAAE,GAAG,GAAG,IAAI,EAAE,2DAA2D;YAC5E,gBAAgB,EAAE,EAAE;YACpB,mBAAmB,EAAE,CAAC;SACvB,CAAC,CAAC;QAEH,oCAAoC;QACpC,IAAI,YAAY,KAAK,CAAC,EAAE,CAAC;YACvB,OAAO,IAAS,CAAC;QACnB,CAAC;aAAM,IAAI,YAAY,KAAK,IAAI,EAAE,CAAC;YACjC,4DAA4D;YAC5D,OAAO,EAAE,GAAI,IAAY,CAAC,UAAU,EAAO,CAAC;QAC9C,CAAC;QAED,OAAO,IAAS,CAAC;IACnB,CAAC;CACF;AAED,MAAM,uBAAuB,GAAG,IAAI,iBAAiB,EAAE,CAAC;AAExD,eAAe,uBAAuB,CAAC"}
|
package/dist/core/errors.d.ts
CHANGED
|
@@ -8,6 +8,15 @@ export declare class SessionEncryptionError extends AuthKitError {
|
|
|
8
8
|
export declare class TokenValidationError extends AuthKitError {
|
|
9
9
|
constructor(message: string, cause?: unknown);
|
|
10
10
|
}
|
|
11
|
+
export declare class OAuthStateMismatchError extends AuthKitError {
|
|
12
|
+
constructor(message: string, cause?: unknown);
|
|
13
|
+
}
|
|
14
|
+
export declare class PKCECookieMissingError extends AuthKitError {
|
|
15
|
+
constructor(message: string, cause?: unknown);
|
|
16
|
+
}
|
|
17
|
+
export declare class PKCEPayloadTooLargeError extends AuthKitError {
|
|
18
|
+
constructor(message: string, cause?: unknown);
|
|
19
|
+
}
|
|
11
20
|
export declare class TokenRefreshError extends AuthKitError {
|
|
12
21
|
readonly userId?: string;
|
|
13
22
|
readonly sessionId?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/core/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,YAAa,SAAQ,KA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MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;CAI7C;AAED,qBAAa,sBAAuB,SAAQ,YAAY;gBAC1C,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;CAI7C;AAED,qBAAa,wBAAyB,SAAQ,YAAY;gBAC5C,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;CAI7C;AAED,qBAAa,iBAAkB,SAAQ,YAAY;IACjD,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;gBAG1B,OAAO,EAAE,MAAM,EACf,KAAK,CAAC,EAAE,OAAO,EACf,OAAO,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE;CAOpD"}
|
package/dist/core/errors.js
CHANGED
|
@@ -19,6 +19,24 @@ export class TokenValidationError extends AuthKitError {
|
|
|
19
19
|
this.name = 'TokenValidationError';
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
+
export class OAuthStateMismatchError extends AuthKitError {
|
|
23
|
+
constructor(message, cause) {
|
|
24
|
+
super(message, cause);
|
|
25
|
+
this.name = 'OAuthStateMismatchError';
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export class PKCECookieMissingError extends AuthKitError {
|
|
29
|
+
constructor(message, cause) {
|
|
30
|
+
super(message, cause);
|
|
31
|
+
this.name = 'PKCECookieMissingError';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export class PKCEPayloadTooLargeError extends AuthKitError {
|
|
35
|
+
constructor(message, cause) {
|
|
36
|
+
super(message, cause);
|
|
37
|
+
this.name = 'PKCEPayloadTooLargeError';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
22
40
|
export class TokenRefreshError extends AuthKitError {
|
|
23
41
|
userId;
|
|
24
42
|
sessionId;
|
package/dist/core/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/core/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAa,SAAQ,KAAK;IACrC,IAAI,CAAuB;IAE3B,YAAY,OAAe,EAAE,KAAe,EAAE,IAA0B;QACtE,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;QAC3B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAED,MAAM,OAAO,sBAAuB,SAAQ,YAAY;IACtD,YAAY,OAAe,EAAE,KAAe;QAC1C,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;IACvC,CAAC;CACF;AAED,MAAM,OAAO,oBAAqB,SAAQ,YAAY;IACpD,YAAY,OAAe,EAAE,KAAe;QAC1C,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACrC,CAAC;CACF;AAED,MAAM,OAAO,iBAAkB,SAAQ,YAAY;IACxC,MAAM,CAAU;IAChB,SAAS,CAAU;IAE5B,YACE,OAAe,EACf,KAAe,EACf,OAAiD;QAEjD,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,IAAI,CAAC,MAAM,GAAG,OAAO,EAAE,MAAM,CAAC;QAC9B,IAAI,CAAC,SAAS,GAAG,OAAO,EAAE,SAAS,CAAC;IACtC,CAAC;CACF"}
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/core/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAa,SAAQ,KAAK;IACrC,IAAI,CAAuB;IAE3B,YAAY,OAAe,EAAE,KAAe,EAAE,IAA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CAAC,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,IAAI,CAAC,MAAM,GAAG,OAAO,EAAE,MAAM,CAAC;QAC9B,IAAI,CAAC,SAAS,GAAG,OAAO,EAAE,SAAS,CAAC;IACtC,CAAC;CACF"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Stable prefix for all PKCE verifier cookies. */
|
|
2
|
+
export declare const PKCE_COOKIE_PREFIX = "wos-auth-verifier";
|
|
3
|
+
/**
|
|
4
|
+
* FNV-1a 32-bit hash of the input, returned as a zero-padded 8-char
|
|
5
|
+
* lowercase hex string. Used purely as a namespacing mechanism — not
|
|
6
|
+
* security-sensitive. Collision probability is ~1/4B per pair; a
|
|
7
|
+
* collision routes one flow's callback to the wrong cookie, which
|
|
8
|
+
* then fails byte-equality in `verifyCallbackState` (fail-closed).
|
|
9
|
+
*/
|
|
10
|
+
export declare function fnv1a32Hex(input: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* Derive a flow-specific PKCE verifier cookie name from the sealed
|
|
13
|
+
* state blob. Each concurrent OAuth flow gets its own cookie so
|
|
14
|
+
* parallel sign-ins from multiple tabs don't clobber each other.
|
|
15
|
+
*/
|
|
16
|
+
export declare function getPKCECookieNameForState(state: string): string;
|
|
17
|
+
//# sourceMappingURL=cookieName.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cookieName.d.ts","sourceRoot":"","sources":["../../../src/core/pkce/cookieName.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,eAAO,MAAM,kBAAkB,sBAAsB,CAAC;AAEtD;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAOhD;AAED;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE/D"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Stable prefix for all PKCE verifier cookies. */
|
|
2
|
+
export const PKCE_COOKIE_PREFIX = 'wos-auth-verifier';
|
|
3
|
+
/**
|
|
4
|
+
* FNV-1a 32-bit hash of the input, returned as a zero-padded 8-char
|
|
5
|
+
* lowercase hex string. Used purely as a namespacing mechanism — not
|
|
6
|
+
* security-sensitive. Collision probability is ~1/4B per pair; a
|
|
7
|
+
* collision routes one flow's callback to the wrong cookie, which
|
|
8
|
+
* then fails byte-equality in `verifyCallbackState` (fail-closed).
|
|
9
|
+
*/
|
|
10
|
+
export function fnv1a32Hex(input) {
|
|
11
|
+
let hash = 0x811c9dc5;
|
|
12
|
+
const bytes = new TextEncoder().encode(input);
|
|
13
|
+
for (const byte of bytes) {
|
|
14
|
+
hash = Math.imul(hash ^ byte, 0x01000193) >>> 0;
|
|
15
|
+
}
|
|
16
|
+
return hash.toString(16).padStart(8, '0');
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Derive a flow-specific PKCE verifier cookie name from the sealed
|
|
20
|
+
* state blob. Each concurrent OAuth flow gets its own cookie so
|
|
21
|
+
* parallel sign-ins from multiple tabs don't clobber each other.
|
|
22
|
+
*/
|
|
23
|
+
export function getPKCECookieNameForState(state) {
|
|
24
|
+
return `${PKCE_COOKIE_PREFIX}-${fnv1a32Hex(state)}`;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=cookieName.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cookieName.js","sourceRoot":"","sources":["../../../src/core/pkce/cookieName.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,MAAM,CAAC,MAAM,kBAAkB,GAAG,mBAAmB,CAAC;AAEtD;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,IAAI,IAAI,GAAG,UAAU,CAAC;IACtB,MAAM,KAAK,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC9C,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC;IAClD,CAAC;IACD,OAAO,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;AAC5C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,yBAAyB,CAAC,KAAa;IACrD,OAAO,GAAG,kBAAkB,IAAI,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;AACtD,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AuthKitConfig } from '../config/types.js';
|
|
2
|
+
import type { CookieOptions } from '../session/types.js';
|
|
3
|
+
/**
|
|
4
|
+
* PKCE verifier cookie lifetime (seconds). Matches the 10-minute convention
|
|
5
|
+
* used by Arctic, openid-client, Clerk, and Okta for short-lived OAuth state
|
|
6
|
+
* cookies — long enough to accommodate slow sign-ins (out-of-band 2FA, tab
|
|
7
|
+
* switching) and short enough to keep the replay window tight if the sealed
|
|
8
|
+
* blob leaks.
|
|
9
|
+
*
|
|
10
|
+
* The same value is used as both the `Max-Age` cookie attribute and the
|
|
11
|
+
* sealed payload's TTL, so both layers reject a stale blob.
|
|
12
|
+
*/
|
|
13
|
+
export declare const PKCE_COOKIE_MAX_AGE = 600;
|
|
14
|
+
/**
|
|
15
|
+
* Compute PKCE verifier cookie options from config + the resolved redirect URI.
|
|
16
|
+
*
|
|
17
|
+
* Behavior:
|
|
18
|
+
* - `sameSite: 'strict'` is downgraded to `'lax'` so the cookie survives the
|
|
19
|
+
* cross-site redirect back from WorkOS.
|
|
20
|
+
* - `sameSite: 'none'` is preserved (iframe/embed flows require it).
|
|
21
|
+
* - `secure` is forced to `true` when `sameSite === 'none'` (required by
|
|
22
|
+
* modern browsers). Otherwise it is inferred from the redirect URI's
|
|
23
|
+
* protocol, defaulting fail-closed to `true` on invalid/missing URLs.
|
|
24
|
+
* - `path` is always `/`. The cookie gets sent on every same-origin request
|
|
25
|
+
* during the 10-minute window, which is fine — it's HttpOnly and expires
|
|
26
|
+
* quickly. The tradeoff favors DX: path-scoped cookies are invisible in
|
|
27
|
+
* Chrome DevTools' Application panel from any page outside the scoped
|
|
28
|
+
* path, which makes PKCE wiring look broken even when it works.
|
|
29
|
+
*
|
|
30
|
+
* Internal helper — not exported from the package. Callers get cookie options
|
|
31
|
+
* indirectly via `AuthService.createSignIn` / `clearPendingVerifier`.
|
|
32
|
+
*/
|
|
33
|
+
export declare function getPKCECookieOptions(config: AuthKitConfig, redirectUri?: string): CookieOptions;
|
|
34
|
+
//# sourceMappingURL=cookieOptions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cookieOptions.d.ts","sourceRoot":"","sources":["../../../src/core/pkce/cookieOptions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;;;;;;;GASG;AACH,eAAO,MAAM,mBAAmB,MAAM,CAAC;AAEvC;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,aAAa,EACrB,WAAW,CAAC,EAAE,MAAM,GACnB,aAAa,CAuBf"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PKCE verifier cookie lifetime (seconds). Matches the 10-minute convention
|
|
3
|
+
* used by Arctic, openid-client, Clerk, and Okta for short-lived OAuth state
|
|
4
|
+
* cookies — long enough to accommodate slow sign-ins (out-of-band 2FA, tab
|
|
5
|
+
* switching) and short enough to keep the replay window tight if the sealed
|
|
6
|
+
* blob leaks.
|
|
7
|
+
*
|
|
8
|
+
* The same value is used as both the `Max-Age` cookie attribute and the
|
|
9
|
+
* sealed payload's TTL, so both layers reject a stale blob.
|
|
10
|
+
*/
|
|
11
|
+
export const PKCE_COOKIE_MAX_AGE = 600;
|
|
12
|
+
/**
|
|
13
|
+
* Compute PKCE verifier cookie options from config + the resolved redirect URI.
|
|
14
|
+
*
|
|
15
|
+
* Behavior:
|
|
16
|
+
* - `sameSite: 'strict'` is downgraded to `'lax'` so the cookie survives the
|
|
17
|
+
* cross-site redirect back from WorkOS.
|
|
18
|
+
* - `sameSite: 'none'` is preserved (iframe/embed flows require it).
|
|
19
|
+
* - `secure` is forced to `true` when `sameSite === 'none'` (required by
|
|
20
|
+
* modern browsers). Otherwise it is inferred from the redirect URI's
|
|
21
|
+
* protocol, defaulting fail-closed to `true` on invalid/missing URLs.
|
|
22
|
+
* - `path` is always `/`. The cookie gets sent on every same-origin request
|
|
23
|
+
* during the 10-minute window, which is fine — it's HttpOnly and expires
|
|
24
|
+
* quickly. The tradeoff favors DX: path-scoped cookies are invisible in
|
|
25
|
+
* Chrome DevTools' Application panel from any page outside the scoped
|
|
26
|
+
* path, which makes PKCE wiring look broken even when it works.
|
|
27
|
+
*
|
|
28
|
+
* Internal helper — not exported from the package. Callers get cookie options
|
|
29
|
+
* indirectly via `AuthService.createSignIn` / `clearPendingVerifier`.
|
|
30
|
+
*/
|
|
31
|
+
export function getPKCECookieOptions(config, redirectUri) {
|
|
32
|
+
const configuredSameSite = (config.cookieSameSite ?? 'lax').toLowerCase();
|
|
33
|
+
const sameSite = configuredSameSite === 'none' ? 'none' : 'lax';
|
|
34
|
+
const urlString = redirectUri ?? config.redirectUri;
|
|
35
|
+
let secure = true;
|
|
36
|
+
if (urlString && sameSite !== 'none') {
|
|
37
|
+
try {
|
|
38
|
+
secure = new URL(urlString).protocol === 'https:';
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// Fail-closed: secure stays true.
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
path: '/',
|
|
46
|
+
httpOnly: true,
|
|
47
|
+
secure,
|
|
48
|
+
sameSite,
|
|
49
|
+
maxAge: PKCE_COOKIE_MAX_AGE,
|
|
50
|
+
...(config.cookieDomain ? { domain: config.cookieDomain } : {}),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=cookieOptions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cookieOptions.js","sourceRoot":"","sources":["../../../src/core/pkce/cookieOptions.ts"],"names":[],"mappings":"AAGA;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAEvC;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,oBAAoB,CAClC,MAAqB,EACrB,WAAoB;IAEpB,MAAM,kBAAkB,GAAG,CAAC,MAAM,CAAC,cAAc,IAAI,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;IAC1E,MAAM,QAAQ,GACZ,kBAAkB,KAAK,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC;IAEjD,MAAM,SAAS,GAAG,WAAW,IAAI,MAAM,CAAC,WAAW,CAAC;IACpD,IAAI,MAAM,GAAG,IAAI,CAAC;IAClB,IAAI,SAAS,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;QACrC,IAAI,CAAC;YACH,MAAM,GAAG,IAAI,GAAG,CAAC,SAAS,CAAC,CAAC,QAAQ,KAAK,QAAQ,CAAC;QACpD,CAAC;QAAC,MAAM,CAAC;YACP,kCAAkC;QACpC,CAAC;IACH,CAAC;IAED,OAAO;QACL,IAAI,EAAE,GAAG;QACT,QAAQ,EAAE,IAAI;QACd,MAAM;QACN,QAAQ;QACR,MAAM,EAAE,mBAAmB;QAC3B,GAAG,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAChE,CAAC;AACJ,CAAC"}
|