mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
* fix(auth): try every session cookie, and make sessions per-device Three separate ways a correct password produced a login screen, or a sign-out reached devices it had no business touching. **Two cookies, one name.** Cookie identity is (name, Domain, Path). A browser that had used both tokencircles.com and dev.tokencircles.com held two fm_session cookies, and the prod-scoped one domain-matches the dev API. RFC 6265 sorts equal-path cookies oldest-first, so a first-match-wins reader picked the stalest one every time: login succeeded, the redirect authenticated with the wrong token, and you were back on the login screen. Only on a browser that had seen both origins, which is why it reproduced on one machine and nowhere else. readCookies now returns every value and the first that verifies wins. **The rate limit counted successes.** A successful login proves the credentials are right; charging it to the anti-guessing budget means signing in and out across a few devices locks out a legitimate user. Per-IP (30/15min) and per-email (10/15min), failures only, both cleared on success. The message says how long the wait is. **Logout meant logout everywhere.** The only revocation primitive was users.token_version, which invalidates every token the account has ever been issued, and the "Logout" button used it — so leaving on a laptop ejected you from your phone mid-use. Sessions are now rows: the JWT carries a sid, deleting the row ends exactly one device, and Settings lists where you are signed in with the current device flagged. Sign out everywhere survives as the separate, deliberate thing it is for. Named auth_sessions, not sessions: 0001_init already creates a sessions(sid, sess, expire) table for the retired Express session store, so CREATE TABLE IF NOT EXISTS is a no-op there and the index on user_id fails. Also adds the audit trail this was diagnosed without: auth_logs persists every login and logout outcome, plus session denials that arrived with a cookie. * test(auth): wait for the retry fetch instead of counting ticks The device-list retry assertion drained six microtask ticks and then asserted. Enough here, not enough on the CI runner, where the second fetch had simply not resolved: "expected 1 row, got 0".
42 lines
2.2 KiB
SQL
42 lines
2.2 KiB
SQL
-- Auth audit trail.
|
|
--
|
|
-- error_logs (0013) deliberately records only 5xx: "4xx client errors (validation, auth,
|
|
-- not-found) are intentional control flow and are NOT logged, to keep the signal clean." That is
|
|
-- right for validation errors and wrong for authentication — a user locked out by a 401 loop left
|
|
-- no trace at all on the server, and the only way to find out why was to read the request's Cookie
|
|
-- header off their screen.
|
|
--
|
|
-- Deliberately NOT one row per request. A signed-out browser polls /api/auth/me on every load, so
|
|
-- persisting every denial would be a write per page view and a free amplification vector. Only
|
|
-- these reach D1:
|
|
-- * login / register / logout outcomes — rare, and the ones an audit trail is actually for
|
|
-- * a session refused when the request DID carry a cookie — the interesting failure; a request
|
|
-- with no cookie is simply not signed in
|
|
-- Everything else is a structured console line, captured by Workers Observability.
|
|
CREATE TABLE IF NOT EXISTS auth_logs (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
-- 'login' | 'register' | 'logout' | 'session'
|
|
event TEXT NOT NULL,
|
|
-- 'ok' | 'denied'
|
|
outcome TEXT NOT NULL,
|
|
-- Why it was denied: bad_credentials, captcha, rate_limited, revoked, bad_token, unknown_user…
|
|
reason TEXT,
|
|
user_id INTEGER,
|
|
-- The address that was TRIED, which for a failed login is the only identifier there is. Not a
|
|
-- foreign key: a login attempt for an address with no account is exactly what this must record.
|
|
email TEXT,
|
|
ip TEXT,
|
|
user_agent TEXT,
|
|
-- cf-ray, so a row can be tied back to the Cloudflare request log.
|
|
request_id TEXT,
|
|
-- How many session cookies the request carried. More than one means duplicates across Domain or
|
|
-- Path scopes, which is invisible from the outside and turns a working login into a permanent
|
|
-- 401. This column is the whole reason the incident that prompted the table took an evening.
|
|
cookie_count INTEGER,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_auth_logs_created ON auth_logs(created_at);
|
|
CREATE INDEX IF NOT EXISTS idx_auth_logs_email ON auth_logs(email);
|
|
CREATE INDEX IF NOT EXISTS idx_auth_logs_user ON auth_logs(user_id);
|