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".
33 lines
1.8 KiB
SQL
33 lines
1.8 KiB
SQL
-- One row per signed-in device.
|
|
--
|
|
-- Sessions were stateless: a JWT, and a single `users.token_version` counter that could only ever
|
|
-- revoke ALL of them at once. So "log out" had two possible meanings and the product shipped the
|
|
-- wrong one — signing out on a laptop revoked the phone and the tablet too — and there was no way
|
|
-- to show someone where they were signed in, or to end one session without ending every session.
|
|
--
|
|
-- Named `auth_sessions`, not `sessions`: 0001_init created a `sessions` table for the Express
|
|
-- server's express-session store (sid/sess/expire). Nothing in the Worker reads it, but it may
|
|
-- still hold rows, and dropping someone's table to free up a name is not what a migration is for.
|
|
--
|
|
-- The JWT now carries a `sid`, and this table is what it points at. Deleting a row ends exactly
|
|
-- one device. `token_version` stays as the blunt instrument behind "sign out everywhere", because
|
|
-- it is the only thing that can revoke a token issued before this table existed.
|
|
CREATE TABLE IF NOT EXISTS auth_sessions (
|
|
-- Random uuid, embedded in the JWT as `sid`. Not a secret: holding it grants nothing without
|
|
-- the signed token.
|
|
id TEXT PRIMARY KEY,
|
|
user_id INTEGER NOT NULL,
|
|
-- 'password' | 'google'
|
|
provider TEXT,
|
|
-- What the device was when it signed in. Kept verbatim; the readable label is derived at render
|
|
-- time, so improving the parser never needs a backfill.
|
|
user_agent TEXT,
|
|
ip TEXT,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
-- Updated at most once every few minutes, not on every request — see SESSION_TOUCH_SECONDS.
|
|
last_seen_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_auth_sessions_user ON auth_sessions(user_id);
|
|
CREATE INDEX IF NOT EXISTS idx_auth_sessions_last_seen ON auth_sessions(last_seen_at);
|