-- Passkeys only. No phone numbers, passwords, email or texted codes. -- Rebuild people without the phone token, and add the admin flag. PRAGMA defer_foreign_keys = on; CREATE TABLE people_new ( id TEXT PRIMARY KEY, clinician_npi TEXT, is_admin INTEGER NOT NULL DEFAULT 0, created TEXT NOT NULL DEFAULT (datetime('now')) ); INSERT INTO people_new (id, clinician_npi, created) SELECT id, clinician_npi, created FROM people; DROP TABLE people; ALTER TABLE people_new RENAME TO people; PRAGMA defer_foreign_keys = off; DROP TABLE IF EXISTS sends; -- One row per passkey. Recovery is a second passkey on another device. CREATE TABLE IF NOT EXISTS passkeys ( credential_id TEXT PRIMARY KEY, -- base64url person_id TEXT NOT NULL REFERENCES people(id), public_key TEXT NOT NULL, -- base64url COSE key counter INTEGER NOT NULL DEFAULT 0, transports TEXT, -- JSON array device_type TEXT, -- singleDevice | multiDevice backed_up INTEGER NOT NULL DEFAULT 0, name TEXT, created TEXT NOT NULL DEFAULT (datetime('now')), last_used TEXT ); CREATE INDEX IF NOT EXISTS passkeys_person ON passkeys(person_id); -- Server-side WebAuthn challenges, single use, found by a short-lived HttpOnly cookie. CREATE TABLE IF NOT EXISTS webauthn_challenges ( id TEXT PRIMARY KEY, challenge TEXT NOT NULL, purpose TEXT NOT NULL CHECK (purpose IN ('register','add','admin','signin')), person_id TEXT, -- the account a new passkey belongs to setup_hash TEXT, -- admin setup link being used expires INTEGER NOT NULL -- unix seconds ); CREATE INDEX IF NOT EXISTS webauthn_challenges_expires ON webauthn_challenges(expires); -- New accounts per network per day (HMAC of the IP, never the IP). CREATE TABLE IF NOT EXISTS account_ips ( ip_hash TEXT NOT NULL, at TEXT NOT NULL DEFAULT (datetime('now')) ); CREATE INDEX IF NOT EXISTS account_ips_hash ON account_ips(ip_hash, at); -- One-time links that let a person create the first admin passkey. CREATE TABLE IF NOT EXISTS admin_setup_links ( token_hash TEXT PRIMARY KEY, -- SHA-256 of the token, base64url label TEXT, person_id TEXT, -- who used it expires INTEGER NOT NULL, -- unix seconds used TEXT, created TEXT NOT NULL DEFAULT (datetime('now')) );