SPB Git

Repository Public

003_passkeys_sessions.sql 5.9 KB 87 lines sql

1-- 003 — passkeys (WebAuthn), server-side sessions, single-use challenges, setup/recovery grants, audit, shared rate limits.2-- Replaces the deterministic access cookie of 0.2 (HMAC of ACCESS_KEY). Reversible: db/migrations/down/003_passkeys_sessions.down.sql3-- Idempotent (IF NOT EXISTS everywhere); runs inside one transaction (scripts/db/migrate.ts).45-- ---- owner: stable WebAuthn user handle (random, never an identifier of the person)6ALTER TABLE owner ADD COLUMN IF NOT EXISTS user_handle text;7ALTER TABLE owner ALTER COLUMN password_hash DROP NOT NULL;     -- the Argon2 login of 0.1 is gone; the column is kept, unused8INSERT INTO owner (id, username, password_hash) VALUES (1, 'owner', NULL) ON CONFLICT (id) DO NOTHING;9UPDATE owner SET user_handle = encode(gen_random_bytes(32), 'hex') WHERE user_handle IS NULL;1011-- ---- passkeys: only the credential id and the PUBLIC key are stored; no biometric data ever reaches the server12ALTER TABLE webauthn_credentials ADD COLUMN IF NOT EXISTS device_type  text;                  -- singleDevice | multiDevice (synced passkey)13ALTER TABLE webauthn_credentials ADD COLUMN IF NOT EXISTS backed_up    boolean NOT NULL DEFAULT false;14ALTER TABLE webauthn_credentials ADD COLUMN IF NOT EXISTS aaguid       text;15ALTER TABLE webauthn_credentials ADD COLUMN IF NOT EXISTS last_used_at timestamptz;16ALTER TABLE webauthn_credentials ADD COLUMN IF NOT EXISTS created_via  text NOT NULL DEFAULT 'session';   -- bootstrap | recovery | transition | session17ALTER TABLE webauthn_credentials ADD COLUMN IF NOT EXISTS user_agent   text;1819-- ---- sessions: random 256-bit token in the cookie, only its SHA-256 here; revocable one by one or globally20UPDATE sessions SET revoked_at = now() WHERE revoked_at IS NULL;          -- rows of the removed 0.1 login system21ALTER TABLE sessions ADD COLUMN IF NOT EXISTS credential_id text REFERENCES webauthn_credentials(id) ON DELETE SET NULL;22ALTER TABLE sessions ADD COLUMN IF NOT EXISTS public_id     text;         -- opaque handle shown to the UI (never the token hash)23ALTER TABLE sessions ADD COLUMN IF NOT EXISTS label         text;         -- device label derived from the user agent24ALTER TABLE sessions ADD COLUMN IF NOT EXISTS auth_at       timestamptz;  -- last passkey verification (sensitive operations need a recent one)25ALTER TABLE sessions ADD COLUMN IF NOT EXISTS absolute_expires_at timestamptz;26ALTER TABLE sessions ADD COLUMN IF NOT EXISTS rotated_at    timestamptz;27ALTER TABLE sessions ADD COLUMN IF NOT EXISTS revoked_reason text;28UPDATE sessions SET public_id = 'ses_' || encode(gen_random_bytes(9), 'hex') WHERE public_id IS NULL;29CREATE UNIQUE INDEX IF NOT EXISTS sessions_public_id_idx ON sessions (public_id);30CREATE INDEX IF NOT EXISTS sessions_active_idx ON sessions (last_seen DESC) WHERE revoked_at IS NULL;3132-- ---- WebAuthn challenges: random, short-lived, consumed exactly once (UPDATE … WHERE consumed_at IS NULL RETURNING)33CREATE TABLE IF NOT EXISTS auth_challenges (34  id          text PRIMARY KEY,                       -- random handle kept in an HttpOnly cookie for the ceremony35  challenge   text NOT NULL,                          -- base64url challenge given to the authenticator36  kind        text NOT NULL CHECK (kind IN ('registration','authentication')),37  purpose     text NOT NULL,                          -- login | reauth | register:session | register:bootstrap | register:recovery | register:transition38  session_id  text,                                   -- sha256(token) of the session that asked (reauth / add passkey)39  grant_id    text,                                   -- setup grant that authorises a registration40  ip          text,41  created_at  timestamptz NOT NULL DEFAULT now(),42  expires_at  timestamptz NOT NULL,43  consumed_at timestamptz44);45CREATE INDEX IF NOT EXISTS auth_challenges_expires_idx ON auth_challenges (expires_at);4647-- ---- setup grants: the one-time bootstrap code and the offline recovery codes (only SHA-256 of high-entropy codes)48CREATE TABLE IF NOT EXISTS auth_grants (49  id          text PRIMARY KEY,50  kind        text NOT NULL CHECK (kind IN ('bootstrap','recovery','transition')),51  code_hash   text NOT NULL UNIQUE,52  batch       text,                                   -- recovery codes are generated (and rotated) by batch53  created_at  timestamptz NOT NULL DEFAULT now(),54  expires_at  timestamptz,                            -- bootstrap: minutes; recovery: null (until used or rotated)55  redeemed_at timestamptz,                            -- code typed correctly → enrolment window opened56  redeem_token_hash text,                             -- sha256 of the short-lived enrolment cookie57  redeem_expires_at timestamptz,58  used_at     timestamptz,                            -- a passkey was registered with it → dead59  revoked_at  timestamptz,60  failed_attempts integer NOT NULL DEFAULT 0,61  note        text62);63CREATE INDEX IF NOT EXISTS auth_grants_kind_idx ON auth_grants (kind) WHERE used_at IS NULL AND revoked_at IS NULL;6465-- ---- minimal audit trail (no assertion, no key material, no conversation text)66CREATE TABLE IF NOT EXISTS auth_events (67  id         bigserial PRIMARY KEY,68  at         timestamptz NOT NULL DEFAULT now(),69  event      text NOT NULL,                           -- login.ok | login.fail | passkey.add | passkey.revoke | session.revoke | recovery.used | bootstrap.used | transition.used | ratelimit | …70  ip         text,71  user_agent text,72  session_public_id text,73  detail     jsonb74);75CREATE INDEX IF NOT EXISTS auth_events_at_idx ON auth_events (at DESC);7677-- ---- rate limits shared by every process and surviving restarts (fixed windows)78CREATE TABLE IF NOT EXISTS rate_limits (79  bucket       text NOT NULL,80  window_start timestamptz NOT NULL,81  count        integer NOT NULL DEFAULT 0,82  PRIMARY KEY (bucket, window_start)83);84CREATE INDEX IF NOT EXISTS rate_limits_window_idx ON rate_limits (window_start);8586INSERT INTO settings (key, value) VALUES ('auth', '{"schema": 3}') ON CONFLICT (key) DO NOTHING;87
Files main
Menu

Theme