The verify count now survives a restart of the site, so the specs can no longer share one test admin across server runs: create-test-admin.ts makes two test admins and clears their stored counts; the setup spec signs in as the second. The whole suite passes in one run (20 passed, 1 skipped). Co-Authored-By: Claude Fable 5.2 <[email protected]>
19 lines
1000 B
TypeScript
19 lines
1000 B
TypeScript
import { createHmac } from 'node:crypto'
|
|
|
|
/** RFC 6238 TOTP (SHA-1, 6 digits, 30 s) — the code an authenticator app shows for `secret` (base32). */
|
|
export function totp(secret: string, now = Date.now()): string {
|
|
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
|
|
let bits = ''
|
|
for (const ch of secret.replace(/=+$/, '').toUpperCase()) {bits += alphabet.indexOf(ch).toString(2).padStart(5, '0')}
|
|
const key = Buffer.from((bits.match(/.{8}/g) ?? []).map((b) => parseInt(b, 2)))
|
|
const counter = Buffer.alloc(8)
|
|
counter.writeBigUInt64BE(BigInt(Math.floor(now / 1000 / 30)))
|
|
const hmac = createHmac('sha1', key).update(counter).digest()
|
|
const offset = hmac[hmac.length - 1] & 0xf
|
|
const code = (hmac.readUInt32BE(offset) & 0x7fffffff) % 1_000_000
|
|
return code.toString().padStart(6, '0')
|
|
}
|
|
|
|
/** The second smoke-test admin (create-test-admin.ts makes both): `e2e-admin@…` → `e2e-admin-2@…`. */
|
|
export const secondTestAdmin = (email: string): string => email.replace('@', '-2@')
|