-- 0001: the whole initial schema of im. Every table, its constraints and its lookup indexes. -- Ids are ULID text; timestamps are fixed-width UTC RFC 3339 text; booleans are INTEGER 0/1. -- Never edit this file once it has been applied anywhere; add 0002_... instead. CREATE TABLE users ( id TEXT PRIMARY KEY, email TEXT NOT NULL, email_folded TEXT NOT NULL UNIQUE, name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 100), password_hash TEXT NOT NULL, is_admin INTEGER NOT NULL DEFAULT 0 CHECK (is_admin IN (0, 1)), disabled_at TEXT, -- Sealed TOTP secret. Held while enrolling (totp_enabled_at NULL) and kept after enabling. totp_secret_sealed TEXT, totp_enabled_at TEXT, totp_last_step INTEGER, locale TEXT NOT NULL DEFAULT 'en' CHECK (locale IN ('en', 'tr')), zoneinfo TEXT NOT NULL DEFAULT 'Europe/Istanbul', theme TEXT NOT NULL DEFAULT 'system' CHECK (theme IN ('system', 'light', 'dark')), photo_mime TEXT, picture_version INTEGER NOT NULL DEFAULT 0, created_at TEXT NOT NULL, updated_at TEXT NOT NULL ); -- Ten codes per set, shown once. Only their hashes are stored; used_at marks a consumed code. CREATE TABLE recovery_codes ( user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE, code_hash TEXT NOT NULL, used_at TEXT, PRIMARY KEY (user_id, code_hash) ); -- Browser sessions. The id is the OIDC `sid`. Ended sessions are deleted, after their -- back-channel logout jobs are queued. CREATE TABLE sessions ( id TEXT PRIMARY KEY, user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE, token_hash TEXT NOT NULL UNIQUE, csrf_token TEXT NOT NULL, created_at TEXT NOT NULL, expires_at TEXT NOT NULL, last_seen_at TEXT NOT NULL, ip TEXT, user_agent TEXT, auth_time TEXT NOT NULL, amr TEXT NOT NULL CHECK (amr IN ('pwd', 'pwd otp', 'pwd rc')) ); CREATE INDEX sessions_user ON sessions (user_id); CREATE INDEX sessions_expires ON sessions (expires_at); -- OIDC clients. secret_hash is the hash of the client secret; the secret itself is shown once. CREATE TABLE clients ( id TEXT PRIMARY KEY, name TEXT NOT NULL, secret_hash TEXT NOT NULL, secret_rotated_at TEXT NOT NULL, redirect_uris TEXT NOT NULL DEFAULT '[]' CHECK (json_valid (redirect_uris)), post_logout_redirect_uris TEXT NOT NULL DEFAULT '[]' CHECK (json_valid (post_logout_redirect_uris)), backchannel_logout_uri TEXT, allowed_scopes TEXT NOT NULL, service_key TEXT UNIQUE, created_at TEXT NOT NULL, disabled_at TEXT ); -- The switcher list. im's own row (key 'im') is maintained at boot from base_url. CREATE TABLE services ( key TEXT PRIMARY KEY CHECK ( length(key) BETWEEN 1 AND 32 AND key GLOB '[a-z]*' AND key NOT GLOB '*[^a-z0-9-]*' ), name TEXT NOT NULL, url TEXT NOT NULL, position INTEGER NOT NULL DEFAULT 0, client_id TEXT REFERENCES clients (id) ON DELETE SET NULL, healthy INTEGER CHECK (healthy IS NULL OR healthy IN (0, 1)), checked_at TEXT, check_ms INTEGER, check_error TEXT ); -- One-time authorization codes. A second use of a used code revokes the refresh tokens issued -- from it (refresh_tokens.code_hash). CREATE TABLE auth_codes ( code_hash TEXT PRIMARY KEY, client_id TEXT NOT NULL REFERENCES clients (id) ON DELETE CASCADE, user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE, session_id TEXT NOT NULL REFERENCES sessions (id) ON DELETE CASCADE, redirect_uri TEXT NOT NULL, scope TEXT NOT NULL, nonce TEXT, -- PKCE S256 is mandatory for every client (contracts.md 2.3). code_challenge TEXT NOT NULL, expires_at TEXT NOT NULL, used_at TEXT ); CREATE INDEX auth_codes_expires ON auth_codes (expires_at); -- Refresh tokens, rotated on every use. Presenting a used token revokes its whole family. CREATE TABLE refresh_tokens ( id TEXT PRIMARY KEY, token_hash TEXT NOT NULL UNIQUE, family_id TEXT NOT NULL, client_id TEXT NOT NULL REFERENCES clients (id) ON DELETE CASCADE, user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE, session_id TEXT NOT NULL REFERENCES sessions (id) ON DELETE CASCADE, -- Plain text, not a foreign key: authorization codes are swept while their tokens live on. code_hash TEXT, scope TEXT NOT NULL, created_at TEXT NOT NULL, expires_at TEXT NOT NULL, used_at TEXT, revoked_at TEXT ); CREATE INDEX refresh_tokens_family ON refresh_tokens (family_id); CREATE INDEX refresh_tokens_session ON refresh_tokens (session_id); -- Which clients a browser session has signed in to. Drives back-channel logout and the -- "connected apps" list. CREATE TABLE client_sessions ( session_id TEXT NOT NULL REFERENCES sessions (id) ON DELETE CASCADE, client_id TEXT NOT NULL REFERENCES clients (id) ON DELETE CASCADE, first_at TEXT NOT NULL, last_at TEXT NOT NULL, PRIMARY KEY (session_id, client_id) ); CREATE INDEX client_sessions_client ON client_sessions (client_id); -- Token-signing keys. kid is the first 16 hex characters of the SHA-256 of the public key. The -- current key is the newest one that is active and not retired. CREATE TABLE signing_keys ( kid TEXT PRIMARY KEY, private_sealed TEXT NOT NULL, public_raw BLOB NOT NULL, created_at TEXT NOT NULL, activates_at TEXT NOT NULL, retires_at TEXT ); CREATE INDEX signing_keys_current ON signing_keys (activates_at) WHERE retires_at IS NULL; -- Invitations. The token is stored as its hash. A new invite for an address deletes its older -- pending invites. CREATE TABLE invites ( token_hash TEXT PRIMARY KEY, email_folded TEXT NOT NULL, is_admin INTEGER NOT NULL DEFAULT 0 CHECK (is_admin IN (0, 1)), invited_by TEXT REFERENCES users (id) ON DELETE SET NULL, created_at TEXT NOT NULL, expires_at TEXT NOT NULL, accepted_at TEXT ); CREATE INDEX invites_email ON invites (email_folded); -- Single-use links for password reset and both halves of an email change. The two halves of one -- email change share pair_id. CREATE TABLE account_links ( token_hash TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK (kind IN ('reset', 'email_old', 'email_new')), user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE, pair_id TEXT, new_email TEXT, created_at TEXT NOT NULL, expires_at TEXT NOT NULL, used_at TEXT ); CREATE INDEX account_links_user ON account_links (user_id); CREATE INDEX account_links_pair ON account_links (pair_id); -- Outgoing mail, sent by the mail worker with retries. Links in bodies are never logged. CREATE TABLE mail_outbox ( id TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK (kind IN ( 'invite', 'reset', 'email_old', 'email_new', 'email_done', 'notice', 'test', 'security' )), to_addr TEXT NOT NULL, subject TEXT NOT NULL, body_text TEXT NOT NULL, state TEXT NOT NULL CHECK (state IN ('pending', 'sent', 'failed', 'abandoned')), attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0), next_attempt_at TEXT NOT NULL, last_error TEXT, created_at TEXT NOT NULL, sent_at TEXT ); CREATE INDEX mail_outbox_due ON mail_outbox (state, next_attempt_at); -- Back-channel logout deliveries to clients, sent by the back-channel worker. CREATE TABLE backchannel_outbox ( id TEXT PRIMARY KEY, client_id TEXT NOT NULL REFERENCES clients (id) ON DELETE CASCADE, sid TEXT, sub TEXT NOT NULL, reason TEXT NOT NULL, state TEXT NOT NULL CHECK (state IN ('pending', 'sent', 'failed', 'abandoned')), attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0), next_attempt_at TEXT NOT NULL, last_error TEXT, created_at TEXT NOT NULL, done_at TEXT ); CREATE INDEX backchannel_outbox_due ON backchannel_outbox (state, next_attempt_at); -- The change feed for machine clients. seq is the cursor. Rows older than 30 days are pruned. CREATE TABLE directory_changes ( seq INTEGER PRIMARY KEY AUTOINCREMENT, sub TEXT NOT NULL, kind TEXT NOT NULL CHECK (kind IN ('member', 'deleted')), at TEXT NOT NULL ); CREATE INDEX directory_changes_at ON directory_changes (at); -- Append-only record of changes. actor_id and target_id are not foreign keys, so the trail -- survives the deletion of the user it names. data never holds secrets. CREATE TABLE audit_log ( id TEXT PRIMARY KEY, at TEXT NOT NULL, actor_id TEXT, actor_kind TEXT NOT NULL CHECK (actor_kind IN ('user', 'client', 'cli', 'system', 'anonymous')), action TEXT NOT NULL, target_kind TEXT, target_id TEXT, ip TEXT, data TEXT CHECK (data IS NULL OR json_valid (data)) ); CREATE INDEX audit_log_at ON audit_log (at); CREATE INDEX audit_log_actor ON audit_log (actor_id, at); CREATE INDEX audit_log_target ON audit_log (target_kind, target_id, at); -- Runtime setting overrides (admin UI values and the sealed SMTP password). Precedence is -- override, then config file, then built-in default. CREATE TABLE settings ( key TEXT PRIMARY KEY, value TEXT NOT NULL, updated_at TEXT NOT NULL );