mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
* feat(auth): TOTP two-factor authentication with recovery codes Second factor for password and Google sign-in. Login now withholds the session when the account has a confirmed TOTP credential: the browser gets a 5-minute signed challenge cookie (fm_2fa) and /api/auth/2fa/verify trades a valid authenticator or recovery code for the real session. - RFC 6238 TOTP over WebCrypto (SHA-1/6/30 as authenticator apps expect), verified against the RFC's reference vectors; constant-time compares; last-used-step high-water mark so a code is never accepted twice. - Secrets encrypted at rest (AES-256-GCM under an HKDF key from JWT_SECRET); decrypt failure fails CLOSED - the challenge remains and the SHA-256-hashed single-use recovery codes still sign you in. - Enrollment in Settings: otpauth link + manual key, confirm with a live code, ten recovery codes shown exactly once. Disable demands a code. - Rate limits on verify (10/15min/user), auth_logs twofa events, and the Google callback returns with ?twofa=1 so the SPA opens the code step. * feat(2fa): QR enrollment and a full-loop e2e spec - Enroll now shows a QR code of the otpauth URI, rendered locally with uqr — the shared secret never leaves the page, so external QR image services are not an option. White backing keeps it scannable in dark mode. Manual key entry and the open-directly link stay. - New Playwright spec drives the whole loop through the real UI with a dedicated user: enroll (computing codes from the on-screen secret the way an authenticator app would), wrong-code rejection, TOTP challenge sign-in, recovery-code sign-in, disable via recovery code. - tests/db.ts: shared local-D1 helpers (sql / sqlRows) so auth specs can arrange state the API deliberately refuses to expose. * fix(2fa): close the review findings on the 2FA lifecycle Security: - /2fa/disable now shares the 2fa:<user> attempt bucket with /2fa/verify (and clears it on success). It accepts the same factors, so it must not hand an attacker a fresh guessing budget; before this, a stolen session could brute-force the disable endpoint unmetered. - Enabling 2FA deletes every OTHER auth session. Sessions that predate enrollment got in on one factor — including the intruder the user may be enrolling against. Only the enrolling session survives. - Account deletion now removes totp_credentials and recovery_codes; the encrypted secret and code hashes used to outlive the account. UX / recoverability: - "Create account" with an existing 2FA-protected email now lands on the code challenge instead of a silent reload loop (both login surfaces). - Recovery codes: a Download button joins Copy (clipboard is unavailable in non-secure contexts and the codes are unrecoverable once revealed), and Settings warns loudly when 3 or fewer codes remain. - The challenge's recovery/back controls are real buttons — keyboard and assistive-tech reachable; they were onClick-only anchors, invisible to the tab order, gating the only path in after a lost phone. - Backing out of the challenge strips the ?twofa=1 marker, so a reload no longer reopens a dead challenge for an expired cookie. Not addressed here (accepted, low): recovery-code spend before a failed session write; the two-tab concurrent-setup race (second secret wins).
34 lines
1.6 KiB
SQL
34 lines
1.6 KiB
SQL
-- TOTP two-factor auth (migration 0028).
|
|
--
|
|
-- `secret_enc` is the base32 TOTP secret encrypted with AES-256-GCM under a key HKDF-derived
|
|
-- from JWT_SECRET (info label in twofa.ts). Not plaintext, because a D1 leak must not hand out
|
|
-- every user's second factor together with their password hashes. The trade-off is deliberate:
|
|
-- rotating JWT_SECRET orphans these secrets — TOTP then fails to decrypt and sign-in falls back
|
|
-- to recovery codes, which are hashed (not encrypted) below and survive rotation.
|
|
--
|
|
-- `confirmed_at` is NULL between "show the QR" and "user typed a valid code". Only a confirmed
|
|
-- row makes login demand a second factor; an abandoned setup must never lock anyone out.
|
|
--
|
|
-- `last_used_step` is the RFC 6238 anti-replay high-water mark: a code at or below it is spent,
|
|
-- so shoulder-surfing a code that was just used buys nothing within its 30s window.
|
|
CREATE TABLE IF NOT EXISTS totp_credentials (
|
|
user_id INTEGER PRIMARY KEY,
|
|
secret_enc TEXT NOT NULL,
|
|
key_version INTEGER NOT NULL DEFAULT 1,
|
|
last_used_step INTEGER,
|
|
confirmed_at TEXT,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
);
|
|
|
|
-- Single-use backup codes. Same shape and reasoning as password_resets (0005): only a SHA-256
|
|
-- hash is stored, so a database leak yields nothing typeable — the raw codes exist only on the
|
|
-- sheet the user saved at enrollment.
|
|
CREATE TABLE IF NOT EXISTS recovery_codes (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER NOT NULL,
|
|
code_hash TEXT NOT NULL,
|
|
used_at TEXT,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_recovery_codes_user ON recovery_codes(user_id);
|