Migrate the OAuth provider: rotation with reuse detection, auth_time in seconds #91

Merged
nelsong6 merged 4 commits from oauth-provider-migration into main 2026-08-11 20:22:20 -07:00
nelsong6 commented 2026-08-11 20:06:35 -07:00 (Migrated from github.com)

Stage 3 of the Chess Tactics authentication audit, in this repository because two of its findings
are here rather than there.

Why

Two normative defects in the deprecated oidcProvider plugin, confirmed still present in its own
latest release (1.6.27) and therefore never going to be fixed in it:

  • Refresh rotation never invalidated the presented token. The old code created a new row and
    left the previous refresh token valid until its own expiry, so replaying one produced no conflict
    and rotation bought nothing (RFC 9700 §4.14.2). It also re-stamped the expiry on every refresh,
    so an active chain never expired at all.
  • auth_time was emitted in milliseconds where OIDC Core §2 requires seconds, so a relying
    party checking authentication freshness saw a time in the far future and passed unconditionally.

What this is

Not a rename. Three structural differences:

  1. Clients are data. No trustedClients option; src/oauth-clients.ts declares the four and
    reconciles them into oauth_client at boot, awaited before serve(). A confidential client
    with no secret in the environment throws rather than registering as one anybody could
    impersonate.
  2. One claims hook became three. customIdTokenClaims and customUserInfoClaims both feed
    platformClaims. This is the dangerous one: losing role or groups raises no error, it just
    demotes every Grafana user to Viewer and stops Argo CD matching any RBAC rule.
  3. We own the client-secret hash, so boot seeding needn't go through the plugin's registration
    endpoints.

drizzle/0002 is expand/contract — the two colliding legacy tables are renamed aside, never
dropped, so a rollback finds its data intact.

Verification

npm run test:oauth boots the provider over PGlite (Postgres as WASM) and drives the real HTTP
surface with real Request objects. Eight tests: the schema applies; reconciliation is idempotent
and preserves row ids; a real login carries role/groups/apps in both the id_token and
userinfo
; auth_time is seconds, asserted by wall-clock skew; a rotated refresh token is
refused and replaying it revokes the family
, checked in the database; a confidential client
cannot redeem without its secret; a code is single-use; a bad PKCE verifier is refused; and the
shipped SQL produces exactly the columns the drizzle models map to.

Writing them found two real defects that no typecheck would have caught: the camelCase/snake_case
table mismatch between Better Auth's generator and this repo's drizzle models, and a client secret
written raw where the plugin expected a hash.

Whole suite: 167 passing, tsc --noEmit clean.

Before rolling out

  1. OIDC_CHESS_TACTICS_CLIENT_SECRET in Key Vault, mounted here and in Chess Tactics.
  2. Apply drizzle/0002 before rolling the image.
  3. Every live Grafana, Argo CD and ambience session ends at cutover — the old opaque tokens are
    meaningless to the new plugin.
  4. Watch the first real Grafana and Argo CD login. The claims are proven here; their consumption is
    configured in those systems.
  5. drizzle/0003 drops the *_legacy tables once confirmed — not before.

Pairs with romaine-life/chess-tactics#920, and note the ordering constraint in
docs/oauth-provider-migration.md: fixing rotation without raising the refresh lifetime would
shorten Chess Tactics sessions, because today's bug is the only reason an active chain outlives
seven days. Both changes are in this branch for that reason.

Stage 3 of the Chess Tactics authentication audit, in this repository because two of its findings are here rather than there. ## Why Two normative defects in the deprecated `oidcProvider` plugin, confirmed still present in its own latest release (1.6.27) and therefore never going to be fixed in it: - **Refresh rotation never invalidated the presented token.** The old code created a new row and left the previous refresh token valid until its own expiry, so replaying one produced no conflict and rotation bought nothing (RFC 9700 §4.14.2). It also re-stamped the expiry on every refresh, so an active chain never expired at all. - **`auth_time` was emitted in milliseconds** where OIDC Core §2 requires seconds, so a relying party checking authentication freshness saw a time in the far future and passed unconditionally. ## What this is Not a rename. Three structural differences: 1. **Clients are data.** No `trustedClients` option; `src/oauth-clients.ts` declares the four and reconciles them into `oauth_client` at boot, awaited before `serve()`. A confidential client with no secret in the environment throws rather than registering as one anybody could impersonate. 2. **One claims hook became three.** `customIdTokenClaims` and `customUserInfoClaims` both feed `platformClaims`. This is the dangerous one: losing `role` or `groups` raises no error, it just demotes every Grafana user to Viewer and stops Argo CD matching any RBAC rule. 3. **We own the client-secret hash**, so boot seeding needn't go through the plugin's registration endpoints. `drizzle/0002` is expand/contract — the two colliding legacy tables are renamed aside, never dropped, so a rollback finds its data intact. ## Verification `npm run test:oauth` boots the provider over **PGlite** (Postgres as WASM) and drives the real HTTP surface with real `Request` objects. Eight tests: the schema applies; reconciliation is idempotent and preserves row ids; **a real login carries `role`/`groups`/`apps` in both the id_token and userinfo**; **`auth_time` is seconds**, asserted by wall-clock skew; **a rotated refresh token is refused and replaying it revokes the family**, checked in the database; a confidential client cannot redeem without its secret; a code is single-use; a bad PKCE verifier is refused; and the shipped SQL produces exactly the columns the drizzle models map to. Writing them found two real defects that no typecheck would have caught: the camelCase/snake_case table mismatch between Better Auth's generator and this repo's drizzle models, and a client secret written raw where the plugin expected a hash. Whole suite: 167 passing, `tsc --noEmit` clean. ## Before rolling out 1. `OIDC_CHESS_TACTICS_CLIENT_SECRET` in Key Vault, mounted here and in Chess Tactics. 2. Apply `drizzle/0002` before rolling the image. 3. **Every live Grafana, Argo CD and ambience session ends at cutover** — the old opaque tokens are meaningless to the new plugin. 4. Watch the first real Grafana and Argo CD login. The claims are proven here; their consumption is configured in those systems. 5. `drizzle/0003` drops the `*_legacy` tables once confirmed — not before. Pairs with romaine-life/chess-tactics#920, and note the ordering constraint in `docs/oauth-provider-migration.md`: fixing rotation without raising the refresh lifetime would *shorten* Chess Tactics sessions, because today's bug is the only reason an active chain outlives seven days. Both changes are in this branch for that reason.
Sign in to join this conversation.
No description provided.