toiljs 0.0.89 → 0.0.91
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/CHANGELOG.md +5 -0
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/auth.d.ts +20 -0
- package/build/client/auth.js +55 -3
- package/build/devserver/.tsbuildinfo +1 -1
- package/build/devserver/analytics/index.js +2 -2
- package/examples/basic/server/models/SiteAnalytics.ts +3 -1
- package/package.json +1 -1
- package/server/auth/AuthController.ts +390 -0
- package/src/client/auth.ts +114 -4
- package/src/devserver/analytics/index.ts +10 -6
- package/test/analytics-dev.test.ts +15 -0
- package/test/pqauth-email.test.ts +305 -1
|
@@ -24,7 +24,7 @@ import { __resetDbForTests } from '../src/devserver/db/index.js';
|
|
|
24
24
|
import { __resetRatelimitForTests } from '../src/devserver/config/ratelimit.js';
|
|
25
25
|
import { clearEnvCache } from '../src/devserver/config/dotenv.js';
|
|
26
26
|
import { __sentEmails, __clearSentEmails } from '../src/devserver/email/index.js';
|
|
27
|
-
import { Auth, EmailNotConfirmedError } from '../src/client/auth.js';
|
|
27
|
+
import { Auth, EmailNotConfirmedError, TwoFactorRequiredError } from '../src/client/auth.js';
|
|
28
28
|
import { DataReader, DataWriter } from '../src/io/codec.js';
|
|
29
29
|
|
|
30
30
|
const EXAMPLE_WASM = path.resolve(
|
|
@@ -117,6 +117,21 @@ function tokenFromEmail(kind: 'confirm' | 'reset', to: string): string {
|
|
|
117
117
|
);
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
+
/** Pull the 6-digit code out of the most-recent captured 2FA email (purpose `2fa`) to `to`. */
|
|
121
|
+
function codeFromEmail(to: string): string {
|
|
122
|
+
for (let i = __sentEmails.length - 1; i >= 0; i--) {
|
|
123
|
+
const msg = __sentEmails[i];
|
|
124
|
+
if (msg.to !== to || msg.purpose !== '2fa') continue;
|
|
125
|
+
const hit = /\b(\d{6})\b/.exec(msg.text) ?? /\b(\d{6})\b/.exec(msg.html);
|
|
126
|
+
if (hit) return hit[1];
|
|
127
|
+
}
|
|
128
|
+
throw new Error(
|
|
129
|
+
`no 2fa code emailed to ${to}; captured=${JSON.stringify(
|
|
130
|
+
__sentEmails.map((e) => ({ to: e.to, purpose: e.purpose })),
|
|
131
|
+
)}`,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
120
135
|
describe.skipIf(!haveWasm)('built-in auth: email verification + password reset (client <-> example wasm)', () => {
|
|
121
136
|
let restoreFetch: () => void;
|
|
122
137
|
let mod: WasmServerModule;
|
|
@@ -330,4 +345,293 @@ describe.skipIf(!haveWasm)('built-in auth: email verification + password reset (
|
|
|
330
345
|
},
|
|
331
346
|
60_000,
|
|
332
347
|
);
|
|
348
|
+
|
|
349
|
+
// ---- multi-method 2FA (email) ----
|
|
350
|
+
|
|
351
|
+
it(
|
|
352
|
+
'(a) full email-2FA login round-trip: enable, login requires a code, verify mints the session',
|
|
353
|
+
async () => {
|
|
354
|
+
await Auth.register('dave', 'dave-pw-strong', 'dave@x.com');
|
|
355
|
+
await Auth.login('dave', 'dave-pw-strong'); // 2FA off -> session
|
|
356
|
+
|
|
357
|
+
// Enable email 2FA: setup delivers a code, confirm switches the method on.
|
|
358
|
+
await Auth.setupTwoFactor(Auth.TwoFactorMethod.Email);
|
|
359
|
+
await Auth.confirmTwoFactorSetup(codeFromEmail('dave@x.com'));
|
|
360
|
+
expect(await Auth.twoFactorStatus()).toBe(Auth.TwoFactorMethod.Email);
|
|
361
|
+
|
|
362
|
+
__clearSentEmails();
|
|
363
|
+
jar.clear(); // drop the existing session so login is a clean 2FA challenge
|
|
364
|
+
|
|
365
|
+
// Login now demands a second factor: mutual auth still passes (login()
|
|
366
|
+
// verifies serverConfirm before throwing), but NO session is minted.
|
|
367
|
+
let err: unknown;
|
|
368
|
+
try {
|
|
369
|
+
await Auth.login('dave', 'dave-pw-strong');
|
|
370
|
+
} catch (e) {
|
|
371
|
+
err = e;
|
|
372
|
+
}
|
|
373
|
+
expect(err).toBeInstanceOf(TwoFactorRequiredError);
|
|
374
|
+
const twoFaId = (err as TwoFactorRequiredError).twoFaId;
|
|
375
|
+
expect(twoFaId.length).toBeGreaterThan(0);
|
|
376
|
+
expect(jar.get('toil_sess')).toBeUndefined(); // no session at login/finish
|
|
377
|
+
|
|
378
|
+
// Read the login code out of the captured email and verify -> session.
|
|
379
|
+
const session = await Auth.verifyTwoFactor(twoFaId, codeFromEmail('dave@x.com'));
|
|
380
|
+
expect(session.length).toBeGreaterThan(0);
|
|
381
|
+
|
|
382
|
+
// /auth/me now returns the user.
|
|
383
|
+
const meRes = await fetch('/auth/me');
|
|
384
|
+
expect(meRes.status).toBe(200);
|
|
385
|
+
const me = new DataReader(new Uint8Array(await meRes.arrayBuffer()));
|
|
386
|
+
expect(me.readBytes().length).toBeGreaterThan(0); // toilUserId
|
|
387
|
+
expect(me.readString()).toBe('dave');
|
|
388
|
+
},
|
|
389
|
+
90_000,
|
|
390
|
+
);
|
|
391
|
+
|
|
392
|
+
it(
|
|
393
|
+
'(b) 2FA code security: wrong code rejected + attempt-limited to death, correct code single-use',
|
|
394
|
+
async () => {
|
|
395
|
+
await Auth.register('eve', 'eve-pw-strong', 'eve@x.com');
|
|
396
|
+
await Auth.login('eve', 'eve-pw-strong');
|
|
397
|
+
await Auth.setupTwoFactor(Auth.TwoFactorMethod.Email);
|
|
398
|
+
await Auth.confirmTwoFactorSetup(codeFromEmail('eve@x.com'));
|
|
399
|
+
|
|
400
|
+
// ---- wrong code is rejected, and after TWOFA_MAX_ATTEMPTS the challenge dies ----
|
|
401
|
+
__clearSentEmails();
|
|
402
|
+
jar.clear();
|
|
403
|
+
let err: unknown;
|
|
404
|
+
try {
|
|
405
|
+
await Auth.login('eve', 'eve-pw-strong');
|
|
406
|
+
} catch (e) {
|
|
407
|
+
err = e;
|
|
408
|
+
}
|
|
409
|
+
const twoFaId = (err as TwoFactorRequiredError).twoFaId;
|
|
410
|
+
const code = codeFromEmail('eve@x.com');
|
|
411
|
+
const wrong = code === '000000' ? '111111' : '000000';
|
|
412
|
+
|
|
413
|
+
// 5 (TWOFA_MAX_ATTEMPTS) wrong guesses. Reset the per-IP limiter between
|
|
414
|
+
// each so this exercises the CHALLENGE's own attempt cap, not @ratelimit.
|
|
415
|
+
for (let i = 0; i < 5; i++) {
|
|
416
|
+
__resetRatelimitForTests();
|
|
417
|
+
await expect(Auth.verifyTwoFactor(twoFaId, wrong)).rejects.toThrow();
|
|
418
|
+
}
|
|
419
|
+
// The challenge is now destroyed: even the CORRECT code fails.
|
|
420
|
+
__resetRatelimitForTests();
|
|
421
|
+
await expect(Auth.verifyTwoFactor(twoFaId, code)).rejects.toThrow();
|
|
422
|
+
|
|
423
|
+
// ---- a correct code is single-use: replay fails ----
|
|
424
|
+
__clearSentEmails();
|
|
425
|
+
jar.clear();
|
|
426
|
+
__resetRatelimitForTests();
|
|
427
|
+
let err2: unknown;
|
|
428
|
+
try {
|
|
429
|
+
await Auth.login('eve', 'eve-pw-strong');
|
|
430
|
+
} catch (e) {
|
|
431
|
+
err2 = e;
|
|
432
|
+
}
|
|
433
|
+
const twoFaId2 = (err2 as TwoFactorRequiredError).twoFaId;
|
|
434
|
+
const code2 = codeFromEmail('eve@x.com');
|
|
435
|
+
const session = await Auth.verifyTwoFactor(twoFaId2, code2); // consumes
|
|
436
|
+
expect(session.length).toBeGreaterThan(0);
|
|
437
|
+
__resetRatelimitForTests();
|
|
438
|
+
await expect(Auth.verifyTwoFactor(twoFaId2, code2)).rejects.toThrow(); // replay dead
|
|
439
|
+
},
|
|
440
|
+
120_000,
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
it(
|
|
444
|
+
'(c) cross-flow: reset tokens and 2FA codes are NOT interchangeable (namespace separation)',
|
|
445
|
+
async () => {
|
|
446
|
+
await Auth.register('frank', 'frank-pw-strong', 'frank@x.com');
|
|
447
|
+
|
|
448
|
+
// ---- direction 1: a LIVE reset token is useless at /auth/2fa/verify ----
|
|
449
|
+
await Auth.requestPasswordReset('frank@x.com');
|
|
450
|
+
const resetToken = tokenFromEmail('reset', 'frank@x.com'); // live resetTokens entry
|
|
451
|
+
__resetRatelimitForTests();
|
|
452
|
+
// Present the reset token as BOTH the twoFaId AND the code: /2fa/verify
|
|
453
|
+
// looks in `twoFaLogins` (a different collection) -> nothing -> rejected.
|
|
454
|
+
await expect(Auth.verifyTwoFactor(resetToken, resetToken)).rejects.toThrow();
|
|
455
|
+
|
|
456
|
+
// ---- direction 2: a LIVE 2FA login challenge id is useless at /auth/reset/finish ----
|
|
457
|
+
await Auth.login('frank', 'frank-pw-strong'); // session (2FA still off here)
|
|
458
|
+
__clearSentEmails();
|
|
459
|
+
await Auth.setupTwoFactor(Auth.TwoFactorMethod.Email);
|
|
460
|
+
await Auth.confirmTwoFactorSetup(codeFromEmail('frank@x.com'));
|
|
461
|
+
__clearSentEmails();
|
|
462
|
+
jar.clear();
|
|
463
|
+
__resetRatelimitForTests();
|
|
464
|
+
let err: unknown;
|
|
465
|
+
try {
|
|
466
|
+
await Auth.login('frank', 'frank-pw-strong');
|
|
467
|
+
} catch (e) {
|
|
468
|
+
err = e;
|
|
469
|
+
}
|
|
470
|
+
const twoFaId = (err as TwoFactorRequiredError).twoFaId; // live twoFaLogins key
|
|
471
|
+
const loginCode = codeFromEmail('frank@x.com');
|
|
472
|
+
|
|
473
|
+
// Submit the live 2FA challenge id AND the live 2FA code to
|
|
474
|
+
// /auth/reset/finish as the reset token: reset/finish looks in
|
|
475
|
+
// `resetTokens` -> nothing -> non-200. A live 2FA challenge is NOT a
|
|
476
|
+
// reset token.
|
|
477
|
+
const probes: Uint8Array[] = [hexToBytes(twoFaId), new TextEncoder().encode(loginCode)];
|
|
478
|
+
for (const raw of probes) {
|
|
479
|
+
__resetRatelimitForTests();
|
|
480
|
+
const body = new DataWriter()
|
|
481
|
+
.writeBytes(raw)
|
|
482
|
+
.writeBytes(new Uint8Array(1312)) // valid-length pk (reach the consume step)
|
|
483
|
+
.writeBytes(new Uint8Array(2420)) // valid-length proof
|
|
484
|
+
.toBytes();
|
|
485
|
+
const r = mod.dispatch({
|
|
486
|
+
method: 'POST',
|
|
487
|
+
path: '/auth/reset/finish',
|
|
488
|
+
headers: [
|
|
489
|
+
['host', 'localhost:3000'],
|
|
490
|
+
['content-type', 'application/octet-stream'],
|
|
491
|
+
],
|
|
492
|
+
body,
|
|
493
|
+
});
|
|
494
|
+
expect(r.status).not.toBe(200);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// The cross-flow probes touched NOTHING: the real 2FA login still
|
|
498
|
+
// completes with its real (twoFaId, code).
|
|
499
|
+
__resetRatelimitForTests();
|
|
500
|
+
const session = await Auth.verifyTwoFactor(twoFaId, loginCode);
|
|
501
|
+
expect(session.length).toBeGreaterThan(0);
|
|
502
|
+
},
|
|
503
|
+
120_000,
|
|
504
|
+
);
|
|
505
|
+
|
|
506
|
+
it(
|
|
507
|
+
'(d) NO session cookie is set on the ST_TWOFA_REQUIRED login response',
|
|
508
|
+
async () => {
|
|
509
|
+
await Auth.register('gwen', 'gwen-pw-strong', 'gwen@x.com');
|
|
510
|
+
await Auth.login('gwen', 'gwen-pw-strong');
|
|
511
|
+
await Auth.setupTwoFactor(Auth.TwoFactorMethod.Email);
|
|
512
|
+
await Auth.confirmTwoFactorSetup(codeFromEmail('gwen@x.com'));
|
|
513
|
+
|
|
514
|
+
jar.clear(); // drop every cookie
|
|
515
|
+
__clearSentEmails();
|
|
516
|
+
__resetRatelimitForTests();
|
|
517
|
+
|
|
518
|
+
let err: unknown;
|
|
519
|
+
try {
|
|
520
|
+
await Auth.login('gwen', 'gwen-pw-strong');
|
|
521
|
+
} catch (e) {
|
|
522
|
+
err = e;
|
|
523
|
+
}
|
|
524
|
+
expect(err).toBeInstanceOf(TwoFactorRequiredError);
|
|
525
|
+
// The shim folds any Set-Cookie into the jar; login/finish set NONE.
|
|
526
|
+
expect(jar.get('toil_sess')).toBeUndefined();
|
|
527
|
+
expect(jar.get('toil_user')).toBeUndefined();
|
|
528
|
+
// With no session cookie, the @auth-gated /auth/me is 401.
|
|
529
|
+
const meRes = await fetch('/auth/me');
|
|
530
|
+
expect(meRes.status).toBe(401);
|
|
531
|
+
},
|
|
532
|
+
60_000,
|
|
533
|
+
);
|
|
534
|
+
|
|
535
|
+
it(
|
|
536
|
+
'(e) login<->setup 2FA challenges are separate: a setup code cannot mint a login session, and setup/verify never silently disables 2FA',
|
|
537
|
+
async () => {
|
|
538
|
+
await Auth.register('ivy', 'ivy-pw-strong', 'ivy@x.com');
|
|
539
|
+
await Auth.login('ivy', 'ivy-pw-strong'); // session; 2FA still off
|
|
540
|
+
|
|
541
|
+
// Begin a 2FA SETUP: writes a challenge into `twoFaSetup` (keyed by
|
|
542
|
+
// username), NOT a login challenge into `twoFaLogins`.
|
|
543
|
+
__clearSentEmails();
|
|
544
|
+
__resetRatelimitForTests();
|
|
545
|
+
await Auth.setupTwoFactor(Auth.TwoFactorMethod.Email);
|
|
546
|
+
const setupCode = codeFromEmail('ivy@x.com');
|
|
547
|
+
|
|
548
|
+
// Present the LIVE setup code at /auth/2fa/verify with a fabricated
|
|
549
|
+
// twoFaId: /2fa/verify consults ONLY `twoFaLogins`, so the id misses and
|
|
550
|
+
// a setup code can never mint a login session.
|
|
551
|
+
__resetRatelimitForTests();
|
|
552
|
+
const fakeId = 'ab'.repeat(16); // 16-byte hex, not a real twoFaLogins key
|
|
553
|
+
await expect(Auth.verifyTwoFactor(fakeId, setupCode)).rejects.toThrow();
|
|
554
|
+
|
|
555
|
+
// The probe consumed NOTHING: the real setup still completes with the code.
|
|
556
|
+
__resetRatelimitForTests();
|
|
557
|
+
await Auth.confirmTwoFactorSetup(setupCode);
|
|
558
|
+
expect(await Auth.twoFactorStatus()).toBe(Auth.TwoFactorMethod.Email);
|
|
559
|
+
|
|
560
|
+
// With 2FA now ON but NO pending setup challenge, /auth/2fa/setup/verify is
|
|
561
|
+
// a no-op: a stray code cannot flip (disable) the method. A login challenge
|
|
562
|
+
// (targetMethod=NONE) has no wire path here and can never be routed in to
|
|
563
|
+
// silently disable 2FA.
|
|
564
|
+
__resetRatelimitForTests();
|
|
565
|
+
await expect(Auth.confirmTwoFactorSetup('000000')).rejects.toThrow();
|
|
566
|
+
expect(await Auth.twoFactorStatus()).toBe(Auth.TwoFactorMethod.Email);
|
|
567
|
+
},
|
|
568
|
+
120_000,
|
|
569
|
+
);
|
|
570
|
+
|
|
571
|
+
it(
|
|
572
|
+
'(f) cross-flow: confirm tokens and 2FA codes are NOT interchangeable (namespace separation)',
|
|
573
|
+
async () => {
|
|
574
|
+
setConfirmation(true);
|
|
575
|
+
await Auth.register('jade', 'jade-pw-strong', 'jade@x.com'); // unconfirmed -> confirm token emailed
|
|
576
|
+
const confirmToken = tokenFromEmail('confirm', 'jade@x.com'); // live confirmTokens entry
|
|
577
|
+
|
|
578
|
+
// ---- direction 1: a LIVE confirm token is useless at /auth/2fa/verify ----
|
|
579
|
+
__resetRatelimitForTests();
|
|
580
|
+
// Present the confirm token as BOTH the twoFaId AND the code: /2fa/verify
|
|
581
|
+
// reads `twoFaLogins` (a different collection) -> nothing -> rejected.
|
|
582
|
+
await expect(Auth.verifyTwoFactor(confirmToken, confirmToken)).rejects.toThrow();
|
|
583
|
+
|
|
584
|
+
// The probe consumed NOTHING: the confirm token still confirms the account,
|
|
585
|
+
// and login then succeeds (email now confirmed).
|
|
586
|
+
__resetRatelimitForTests();
|
|
587
|
+
await Auth.confirmEmail(confirmToken);
|
|
588
|
+
__resetRatelimitForTests();
|
|
589
|
+
expect((await Auth.login('jade', 'jade-pw-strong')).length).toBeGreaterThan(0);
|
|
590
|
+
|
|
591
|
+
// ---- direction 2: a LIVE 2FA login id/code is useless at /auth/confirm ----
|
|
592
|
+
// Enable 2FA (jade is confirmed + has a session from the login above).
|
|
593
|
+
__clearSentEmails();
|
|
594
|
+
__resetRatelimitForTests();
|
|
595
|
+
await Auth.setupTwoFactor(Auth.TwoFactorMethod.Email);
|
|
596
|
+
await Auth.confirmTwoFactorSetup(codeFromEmail('jade@x.com'));
|
|
597
|
+
|
|
598
|
+
// Fresh login -> a live `twoFaLogins` challenge (id + emailed code).
|
|
599
|
+
__clearSentEmails();
|
|
600
|
+
jar.clear();
|
|
601
|
+
__resetRatelimitForTests();
|
|
602
|
+
let err: unknown;
|
|
603
|
+
try {
|
|
604
|
+
await Auth.login('jade', 'jade-pw-strong');
|
|
605
|
+
} catch (e) {
|
|
606
|
+
err = e;
|
|
607
|
+
}
|
|
608
|
+
const twoFaId = (err as TwoFactorRequiredError).twoFaId;
|
|
609
|
+
const loginCode = codeFromEmail('jade@x.com');
|
|
610
|
+
|
|
611
|
+
// Submit the live 2FA id AND the live code to /auth/confirm as the confirm
|
|
612
|
+
// token: /auth/confirm consumes from `confirmTokens` (sha256(raw)) -> nothing
|
|
613
|
+
// -> non-200. A 2FA credential is NOT a confirm token.
|
|
614
|
+
const probes: Uint8Array[] = [hexToBytes(twoFaId), new TextEncoder().encode(loginCode)];
|
|
615
|
+
for (const raw of probes) {
|
|
616
|
+
__resetRatelimitForTests();
|
|
617
|
+
const body = new DataWriter().writeBytes(raw).toBytes();
|
|
618
|
+
const r = mod.dispatch({
|
|
619
|
+
method: 'POST',
|
|
620
|
+
path: '/auth/confirm',
|
|
621
|
+
headers: [
|
|
622
|
+
['host', 'localhost:3000'],
|
|
623
|
+
['content-type', 'application/octet-stream'],
|
|
624
|
+
],
|
|
625
|
+
body,
|
|
626
|
+
});
|
|
627
|
+
expect(r.status).not.toBe(200);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// The probes touched NOTHING: the real 2FA login still completes.
|
|
631
|
+
__resetRatelimitForTests();
|
|
632
|
+
const session = await Auth.verifyTwoFactor(twoFaId, loginCode);
|
|
633
|
+
expect(session.length).toBeGreaterThan(0);
|
|
634
|
+
},
|
|
635
|
+
120_000,
|
|
636
|
+
);
|
|
333
637
|
});
|