import test from 'node:test'; import assert from 'node:assert/strict'; import { ResetManager, createSessionToken, hashPassword, hashToken, verifyPassword, } from '../src/auth.js'; import { totpProvisioningUri, verifyTotp } from '../src/totp.js'; test('reset codes are printed, case-insensitive, expiring, and one-time', () => { const messages = []; const manager = new ResetManager({ log: (message) => messages.push(message) }); const code = manager.request(); assert.equal(code.length, 8); assert.equal(messages.length, 1); assert.match(messages[0], new RegExp(`ADMIN RESET CODE: ${code}`)); assert.equal(manager.confirm(` ${code.toLowerCase()} `), true); assert.equal(manager.confirm(code), false); }); test('expired reset codes are rejected', () => { const manager = new ResetManager({ ttlMs: -1, log: () => {} }); const code = manager.request(); assert.equal(manager.confirm(code), false); }); test('password hashes verify and reject a different password', () => { const hash = hashPassword('correct horse battery staple'); assert.equal(verifyPassword('correct horse battery staple', hash), true); assert.equal(verifyPassword('wrong password', hash), false); }); test('TOTP verifies RFC 6238 SHA-1 vectors', () => { const secret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ'; const vectors = [ [59, '287082'], [1111111109, '081804'], [1111111111, '050471'], [1234567890, '005924'], [2000000000, '279037'], ]; for (const [seconds, code] of vectors) { assert.equal(verifyTotp(secret, code, seconds * 1000), true); } assert.match(totpProvisioningUri('Example App', 'admin@example.com', secret), /^otpauth:\/\/totp\//); }); test('session token hashes are not the raw token', () => { const { token, tokenHash } = createSessionToken(); assert.notEqual(token, tokenHash); assert.equal(hashToken(token), tokenHash); });