Skip to content

Add the OIDC assertion exchange endpoint - #602

Open
rgarcia wants to merge 3 commits into
hypeship/oidc-federation-libfrom
hypeship/oidc-token-exchange
Open

rgarcia wants to merge 3 commits into
hypeship/oidc-federation-libfrom
hypeship/oidc-token-exchange

Conversation

@rgarcia

@rgarcia rgarcia commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Second of two stacked pull requests, on top of #601. Adds POST /oauth/token, where a workload trades an identity token from a configured OIDC issuer for a short-lived Hypeman API token carrying its federation rule's permissions.

curl -sS https://hypeman.example.com/oauth/token \
  --data-urlencode grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer \
  --data-urlencode assertion="$IDENTITY_TOKEN" \
  --data-urlencode federation_rule_id=ci

Operator documentation is in docs/oidc-federation.md.

How it fits the existing auth model

  • The issued token is an ordinary API token: HS256, signed with jwt_secret, with sub set to federation:<rule> and an explicit permissions list. The existing verifier, key rotation, hash revocation and scope middleware apply unchanged. docs/jwt-rotation.md gains a paragraph; rotation needs no new step.
  • Tokens live 300 seconds by default and 600 at most, never longer than the identity token they were exchanged for. There is no refresh token.
  • An issued token names its rule and a revision of that rule and its issuer. lib/jwtauth rejects it once the rule is removed or changed, on every path that verifies tokens, and rejects any such token without an explicit permissions list.

The exchange

  • An RFC 7523 jwt-bearer grant: form-encoded, no client authentication, repeated or unknown parameters rejected. federation_rule_id selects the one rule to check and grants nothing by itself.
  • Every refusal of a rule or an assertion is the same invalid_grant; the reason goes to the server log only. temporarily_unavailable (503) means no token can be issued right now.
  • The route is registered outside the OpenAPI spec, like /v2/token and the exec and cp routes: it is form-encoded and must see repeated parameters and the Authorization header.
  • The server sets no read timeout and this route needs no credential, so the form must arrive within 10 seconds.

Replay

A single-use identity token can be exchanged once. Each spent token is written to oidc/spent-assertions under the data directory, and synced, before its API token is returned, and the file is loaded at start. A restart therefore forgets nothing and needs no warm-up: fresh tokens are accepted as soon as the server is up, and spent ones stay refused.

  • Lines are keyed by the issuer URL the token was verified against and its token ID, and each carries a checksum.
  • If the file is missing or does not read in full at start (first start with a federation rule, a lost data directory, damage), the server cannot tell what was spent before. It then refuses single-use tokens whose iat is earlier than that start plus the 30 seconds of clock leeway the verifier allows, and keeps that line in the file across later restarts. For about the first minute the refusal is temporarily_unavailable, so callers retry with a fresh token.
  • A record is dropped once its token has expired, and the file keeps the latest expiry it has dropped. A token expiring at or before that mark is refused, so a host clock that ran ahead and expired records early cannot reopen them when it is corrected. With a correct clock no live token expires that early.
  • If a spent token cannot be recorded, no API token is issued. The server does not start with a federation rule if the file cannot be opened; without any rule the file is not touched.
  • Not detected: a data directory restored from an earlier copy.

Observability

  • hypeman_oidc_exchanges_total{issuer, rule, result}. The labels come from configuration only, never from request input.
  • One log line per exchange with the rule, issuer, result and reason, the SHA-256 of the external subject once the signature is verified, and the issued token's jti. Identity tokens are never logged.

Not in this change

  • Scoping issued tokens to the resources they create. permissions are enforced as for any scoped token; direct registry access under /v2 accepts any valid API token, as it does today.
  • A CLI or SDK entry for the endpoint, .well-known metadata, and rate limiting.

Testing

  • go test ./lib/oidcfederation/ ./lib/jwtauth/ ./cmd/api/ ./lib/scopes/ ./lib/middleware/ ./lib/registry/, and -race on the first two.
  • An end-to-end test exchanges an identity token signed by an inline-key issuer, calls an API route with the result, is refused outside its permissions, and is rejected by a server whose rule changed.
  • A real HTTP/1 server test covers stalled and oversized request bodies.
  • Replay tests cover restarts on the same record, a lost record followed by further restarts, damaged records (including single changed digits), a failed write, a rewrite that cannot be made durable, an issuer rename, a host clock that ran ahead, an identity token held past other tokens' expiry, and the fallback with issuer clocks running fast and slow.
  • The full make test suite was not run locally (it needs KVM and the hypervisor binaries); CI runs it.

Design notes for the whole stack: https://gist.github.com/rgarcia/d06b17f03b818f3d4dae431b5efcab9d

Reviewed with sol-loop: 3 rounds with openai-codex/gpt-6.2-sol on extra-high reasoning, and 5 more for the durable replay record. Sol verdict: approved.

POST /oauth/token trades an identity token from a configured OIDC
issuer for a short-lived API token carrying the matching federation
rule's permissions. The exchange is an RFC 7523 jwt-bearer grant with
no client authentication, and every refusal reads the same to the
caller.

Single-use assertions are recorded in memory until they expire, and
ones issued before the server started are refused. Issued tokens name
their rule, and the JWT verifier rejects them once that rule or its
issuer is removed or changed.
The record of spent single-use identity tokens lived in memory only, so
after a restart the server had to refuse tokens issued around its start,
which made the exchange unavailable for about 30 seconds.

Write each spent token to a file under the data directory before its API
token is returned, and load the file at start. A restart now forgets
nothing and exchanges work as soon as the server is up. The time-based
floor remains only for a start that finds the file missing or damaged,
and the file remembers it across later restarts.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit c0b41c2. Configure here.

Comment thread lib/oidcfederation/spentlog.go Resolved
A record of a spent assertion is dropped once the host clock says its
assertion has expired. A clock that ran ahead dropped records early, and
after it was corrected their assertions could be exchanged again.

The record now keeps the latest expiry it has dropped, at a start and
while running, and refuses an assertion expiring at or before it. With a
correct clock no assertion still within its lifetime expires that early.
Both that mark and the floor are checked under the same lock as
recording the spend, and a file counts as complete only if both of their
lines were read.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant