forgejo-mcp-broker/internal
Ole-Morten Duesund 9c8cf40501 feat(oauth): bearer-token middleware (forgejo-mcp-broker-ytw)
Adds Authenticator.RequireBearer — http middleware that gates downstream
handlers on a valid broker access token.

Lookup path:
  1. Read Authorization: Bearer <token> header.
  2. SHA-256 the token, query access_tokens by token_hash.
  3. Reject expired or revoked rows.
  4. Build a Session (client_id, forgejo user info, upstream token,
     scopes) and attach to r.Context() under a typed key.

Downstream handlers (the MCP endpoint shipping in 5a) read the
upstream Forgejo token via SessionFromContext to spawn forgejo-mcp
subprocesses scoped to the right user.

Failures emit 401 with an RFC 6750 §3 WWW-Authenticate header carrying
distinct error codes (invalid_request for missing/malformed headers,
invalid_token with reason=expired/revoked/unknown for token problems).
The body stays empty so a confused browser doesn't render auth errors;
all detail rides in the header where compliant clients look for it.

Tests: 90.9% on RequireBearer, 91.7% on lookupSession. Covers valid
token, missing/wrong-scheme/empty Authorization, unknown token,
expired token (clock-advanced past AccessTokenTTL), revoked token (via
the public /oauth/revoke endpoint).

Closes forgejo-mcp-broker-ytw.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-27 17:10:21 +02:00
..
bridge test(bridge): integration test against real forgejo-mcp (forgejo-mcp-broker-xot) 2026-04-27 16:28:32 +02:00
buildinfo feat: bootstrap Go project layout (forgejo-mcp-broker-n84) 2026-04-24 16:54:27 +02:00
config feat(config): flag + env parsing with validation (forgejo-mcp-broker-9nq) 2026-04-24 17:10:18 +02:00
forgejo feat(forgejo): upstream OAuth client (forgejo-mcp-broker-b9i) 2026-04-27 13:31:19 +02:00
httpserver feat(httpserver,log): /healthz, graceful shutdown, slog constructor 2026-04-24 17:26:32 +02:00
log feat(httpserver,log): /healthz, graceful shutdown, slog constructor 2026-04-24 17:26:32 +02:00
oauth feat(oauth): bearer-token middleware (forgejo-mcp-broker-ytw) 2026-04-27 17:10:21 +02:00
store feat(store): OAuth tables migration (forgejo-mcp-broker-cpb) 2026-04-27 13:28:12 +02:00
supervisor test(supervisor): stress tests for FD/goroutine/zombie leaks (forgejo-mcp-broker-31t) 2026-04-27 16:04:34 +02:00