-- Phase 1 accounts: invite keys, sign-in providers, API tokens and service accounts. -- Service accounts are rows of `users` with kind 'service'. They have no password and no -- namespace of their own; their username is '.', which no person's can be, -- since person usernames may not contain '.'. ALTER TABLE users ADD COLUMN kind TEXT NOT NULL DEFAULT 'person' CHECK (kind IN ('person', 'service')); ALTER TABLE users ADD COLUMN owner_namespace TEXT COLLATE NOCASE REFERENCES namespaces (name) ON DELETE CASCADE; CREATE INDEX users_owner_namespace ON users (owner_namespace) WHERE owner_namespace IS NOT NULL; -- Invite keys, as in piki. The key is kept in plain text so admins can copy the link again. CREATE TABLE invite_keys ( id TEXT PRIMARY KEY, invite_key TEXT NOT NULL UNIQUE, name TEXT NOT NULL, max_uses INTEGER NOT NULL CHECK (max_uses >= 1), active INTEGER NOT NULL DEFAULT 1 CHECK (active IN (0, 1)), created_by TEXT REFERENCES users (id) ON DELETE SET NULL, created_at INTEGER NOT NULL ); CREATE TABLE invite_key_uses ( invite_key_id TEXT NOT NULL REFERENCES invite_keys (id) ON DELETE CASCADE, user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE, used_at INTEGER NOT NULL, PRIMARY KEY (invite_key_id, user_id) ); -- Sign-in providers, as in piki. Client secrets are AES-GCM ciphertext under the secret key. CREATE TABLE oauth_provider_configs ( provider TEXT PRIMARY KEY CHECK (provider IN ('github', 'discord', 'google')), client_id TEXT NOT NULL DEFAULT '', client_secret BLOB, enabled INTEGER NOT NULL DEFAULT 0 CHECK (enabled IN (0, 1)), updated_by TEXT REFERENCES users (id) ON DELETE SET NULL, updated_at INTEGER NOT NULL ); -- A provider account linked to a lore-forge account. Never matched by email or username. CREATE TABLE oauth_identities ( provider TEXT NOT NULL CHECK (provider IN ('github', 'discord', 'google')), subject_id TEXT NOT NULL, user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE, provider_username TEXT NOT NULL DEFAULT '', provider_display_name TEXT NOT NULL DEFAULT '', created_at INTEGER NOT NULL, last_used_at INTEGER NOT NULL, PRIMARY KEY (provider, subject_id), UNIQUE (user_id, provider) ); -- One sign-in or link attempt. Single use; the state value is stored hashed. CREATE TABLE oauth_flows ( state_hash BLOB PRIMARY KEY, provider TEXT NOT NULL CHECK (provider IN ('github', 'discord', 'google')), intent TEXT NOT NULL CHECK (intent IN ('signin', 'link')), user_id TEXT REFERENCES users (id) ON DELETE CASCADE, session_id TEXT REFERENCES sessions (id) ON DELETE CASCADE, next_path TEXT NOT NULL DEFAULT '/', pkce_verifier TEXT NOT NULL, nonce TEXT NOT NULL DEFAULT '', persistent INTEGER NOT NULL DEFAULT 0 CHECK (persistent IN (0, 1)), created_at INTEGER NOT NULL, expires_at INTEGER NOT NULL, used_at INTEGER ); CREATE INDEX oauth_flows_expiry ON oauth_flows (expires_at); -- API tokens of people and service accounts. Scopes: 'lore' signs the Lore CLI in (API-key -- token exchange, or a service account's client secret); 'api' reads the JSON API. CREATE TABLE api_tokens ( id TEXT PRIMARY KEY, token_hash BLOB NOT NULL UNIQUE, user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE, name TEXT NOT NULL, scopes TEXT NOT NULL, created_by TEXT REFERENCES users (id) ON DELETE SET NULL, created_at INTEGER NOT NULL, expires_at INTEGER, last_used_at INTEGER, revoked_at INTEGER ); CREATE INDEX api_tokens_user ON api_tokens (user_id); -- A CLI login started with an API token ends when the token is revoked. ALTER TABLE cli_logins ADD COLUMN api_token_id TEXT REFERENCES api_tokens (id) ON DELETE CASCADE;