# ADR-0131: Keycloak login and the grant broker ## Status Accepted (2026-10-15). DEV first; stage and World0 need their own realm, issuer key and review. ## Context Region servers accept 160-byte session grants signed with an ECDSA P-256 issuer key (ADR-0091). Grants are single-use per account: each carries a serial that must exceed the newest one the world accepted, and a session can be extended with a newer grant (`SessionRefresh`). Until now grants were minted offline by `evo-session-probe grants` with the issuer key on the operator's host, for hand-made accounts. Players need to register, log in and log out, and grants must never be minted on a client. ## Decision ### Identity - Keycloak realm `aeternalis` at `auth.aeternalisgames.com` is the identity provider: registration, email verification, password reset and MFA live there. Users sign in with their email. - The game client is the public client `aevolution-game`: authorization code with PKCE S256 in the system browser, redirect `http://127.0.0.1:47600/callback` (RFC 8252 loopback). Device authorization is enabled for test tools. - An audience mapper adds `aevolution-grants` to access tokens. Access tokens live 5 minutes; refresh tokens rotate and are single-use. ### The grant broker (`evo-grant-broker`) - A small HTTP service behind `grants.aeternalisgames.com` (TLS at the ingress). It holds the issuer private key (Secret `aevolution-grant-issuer`) and a database login; it never calls Keycloak. It verifies access tokens itself against the realm's public keys, pinned from the realm's JWKS in Git (a Keycloak key rotation is a Git change and a sync). - `POST /grant` with `Authorization: Bearer `: the token must be an RS256 access token (`typ` Bearer) from the realm issuer, for audience `aevolution-grants`, authorized party `aevolution-game`, unexpired (30 s leeway), with a subject and a verified email. The broker maps (issuer, subject) to a stable game account, created on first use, so registering is the first login. It takes the next grant serial for that account (above any serial the world has accepted), routes to the region holding the account's active life or else the configured default region, and returns a 10-minute grant with the region's endpoint. - `POST /logout` with the same bearer: the account's accepted serial is raised past every serial issued so far and its current control is expired. Outstanding grants are then stale, and a live session closes at its next control renewal (at most 5 s). Refused tokens get 401 with a JSON reason (`invalid_token`, `token_expired`, `email_not_verified`, `token_not_for_this_service`); a second grant for the same account within 2 s, 429; an unreachable database or unrouted region, 503. - `GET /healthz` for probes. - Responses carry no secret but the grant itself; nothing logs tokens or grants. ### Database (schema v39) - `account_identities(issuer, subject, account_id, last_serial, created_at)`. - `issue_account_grant(world, issuer, subject)` and `revoke_account_grants(world, issuer, subject)`, SECURITY DEFINER and granted to the runtime group role. The broker logs in with its own login in that group (DEV); a dedicated broker role is a later least-privilege step. ### Client contract 1. Sign in (authorization code + PKCE), keep the refresh token in the OS credential store, never on disk in clear. 2. `POST https://grants.aeternalisgames.com/grant` with the access token. The JSON answer is `{"grant":"","account":"","region":N,"endpoint":"host:port", "server_name":"name","expires_at":unix_seconds}`. 3. Connect with the grant. About one minute before `expires_at`, refresh the access token if needed, fetch a new grant and send `SessionRefresh`. 4. On logout: `POST /logout`, then revoke the refresh token at Keycloak's revocation endpoint. ### Client implementation (Windows engineering client) - `evo_client_auth`: the protocol parsers (`protocol.cpp`), one worker thread (`AuthClient`, `client.cpp`) and the Windows platform (`windows.cpp`). On other systems sign-in is unavailable, and the manual grant-bundle path remains. - Sign-in opens the system browser with PKCE S256 and a random state. A loopback listener on `127.0.0.1:47600` accepts exactly one `/callback` with that state and an `iss` of the realm, if present. The password never reaches the client. - The refresh token is stored only in Windows Credential Manager (`AeternalisEvolution/DEV/refresh-token/v1`). Rotation removes the stored token before it is sent, so a lost reply ends the login instead of replaying a consumed token. Access tokens and grants stay in memory and are zeroed when dropped. - A grant reply is accepted only if its JSON agrees with the grant record: marker, version, dev environment, account, region, expiry (at most 15 minutes ahead) and a nonzero serial. The region still verifies the signature. - The region CA file is still required from the launcher. Endpoint and TLS name come from the broker's route. - The session asks for a new grant about 60 s before its expiry, extended by each accepted `SessionRefresh`, and after a lost connection. It never asks for a session that was superseded or cancelled. Requests are at least 5 s apart. - Sign-out closes the session, then calls `/logout` (broker revocation) and Keycloak revocation, and clears the stored token. If revocation cannot be confirmed, the client says so: issued grants may live until they expire. ### Deployment (DEV) - `deploy/kubernetes/grant-broker-dev`, a component of `region/dev` so it runs the same pinned server image (`/usr/local/bin/evo-grant-broker`). One replica, non-root, read-only root filesystem, port 8080, probes on `/healthz`. - Out of band in `evolution-dev`: Secret `aevolution-grant-issuer` (key `issuer.key`, the raw 32-byte dev issuer scalar), Secret `aevolution-grant-broker-pg` (`pg_service.conf` with service `[evolution]`, `pgpass`, `ca.crt`) and the Ingress for `grants.aeternalisgames.com` (the Argo project admits no Ingress). - Public access is HTTPS on 443 only, terminated by ingress-nginx. Port 8080 is the in-cluster backend: a ClusterIP Service, and a NetworkPolicy that admits only the ingress-nginx pods and nothing on the LAN. Egress goes to PostgreSQL only. - The grant lifetime (600 s) must stay within the region's `REGION_M10_MAX_GRANT_LIFETIME`. ## Alternatives - **Verify Keycloak tokens in every region:** rejected; it changes the Hello wire format, puts identity mapping in every region and spreads key rotation. - **Long-lived grants without refresh:** rejected; a stolen grant would stay usable. - **Our own password store:** rejected; Keycloak already provides it properly. ## Consequences Players log in with an email account; the issuer key leaves the operator's host for a Secret; hand-minted grants remain for tests. A new login supersedes the old session (unchanged). Revocation is immediate within one renewal.