im
Clone
https://git.tahinli.com/Tahinli/im.git
0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits
| 1 | -- 0001: the whole initial schema of im. Every table, its constraints and its lookup indexes. |
| 2 | -- Ids are ULID text; timestamps are fixed-width UTC RFC 3339 text; booleans are INTEGER 0/1. |
| 3 | -- Never edit this file once it has been applied anywhere; add 0002_... instead. |
| 4 | |
| 5 | ( |
| 6 | id TEXT PRIMARY KEY, |
| 7 | email TEXT NOT NULL, |
| 8 | email_folded TEXT NOT NULL UNIQUE, |
| 9 | name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 100), |
| 10 | password_hash TEXT NOT NULL, |
| 11 | is_admin INTEGER NOT NULL DEFAULT 0 CHECK (is_admin IN (0, 1)), |
| 12 | disabled_at TEXT, |
| 13 | -- Sealed TOTP secret. Held while enrolling (totp_enabled_at NULL) and kept after enabling. |
| 14 | totp_secret_sealed TEXT, |
| 15 | totp_enabled_at TEXT, |
| 16 | totp_last_step INTEGER, |
| 17 | locale TEXT NOT NULL DEFAULT 'en' CHECK (locale IN ('en', 'tr')), |
| 18 | zoneinfo TEXT NOT NULL DEFAULT 'Europe/Istanbul', |
| 19 | theme TEXT NOT NULL DEFAULT 'system' CHECK (theme IN ('system', 'light', 'dark')), |
| 20 | photo_mime TEXT, |
| 21 | picture_version INTEGER NOT NULL DEFAULT 0, |
| 22 | created_at TEXT NOT NULL, |
| 23 | updated_at TEXT NOT NULL |
| 24 | ); |
| 25 | |
| 26 | -- Ten codes per set, shown once. Only their hashes are stored; used_at marks a consumed code. |
| 27 | ( |
| 28 | user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE, |
| 29 | code_hash TEXT NOT NULL, |
| 30 | used_at TEXT, |
| 31 | PRIMARY KEY (user_id, code_hash) |
| 32 | ); |
| 33 | |
| 34 | -- Browser sessions. The id is the OIDC `sid`. Ended sessions are deleted, after their |
| 35 | -- back-channel logout jobs are queued. |
| 36 | ( |
| 37 | id TEXT PRIMARY KEY, |
| 38 | user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE, |
| 39 | token_hash TEXT NOT NULL UNIQUE, |
| 40 | csrf_token TEXT NOT NULL, |
| 41 | created_at TEXT NOT NULL, |
| 42 | expires_at TEXT NOT NULL, |
| 43 | last_seen_at TEXT NOT NULL, |
| 44 | ip TEXT, |
| 45 | user_agent TEXT, |
| 46 | auth_time TEXT NOT NULL, |
| 47 | amr TEXT NOT NULL CHECK (amr IN ('pwd', 'pwd otp', 'pwd rc')) |
| 48 | ); |
| 49 | ON sessions (user_id); |
| 50 | ON sessions (expires_at); |
| 51 | |
| 52 | -- OIDC clients. secret_hash is the hash of the client secret; the secret itself is shown once. |
| 53 | ( |
| 54 | id TEXT PRIMARY KEY, |
| 55 | name TEXT NOT NULL, |
| 56 | secret_hash TEXT NOT NULL, |
| 57 | secret_rotated_at TEXT NOT NULL, |
| 58 | redirect_uris TEXT NOT NULL DEFAULT '[]' CHECK (json_valid (redirect_uris)), |
| 59 | post_logout_redirect_uris TEXT NOT NULL DEFAULT '[]' CHECK (json_valid (post_logout_redirect_uris)), |
| 60 | backchannel_logout_uri TEXT, |
| 61 | allowed_scopes TEXT NOT NULL, |
| 62 | service_key TEXT UNIQUE, |
| 63 | created_at TEXT NOT NULL, |
| 64 | disabled_at TEXT |
| 65 | ); |
| 66 | |
| 67 | -- The switcher list. im's own row (key 'im') is maintained at boot from base_url. |
| 68 | ( |
| 69 | key TEXT PRIMARY KEY CHECK ( |
| 70 | length(key) BETWEEN 1 AND 32 |
| 71 | AND key GLOB '[a-z]*' |
| 72 | AND key NOT GLOB '*[^a-z0-9-]*' |
| 73 | ), |
| 74 | name TEXT NOT NULL, |
| 75 | url TEXT NOT NULL, |
| 76 | position INTEGER NOT NULL DEFAULT 0, |
| 77 | client_id TEXT REFERENCES clients (id) ON DELETE SET NULL, |
| 78 | healthy INTEGER CHECK (healthy IS NULL OR healthy IN (0, 1)), |
| 79 | checked_at TEXT, |
| 80 | check_ms INTEGER, |
| 81 | check_error TEXT |
| 82 | ); |
| 83 | |
| 84 | -- One-time authorization codes. A second use of a used code revokes the refresh tokens issued |
| 85 | -- from it (refresh_tokens.code_hash). |
| 86 | ( |
| 87 | code_hash TEXT PRIMARY KEY, |
| 88 | client_id TEXT NOT NULL REFERENCES clients (id) ON DELETE CASCADE, |
| 89 | user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE, |
| 90 | session_id TEXT NOT NULL REFERENCES sessions (id) ON DELETE CASCADE, |
| 91 | redirect_uri TEXT NOT NULL, |
| 92 | scope TEXT NOT NULL, |
| 93 | nonce TEXT, |
| 94 | -- PKCE S256 is mandatory for every client (contracts.md 2.3). |
| 95 | code_challenge TEXT NOT NULL, |
| 96 | expires_at TEXT NOT NULL, |
| 97 | used_at TEXT |
| 98 | ); |
| 99 | ON auth_codes (expires_at); |
| 100 | |
| 101 | -- Refresh tokens, rotated on every use. Presenting a used token revokes its whole family. |
| 102 | ( |
| 103 | id TEXT PRIMARY KEY, |
| 104 | token_hash TEXT NOT NULL UNIQUE, |
| 105 | family_id TEXT NOT NULL, |
| 106 | client_id TEXT NOT NULL REFERENCES clients (id) ON DELETE CASCADE, |
| 107 | user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE, |
| 108 | session_id TEXT NOT NULL REFERENCES sessions (id) ON DELETE CASCADE, |
| 109 | -- Plain text, not a foreign key: authorization codes are swept while their tokens live on. |
| 110 | code_hash TEXT, |
| 111 | scope TEXT NOT NULL, |
| 112 | created_at TEXT NOT NULL, |
| 113 | expires_at TEXT NOT NULL, |
| 114 | used_at TEXT, |
| 115 | revoked_at TEXT |
| 116 | ); |
| 117 | ON refresh_tokens (family_id); |
| 118 | ON refresh_tokens (session_id); |
| 119 | |
| 120 | -- Which clients a browser session has signed in to. Drives back-channel logout and the |
| 121 | -- "connected apps" list. |
| 122 | ( |
| 123 | session_id TEXT NOT NULL REFERENCES sessions (id) ON DELETE CASCADE, |
| 124 | client_id TEXT NOT NULL REFERENCES clients (id) ON DELETE CASCADE, |
| 125 | first_at TEXT NOT NULL, |
| 126 | last_at TEXT NOT NULL, |
| 127 | PRIMARY KEY (session_id, client_id) |
| 128 | ); |
| 129 | ON client_sessions (client_id); |
| 130 | |
| 131 | -- Token-signing keys. kid is the first 16 hex characters of the SHA-256 of the public key. The |
| 132 | -- current key is the newest one that is active and not retired. |
| 133 | ( |
| 134 | kid TEXT PRIMARY KEY, |
| 135 | private_sealed TEXT NOT NULL, |
| 136 | public_raw BLOB NOT NULL, |
| 137 | created_at TEXT NOT NULL, |
| 138 | activates_at TEXT NOT NULL, |
| 139 | retires_at TEXT |
| 140 | ); |
| 141 | ON signing_keys (activates_at) WHERE retires_at IS NULL; |
| 142 | |
| 143 | -- Invitations. The token is stored as its hash. A new invite for an address deletes its older |
| 144 | -- pending invites. |
| 145 | ( |
| 146 | token_hash TEXT PRIMARY KEY, |
| 147 | email_folded TEXT NOT NULL, |
| 148 | is_admin INTEGER NOT NULL DEFAULT 0 CHECK (is_admin IN (0, 1)), |
| 149 | invited_by TEXT REFERENCES users (id) ON DELETE SET NULL, |
| 150 | created_at TEXT NOT NULL, |
| 151 | expires_at TEXT NOT NULL, |
| 152 | accepted_at TEXT |
| 153 | ); |
| 154 | ON invites (email_folded); |
| 155 | |
| 156 | -- Single-use links for password reset and both halves of an email change. The two halves of one |
| 157 | -- email change share pair_id. |
| 158 | ( |
| 159 | token_hash TEXT PRIMARY KEY, |
| 160 | kind TEXT NOT NULL CHECK (kind IN ('reset', 'email_old', 'email_new')), |
| 161 | user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE, |
| 162 | pair_id TEXT, |
| 163 | new_email TEXT, |
| 164 | created_at TEXT NOT NULL, |
| 165 | expires_at TEXT NOT NULL, |
| 166 | used_at TEXT |
| 167 | ); |
| 168 | ON account_links (user_id); |
| 169 | ON account_links (pair_id); |
| 170 | |
| 171 | -- Outgoing mail, sent by the mail worker with retries. Links in bodies are never logged. |
| 172 | ( |
| 173 | id TEXT PRIMARY KEY, |
| 174 | kind TEXT NOT NULL CHECK (kind IN ( |
| 175 | 'invite', 'reset', 'email_old', 'email_new', 'email_done', 'notice', 'test', 'security' |
| 176 | )), |
| 177 | to_addr TEXT NOT NULL, |
| 178 | subject TEXT NOT NULL, |
| 179 | body_text TEXT NOT NULL, |
| 180 | state TEXT NOT NULL CHECK (state IN ('pending', 'sent', 'failed', 'abandoned')), |
| 181 | attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0), |
| 182 | next_attempt_at TEXT NOT NULL, |
| 183 | last_error TEXT, |
| 184 | created_at TEXT NOT NULL, |
| 185 | sent_at TEXT |
| 186 | ); |
| 187 | ON mail_outbox (state, next_attempt_at); |
| 188 | |
| 189 | -- Back-channel logout deliveries to clients, sent by the back-channel worker. |
| 190 | ( |
| 191 | id TEXT PRIMARY KEY, |
| 192 | client_id TEXT NOT NULL REFERENCES clients (id) ON DELETE CASCADE, |
| 193 | sid TEXT, |
| 194 | sub TEXT NOT NULL, |
| 195 | reason TEXT NOT NULL, |
| 196 | state TEXT NOT NULL CHECK (state IN ('pending', 'sent', 'failed', 'abandoned')), |
| 197 | attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0), |
| 198 | next_attempt_at TEXT NOT NULL, |
| 199 | last_error TEXT, |
| 200 | created_at TEXT NOT NULL, |
| 201 | done_at TEXT |
| 202 | ); |
| 203 | ON backchannel_outbox (state, next_attempt_at); |
| 204 | |
| 205 | -- The change feed for machine clients. seq is the cursor. Rows older than 30 days are pruned. |
| 206 | ( |
| 207 | seq INTEGER PRIMARY KEY AUTOINCREMENT, |
| 208 | sub TEXT NOT NULL, |
| 209 | kind TEXT NOT NULL CHECK (kind IN ('member', 'deleted')), |
| 210 | at TEXT NOT NULL |
| 211 | ); |
| 212 | ON directory_changes (at); |
| 213 | |
| 214 | -- Append-only record of changes. actor_id and target_id are not foreign keys, so the trail |
| 215 | -- survives the deletion of the user it names. data never holds secrets. |
| 216 | ( |
| 217 | id TEXT PRIMARY KEY, |
| 218 | at TEXT NOT NULL, |
| 219 | actor_id TEXT, |
| 220 | actor_kind TEXT NOT NULL CHECK (actor_kind IN ('user', 'client', 'cli', 'system', 'anonymous')), |
| 221 | action TEXT NOT NULL, |
| 222 | target_kind TEXT, |
| 223 | target_id TEXT, |
| 224 | ip TEXT, |
| 225 | data TEXT CHECK (data IS NULL OR json_valid (data)) |
| 226 | ); |
| 227 | ON audit_log (at); |
| 228 | ON audit_log (actor_id, at); |
| 229 | ON audit_log (target_kind, target_id, at); |
| 230 | |
| 231 | -- Runtime setting overrides (admin UI values and the sealed SMTP password). Precedence is |
| 232 | -- override, then config file, then built-in default. |
| 233 | ( |
| 234 | key TEXT PRIMARY KEY, |
| 235 | value TEXT NOT NULL, |
| 236 | updated_at TEXT NOT NULL |
| 237 | ); |