@workos/authkit-session 0.4.0 → 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.
@@ -1,10 +1,8 @@
1
1
  import type { WorkOS } from '@workos-inc/node';
2
2
  import { AuthKitCore } from '../core/AuthKitCore.js';
3
3
  import type { AuthKitConfig } from '../core/config/types.js';
4
- import {
5
- getPKCECookieOptions,
6
- PKCE_COOKIE_NAME,
7
- } from '../core/pkce/cookieOptions.js';
4
+ import { getPKCECookieNameForState } from '../core/pkce/cookieName.js';
5
+ import { getPKCECookieOptions } from '../core/pkce/cookieOptions.js';
8
6
  import { AuthOperations } from '../operations/AuthOperations.js';
9
7
  import type {
10
8
  AuthResult,
@@ -229,15 +227,15 @@ export class AuthService<TRequest, TResponse> {
229
227
  response: TResponse | undefined,
230
228
  options: GetAuthorizationUrlOptions = {},
231
229
  ): Promise<CreateAuthorizationResult<TResponse>> {
232
- const { url, sealedState, cookieOptions } =
230
+ const { url, sealedState, cookieName, cookieOptions } =
233
231
  await this.operations.createAuthorization(options);
234
232
  const write = await this.storage.setCookie(
235
233
  response,
236
- PKCE_COOKIE_NAME,
234
+ cookieName,
237
235
  sealedState,
238
236
  cookieOptions,
239
237
  );
240
- return { url, ...write };
238
+ return { url, cookieName, ...write };
241
239
  }
242
240
 
243
241
  /**
@@ -267,24 +265,28 @@ export class AuthService<TRequest, TResponse> {
267
265
  }
268
266
 
269
267
  /**
270
- * Emit a `Set-Cookie` header that clears the PKCE verifier cookie.
268
+ * Emit a `Set-Cookie` header that clears the PKCE verifier cookie
269
+ * for the flow identified by `state`.
271
270
  *
272
- * Use on any exit path where a sign-in was started (verifier cookie
273
- * written) but `handleCallback` will not run to clear it — OAuth error
274
- * responses, missing `code`, early bail-outs.
271
+ * **Breaking change in 0.5.0.** The `state` option is now required
272
+ * — the per-flow cookie naming scheme has no single "legacy" name
273
+ * to clear. Callers typically read `state` from the callback URL;
274
+ * when `state` is absent (malformed callback), do not call this
275
+ * method. The 10-minute PKCE TTL cleans up orphans.
275
276
  *
276
277
  * Pass `options.redirectUri` on requests that used a per-request
277
- * `redirectUri` override at sign-in time, so the delete cookie's computed
278
- * attributes (notably `secure`) match what was originally set.
278
+ * `redirectUri` override at sign-in time, so the delete cookie's
279
+ * computed attributes (notably `secure`) match the original set.
279
280
  */
280
281
  async clearPendingVerifier(
281
282
  response: TResponse | undefined,
282
- options?: Pick<GetAuthorizationUrlOptions, 'redirectUri'>,
283
+ options: { state: string; redirectUri?: string },
283
284
  ): Promise<{ response?: TResponse; headers?: HeadersBag }> {
285
+ const cookieName = getPKCECookieNameForState(options.state);
284
286
  return this.storage.clearCookie(
285
287
  response,
286
- PKCE_COOKIE_NAME,
287
- getPKCECookieOptions(this.config, options?.redirectUri),
288
+ cookieName,
289
+ getPKCECookieOptions(this.config, options.redirectUri),
288
290
  );
289
291
  }
290
292
 
@@ -321,7 +323,12 @@ export class AuthService<TRequest, TResponse> {
321
323
  state: string | undefined;
322
324
  },
323
325
  ) {
324
- const cookieValue = await this.storage.getCookie(request, PKCE_COOKIE_NAME);
326
+ const cookieName = options.state
327
+ ? getPKCECookieNameForState(options.state)
328
+ : null;
329
+ const cookieValue = cookieName
330
+ ? await this.storage.getCookie(request, cookieName)
331
+ : null;
325
332
 
326
333
  let unsealed;
327
334
  try {
@@ -337,7 +344,7 @@ export class AuthService<TRequest, TResponse> {
337
344
  // case so the Set-Cookie is accepted over http:// callbacks too.
338
345
  // The `sameSite: 'none'` case already forces Secure on both set and
339
346
  // clear, so there's no scheme-mismatch risk there.
340
- await this.bestEffortClearVerifier(response, undefined, {
347
+ await this.bestEffortClearVerifier(response, cookieName, undefined, {
341
348
  schemeAgnostic: true,
342
349
  });
343
350
  throw err;
@@ -363,11 +370,15 @@ export class AuthService<TRequest, TResponse> {
363
370
 
364
371
  const encryptedSession = await this.core.encryptSession(session);
365
372
  const save = await this.storage.saveSession(response, encryptedSession);
366
- const clear = await this.storage.clearCookie(
367
- save.response ?? response,
368
- PKCE_COOKIE_NAME,
369
- clearOptions,
370
- );
373
+
374
+ let clear: { response?: TResponse; headers?: HeadersBag } = {};
375
+ if (cookieName) {
376
+ clear = await this.storage.clearCookie(
377
+ save.response ?? response,
378
+ cookieName,
379
+ clearOptions,
380
+ );
381
+ }
371
382
 
372
383
  return {
373
384
  response: clear.response ?? save.response,
@@ -377,7 +388,7 @@ export class AuthService<TRequest, TResponse> {
377
388
  authResponse,
378
389
  };
379
390
  } catch (err) {
380
- await this.bestEffortClearVerifier(response, redirectUri);
391
+ await this.bestEffortClearVerifier(response, cookieName, redirectUri);
381
392
  throw err;
382
393
  }
383
394
  }
@@ -397,15 +408,17 @@ export class AuthService<TRequest, TResponse> {
397
408
  */
398
409
  private async bestEffortClearVerifier(
399
410
  response: TResponse | undefined,
411
+ cookieName: string | null,
400
412
  redirectUri: string | undefined,
401
413
  { schemeAgnostic = false }: { schemeAgnostic?: boolean } = {},
402
414
  ): Promise<void> {
415
+ if (!cookieName) return; // nothing to clear — no state on the URL.
403
416
  const options = getPKCECookieOptions(this.config, redirectUri);
404
417
  if (schemeAgnostic && options.sameSite === 'lax') {
405
418
  options.secure = false;
406
419
  }
407
420
  try {
408
- await this.storage.clearCookie(response, PKCE_COOKIE_NAME, options);
421
+ await this.storage.clearCookie(response, cookieName, options);
409
422
  } catch {
410
423
  // Swallow: cleanup is opportunistic; callers get the original error.
411
424
  }