@workos/authkit-session 0.4.0 → 0.5.1
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 +23 -6
- 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 +0 -2
- package/dist/core/pkce/cookieOptions.d.ts.map +1 -1
- package/dist/core/pkce/cookieOptions.js +0 -2
- package/dist/core/pkce/cookieOptions.js.map +1 -1
- package/dist/core/pkce/generateAuthorizationUrl.d.ts +1 -0
- package/dist/core/pkce/generateAuthorizationUrl.d.ts.map +1 -1
- package/dist/core/pkce/generateAuthorizationUrl.js +5 -2
- package/dist/core/pkce/generateAuthorizationUrl.js.map +1 -1
- package/dist/core/session/types.d.ts +8 -3
- package/dist/core/session/types.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/service/AuthService.d.ts +13 -7
- package/dist/service/AuthService.d.ts.map +1 -1
- package/dist/service/AuthService.js +34 -18
- package/dist/service/AuthService.js.map +1 -1
- package/dist/utils.d.ts +12 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +26 -0
- package/dist/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/core/pkce/cookieName.spec.ts +56 -0
- package/src/core/pkce/cookieName.ts +27 -0
- package/src/core/pkce/cookieOptions.ts +0 -3
- package/src/core/pkce/generateAuthorizationUrl.ts +6 -6
- package/src/core/pkce/pkce.spec.ts +9 -0
- package/src/core/session/types.ts +8 -4
- package/src/index.ts +8 -0
- package/src/service/AuthService.spec.ts +227 -91
- package/src/service/AuthService.ts +40 -26
- package/src/utils.spec.ts +58 -1
- package/src/utils.ts +28 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** Stable prefix for all PKCE verifier cookies. */
|
|
2
|
+
export const PKCE_COOKIE_PREFIX = 'wos-auth-verifier';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* FNV-1a 32-bit hash of the input, returned as a zero-padded 8-char
|
|
6
|
+
* lowercase hex string. Used purely as a namespacing mechanism — not
|
|
7
|
+
* security-sensitive. Collision probability is ~1/4B per pair; a
|
|
8
|
+
* collision routes one flow's callback to the wrong cookie, which
|
|
9
|
+
* then fails byte-equality in `verifyCallbackState` (fail-closed).
|
|
10
|
+
*/
|
|
11
|
+
export function fnv1a32Hex(input: string): string {
|
|
12
|
+
let hash = 0x811c9dc5;
|
|
13
|
+
const bytes = new TextEncoder().encode(input);
|
|
14
|
+
for (const byte of bytes) {
|
|
15
|
+
hash = Math.imul(hash ^ byte, 0x01000193) >>> 0;
|
|
16
|
+
}
|
|
17
|
+
return hash.toString(16).padStart(8, '0');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Derive a flow-specific PKCE verifier cookie name from the sealed
|
|
22
|
+
* state blob. Each concurrent OAuth flow gets its own cookie so
|
|
23
|
+
* parallel sign-ins from multiple tabs don't clobber each other.
|
|
24
|
+
*/
|
|
25
|
+
export function getPKCECookieNameForState(state: string): string {
|
|
26
|
+
return `${PKCE_COOKIE_PREFIX}-${fnv1a32Hex(state)}`;
|
|
27
|
+
}
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import type { AuthKitConfig } from '../config/types.js';
|
|
2
2
|
import type { CookieOptions } from '../session/types.js';
|
|
3
3
|
|
|
4
|
-
/** Name of the PKCE verifier cookie on the wire. */
|
|
5
|
-
export const PKCE_COOKIE_NAME = 'wos-auth-verifier';
|
|
6
|
-
|
|
7
4
|
/**
|
|
8
5
|
* PKCE verifier cookie lifetime (seconds). Matches the 10-minute convention
|
|
9
6
|
* used by Arctic, openid-client, Clerk, and Okta for short-lived OAuth state
|
|
@@ -7,7 +7,8 @@ import type {
|
|
|
7
7
|
GetAuthorizationUrlOptions,
|
|
8
8
|
SessionEncryption,
|
|
9
9
|
} from '../session/types.js';
|
|
10
|
-
import {
|
|
10
|
+
import { getPKCECookieNameForState } from './cookieName.js';
|
|
11
|
+
import { getPKCECookieOptions } from './cookieOptions.js';
|
|
11
12
|
import { sealState } from './state.js';
|
|
12
13
|
|
|
13
14
|
/**
|
|
@@ -37,6 +38,7 @@ export const PKCE_MAX_COOKIE_BYTES = 3800;
|
|
|
37
38
|
export interface GeneratedAuthorizationUrl {
|
|
38
39
|
url: string;
|
|
39
40
|
sealedState: string;
|
|
41
|
+
cookieName: string;
|
|
40
42
|
cookieOptions: CookieOptions;
|
|
41
43
|
}
|
|
42
44
|
|
|
@@ -89,11 +91,8 @@ export async function generateAuthorizationUrl(params: {
|
|
|
89
91
|
// returnPathname combined with near-max state, or an unusually long
|
|
90
92
|
// cookieDomain attribute.
|
|
91
93
|
const cookieOptions = getPKCECookieOptions(config, redirectUri);
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
sealedState,
|
|
95
|
-
cookieOptions,
|
|
96
|
-
);
|
|
94
|
+
const cookieName = getPKCECookieNameForState(sealedState);
|
|
95
|
+
const serialized = serializeCookie(cookieName, sealedState, cookieOptions);
|
|
97
96
|
const cookieBytes = new TextEncoder().encode(serialized).byteLength;
|
|
98
97
|
if (cookieBytes > PKCE_MAX_COOKIE_BYTES) {
|
|
99
98
|
throw new PKCEPayloadTooLargeError(
|
|
@@ -118,6 +117,7 @@ export async function generateAuthorizationUrl(params: {
|
|
|
118
117
|
return {
|
|
119
118
|
url,
|
|
120
119
|
sealedState,
|
|
120
|
+
cookieName,
|
|
121
121
|
cookieOptions,
|
|
122
122
|
};
|
|
123
123
|
}
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
PKCECookieMissingError,
|
|
6
6
|
PKCEPayloadTooLargeError,
|
|
7
7
|
} from '../errors.js';
|
|
8
|
+
import { getPKCECookieNameForState } from './cookieName.js';
|
|
8
9
|
import {
|
|
9
10
|
generateAuthorizationUrl,
|
|
10
11
|
PKCE_MAX_COOKIE_BYTES,
|
|
@@ -89,6 +90,14 @@ describe('PKCE end-to-end round-trip', () => {
|
|
|
89
90
|
).rejects.toThrow(PKCECookieMissingError);
|
|
90
91
|
});
|
|
91
92
|
|
|
93
|
+
it('returns cookieName derived from the sealed state', async () => {
|
|
94
|
+
const result = await generate();
|
|
95
|
+
expect(result.cookieName).toBe(
|
|
96
|
+
getPKCECookieNameForState(result.sealedState),
|
|
97
|
+
);
|
|
98
|
+
expect(result.cookieName).toMatch(/^wos-auth-verifier-[0-9a-f]{8}$/);
|
|
99
|
+
});
|
|
100
|
+
|
|
92
101
|
it('concurrent sign-ins produce distinct sealedStates (cross-flow rejection)', async () => {
|
|
93
102
|
const core = makeCore();
|
|
94
103
|
const a = await generate();
|
|
@@ -176,13 +176,17 @@ export interface SessionEncryption {
|
|
|
176
176
|
* The verifier cookie is written internally via `SessionStorage.setCookie` — callers
|
|
177
177
|
* only need to redirect the browser to `url` and apply any returned `headers`/`response`.
|
|
178
178
|
*/
|
|
179
|
-
export
|
|
179
|
+
export type CreateAuthorizationResult<TResponse> = {
|
|
180
180
|
url: string;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
export type CreateAuthorizationResult<TResponse> = GetAuthorizationUrlResult & {
|
|
184
181
|
response?: TResponse;
|
|
185
182
|
headers?: HeadersBag;
|
|
183
|
+
/**
|
|
184
|
+
* Name of the PKCE verifier cookie written during this call. Useful
|
|
185
|
+
* for assertion-in-tests and for adapters that want to log the flow
|
|
186
|
+
* identifier. NOT the shape `clearPendingVerifier` consumes — that
|
|
187
|
+
* method takes `state`, not `cookieName`.
|
|
188
|
+
*/
|
|
189
|
+
cookieName: string;
|
|
186
190
|
};
|
|
187
191
|
|
|
188
192
|
export interface CookieOptions {
|
package/src/index.ts
CHANGED
|
@@ -37,6 +37,14 @@ export { AuthOperations } from './operations/AuthOperations.js';
|
|
|
37
37
|
// ============================================
|
|
38
38
|
export { CookieSessionStorage } from './core/session/CookieSessionStorage.js';
|
|
39
39
|
|
|
40
|
+
// ============================================
|
|
41
|
+
// PKCE Helpers
|
|
42
|
+
// ============================================
|
|
43
|
+
export {
|
|
44
|
+
PKCE_COOKIE_PREFIX,
|
|
45
|
+
getPKCECookieNameForState,
|
|
46
|
+
} from './core/pkce/cookieName.js';
|
|
47
|
+
|
|
40
48
|
// ============================================
|
|
41
49
|
// Encryption Fallback
|
|
42
50
|
// ============================================
|
|
@@ -224,6 +224,55 @@ describe('AuthService', () => {
|
|
|
224
224
|
});
|
|
225
225
|
|
|
226
226
|
describe('createAuthorization()', () => {
|
|
227
|
+
it('returns url + cookieName and writes under the derived per-flow name', async () => {
|
|
228
|
+
const realStorage = makeStorage();
|
|
229
|
+
const authService = new AuthService(
|
|
230
|
+
mockConfig as any,
|
|
231
|
+
realStorage as any,
|
|
232
|
+
makeClient() as any,
|
|
233
|
+
sessionEncryption,
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
const { url, cookieName, headers } = await authService.createSignIn(
|
|
237
|
+
undefined,
|
|
238
|
+
{
|
|
239
|
+
returnPathname: '/foo',
|
|
240
|
+
},
|
|
241
|
+
);
|
|
242
|
+
expect(url).toMatch(/^https:\/\//);
|
|
243
|
+
expect(cookieName).toMatch(/^wos-auth-verifier-[0-9a-f]{8}$/);
|
|
244
|
+
|
|
245
|
+
// Storage must have been called with the derived name, not the legacy name.
|
|
246
|
+
expect(realStorage.cookies.has('wos-auth-verifier')).toBe(false);
|
|
247
|
+
expect(realStorage.cookies.get(cookieName)).toBeTruthy();
|
|
248
|
+
|
|
249
|
+
// Set-Cookie header reflects the same name.
|
|
250
|
+
const setCookie = Array.isArray(headers?.['Set-Cookie'])
|
|
251
|
+
? headers!['Set-Cookie'].join('\n')
|
|
252
|
+
: (headers?.['Set-Cookie'] ?? '');
|
|
253
|
+
expect(setCookie).toContain(`${cookieName}=`);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('isolates concurrent flows: two sign-ins produce two distinct cookies', async () => {
|
|
257
|
+
const realStorage = makeStorage();
|
|
258
|
+
const authService = new AuthService(
|
|
259
|
+
mockConfig as any,
|
|
260
|
+
realStorage as any,
|
|
261
|
+
makeClient() as any,
|
|
262
|
+
sessionEncryption,
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
const a = await authService.createSignIn(undefined, {
|
|
266
|
+
returnPathname: '/a',
|
|
267
|
+
});
|
|
268
|
+
const b = await authService.createSignIn(undefined, {
|
|
269
|
+
returnPathname: '/b',
|
|
270
|
+
});
|
|
271
|
+
expect(a.cookieName).not.toBe(b.cookieName);
|
|
272
|
+
expect(realStorage.cookies.get(a.cookieName)).toBeTruthy();
|
|
273
|
+
expect(realStorage.cookies.get(b.cookieName)).toBeTruthy();
|
|
274
|
+
});
|
|
275
|
+
|
|
227
276
|
it('returns url and writes the verifier cookie via storage.setCookie', async () => {
|
|
228
277
|
const realStorage = makeStorage();
|
|
229
278
|
const realService = new AuthService(
|
|
@@ -236,11 +285,9 @@ describe('AuthService', () => {
|
|
|
236
285
|
const result = await realService.createAuthorization('res');
|
|
237
286
|
|
|
238
287
|
expect(result.url).toContain('authorize');
|
|
239
|
-
expect(realStorage.cookies.get(
|
|
240
|
-
expect(result.headers?.['Set-Cookie']).toContain(
|
|
241
|
-
expect(realStorage.lastSetOptions.get(
|
|
242
|
-
'/',
|
|
243
|
-
);
|
|
288
|
+
expect(realStorage.cookies.get(result.cookieName)).toBeTruthy();
|
|
289
|
+
expect(result.headers?.['Set-Cookie']).toContain(`${result.cookieName}=`);
|
|
290
|
+
expect(realStorage.lastSetOptions.get(result.cookieName)?.path).toBe('/');
|
|
244
291
|
});
|
|
245
292
|
});
|
|
246
293
|
|
|
@@ -257,7 +304,7 @@ describe('AuthService', () => {
|
|
|
257
304
|
const result = await realService.createSignIn('res');
|
|
258
305
|
|
|
259
306
|
expect(result.url).toContain('screen_hint=sign-in');
|
|
260
|
-
expect(realStorage.cookies.get(
|
|
307
|
+
expect(realStorage.cookies.get(result.cookieName)).toBeTruthy();
|
|
261
308
|
});
|
|
262
309
|
});
|
|
263
310
|
|
|
@@ -274,7 +321,7 @@ describe('AuthService', () => {
|
|
|
274
321
|
const result = await realService.createSignUp('res');
|
|
275
322
|
|
|
276
323
|
expect(result.url).toContain('screen_hint=sign-up');
|
|
277
|
-
expect(realStorage.cookies.get(
|
|
324
|
+
expect(realStorage.cookies.get(result.cookieName)).toBeTruthy();
|
|
278
325
|
});
|
|
279
326
|
});
|
|
280
327
|
|
|
@@ -288,11 +335,12 @@ describe('AuthService', () => {
|
|
|
288
335
|
sessionEncryption,
|
|
289
336
|
);
|
|
290
337
|
|
|
291
|
-
await realService.
|
|
338
|
+
const { cookieName } = await realService.createSignIn(undefined);
|
|
339
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
292
340
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
);
|
|
341
|
+
await realService.clearPendingVerifier('res', { state: sealedState });
|
|
342
|
+
|
|
343
|
+
expect(realStorage.lastClearOptions.get(cookieName)?.path).toBe('/');
|
|
296
344
|
});
|
|
297
345
|
|
|
298
346
|
it('accepts undefined response for headers-only adapters', async () => {
|
|
@@ -304,9 +352,58 @@ describe('AuthService', () => {
|
|
|
304
352
|
sessionEncryption,
|
|
305
353
|
);
|
|
306
354
|
|
|
307
|
-
const
|
|
355
|
+
const { cookieName } = await realService.createSignIn(undefined);
|
|
356
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
357
|
+
|
|
358
|
+
const result = await realService.clearPendingVerifier(undefined, {
|
|
359
|
+
state: sealedState,
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
expect(result.headers?.['Set-Cookie']).toContain(`${cookieName}=`);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it('clears the flow-specific cookie derived from state', async () => {
|
|
366
|
+
const realStorage = makeStorage();
|
|
367
|
+
const realService = new AuthService(
|
|
368
|
+
mockConfig as any,
|
|
369
|
+
realStorage as any,
|
|
370
|
+
makeClient() as any,
|
|
371
|
+
sessionEncryption,
|
|
372
|
+
);
|
|
373
|
+
|
|
374
|
+
const { cookieName } = await realService.createSignIn(undefined);
|
|
375
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
376
|
+
|
|
377
|
+
const clearCookieSpy = vi.spyOn(realStorage, 'clearCookie');
|
|
378
|
+
await realService.clearPendingVerifier(undefined, { state: sealedState });
|
|
308
379
|
|
|
309
|
-
expect(
|
|
380
|
+
expect(clearCookieSpy).toHaveBeenCalledWith(
|
|
381
|
+
undefined,
|
|
382
|
+
cookieName,
|
|
383
|
+
expect.any(Object),
|
|
384
|
+
);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it('threads redirectUri into the cookie options', async () => {
|
|
388
|
+
const realStorage = makeStorage();
|
|
389
|
+
const realService = new AuthService(
|
|
390
|
+
mockConfig as any,
|
|
391
|
+
realStorage as any,
|
|
392
|
+
makeClient() as any,
|
|
393
|
+
sessionEncryption,
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
const { cookieName } = await realService.createSignIn(undefined, {
|
|
397
|
+
redirectUri: 'https://custom.example/cb',
|
|
398
|
+
});
|
|
399
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
400
|
+
|
|
401
|
+
await realService.clearPendingVerifier(undefined, {
|
|
402
|
+
state: sealedState,
|
|
403
|
+
redirectUri: 'https://custom.example/cb',
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
expect(realStorage.lastClearOptions.get(cookieName)?.path).toBe('/');
|
|
310
407
|
});
|
|
311
408
|
});
|
|
312
409
|
|
|
@@ -330,11 +427,11 @@ describe('AuthService', () => {
|
|
|
330
427
|
sessionEncryption,
|
|
331
428
|
);
|
|
332
429
|
|
|
333
|
-
await realService.createAuthorization('res', {
|
|
430
|
+
const { cookieName } = await realService.createAuthorization('res', {
|
|
334
431
|
returnPathname: '/dashboard',
|
|
335
432
|
state: 'my.custom.state',
|
|
336
433
|
});
|
|
337
|
-
const sealedState = realStorage.cookies.get(
|
|
434
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
338
435
|
|
|
339
436
|
const result = await realService.handleCallback('req', 'res', {
|
|
340
437
|
code: 'auth-code-xyz',
|
|
@@ -356,8 +453,8 @@ describe('AuthService', () => {
|
|
|
356
453
|
sessionEncryption,
|
|
357
454
|
);
|
|
358
455
|
|
|
359
|
-
await realService.createAuthorization('res');
|
|
360
|
-
const sealedState = realStorage.cookies.get(
|
|
456
|
+
const { cookieName } = await realService.createAuthorization('res');
|
|
457
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
361
458
|
|
|
362
459
|
const result = await realService.handleCallback('req', 'res', {
|
|
363
460
|
code: 'code',
|
|
@@ -371,10 +468,10 @@ describe('AuthService', () => {
|
|
|
371
468
|
(setCookie as string[]).some(c => c.startsWith('wos-session=')),
|
|
372
469
|
).toBe(true);
|
|
373
470
|
expect(
|
|
374
|
-
(setCookie as string[]).some(c => c.startsWith(
|
|
471
|
+
(setCookie as string[]).some(c => c.startsWith(`${cookieName}=`)),
|
|
375
472
|
).toBe(true);
|
|
376
473
|
expect(
|
|
377
|
-
(setCookie as string[]).find(c => c.startsWith(
|
|
474
|
+
(setCookie as string[]).find(c => c.startsWith(`${cookieName}=`)),
|
|
378
475
|
).toContain('Max-Age=0');
|
|
379
476
|
});
|
|
380
477
|
|
|
@@ -421,8 +518,8 @@ describe('AuthService', () => {
|
|
|
421
518
|
sessionEncryption,
|
|
422
519
|
);
|
|
423
520
|
|
|
424
|
-
await realService.createAuthorization('res');
|
|
425
|
-
const sealedState = realStorage.cookies.get(
|
|
521
|
+
const { cookieName } = await realService.createAuthorization('res');
|
|
522
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
426
523
|
|
|
427
524
|
const result = await realService.handleCallback('req', 'res', {
|
|
428
525
|
code: 'code',
|
|
@@ -436,7 +533,7 @@ describe('AuthService', () => {
|
|
|
436
533
|
(setCookie as string[]).some(c => c.startsWith('wos-session=')),
|
|
437
534
|
).toBe(true);
|
|
438
535
|
expect(
|
|
439
|
-
(setCookie as string[]).some(c => c.startsWith(
|
|
536
|
+
(setCookie as string[]).some(c => c.startsWith(`${cookieName}=`)),
|
|
440
537
|
).toBe(true);
|
|
441
538
|
});
|
|
442
539
|
|
|
@@ -449,17 +546,15 @@ describe('AuthService', () => {
|
|
|
449
546
|
sessionEncryption,
|
|
450
547
|
);
|
|
451
548
|
|
|
452
|
-
await realService.createAuthorization('res');
|
|
453
|
-
const sealedState = realStorage.cookies.get(
|
|
549
|
+
const { cookieName } = await realService.createAuthorization('res');
|
|
550
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
454
551
|
|
|
455
552
|
await realService.handleCallback('req', 'res', {
|
|
456
553
|
code: 'code',
|
|
457
554
|
state: sealedState,
|
|
458
555
|
});
|
|
459
556
|
|
|
460
|
-
expect(realStorage.lastClearOptions.get(
|
|
461
|
-
'/',
|
|
462
|
-
);
|
|
557
|
+
expect(realStorage.lastClearOptions.get(cookieName)?.path).toBe('/');
|
|
463
558
|
});
|
|
464
559
|
|
|
465
560
|
it('throws OAuthStateMismatchError when storage cookie differs from url state', async () => {
|
|
@@ -471,13 +566,11 @@ describe('AuthService', () => {
|
|
|
471
566
|
sessionEncryption,
|
|
472
567
|
);
|
|
473
568
|
|
|
474
|
-
await realService.createAuthorization('res');
|
|
475
|
-
const sealedState = realStorage.cookies.get(
|
|
476
|
-
// Tamper the stored cookie
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
sealedState.slice(0, -2) + 'XX',
|
|
480
|
-
);
|
|
569
|
+
const { cookieName } = await realService.createAuthorization('res');
|
|
570
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
571
|
+
// Tamper the stored cookie under the derived name that
|
|
572
|
+
// handleCallback reads.
|
|
573
|
+
realStorage.cookies.set(cookieName, sealedState.slice(0, -2) + 'XX');
|
|
481
574
|
|
|
482
575
|
await expect(
|
|
483
576
|
realService.handleCallback('req', 'res', {
|
|
@@ -496,9 +589,11 @@ describe('AuthService', () => {
|
|
|
496
589
|
sessionEncryption,
|
|
497
590
|
);
|
|
498
591
|
|
|
499
|
-
await realService.createAuthorization('res');
|
|
500
|
-
const sealedState = realStorage.cookies.get(
|
|
501
|
-
|
|
592
|
+
const { cookieName } = await realService.createAuthorization('res');
|
|
593
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
594
|
+
// Delete the derived-name cookie so handleCallback's read finds
|
|
595
|
+
// nothing — the scenario under test.
|
|
596
|
+
realStorage.cookies.delete(cookieName);
|
|
502
597
|
|
|
503
598
|
await expect(
|
|
504
599
|
realService.handleCallback('req', 'res', {
|
|
@@ -536,8 +631,8 @@ describe('AuthService', () => {
|
|
|
536
631
|
sessionEncryption,
|
|
537
632
|
);
|
|
538
633
|
|
|
539
|
-
await realService.createAuthorization('res');
|
|
540
|
-
const sealedState = realStorage.cookies.get(
|
|
634
|
+
const { cookieName } = await realService.createAuthorization('res');
|
|
635
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
541
636
|
|
|
542
637
|
const result = await realService.handleCallback('req', 'res', {
|
|
543
638
|
code: 'c',
|
|
@@ -556,12 +651,9 @@ describe('AuthService', () => {
|
|
|
556
651
|
sessionEncryption,
|
|
557
652
|
);
|
|
558
653
|
|
|
559
|
-
await realService.createAuthorization('res');
|
|
560
|
-
const sealedState = realStorage.cookies.get(
|
|
561
|
-
realStorage.cookies.set(
|
|
562
|
-
'wos-auth-verifier',
|
|
563
|
-
sealedState.slice(0, -2) + 'XX',
|
|
564
|
-
);
|
|
654
|
+
const { cookieName } = await realService.createAuthorization('res');
|
|
655
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
656
|
+
realStorage.cookies.set(cookieName, sealedState.slice(0, -2) + 'XX');
|
|
565
657
|
|
|
566
658
|
await expect(
|
|
567
659
|
realService.handleCallback('req', 'res', {
|
|
@@ -570,9 +662,7 @@ describe('AuthService', () => {
|
|
|
570
662
|
}),
|
|
571
663
|
).rejects.toThrow(OAuthStateMismatchError);
|
|
572
664
|
|
|
573
|
-
expect(realStorage.lastClearOptions.get(
|
|
574
|
-
'/',
|
|
575
|
-
);
|
|
665
|
+
expect(realStorage.lastClearOptions.get(cookieName)?.path).toBe('/');
|
|
576
666
|
});
|
|
577
667
|
|
|
578
668
|
it('emits a scheme-agnostic (secure=false, sameSite=lax) delete on pre-unseal failure', async () => {
|
|
@@ -589,19 +679,14 @@ describe('AuthService', () => {
|
|
|
589
679
|
sessionEncryption,
|
|
590
680
|
);
|
|
591
681
|
|
|
592
|
-
await realService.createAuthorization('res', {
|
|
682
|
+
const { cookieName } = await realService.createAuthorization('res', {
|
|
593
683
|
redirectUri: 'http://localhost:3000/callback',
|
|
594
684
|
});
|
|
595
685
|
// Confirm the original set was secure=false (the scenario we're
|
|
596
686
|
// proving we can still clean up).
|
|
597
|
-
expect(realStorage.lastSetOptions.get(
|
|
598
|
-
|
|
599
|
-
);
|
|
600
|
-
const sealedState = realStorage.cookies.get('wos-auth-verifier')!;
|
|
601
|
-
realStorage.cookies.set(
|
|
602
|
-
'wos-auth-verifier',
|
|
603
|
-
sealedState.slice(0, -2) + 'XX',
|
|
604
|
-
);
|
|
687
|
+
expect(realStorage.lastSetOptions.get(cookieName)?.secure).toBe(false);
|
|
688
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
689
|
+
realStorage.cookies.set(cookieName, sealedState.slice(0, -2) + 'XX');
|
|
605
690
|
|
|
606
691
|
await expect(
|
|
607
692
|
realService.handleCallback('req', 'res', {
|
|
@@ -610,7 +695,7 @@ describe('AuthService', () => {
|
|
|
610
695
|
}),
|
|
611
696
|
).rejects.toThrow(OAuthStateMismatchError);
|
|
612
697
|
|
|
613
|
-
const clearOpts = realStorage.lastClearOptions.get(
|
|
698
|
+
const clearOpts = realStorage.lastClearOptions.get(cookieName);
|
|
614
699
|
expect(clearOpts?.secure).toBe(false);
|
|
615
700
|
expect(clearOpts?.sameSite).toBe('lax');
|
|
616
701
|
expect(clearOpts?.path).toBe('/');
|
|
@@ -625,12 +710,9 @@ describe('AuthService', () => {
|
|
|
625
710
|
sessionEncryption,
|
|
626
711
|
);
|
|
627
712
|
|
|
628
|
-
await realService.createAuthorization('res');
|
|
629
|
-
const sealedState = realStorage.cookies.get(
|
|
630
|
-
realStorage.cookies.set(
|
|
631
|
-
'wos-auth-verifier',
|
|
632
|
-
sealedState.slice(0, -2) + 'XX',
|
|
633
|
-
);
|
|
713
|
+
const { cookieName } = await realService.createAuthorization('res');
|
|
714
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
715
|
+
realStorage.cookies.set(cookieName, sealedState.slice(0, -2) + 'XX');
|
|
634
716
|
|
|
635
717
|
await expect(
|
|
636
718
|
realService.handleCallback('req', 'res', {
|
|
@@ -639,7 +721,7 @@ describe('AuthService', () => {
|
|
|
639
721
|
}),
|
|
640
722
|
).rejects.toThrow(OAuthStateMismatchError);
|
|
641
723
|
|
|
642
|
-
const clearOpts = realStorage.lastClearOptions.get(
|
|
724
|
+
const clearOpts = realStorage.lastClearOptions.get(cookieName);
|
|
643
725
|
expect(clearOpts?.secure).toBe(true);
|
|
644
726
|
expect(clearOpts?.sameSite).toBe('none');
|
|
645
727
|
});
|
|
@@ -653,9 +735,11 @@ describe('AuthService', () => {
|
|
|
653
735
|
sessionEncryption,
|
|
654
736
|
);
|
|
655
737
|
|
|
656
|
-
await realService.createAuthorization('res');
|
|
657
|
-
const sealedState = realStorage.cookies.get(
|
|
658
|
-
|
|
738
|
+
const { cookieName } = await realService.createAuthorization('res');
|
|
739
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
740
|
+
// Delete the derived-name cookie so handleCallback's read returns
|
|
741
|
+
// null and triggers the missing-cookie path under test.
|
|
742
|
+
realStorage.cookies.delete(cookieName);
|
|
659
743
|
|
|
660
744
|
await expect(
|
|
661
745
|
realService.handleCallback('req', 'res', {
|
|
@@ -664,9 +748,7 @@ describe('AuthService', () => {
|
|
|
664
748
|
}),
|
|
665
749
|
).rejects.toThrow(PKCECookieMissingError);
|
|
666
750
|
|
|
667
|
-
expect(realStorage.lastClearOptions.get(
|
|
668
|
-
'/',
|
|
669
|
-
);
|
|
751
|
+
expect(realStorage.lastClearOptions.get(cookieName)?.path).toBe('/');
|
|
670
752
|
});
|
|
671
753
|
|
|
672
754
|
it('best-effort clears the verifier cookie when authenticateWithCode throws', async () => {
|
|
@@ -682,8 +764,8 @@ describe('AuthService', () => {
|
|
|
682
764
|
sessionEncryption,
|
|
683
765
|
);
|
|
684
766
|
|
|
685
|
-
await realService.createAuthorization('res');
|
|
686
|
-
const sealedState = realStorage.cookies.get(
|
|
767
|
+
const { cookieName } = await realService.createAuthorization('res');
|
|
768
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
687
769
|
|
|
688
770
|
await expect(
|
|
689
771
|
realService.handleCallback('req', 'res', {
|
|
@@ -692,9 +774,7 @@ describe('AuthService', () => {
|
|
|
692
774
|
}),
|
|
693
775
|
).rejects.toThrow('WorkOS exchange failed');
|
|
694
776
|
|
|
695
|
-
expect(realStorage.lastClearOptions.get(
|
|
696
|
-
'/',
|
|
697
|
-
);
|
|
777
|
+
expect(realStorage.lastClearOptions.get(cookieName)?.path).toBe('/');
|
|
698
778
|
});
|
|
699
779
|
|
|
700
780
|
it('best-effort clears the verifier cookie when saveSession throws', async () => {
|
|
@@ -709,8 +789,8 @@ describe('AuthService', () => {
|
|
|
709
789
|
sessionEncryption,
|
|
710
790
|
);
|
|
711
791
|
|
|
712
|
-
await realService.createAuthorization('res');
|
|
713
|
-
const sealedState = realStorage.cookies.get(
|
|
792
|
+
const { cookieName } = await realService.createAuthorization('res');
|
|
793
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
714
794
|
|
|
715
795
|
await expect(
|
|
716
796
|
realService.handleCallback('req', 'res', {
|
|
@@ -719,9 +799,7 @@ describe('AuthService', () => {
|
|
|
719
799
|
}),
|
|
720
800
|
).rejects.toThrow('storage write failed');
|
|
721
801
|
|
|
722
|
-
expect(realStorage.lastClearOptions.get(
|
|
723
|
-
'/',
|
|
724
|
-
);
|
|
802
|
+
expect(realStorage.lastClearOptions.get(cookieName)?.path).toBe('/');
|
|
725
803
|
});
|
|
726
804
|
|
|
727
805
|
it('swallows clearCookie errors so the original failure propagates', async () => {
|
|
@@ -740,8 +818,8 @@ describe('AuthService', () => {
|
|
|
740
818
|
sessionEncryption,
|
|
741
819
|
);
|
|
742
820
|
|
|
743
|
-
await realService.createAuthorization('res');
|
|
744
|
-
const sealedState = realStorage.cookies.get(
|
|
821
|
+
const { cookieName } = await realService.createAuthorization('res');
|
|
822
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
745
823
|
|
|
746
824
|
await expect(
|
|
747
825
|
realService.handleCallback('req', 'res', {
|
|
@@ -762,23 +840,81 @@ describe('AuthService', () => {
|
|
|
762
840
|
|
|
763
841
|
// Override with an http:// URL so the `secure` attribute differs from
|
|
764
842
|
// the https:// default — proves handleCallback used the override.
|
|
765
|
-
await realService.createAuthorization('res', {
|
|
843
|
+
const { cookieName } = await realService.createAuthorization('res', {
|
|
766
844
|
redirectUri: 'http://localhost:3000/callback',
|
|
767
845
|
});
|
|
768
|
-
const sealedState = realStorage.cookies.get(
|
|
846
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
769
847
|
|
|
770
|
-
expect(realStorage.lastSetOptions.get(
|
|
771
|
-
false,
|
|
772
|
-
);
|
|
848
|
+
expect(realStorage.lastSetOptions.get(cookieName)?.secure).toBe(false);
|
|
773
849
|
|
|
774
850
|
await realService.handleCallback('req', 'res', {
|
|
775
851
|
code: 'code',
|
|
776
852
|
state: sealedState,
|
|
777
853
|
});
|
|
778
854
|
|
|
779
|
-
expect(
|
|
780
|
-
|
|
781
|
-
|
|
855
|
+
expect(realStorage.lastClearOptions.get(cookieName)?.secure).toBe(false);
|
|
856
|
+
});
|
|
857
|
+
|
|
858
|
+
describe('per-flow cookie isolation', () => {
|
|
859
|
+
it('reads and clears the cookie derived from URL state', async () => {
|
|
860
|
+
// Start a flow. createSignIn wrote under the derived name.
|
|
861
|
+
const realStorage = makeStorage();
|
|
862
|
+
const authService = new AuthService(
|
|
863
|
+
mockConfig as any,
|
|
864
|
+
realStorage as any,
|
|
865
|
+
makeClient() as any,
|
|
866
|
+
sessionEncryption,
|
|
867
|
+
);
|
|
868
|
+
const { cookieName } = await authService.createSignIn(undefined);
|
|
869
|
+
const sealedState = realStorage.cookies.get(cookieName)!;
|
|
870
|
+
|
|
871
|
+
const result = await authService.handleCallback(
|
|
872
|
+
'req' as never,
|
|
873
|
+
'res' as never,
|
|
874
|
+
{ code: 'abc', state: sealedState },
|
|
875
|
+
);
|
|
876
|
+
|
|
877
|
+
const setCookies = Array.isArray(result.headers?.['Set-Cookie'])
|
|
878
|
+
? (result.headers!['Set-Cookie'] as string[])
|
|
879
|
+
: ([result.headers?.['Set-Cookie']].filter(Boolean) as string[]);
|
|
880
|
+
const deleteLine = setCookies.find(c => c.startsWith(`${cookieName}=`));
|
|
881
|
+
expect(deleteLine).toBeDefined();
|
|
882
|
+
expect(deleteLine).toContain('Max-Age=0');
|
|
883
|
+
});
|
|
884
|
+
|
|
885
|
+
it("does not touch another concurrent flow's cookie", async () => {
|
|
886
|
+
const realStorage = makeStorage();
|
|
887
|
+
const authService = new AuthService(
|
|
888
|
+
mockConfig as any,
|
|
889
|
+
realStorage as any,
|
|
890
|
+
makeClient() as any,
|
|
891
|
+
sessionEncryption,
|
|
892
|
+
);
|
|
893
|
+
const a = await authService.createSignIn(undefined, {
|
|
894
|
+
returnPathname: '/a',
|
|
895
|
+
});
|
|
896
|
+
const b = await authService.createSignIn(undefined, {
|
|
897
|
+
returnPathname: '/b',
|
|
898
|
+
});
|
|
899
|
+
const sealedA = realStorage.cookies.get(a.cookieName)!;
|
|
900
|
+
|
|
901
|
+
const result = await authService.handleCallback(
|
|
902
|
+
'req' as never,
|
|
903
|
+
'res' as never,
|
|
904
|
+
{ code: 'abc', state: sealedA },
|
|
905
|
+
);
|
|
906
|
+
|
|
907
|
+
const setCookies = Array.isArray(result.headers?.['Set-Cookie'])
|
|
908
|
+
? (result.headers!['Set-Cookie'] as string[])
|
|
909
|
+
: ([result.headers?.['Set-Cookie']].filter(Boolean) as string[]);
|
|
910
|
+
// Flow A's cookie gets a delete. Flow B's cookie must NOT.
|
|
911
|
+
expect(setCookies.some(c => c.startsWith(`${a.cookieName}=`))).toBe(
|
|
912
|
+
true,
|
|
913
|
+
);
|
|
914
|
+
expect(setCookies.some(c => c.startsWith(`${b.cookieName}=`))).toBe(
|
|
915
|
+
false,
|
|
916
|
+
);
|
|
917
|
+
});
|
|
782
918
|
});
|
|
783
919
|
});
|
|
784
920
|
});
|