Branchwell

Docley / app / migrations / 0003_passkeys.sql

1-- Passkeys only. No phone numbers, passwords, email or texted codes.
2-- Rebuild people without the phone token, and add the admin flag.
3PRAGMA defer_foreign_keys = on;
4CREATE TABLE people_new (
5 id TEXT PRIMARY KEY,
6 clinician_npi TEXT,
7 is_admin INTEGER NOT NULL DEFAULT 0,
8 created TEXT NOT NULL DEFAULT (datetime('now'))
9);
10INSERT INTO people_new (id, clinician_npi, created) SELECT id, clinician_npi, created FROM people;
11DROP TABLE people;
12ALTER TABLE people_new RENAME TO people;
13PRAGMA defer_foreign_keys = off;
14
15DROP TABLE IF EXISTS sends;
16
17-- One row per passkey. Recovery is a second passkey on another device.
18CREATE TABLE IF NOT EXISTS passkeys (
19 credential_id TEXT PRIMARY KEY, -- base64url
20 person_id TEXT NOT NULL REFERENCES people(id),
21 public_key TEXT NOT NULL, -- base64url COSE key
22 counter INTEGER NOT NULL DEFAULT 0,
23 transports TEXT, -- JSON array
24 device_type TEXT, -- singleDevice | multiDevice
25 backed_up INTEGER NOT NULL DEFAULT 0,
26 name TEXT,
27 created TEXT NOT NULL DEFAULT (datetime('now')),
28 last_used TEXT
29);
30CREATE INDEX IF NOT EXISTS passkeys_person ON passkeys(person_id);
31
32-- Server-side WebAuthn challenges, single use, found by a short-lived HttpOnly cookie.
33CREATE TABLE IF NOT EXISTS webauthn_challenges (
34 id TEXT PRIMARY KEY,
35 challenge TEXT NOT NULL,
36 purpose TEXT NOT NULL CHECK (purpose IN ('register','add','admin','signin')),
37 person_id TEXT, -- the account a new passkey belongs to
38 setup_hash TEXT, -- admin setup link being used
39 expires INTEGER NOT NULL -- unix seconds
40);
41CREATE INDEX IF NOT EXISTS webauthn_challenges_expires ON webauthn_challenges(expires);
42
43-- New accounts per network per day (HMAC of the IP, never the IP).
44CREATE TABLE IF NOT EXISTS account_ips (
45 ip_hash TEXT NOT NULL,
46 at TEXT NOT NULL DEFAULT (datetime('now'))
47);
48CREATE INDEX IF NOT EXISTS account_ips_hash ON account_ips(ip_hash, at);
49
50-- One-time links that let a person create the first admin passkey.
51CREATE TABLE IF NOT EXISTS admin_setup_links (
52 token_hash TEXT PRIMARY KEY, -- SHA-256 of the token, base64url
53 label TEXT,
54 person_id TEXT, -- who used it
55 expires INTEGER NOT NULL, -- unix seconds
56 used TEXT,
57 created TEXT NOT NULL DEFAULT (datetime('now'))
58);