# Accounts and sign-in Status: built. Provider sign-in is tested against an in-process fake provider only; no real GitHub, Discord or Google app has been used yet. Last updated 2026-11-25. The account system follows **piki**, the owner's Go project (`../piki`). Piki's `agents/data-auth-services.md` maps its auth code; the schema is in `internal/db/migrations/` (`013_oauth.sql`, `016_google_sso_sessions.sql`). Where this document says "as in piki", that is the behaviour to match. ## Principles - **The lore-forge account is the identity.** GitHub, Discord and Google are ways to sign in to an existing account, not sources of identity. - **No second factor is required.** The "2FA" in the original brief meant sign-in through those providers. - **`loreserver` never sees credentials.** It trusts tokens that lore-forge signs. ## Getting an account As in piki: - **First run:** `/setup` creates the instance owner with a username and password. It is only available while no user exists. - **Signup mode:** an owner setting with three values. `closed` (the default): admins create accounts. `invite`: only invite links work. `open`: anyone who can reach the site signs up at `/signup`. - **Invite keys:** admins create keys with a name, a maximum number of uses and an active flag; each use is recorded. Signup with a key uses `/signup/`. Invite links work in the `invite` and `open` modes. The use is counted in the same transaction that creates the account, so a key cannot be used more often than allowed by racing requests. - **Admin-created accounts:** admins create an account with a password from `/admin`, or with `lore-forge create-user` on the server. ## Signing in Two methods, both ending in the same kind of session: 1. **Username and password.** 2. **A linked provider:** GitHub, Discord or Google. Passkeys are not offered; see below. Password sign-in is rate limited in memory per username: ten failures within 15 minutes block further attempts for that username until the window passes. The limit resets on restart. ### Provider sign-in As in piki: - **Existing accounts only.** A provider can sign a person in only after they linked it from their profile while signed in. - **Explicit linking, never by email.** A link is keyed by `(provider, subject id)`. Sign-ins are never matched to accounts by email address or username. This prevents takeover through a provider account that merely shares an email. - **One link per provider per account.** - **Provider tokens are not kept.** They are used once to read the identity, then revoked (GitHub, Discord) or discarded (Google). - **Flows are single-use.** Each sign-in or link attempt is a row with a hashed state value, an intent (`signin` or `link`), a PKCE verifier, a nonce where the provider supports it, and an expiry. - **Start is a CSRF-protected POST** from the login form, so the "stay signed in" choice travels with the flow. Protocol notes: - GitHub and Discord are plain OAuth 2.0 authorization-code flows followed by an identity request. - Google is OpenID Connect with PKCE and a per-flow nonce; identity comes from the ID token. The ID token's signature is not checked: it arrives directly from Google's token endpoint over TLS, which OpenID Connect allows. Its issuer, audience, expiry and nonce are checked. - Discord's flow uses no PKCE, as in piki. - A flow expires after 10 minutes and is consumed with a single conditional update, so a state value works once. - Unlinking is refused when the provider is the account's last sign-in method and it has no password. **Assumed, not verified:** the providers' endpoints and response shapes are taken from piki and the providers' documentation. Tests run the whole flow against a fake provider served in-process; the first real sign-in will confirm them. ### Provider configuration As in piki: - The owner enters each provider's client ID and secret in the settings UI and enables it. - The settings page shows the exact callback URL to register with the provider. - Secrets are stored encrypted (AES-GCM) with a 32-byte key supplied through the environment. The same key protects lore-forge's token signing keys (see [token-issuer.md](token-issuer.md)). - The owner sets the instance's canonical public URL. Callback URLs and the issuer URL derive from it, never from request headers. ### Passkeys **Dropped for now (owner, 2026-11-25).** Passwords and provider sign-in cover a self-hosted studio, and passkeys would bring WebAuthn code and the UI's first script. If they return, the design is piki's: optional, registered from the settings while signed in, usernameless sign-in with user verification, only the public key stored. ## Sessions As in piki: - A session token is random; only its hash is stored. - Cookies are `HttpOnly`, `Secure` in production and `SameSite=Lax`. Unsafe methods require a CSRF token. - "Stay signed in" keeps a session for 30 days after last use, up to 90 days in total. - Each session records how it was created (password or provider) and its user agent. - Users see their active sessions in their settings and can revoke one or all others. - Changing the password ends the account's other sessions. Disabling an account ends all its sessions and CLI logins. ### What lore-forge adds Lore CLI logins appear in the same list. Each `lore auth login` produces a refresh token; it is listed beside browser sessions with its creation time and last use, and revoking it ends that CLI login. See [token-issuer.md](token-issuer.md). ## Device approval `lore auth login` opens the browser on a lore-forge page carrying a short code. - If the person is not signed in, they sign in by any method above. - They then see which device is asking and approve or deny. - If already signed in, approval is one click. This page is part of the web UI and uses the ordinary browser session. ## Instance roles Separate from repository permissions: | Role | Can | | --- | --- | | Owner | Everything, including provider configuration and instance settings | | Admin | Manage users, invites and organisations | | Member | Use the instance according to their grants | Repository and organisation permissions are in [namespaces-and-access.md](namespaces-and-access.md). ## API tokens As in piki, a user can create named API tokens, stored as SHA-256 hashes, with scopes and an optional expiry. The secret starts with `lf_` and is shown once. Two scopes: - `api`: lore-forge's JSON API under `/api/v1` (`Authorization: Bearer`); - `lore`: logging the Lore CLI in without a browser, for CI. Lore sends it as a token exchange with the subject token type `api-key` or `urn:lore:params:oauth:token-type:api-key`. A CLI login started from a token records the token; revoking the token ends those logins. Verified with `lore auth login --token-type api-key` by `scripts/e2e-lore.sh`. ## Service accounts For CI and bots that should not act as a person. A service account belongs to a user or an organisation namespace and is a `users` row with `kind = 'service'`. Its username is `.`; person usernames cannot contain dots, so the two never collide. It has no password and cannot sign in to the web UI. It holds grants like any user. Its secrets are API tokens with the `lore` scope. It logs in with the client credentials grant: client ID is its username, client secret is a token. The login lasts an hour (or until the token expires) and gets no refresh token. Owners of the namespace manage it under `/settings/service-accounts` or `/orgs//service-accounts`. Verified with `lore auth login --client-id` by `scripts/e2e-lore.sh`. ## Identity recorded by Lore Lore writes the caller's identity into revision history permanently. **Decided (owner, 2026-11-25):** the token carries a stable internal user ID, not the username, and lore-forge answers Lore's name lookup service. Usernames can then change without rewriting history, and the CLI still shows names. ## Open questions - Whether an invite may be redeemed directly with a provider, creating an account that never has a password. Piki does not allow this; the design currently matches piki. - Whether usernames can be changed. The identity decision above allows it as far as Lore history is concerned; it still depends on namespace renames (see [namespaces-and-access.md](namespaces-and-access.md)).