@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
|
@@ -1,9 +1,12 @@
|
|
|
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 { getPKCECookieNameForState } from '../core/pkce/cookieName.js';
|
|
5
|
+
import { getPKCECookieOptions } from '../core/pkce/cookieOptions.js';
|
|
4
6
|
import { AuthOperations } from '../operations/AuthOperations.js';
|
|
5
7
|
import type {
|
|
6
8
|
AuthResult,
|
|
9
|
+
CreateAuthorizationResult,
|
|
7
10
|
CustomClaims,
|
|
8
11
|
GetAuthorizationUrlOptions,
|
|
9
12
|
HeadersBag,
|
|
@@ -12,6 +15,39 @@ import type {
|
|
|
12
15
|
SessionStorage,
|
|
13
16
|
} from '../core/session/types.js';
|
|
14
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Merge two `HeadersBag` values. `Set-Cookie` matching is case-insensitive;
|
|
20
|
+
* existing key casing is preserved. Multiple `Set-Cookie` values are
|
|
21
|
+
* concatenated into a `string[]`. Other keys are shallow-merged (second wins).
|
|
22
|
+
*/
|
|
23
|
+
function mergeHeaderBags(
|
|
24
|
+
a: HeadersBag | undefined,
|
|
25
|
+
b: HeadersBag | undefined,
|
|
26
|
+
): HeadersBag | undefined {
|
|
27
|
+
if (!a) return b;
|
|
28
|
+
if (!b) return a;
|
|
29
|
+
const merged: HeadersBag = { ...a };
|
|
30
|
+
let setCookieKey = Object.keys(merged).find(
|
|
31
|
+
k => k.toLowerCase() === 'set-cookie',
|
|
32
|
+
);
|
|
33
|
+
for (const [key, value] of Object.entries(b)) {
|
|
34
|
+
if (key.toLowerCase() !== 'set-cookie') {
|
|
35
|
+
merged[key] = value;
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (!setCookieKey) {
|
|
39
|
+
merged[key] = value;
|
|
40
|
+
setCookieKey = key;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const left = merged[setCookieKey]!;
|
|
44
|
+
const leftArr = Array.isArray(left) ? left : [left];
|
|
45
|
+
const rightArr = Array.isArray(value) ? value : [value];
|
|
46
|
+
merged[setCookieKey] = [...leftArr, ...rightArr];
|
|
47
|
+
}
|
|
48
|
+
return merged;
|
|
49
|
+
}
|
|
50
|
+
|
|
15
51
|
/**
|
|
16
52
|
* Framework-agnostic authentication service.
|
|
17
53
|
*
|
|
@@ -23,7 +59,7 @@ import type {
|
|
|
23
59
|
* Provides common patterns:
|
|
24
60
|
* - `withAuth()` - Validate session with auto-refresh
|
|
25
61
|
* - `handleCallback()` - Process OAuth callback
|
|
26
|
-
* - `signOut()`, `
|
|
62
|
+
* - `signOut()`, `createSignIn()`, etc. - Delegate to AuthOperations
|
|
27
63
|
*
|
|
28
64
|
* **Used by:** @workos/authkit-tanstack-react-start
|
|
29
65
|
*/
|
|
@@ -44,7 +80,7 @@ export class AuthService<TRequest, TResponse> {
|
|
|
44
80
|
this.storage = storage;
|
|
45
81
|
this.client = client;
|
|
46
82
|
this.core = new AuthKitCore(config, client, encryption);
|
|
47
|
-
this.operations = new AuthOperations(this.core, client, config);
|
|
83
|
+
this.operations = new AuthOperations(this.core, client, config, encryption);
|
|
48
84
|
}
|
|
49
85
|
|
|
50
86
|
/**
|
|
@@ -179,28 +215,79 @@ export class AuthService<TRequest, TResponse> {
|
|
|
179
215
|
}
|
|
180
216
|
|
|
181
217
|
/**
|
|
182
|
-
*
|
|
218
|
+
* Create an authorization URL and write the PKCE verifier cookie.
|
|
219
|
+
*
|
|
220
|
+
* @param response - Framework-specific response object (may be undefined —
|
|
221
|
+
* adapters that mutate responses in-place will get back a mutated copy).
|
|
222
|
+
* @param options - screenHint, returnPathname, custom state, redirectUri, etc.
|
|
223
|
+
* @returns The URL to redirect the browser to, plus storage's
|
|
224
|
+
* `{ response?, headers? }` carrying the verifier `Set-Cookie`.
|
|
183
225
|
*/
|
|
184
|
-
async
|
|
185
|
-
|
|
226
|
+
async createAuthorization(
|
|
227
|
+
response: TResponse | undefined,
|
|
228
|
+
options: GetAuthorizationUrlOptions = {},
|
|
229
|
+
): Promise<CreateAuthorizationResult<TResponse>> {
|
|
230
|
+
const { url, sealedState, cookieName, cookieOptions } =
|
|
231
|
+
await this.operations.createAuthorization(options);
|
|
232
|
+
const write = await this.storage.setCookie(
|
|
233
|
+
response,
|
|
234
|
+
cookieName,
|
|
235
|
+
sealedState,
|
|
236
|
+
cookieOptions,
|
|
237
|
+
);
|
|
238
|
+
return { url, cookieName, ...write };
|
|
186
239
|
}
|
|
187
240
|
|
|
188
241
|
/**
|
|
189
|
-
* Convenience:
|
|
242
|
+
* Convenience: Create sign-in URL and write the PKCE verifier cookie.
|
|
190
243
|
*/
|
|
191
|
-
async
|
|
244
|
+
async createSignIn(
|
|
245
|
+
response: TResponse | undefined,
|
|
192
246
|
options: Omit<GetAuthorizationUrlOptions, 'screenHint'> = {},
|
|
193
|
-
) {
|
|
194
|
-
return this.
|
|
247
|
+
): Promise<CreateAuthorizationResult<TResponse>> {
|
|
248
|
+
return this.createAuthorization(response, {
|
|
249
|
+
...options,
|
|
250
|
+
screenHint: 'sign-in',
|
|
251
|
+
});
|
|
195
252
|
}
|
|
196
253
|
|
|
197
254
|
/**
|
|
198
|
-
* Convenience:
|
|
255
|
+
* Convenience: Create sign-up URL and write the PKCE verifier cookie.
|
|
199
256
|
*/
|
|
200
|
-
async
|
|
257
|
+
async createSignUp(
|
|
258
|
+
response: TResponse | undefined,
|
|
201
259
|
options: Omit<GetAuthorizationUrlOptions, 'screenHint'> = {},
|
|
202
|
-
) {
|
|
203
|
-
return this.
|
|
260
|
+
): Promise<CreateAuthorizationResult<TResponse>> {
|
|
261
|
+
return this.createAuthorization(response, {
|
|
262
|
+
...options,
|
|
263
|
+
screenHint: 'sign-up',
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Emit a `Set-Cookie` header that clears the PKCE verifier cookie
|
|
269
|
+
* for the flow identified by `state`.
|
|
270
|
+
*
|
|
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.
|
|
276
|
+
*
|
|
277
|
+
* Pass `options.redirectUri` on requests that used a per-request
|
|
278
|
+
* `redirectUri` override at sign-in time, so the delete cookie's
|
|
279
|
+
* computed attributes (notably `secure`) match the original set.
|
|
280
|
+
*/
|
|
281
|
+
async clearPendingVerifier(
|
|
282
|
+
response: TResponse | undefined,
|
|
283
|
+
options: { state: string; redirectUri?: string },
|
|
284
|
+
): Promise<{ response?: TResponse; headers?: HeadersBag }> {
|
|
285
|
+
const cookieName = getPKCECookieNameForState(options.state);
|
|
286
|
+
return this.storage.clearCookie(
|
|
287
|
+
response,
|
|
288
|
+
cookieName,
|
|
289
|
+
getPKCECookieOptions(this.config, options.redirectUri),
|
|
290
|
+
);
|
|
204
291
|
}
|
|
205
292
|
|
|
206
293
|
/**
|
|
@@ -213,76 +300,127 @@ export class AuthService<TRequest, TResponse> {
|
|
|
213
300
|
|
|
214
301
|
/**
|
|
215
302
|
* Handle OAuth callback.
|
|
216
|
-
* This creates a new session after successful authentication.
|
|
217
303
|
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
304
|
+
* Reads the verifier cookie via storage, verifies it against the URL
|
|
305
|
+
* `state`, exchanges the code, saves the session, and — on success —
|
|
306
|
+
* also emits a verifier-delete `Set-Cookie` so `HeadersBag['Set-Cookie']`
|
|
307
|
+
* carries both entries as a `string[]`. Adapters MUST append each
|
|
308
|
+
* `Set-Cookie` as its own header (never comma-join).
|
|
309
|
+
*
|
|
310
|
+
* Error-path cleanup: if any step after the cookie read throws
|
|
311
|
+
* (state mismatch, tampered seal, code-exchange failure, session-save
|
|
312
|
+
* failure), `handleCallback` attempts a best-effort `clearCookie` on the
|
|
313
|
+
* response before rethrowing so the verifier does not linger for its full
|
|
314
|
+
* 600s TTL. Response-mutating adapters get the delete `Set-Cookie`
|
|
315
|
+
* applied in place; headers-only adapters should still call
|
|
316
|
+
* `clearPendingVerifier` in their catch block to capture the headers bag.
|
|
222
317
|
*/
|
|
223
318
|
async handleCallback(
|
|
224
|
-
|
|
319
|
+
request: TRequest,
|
|
225
320
|
response: TResponse,
|
|
226
|
-
options: {
|
|
321
|
+
options: {
|
|
322
|
+
code: string;
|
|
323
|
+
state: string | undefined;
|
|
324
|
+
},
|
|
227
325
|
) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
326
|
+
const cookieName = options.state
|
|
327
|
+
? getPKCECookieNameForState(options.state)
|
|
328
|
+
: null;
|
|
329
|
+
const cookieValue = cookieName
|
|
330
|
+
? await this.storage.getCookie(request, cookieName)
|
|
331
|
+
: null;
|
|
233
332
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}
|
|
333
|
+
let unsealed;
|
|
334
|
+
try {
|
|
335
|
+
unsealed = await this.core.verifyCallbackState({
|
|
336
|
+
stateFromUrl: options.state,
|
|
337
|
+
cookieValue: cookieValue ?? undefined,
|
|
338
|
+
});
|
|
339
|
+
} catch (err) {
|
|
340
|
+
// Pre-unseal failure — we don't know the per-request redirectUri
|
|
341
|
+
// override, so emit a scheme-agnostic delete: same (name, domain,
|
|
342
|
+
// path) tuple as the original set (what the browser uses to match
|
|
343
|
+
// for replacement), with `Secure` dropped in the `sameSite: 'lax'`
|
|
344
|
+
// case so the Set-Cookie is accepted over http:// callbacks too.
|
|
345
|
+
// The `sameSite: 'none'` case already forces Secure on both set and
|
|
346
|
+
// clear, so there's no scheme-mismatch risk there.
|
|
347
|
+
await this.bestEffortClearVerifier(response, cookieName, undefined, {
|
|
348
|
+
schemeAgnostic: true,
|
|
349
|
+
});
|
|
350
|
+
throw err;
|
|
351
|
+
}
|
|
241
352
|
|
|
242
|
-
const
|
|
243
|
-
const
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
353
|
+
const { codeVerifier, returnPathname, customState, redirectUri } = unsealed;
|
|
354
|
+
const clearOptions = getPKCECookieOptions(this.config, redirectUri);
|
|
355
|
+
|
|
356
|
+
try {
|
|
357
|
+
const authResponse =
|
|
358
|
+
await this.client.userManagement.authenticateWithCode({
|
|
359
|
+
code: options.code,
|
|
360
|
+
clientId: this.config.clientId,
|
|
361
|
+
codeVerifier,
|
|
362
|
+
});
|
|
247
363
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
364
|
+
const session: Session = {
|
|
365
|
+
accessToken: authResponse.accessToken,
|
|
366
|
+
refreshToken: authResponse.refreshToken,
|
|
367
|
+
user: authResponse.user,
|
|
368
|
+
impersonator: authResponse.impersonator,
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
const encryptedSession = await this.core.encryptSession(session);
|
|
372
|
+
const save = await this.storage.saveSession(response, encryptedSession);
|
|
251
373
|
|
|
252
|
-
|
|
253
|
-
if (
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
.replace(/-/g, '+')
|
|
260
|
-
.replace(/_/g, '/');
|
|
261
|
-
const parsed = JSON.parse(atob(decoded));
|
|
262
|
-
returnPathname = parsed.returnPathname || '/';
|
|
263
|
-
} catch {
|
|
264
|
-
// Malformed internal state, use default
|
|
265
|
-
}
|
|
266
|
-
} else {
|
|
267
|
-
try {
|
|
268
|
-
const parsed = JSON.parse(atob(options.state));
|
|
269
|
-
if (parsed.returnPathname) {
|
|
270
|
-
returnPathname = parsed.returnPathname;
|
|
271
|
-
} else {
|
|
272
|
-
customState = options.state;
|
|
273
|
-
}
|
|
274
|
-
} catch {
|
|
275
|
-
customState = options.state;
|
|
276
|
-
}
|
|
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
|
+
);
|
|
277
381
|
}
|
|
382
|
+
|
|
383
|
+
return {
|
|
384
|
+
response: clear.response ?? save.response,
|
|
385
|
+
headers: mergeHeaderBags(save.headers, clear.headers),
|
|
386
|
+
returnPathname: returnPathname ?? '/',
|
|
387
|
+
state: customState,
|
|
388
|
+
authResponse,
|
|
389
|
+
};
|
|
390
|
+
} catch (err) {
|
|
391
|
+
await this.bestEffortClearVerifier(response, cookieName, redirectUri);
|
|
392
|
+
throw err;
|
|
278
393
|
}
|
|
394
|
+
}
|
|
279
395
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
396
|
+
/**
|
|
397
|
+
* Best-effort verifier cleanup on a `handleCallback` error path.
|
|
398
|
+
*
|
|
399
|
+
* Mutates the response in place for response-mutating adapters.
|
|
400
|
+
* Swallows storage errors — cleanup must never mask the original failure.
|
|
401
|
+
* The original error is always rethrown by the caller.
|
|
402
|
+
*
|
|
403
|
+
* `schemeAgnostic` (pre-unseal failures only): drops the `Secure` attribute
|
|
404
|
+
* on the delete when `sameSite === 'lax'` so the browser accepts the
|
|
405
|
+
* `Set-Cookie` over http:// dev callbacks. Cookie replacement matches on
|
|
406
|
+
* (name, domain, path), not `Secure`, so the tuple-matched original cookie
|
|
407
|
+
* is still cleared regardless of its original `Secure` flag.
|
|
408
|
+
*/
|
|
409
|
+
private async bestEffortClearVerifier(
|
|
410
|
+
response: TResponse | undefined,
|
|
411
|
+
cookieName: string | null,
|
|
412
|
+
redirectUri: string | undefined,
|
|
413
|
+
{ schemeAgnostic = false }: { schemeAgnostic?: boolean } = {},
|
|
414
|
+
): Promise<void> {
|
|
415
|
+
if (!cookieName) return; // nothing to clear — no state on the URL.
|
|
416
|
+
const options = getPKCECookieOptions(this.config, redirectUri);
|
|
417
|
+
if (schemeAgnostic && options.sameSite === 'lax') {
|
|
418
|
+
options.secure = false;
|
|
419
|
+
}
|
|
420
|
+
try {
|
|
421
|
+
await this.storage.clearCookie(response, cookieName, options);
|
|
422
|
+
} catch {
|
|
423
|
+
// Swallow: cleanup is opportunistic; callers get the original error.
|
|
424
|
+
}
|
|
287
425
|
}
|
|
288
426
|
}
|
|
@@ -40,9 +40,10 @@ describe('createAuthService', () => {
|
|
|
40
40
|
expect(typeof service.saveSession).toBe('function');
|
|
41
41
|
expect(typeof service.clearSession).toBe('function');
|
|
42
42
|
expect(typeof service.signOut).toBe('function');
|
|
43
|
-
expect(typeof service.
|
|
44
|
-
expect(typeof service.
|
|
45
|
-
expect(typeof service.
|
|
43
|
+
expect(typeof service.createAuthorization).toBe('function');
|
|
44
|
+
expect(typeof service.createSignIn).toBe('function');
|
|
45
|
+
expect(typeof service.createSignUp).toBe('function');
|
|
46
|
+
expect(typeof service.clearPendingVerifier).toBe('function');
|
|
46
47
|
expect(typeof service.getWorkOS).toBe('function');
|
|
47
48
|
expect(typeof service.handleCallback).toBe('function');
|
|
48
49
|
});
|
package/src/service/factory.ts
CHANGED
|
@@ -72,9 +72,12 @@ export function createAuthService<TRequest, TResponse>(options: {
|
|
|
72
72
|
getService().switchOrganization(session, organizationId),
|
|
73
73
|
refreshSession: (session, organizationId) =>
|
|
74
74
|
getService().refreshSession(session, organizationId),
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
createAuthorization: (response, opts) =>
|
|
76
|
+
getService().createAuthorization(response, opts),
|
|
77
|
+
createSignIn: (response, opts) => getService().createSignIn(response, opts),
|
|
78
|
+
createSignUp: (response, opts) => getService().createSignUp(response, opts),
|
|
79
|
+
clearPendingVerifier: (response, opts) =>
|
|
80
|
+
getService().clearPendingVerifier(response, opts),
|
|
78
81
|
getWorkOS: () => getService().getWorkOS(),
|
|
79
82
|
handleCallback: (request, response, opts) =>
|
|
80
83
|
getService().handleCallback(request, response, opts),
|
package/src/utils.ts
CHANGED
|
@@ -19,3 +19,25 @@ export function once<TArgs extends unknown[], TReturn>(
|
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Constant-time byte-array equality.
|
|
25
|
+
*
|
|
26
|
+
* Used for comparing security-sensitive values (OAuth state, tokens, MACs)
|
|
27
|
+
* where early-return on mismatch would leak timing information about how
|
|
28
|
+
* many leading bytes matched.
|
|
29
|
+
*
|
|
30
|
+
* Runtime-portable: no `node:crypto`, no `Buffer` — works in Node, browsers,
|
|
31
|
+
* and edge runtimes. Callers produce `Uint8Array`s via `TextEncoder`.
|
|
32
|
+
*
|
|
33
|
+
* Contract:
|
|
34
|
+
* - Returns false immediately if lengths differ (length is not secret).
|
|
35
|
+
* - Once past the length gate, inspects EVERY byte regardless of mismatches.
|
|
36
|
+
* - No branches whose count depends on byte values.
|
|
37
|
+
*/
|
|
38
|
+
export function constantTimeEqual(a: Uint8Array, b: Uint8Array): boolean {
|
|
39
|
+
if (a.length !== b.length) return false;
|
|
40
|
+
let diff = 0;
|
|
41
|
+
for (let i = 0; i < a.length; i++) diff |= a[i]! ^ b[i]!;
|
|
42
|
+
return diff === 0;
|
|
43
|
+
}
|