airi/server/apps/auth
RainbowBird b3a6d60718
Some checks failed
CI / Detect changes (push) Has been cancelled
Cloudflare Pages (Auth UI) / Deploy - ui-server-auth (push) Has been cancelled
Cloudflare Workers / Build - docs (push) Has been cancelled
E2E Test - Tamagotchi (Linux) / X11 window render (Xvfb) (push) Has been cancelled
E2E Test - Tamagotchi (Linux) / Wayland window render (weston headless) (push) Has been cancelled
CI / Build Test (stage-tamagotchi) (push) Has been cancelled
CI / Unit Test (stage-tamagotchi) (push) Has been cancelled
CI / Unit Test (stage-ui-1) (push) Has been cancelled
CI / Lint (push) Has been cancelled
CI / Build Test (stage-web) (push) Has been cancelled
CI / Build Test (ui-playgrounds) (push) Has been cancelled
CI / Build Test (stage-tamagotchi-godot) (push) Has been cancelled
CI / Unit Test (others) (push) Has been cancelled
CI / Unit Test (api-server-1) (push) Has been cancelled
CI / Unit Test (api-server-2) (push) Has been cancelled
CI / Unit Test (stage-ui-2) (push) Has been cancelled
CI / Unit Test (push) Has been cancelled
CI / Type Check (others) (push) Has been cancelled
CI / Type Check (desktop) (push) Has been cancelled
CI / Type Check (web) (push) Has been cancelled
CI / Type Check (push) Has been cancelled
CI / Check Provenance (push) Has been cancelled
Cloudflare Workers / Deploy - stage-web (push) Has been cancelled
feat(api-server): add a script to rotate the LLM router master key (#3545)
## Summary

- Add `server/apps/api/src/scripts/rotate-llm-router-master-key`. It
re-encrypts stored provider keys from `LLM_ROUTER_MASTER_KEY_PREVIOUS`
to `LLM_ROUTER_MASTER_KEY`.
- Document the `LLM_ROUTER_MASTER_KEY` rotation in the API README.
- Document the `BETTER_AUTH_SECRET` rotation in the Auth README. It
needs no code: Better Auth reads `BETTER_AUTH_SECRETS` itself, and
expiring the old JWKS keys makes the jwt plugin create a key under the
new secret.

## Context

The maintainer asked for a script to rotate `LLM_ROUTER_MASTER_KEY` and
`BETTER_AUTH_SECRET`, plus a rotation plan.

Replacing `BETTER_AUTH_SECRET` alone breaks OIDC. The jwt plugin
encrypts the JWKS private keys with the secret, so token signing fails
after the change. Better Auth 1.6.25 supports versioned `secrets` for
this case.

This PR does not change any deployed variable. The rotation itself is a
separate operator step.

## Change map

| Module | Before | After |
| --- | --- | --- |
| API envelope crypto users | AAD helpers were private |
`providerConfigAad` and `STREAM_MODEL_LABEL_FALLBACK` are exported for
the script |
| API rotation script | none | Re-encrypts `config_kv`
`LLM_ROUTER_CONFIG`, `UNSPEECH_UPSTREAM`, and
`user_provider_configs.config` |
| Auth README | no rotation steps | `BETTER_AUTH_SECRETS` plus JWKS key
expiry, no code change |

## Architecture and behavior

```mermaid
sequenceDiagram
  participant Op as Operator
  participant API as API container
  participant PG as Postgres
  participant R as Redis
  Op->>API: rotate-llm-router-master-key --apply
  API->>PG: SELECT config_kv ... FOR UPDATE
  API->>API: decrypt with previous key, encrypt with current key (same AAD)
  API->>PG: UPDATE config_kv
  API->>R: DEL config:<key>, PUBLISH configkv:invalidate
  API->>PG: UPDATE user_provider_configs WHERE id = ? AND config = <old>
  API->>PG: second read-only pass
  API-->>Op: exit 0 / 1 / 2
```

Exit codes of the script: `0` means the old key can be removed. `1`
means some values open with no configured key. `2` means some values
still need the old key.

## Boundaries and risks

| Invariant or boundary | Failure mode | Protection | Evidence or gap |
| --- | --- | --- | --- |
| AAD must match each reader | Re-encrypted key cannot be read | Uses
the model map key, `streaming-tts`, and `providerConfigAad`, the same as
the readers | Unit tests in `rotation.test.ts` |
| Concurrent admin write to ConfigKV | Script overwrites a new config |
Row lock in one transaction | Not run against a real database |
| Concurrent user edit of a provider config | Script overwrites the edit
| Update matches the old ciphertext. A miss counts as a conflict | Not
run against a real database |
| Other ConfigKV fields | Rewrite drops or adds fields | `looseObject`
schemas keep unknown fields and add no defaults | Unit tests check
`headers`, `provider`, and `defaults` |
| Wrong old key | Data is lost | Values that neither key opens stay
unchanged and give exit code 1 | Unit tests |
| JWKS keys after an auth secret rotation | Removing
`BETTER_AUTH_SECRETS` later breaks decryption of new keys | README says
to keep `BETTER_AUTH_SECRETS` | Documented only |

Rotating the auth secret signs out every cookie and bearer session. This
is Better Auth behavior: it signs cookies with the current secret only.
OIDC refresh tokens are stored as SHA-256 hashes and stay valid.

## Verification

Run in an rc Workspace on the first commit of this branch. The second
commit removes the auth script and only changes the Auth README.

- `pnpm -F @proj-airi/api-server typecheck`: pass
- `pnpm -F @proj-airi/api-server exec vitest run`: 995 pass, 5 fail. The
failures are PGlite 5 s timeouts in `mock-db.test.ts` and
`flux-usage-migration.test.ts`. They fail the same way on the merge base
in the same Workspace. `audio-speech-ws/route.test.ts` failed once in
the full run and passed alone.
- `pnpm exec eslint` on the changed files: no errors

Not verified: running the script against a real Postgres and Redis.

## Agent

- Harness: Claude Code
- Model: claude-opus-5-5

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-11-18 13:49:09 +08:00
..
src fix(auth,stage-ui): require an active session to link an account (#3489) 2026-11-11 21:42:49 +08:00
Dockerfile ci: reduce CI time without removing checks (#3330) 2026-11-02 17:52:33 +08:00
instrumentation.ts chore(deps): bump dependencies 2026-08-26 19:01:04 +08:00
package.json release: v0.15.4 2026-11-13 03:53:10 +07:00
README.md feat(api-server): add a script to rotate the LLM router master key (#3545) 2026-11-18 13:49:09 +08:00
tsconfig.json refactor(server): split independent auth service (#2202) 2026-08-12 23:33:58 +08:00
vitest.config.ts refactor(server): split independent auth service (#2202) 2026-08-12 23:33:58 +08:00

AIRI Auth Server

Standalone authentication and identity application for Project AIRI.

Responsibilities

  • Better Auth session, social login, magic-link, password, and OIDC flows.
  • /api/auth/*, /auth/*, and authentication discovery endpoints.
  • Auth-owned Redis configuration, transactional email, and auth telemetry.
  • Calling the resource API over the deployment's private network before deleting business data.

Code layout

The runtime is intentionally flat. Its main boundaries are:

  • auth.ts: Better Auth configuration and identity lifecycle hooks.
  • routes.ts: the complete public Auth HTTP surface and request authentication.
  • server.ts: dependency composition, health checks, and process lifecycle.
  • resource-api.ts: the single private Auth-to-resource-API boundary.
  • rate-limit.ts and otel.ts: cross-route operational policies.
  • email.ts and oidc-jwt-bearer.ts: substantial external integration modules.

Small shared contracts stay beside those boundaries (db.ts, env.ts, error.ts, and origin.ts). Tests are collected under src/tests; Better Auth schema-generation wiring is isolated under src/tooling.

Account deletion

POST /api/auth/delete-account accepts { "confirm": true } after the client confirms permanent deletion. It requires an authoritative session created within the last five minutes and returns { "success": true, "message": "User deleted" } without email confirmation. On SESSION_NOT_FRESH, the client must request sign-in again. The existing /api/auth/delete-user email flow is unchanged. See deletion policy and rollout.

Run locally

pnpm -F @proj-airi/auth-server dev

The service reads .env.local from this directory. PUBLIC_URL is the public issuer origin presented through Caddy; RESOURCE_SERVER_URL is the private resource API used for internal calls.

To run PostgreSQL, Redis, the resource API, and Auth together from the repository root:

pnpm dev:backend

server/docker-compose.yaml exposes only the local Caddy gateway on http://localhost:6112; API and Auth stay on its private network. The internal /internal/* boundary has no application token, and Caddy rejects that path at the public edge.

Native Google sign-in

Set AUTH_GOOGLE_NATIVE_CLIENT_IDS to a comma-separated list of additional Google OAuth client IDs. For Android Credential Manager, include the Web client ID passed as serverClientId.

  • For the standalone Auth process, set the variable in server/apps/auth/.env.local.
  • For pnpm dev:backend, set it in server/apps/api/.env.local. Compose loads server/apps/api/.env and .env.local into the Auth container, in that order. It does not load server/apps/auth/.env.local. Run pnpm dev:backend again after edits so Compose recreates the container with the updated values.
  • For Railway, set it in the Auth service variables for the target environment.
AUTH_GOOGLE_NATIVE_CLIENT_IDS=123456789-native.apps.googleusercontent.com

The original AUTH_GOOGLE_CLIENT_ID stays first in the provider configuration. Browser authorization still uses that client and AUTH_GOOGLE_CLIENT_SECRET. Native ID tokens can use any configured audience. Better Auth checks the token signature, issuer, expiry, and supplied nonce. Omit the new variable to keep the existing configuration. No database migration is required.

Google ID token sign-in can create an account without a Google access or refresh token. Account deletion continues when neither token is stored, because AIRI has no Google API credential to revoke. This does not revoke consent in the user's Google Account. If either token is stored, Auth must complete its existing revocation policy before it deletes AIRI data.

Railway

Deploy this as the Auth Railway service. Keep the service Root Directory at the repository root because the Dockerfile copies workspace manifests and server/packages/auth-shared. The project-level Infrastructure as Code file is proj-airi/airi-railway/.railway/railway.ts. It owns the Dockerfile, start command, /readyz healthcheck, and watch patterns.

Set PUBLIC_URL to this service's canonical public issuer URL, and make the Resource API's AUTH_SERVER_URL exactly the same value. Set RESOURCE_SERVER_URL from the Resource API's Railway private domain; do not run shared database migrations from Auth. See server/README.md for the complete service contract.

Rotate BETTER_AUTH_SECRET

Better Auth uses the secret in two ways:

  • It signs session cookies, bearer session tokens, OAuth state, and email verification tokens with the current secret only.
  • It encrypts the JWKS private keys in jwks.private_key. OIDC access tokens are signed with these keys.

A rotation signs out every cookie and bearer session, and it invalidates unused verification links. OIDC refresh tokens stay valid.

Do not only replace BETTER_AUTH_SECRET. Then Better Auth cannot decrypt the JWKS private keys, and OIDC sign-in fails. Use BETTER_AUTH_SECRETS instead. It holds <version>:<secret> pairs, and the first pair is the current secret.

Better Auth reads BETTER_AUTH_SECRETS from the environment itself, so a rotation needs no code.

Steps:

  1. Generate a secret: openssl rand -base64 32.
  2. Set BETTER_AUTH_SECRETS=2:<new secret>. Keep the old value in BETTER_AUTH_SECRET, so Better Auth can still read the current JWKS keys. Deploy.
  3. Expire the current JWKS keys: UPDATE jwks SET expires_at = now() WHERE expires_at IS NULL OR expires_at > now();
  4. Sign in once. The jwt plugin signs with the newest key only. The newest key is expired, so the plugin creates a key under the new secret.
  5. Give the old keys one more hour: UPDATE jwks SET expires_at = now() + interval '1 hour' WHERE id IN (<old key ids>);. The plugin still signs with the new key, because it picks the newest key by creation time. The Auth service's own JWT check (oidc-jwt-bearer.ts) skips expired keys, so without this step it rejects access tokens that the old keys signed.
  6. Set BETTER_AUTH_SECRET to the new secret and deploy. No stored value needs the old secret now.
  7. After the hour, delete the old keys. Access tokens live one hour, so no valid token uses them.

The JWKS endpoint keeps expired keys for 30 days. Until step 7, someone with the old secret and database access can still read the old private keys.

Keep BETTER_AUTH_SECRETS after the rotation. New keys use its versioned format, and Better Auth reads that format only when BETTER_AUTH_SECRETS is set. For the next rotation, put the new pair first, for example 3:<newer secret>,2:<new secret>.

Do not use it for

  • Product APIs, billing, model routing, chat, or WebSocket business state.
  • Importing modules from server/apps/api.
  • Running the shared database migration history during normal process startup.

Auth tables and principal contracts live in @proj-airi/auth-shared. Drizzle reads shared migration files during API startup. The API remains the migration owner.