/** * twoFactor.confirmByEmail building blocks: the switch is read strictly, the code check agrees with the 2FA package's * own (RFC 6238 vectors; one period either side, like otpauth's window 1), addresses are masked, and the notice hooks see every transition. The HTTP flow is in * enrolmentConfirmHttp.test.ts. */ import { describe, expect, test } from 'vitest' import { buildTotpNoticeHooks, maskAddress, resolveConfirmByEmail, verifyTotpCode } from '../src/modules/security/totp/enrolmentConfirm.js' describe('confirmByEmail options', () => { test('on by default; false turns it off; nonsense stops the start', () => { expect(resolveConfirmByEmail(undefined)).toMatchObject({ adminConfirm: true, linkMinutes: 30, logLinkWhenMailFails: true, maxMailsPerHour: 5, notices: true, sameBrowser: true }) expect(resolveConfirmByEmail(false)).toBeNull() expect(resolveConfirmByEmail({ linkMinutes: 10, sameBrowser: false })).toMatchObject({ linkMinutes: 10, sameBrowser: false }) for (const bad of ['false', 0, [], { sameBrowser: 'false' }, { linkMinutes: 0 }, { linkMinutes: 2000 }, { maxMailsPerHour: 1.5 }]) { expect(() => resolveConfirmByEmail(bad), JSON.stringify(bad)).toThrow(/confirmByEmail/) } }) }) describe('the code check', () => { const secret = 'JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP' test('RFC 6238 test vectors (SHA1, SHA256, SHA512; 8 digits) and one period either side, not two', () => { // RFC 6238 Appendix B: seeds "1234567890…" (20 / 32 / 64 bytes) in base32 const sha1 = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ' const sha256 = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZA' const sha512 = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNA' const at = (s: number) => s * 1000 expect(verifyTotpCode(sha1, '94287082', { digits: 8 }, at(59))).toBe(true) expect(verifyTotpCode(sha256, '46119246', { algorithm: 'SHA256', digits: 8 }, at(59))).toBe(true) expect(verifyTotpCode(sha512, '90693936', { algorithm: 'SHA512', digits: 8 }, at(59))).toBe(true) expect(verifyTotpCode(sha1, '07081804', { digits: 8 }, at(1111111109))).toBe(true) // the same code 30 s later still passes (window 1), 90 s later not expect(verifyTotpCode(sha1, '07081804', { digits: 8 }, at(1111111109 + 30))).toBe(true) expect(verifyTotpCode(sha1, '07081804', { digits: 8 }, at(1111111109 + 90))).toBe(false) expect(verifyTotpCode(sha1, '1234567', { digits: 8 }, at(59))).toBe(false) expect(verifyTotpCode('not base32!', '123456', {}, at(59))).toBe(false) expect(verifyTotpCode('ABCD', '123456', {}, at(59))).toBe(false) // too short a key void secret }) test('addresses are masked on the screen', () => { expect(maskAddress('jan.kowalski@firma.pl')).toBe('j…@firma.pl') expect(maskAddress('nope')).toBe('…') }) }) describe('notice hooks', () => { const { beforeChange } = buildTotpNoticeHooks('users', { notices: true }) const change = (before: unknown, after: unknown, operation = 'update') => { const context: Record = {} beforeChange({ context, data: { totpSecret: after }, operation, originalDoc: { totpSecret: before } } as never) return context.iaplTotpChange ?? null } test('on, off, changed; nothing for a write that does not touch the secret, or a create', () => { expect(change(null, 'A')).toBe('on') expect(change('A', null)).toBe('off') expect(change('A', 'B')).toBe('changed') expect(change('A', 'A')).toBeNull() expect(change(null, null)).toBeNull() expect(change(null, 'A', 'create')).toBeNull() }) })