Skip to content

feat(oauth): OAuth 2.1 server, admin list, consent and grants pages - #82

Merged
bketelsen merged 8 commits into
mainfrom
irulan/fb54dcc7
Nov 16, 2026
Merged

bketelsen merged 8 commits into
mainfrom
irulan/fb54dcc7

Conversation

@frostyard-machines

Copy link
Copy Markdown
Contributor

Summary

lodge issues agent access through OAuth 2.1 and an admin's consent page
(ADR-0023,
contract in docs/specs/oauth.md). This is task t6 of
the MCP-first plan for frostyard's apps. It reads the hearth manifest format
of frostyard/hearth#2 (schema/manifest-v1.json).

  • ADR-0023 amends 2 rules of lodge-http.md:
    "No browser route writes" (the consent decision and the grants page's
    revocation write, under same-origin, a CSRF token and the request hash) and
    "the control socket is the only place that issues credentials" (an OAuth
    token is issued only after an admin approves).
  • OAuth 2.1 on github.com/ory/fosite (v0.49.0), imported only by
    internal/oauth (internal/repocheck/fosite_test.go):
    • discovery at /.well-known/oauth-authorization-server and
      /.well-known/oauth-protected-resource
    • dynamic registration of public clients (lgc_ IDs, token_endpoint_auth_method none)
    • PKCE S256 required, plain refused
    • an http loopback IP redirect matches on any port (RFC 8252)
    • opaque HMAC tokens lgo_at_ (1 h), lgo_rt_ (30 d), lgo_ac_ (10 min);
      only signatures are stored
    • refresh rotation; a reused refresh token or code revokes its family
    • revocation (RFC 7009)
  • Admins: lodgectl allow admin LOGIN, disallow admin LOGIN and
    admins, on the control socket. Only a listed admin's tailnet login passes
    /oauth/authorize, the consent page and the grants page.
  • Consent page /oauth/consent/<id>: client, principal, app,
    capabilities and selectors from the app's hearth manifest. The admin
    narrows scopes or paths, approves or declines. A request is decided once
    and expires after 24 h; at most 64 wait.
  • Principals and grants: a client is bound to 1 principal at its first
    approved sign-in and carries scope mcp, events or both. The grants page
    lists principals, clients, grants and last use, and revokes a client or a
    principal.
  • /mcp/<id> and its lgm_ tokens are unchanged. Existing tests are
    unchanged (the diff of existing _test.go files is additions only).
  • The app ID oauth is reserved, and app.json gets an optional hearth
    member naming the hearth manifest.

Overlap: #81 (embedded NATS) touches docs/specs/lodge-http.md,
docs/specs/app-contract.md, docs/design/overview.md, AGENTS.md,
.github/risk-paths.json, cmd/lodge/main.go, internal/gateway/gateway.go,
kit/manifest/manifest.go and go.mod. Whichever merges second takes a merge
of main. The events scope is recorded now; #81's broker reads it later.

Risk tier

Risk tier: 3 — new authentication and credential issuance, and a browser route that writes.

Matched paths in .github/risk-paths.json and why the change is safe:

  • internal/oauth/** (L1, new): the OAuth server, its storage and the
    pages. Tokens are HMACs with a 32-byte key made at the first start; only
    signatures are stored (TestNoTokenInRows). Every decision needs an
    admin, same-origin, the request's CSRF token and the request hash, each
    with a negative test.
  • internal/gateway/oauth.go (L1, new): the checks of the OAuth and consent
    routes, in the order of lodge-http.md's "OAuth routes". OAuth routes refuse
    any Origin; consent routes run every identity check and the admin check.
  • internal/gateway/gateway.go (L1): 1 Config field and 1 dispatch case.
    Other routes are untouched.
  • cmd/lodge/main.go (L1): wiring only; mcpBase renamed base and shared
    with the issuer.
  • internal/allow/**, internal/control/** (L1): the admin list and its 3
    control methods, modeled on the login list.
  • internal/apps/apps.go (L1): reads the optional hearth manifest (regular
    file, 1 MiB cap, format major 1, matching app ID); a bad one disables only
    that app.
  • kit/manifest/** (L1): the reserved ID oauth and the optional hearth
    member. Additive: an existing app.json stays valid.
  • internal/store/migrations/** (L1): migration 0005 only creates new
    tables; no existing table changes.
  • internal/repocheck/** (L2): a new test of fosite's import boundary.
  • .github/risk-paths.json (L2): adds the 2 new L1 globs above.

Docs housekeeping

  • New docs started from their category's TEMPLATE.md
  • Every new doc indexed in docs/README.md
  • Cross-links added in both directions (ADR ↔ design ↔ spec ↔ plan)
  • New significant decision recorded as an ADR first, in this PR
  • New or edited ADRs are fact-only: context, decision and consequences

Verification

  • make ci green before the first push (verify, race, cross, and client-check, ui-check, shell-check in nsl)
  • make vulncheck: "No vulnerabilities found" in both modules (fosite's indirect dependencies are raised for it)
  • Tests: internal/oauth/oauth_test.go, internal/gateway/oauth_test.go
    (discovery, registration, PKCE S256, loopback redirect on a random port,
    refresh rotation, reuse revokes the family, admin only, consent negatives:
    Origin, Sec-Fetch-Site, CSRF, hash, decided twice, expired, narrowing),
    plus the admin list in allow, control and lodgectl.
  • Cross-review and revert test: recorded in a comment below.

… page

Adds the OAuth spec (docs/specs/oauth.md), the OAuth and consent routes
in lodge-http.md with the 2 amended rules, the reserved app ID oauth and
the hearth member in the app contract, Phase 7 of the roadmap, and the
user docs for admins, consent and grants.
lodge issues agent access through OAuth 2.1 on github.com/ory/fosite
(ADR-0023, docs/specs/oauth.md): discovery, dynamic registration of
public clients, PKCE S256, loopback redirects on any port, refresh
rotation with family revocation on reuse, and revocation. An admin
listed with lodgectl allow admin decides each sign-in and access
request on the consent page, under same-origin, a CSRF token and the
request hash. The grants page lists principals, clients, grants and last
use, and revokes a client or a principal. /mcp/<id> lgm_ tokens are
unchanged.
@github-actions github-actions Bot added level:L1 Read every line risk:high Touches a path in .github/risk-paths.json labels Nov 16, 2026
A sign-in approval decides the request, makes the principal and binds
the client in one transaction, with a bind that never changes a bound
client; a sign-in for another principal than the client's is refused
with 409. An access approval writes its grant in the same transaction.
Registration checks the unbound-client limit in its insert, and the
grants page takes its CSRF token and timestamp from one clock read.
A code or refresh token is consumed by one conditional update; a second
use revokes its family in a transaction of its own, after fosite's
transaction is gone. A nested rollback poisons the outer transaction,
and storage reads run in the transaction. Revoking a client or a
principal is one transaction, and so is creating a request with its
client check and the pending limit. The sweep keeps the evidence of a
used token while its family lives. The request hash sorts selector keys.
Revoking an access token revokes its family (RFC 7009, section 2.1).
The docs state each consent route's write and each refusal's status.
…mmit

A family whose revocation write fails stays denied in memory, and the
sweep writes it again until it is stored. The grant hook runs after the
approval's transaction commits.
A concurrent second use of a code or refresh token revokes its family in
fosite's own transaction, stores no token for it, and answers
invalid_grant, so the revocation is as durable as the database. A code
read as used at fosite's second read revokes its family before the
answer. Request creation re-checks the client, the principal and the
pending limit in its insert statement.
…orting it

fosite revokes a used code's tokens in 2 writes with no transaction, so
a refresh between them could issue a pair that survives. The storage now
revokes the family in 1 transaction before it reports the used code.
@frostyard-machines

Copy link
Copy Markdown
Contributor Author

Cross-review record

Reviewer: openai/gpt-6.1-sol. Coder: deepseek/deepseek-flash. Base: 78f5e20dafbfdf4294d14a97ecc3c27fa65aaf03.

Round Range Result
Initial (5 batches, all files) 78f5e20..4b50c66 26 blocking findings, all accepted
Re-review 1 4b50c66..5a38d84 2 new blocking findings, 3 weak guards
Re-review 2 5a38d84..9f0590a 3 blocking (in-memory revocation set: lost on restart, read race; a timing-based guard)
Re-review 3 9f0590a..77214f5 earlier findings fixed; 2 new: R3.1 (fosite's first-read used-code revocation is 2 writes with no transaction), R3.2 (a correction record was rewritten)

The 4 review rounds of the budget are used. Head 90d8f1c fixes R3.1 and R3.2:

  • R3.1: GetAuthorizeCodeSession revokes a used code's whole family in 1 transaction before it reports ErrInvalidatedAuthorizeCode. If that write fails, it returns another error, and nothing claims a revocation. Guard: TestUsedCodeRevokesItsFamilyFirst, which fails on 77214f5 (4 of 4 runs).
  • R3.2: the record is restored byte for byte to 9f0590a, and the new record 2026-11-16-pr-82-reuse-revokes-in-one-transaction.json is added.

No reviewer has checked 77214f5..90d8f1c yet. This PR stays Not ready until a further re-review is authorized and passes. make check, make ci (with the nsl checks) and make vulncheck pass locally at 90d8f1c.

@frostyard-machines

Copy link
Copy Markdown
Contributor Author

Cross-review: Ready at 90d8f1c

An extra re-review (re-review 4) by openai/gpt-6.1-sol checked 77214f51258f310243a58da0a8367a9271ded87a..90d8f1cf440dd9e3bc783a761192bac8d8b9c667. Its verdict is Ready, with Blocking: none. R3.1 and R3.2 are fixed, and no finding is new.

Revert test

The test ran in a scratch worktree from 90d8f1c. Each fix was reverted alone, with its tests kept, and each targeted test failed on an assertion:

Fix Reverted Test Failure
R3.1 the family revocation in GetAuthorizeCodeSession go test -count=1 -run TestUsedCodeRevokesItsFamilyFirst ./internal/oauth/ "the family's refresh token was still active while its revocation was written"
R1.1 the reusedIn skip in CreateAccessTokenSession go test -count=1 -run TestReuseRevokesInTheTransaction ./internal/oauth/ "the loser's access token = , want it never stored"
B1.1 the conditional bind AND (principal IS NULL OR principal = ?) go test -count=1 -run TestOAuthConcurrentBinding ./internal/gateway/ "2 codes and 0 conflicts, want 1 and 1"
B3.2 json.Deterministic(true) in the request hash go test -count=1 -run TestRequestHashIsDeterministic ./internal/oauth/ the hash differs from the fixed value and changes between runs

The scratch worktree was restored and removed. All CI checks pass at 90d8f1c.

@frostyard-machines frostyard-machines added ready-for-human Reviewed, CI green: ready for Brian to review and merge and removed ready-for-human Reviewed, CI green: ready for Brian to review and merge labels Nov 16, 2026
Takes the embedded NATS broker (#81). Both reserve an app ID: the list
is now app, device, lodge, mcp, nats and oauth. The gateway serves /nats
and the OAuth routes, and the daemon passes both the broker and the
OAuth server to it.
@frostyard-machines

Copy link
Copy Markdown
Contributor Author

Merge of main: unchanged-update exemption

PR #81 merged into main, so this PR needed a merge of main. History was not rewritten and nothing was force-pushed.

  • Reviewed head: 90d8f1cf440dd9e3bc783a761192bac8d8b9c667, reviewed by openai/gpt-6.1-sol with verdict Ready and Blocking: none.
  • Old base: 78f5e20dafbfdf4294d14a97ecc3c27fa65aaf03. New base: c3b136dd41c6f60238d378d9010adac2e1a4c490 (the merge of feat(broker): embed a NATS broker with JetStream for app events #81).
  • New head: ec8609ce507f30f4097bc6791423daa755fefe98, a merge commit.

Comparison. For each of the PR's 48 files, I compared the PR's own +/- lines in git diff 78f5e20 90d8f1c -- <file> with those in git diff origin/main ec8609c -- <file>. 44 files are identical. The 4 others differ only where a conflict was resolved, and each resolution keeps both sides:

File Resolved hunk
kit/manifest/manifest.go reserved IDs app, device, lodge, mcp, nats, oauth (list, comment and error text)
docs/specs/app-contract.md the same reserved-ID list; the rationale line names ADR-0022 and ADR-0023
docs/specs/lodge-http.md the /nats route row, then the OAuth rows; "The broker" section, then "OAuth routes"; the rationale line names both ADRs
cmd/lodge/main.go gcfg := gateway.Config{..., OAuth: oauthSrv, Log: log}, if br != nil { gcfg.Broker = br }, g := gateway.New(gcfg); the PR's base atomic replaces mcpBase

These conflicts resolved with no line of the PR changed: internal/gateway/gateway.go (the /nats case, then the .well-known/oauth case), .github/risk-paths.json, docs/README.md, go.mod and go.sum (union, then go mod tidy).

Compatibility check. openai/gpt-6.1-sol read main's changes since 78f5e20 and the resolutions, read-only. Its verdict is Compatible: the routes do not overlap, the wiring keeps OAuth and adds the broker, both IDs are reserved, and fosite and the OAuth dependency versions are unchanged. The only shared change is klauspost/compress v1.20.3 to v1.20.4, which OAuth does not use directly.

Checks. make check, make ci (with the nsl checks) and make vulncheck pass locally at ec8609c. All CI checks pass at ec8609c. The first CI run failed once in internal/broker TestDevelopmentUIDNamesItsApp ("write: broken pipe, want an authorization violation"). That test came from #81 and is not in this PR. It passed 30 of 30 times with -race locally and passed on the re-run.

@frostyard-machines frostyard-machines added the ready-for-human Reviewed, CI green: ready for Brian to review and merge label Nov 16, 2026
@bketelsen
bketelsen merged commit 6e89346 into main Nov 16, 2026
19 of 20 checks passed
@bketelsen
bketelsen deleted the irulan/fb54dcc7 branch November 16, 2026 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:L1 Read every line ready-for-human Reviewed, CI green: ready for Brian to review and merge risk:high Touches a path in .github/risk-paths.json

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants