Skip to content

feat(identitybroker): authenticate the broker to AS-1 and AS-2 with private_key_jwt - #2573

Merged
ChrisJBurns merged 5 commits into
mainfrom
feat/identitybroker-client-auth
Oct 27, 2026
Merged

ChrisJBurns merged 5 commits into
mainfrom
feat/identitybroker-client-auth

Conversation

@ChrisJBurns

@ChrisJBurns ChrisJBurns commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2489.

The identity broker can now prove to AS-1 and AS-2 that it is the OAuth client mecatl: it signs a fresh private_key_jwt client assertion (RFC 7523) for every token-endpoint request, with its own key. #2492 (session open), #2533 and #2535 (grant and redemption), and #2538 build on this.

What changes

  • Client assertions (internal/identitybroker/clientassertion.go): NewClientAssertionSigner(crypto.Signer, lifetime) mints assertions as design step (5) specifies: header typ: client-authentication+jwt, alg: ES256, kid = the key's RFC 7638 thumbprint; claims iss = sub = mecatl, aud = the server's issuer as a single string, iat, exp with a lifetime of 1 to 60 seconds (default 60), and a random jti per assertion. JWKS() publishes the public key as an inline JWK set, which is how ToolHive registers it (it rejects jwks_uri on purpose). P-256 keys only, through crypto.Signer so the key can later move to a KMS or HSM. The agent SVID signer shares one publicJWK helper for the thumbprint; SVID output is unchanged.

  • Refusals (internal/identitybroker/serverrefusal.go): ServerRefusal{Status, Code, Reason}, ErrNoResponse, and the design's mapping (credentials-and-lifecycle.md, "Error reasons"): IsConfigurationFault (invalid_client, unauthorized_client, invalid_target, an OAuth refusal with no reason or with a reason that is not a known code, or a certificate that does not verify, ErrTransportConfiguration) and IsUnavailable (status 429, 502, 503, or 504, the code server_error or temporarily_unavailable at any status, or no response). This matches the design's error-reasons table, which this PR extends with server_error and temporarily_unavailable next to HTTP 503, 429, 502, and 504. A cancellation while reading the response returns the context error. They live in the broker package, which has no network code, so Convert the sign-in at session open #2492 and Per-call grant and gateway token #2535 can map them to gRPC statuses inside the broker. Only known OAuth error codes and the design's reason codes are kept and shown; anything else becomes unknown, so a server cannot put free text or a token into an error. A request that got no response is reported by a fixed kind (timeout, dns, tls, connection refused, connection closed, other), never the transport error's text.

  • Token-endpoint client (internal/identitybroker/adapter/authserverclient): posts forms to a configured token or revocation endpoint (https only, no user info) with client_assertion_type and a fresh assertion whose audience is that server's issuer; never a client secret or Basic authentication, and refuses forms that try to carry client credentials. It requires the caller's *http.Client, always refuses redirects (on a copy of that client, so a redirect cannot resend the form elsewhere), caps the response at 1 MiB, and returns the core refusal types. Retry is the caller's choice: each call mints a new assertion and jti, and ErrNoResponse says a caller may retry once.

  • Layout for extraction: everything that belongs to the broker now lives under internal/identitybroker/: the core (no network code, as before) and its adapters under internal/identitybroker/adapter/, so the broker can move into its own service as one tree. Lint rules keep it that way: the core's no-network rule excludes adapter/**; the adapters may import only the standard library, the broker core, and golang-jwt; and only internal/app may import the broker's adapters.

  • The broker client key (internal/app/identitybroker.go): generated at startup, separate from the agent issuer key (one key per role), and kept inside the broker's wiring; it is not exposed on Built. Nothing uses it until Convert the sign-in at session open #2492 passes it into the broker. Until Keys and gateway entries for the identity broker #2538 supplies an operator key it is per process, so an external server holding it inline would need it re-registered after a restart; in-process servers register it at each build.

  • Harness (internal/testutil/identityharness): AS-1's assertion check now allows the clock skew on exp and iat, as the design does (ClientAssertionClockSkew), so a replay just after exp is refused by the used jti, not by expiry.

  • Design: the error-reasons table in credentials-and-lifecycle.md now also lists the error codes server_error and temporarily_unavailable as retryable, matching steps (7) and (8).

Not in this PR

Testing

  • Unit: the exact assertion header and claims, a unique jti per call, lifetimes 1 s, 30 s, and 60 s accepted and 61 s, below 1 s, and negative refused, non-P-256 and opaque signers, and the JWK set round-tripping to a key that verifies an assertion, with the kid checked against an independently computed thumbprint.
  • Refusal parsing and classification, including attempts to inject free text, invalid_target, a missing body, a 307, wrapped errors, and ErrNoResponse.
  • Against the offline harness's AS-1, with the production key registered from the published JWK set:
    • a session-open exchange authenticates; the emitted header (typ, alg) and claims (iss, sub, aud, lifetime, non-empty jti) are checked;
    • a wrong audience (the token endpoint), an expired or over-long assertion, a reused jti, and an unregistered key are refused with invalid_client and classified as configuration faults; invalid_client and unauthorized_client with a reason are configuration faults, a 400 with a reason is not, and a 503 is unavailable;
    • a leaked AS-1 access token presented without the broker's key is refused;
    • after a lost response, a retry with a new assertion succeeds;
    • a fresh assertion past exp but within the skew is accepted, and a replay of a used assertion within that window is refused (mutation-checked against jti retention);
    • revocation authenticates;
    • a redirect to another server sends that server nothing, and an oversized response is refused;
    • no error string contains an assertion or token value.
  • The key-separation check: the broker client key differs from the agent issuer key and between builds.
  • go test -race on internal/identitybroker, internal/adapter/authserverclient, the harness, and the app's identity broker tests; cmd/mecak8s; go vet; the offline demo. task lint is clean except the existing gosec finding in environment/microvm/launch_ownership_darwin.go.

API compatibility

No wire, engine API, or stored-data change. New internal packages and types only; nothing user-visible changes.

ChrisJBurns added a commit that referenced this pull request Oct 27, 2026
The error-reasons table named only HTTP 503 as retryable. A 429, 502, or
504 from a proxy or rate limiter in front of AS-1 or AS-2 is a passing
outage too, not a configuration fault, so map it to UNAVAILABLE as the
broker's token-endpoint client already does (#2573).

Co-authored-by: Mecatl <[email protected]>
@ChrisJBurns
ChrisJBurns requested a review from jhrozek October 27, 2026 01:08

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good. The assertion matches rfc7523bis (issuer as the sole aud, typ, fresh jti), refusals can't carry server text, redirects are refused on a copy of the caller's client, and the key is kept apart from the agent issuer key.

A few non-blocking nits inline. The first two matter most because session open and redemption will build on IsConfigurationFault / IsUnavailable; fixing them here or in a follow-up is fine.

ChrisJBurns added a commit that referenced this pull request Oct 27, 2026
…icate failures

Address review on #2573:

- server_error and temporarily_unavailable are retryable at any status,
  as design steps (7) and (8) say; the error-reasons table now says so.
- A reason that is not a known code (for example fosite's prose
  error_description) counts as no reason, so the refusal is a
  configuration fault, as the design treats it.
- A cancellation while reading the response returns the context error,
  and a certificate that does not verify returns
  ErrTransportConfiguration, a configuration fault, not a retryable
  ErrNoResponse.
- The AS-1 test decodes the client_assertion the harness received and
  checks its kid against the published key set.

Co-authored-by: Mecatl <[email protected]>
ChrisJBurns and others added 5 commits October 27, 2026 14:16
…rivate_key_jwt

Add client assertions for the broker's OAuth client mecatl (RFC 7523,
design step (5)): ES256 over a crypto.Signer, typ
client-authentication+jwt, kid as the RFC 7638 thumbprint, iss=sub=mecatl,
aud the server's issuer, a lifetime of at most 60 seconds, and a random
jti, with the public key published as an inline JWK set.

Add the authorization-server refusal types and the design's mapping to
configuration faults and retryable errors in the broker package, and a
token-endpoint client (internal/adapter/authserverclient) that posts
forms with a fresh assertion, never a client secret, refuses redirects,
and caps responses. mecak8s generates a separate broker client key at
startup, kept inside the broker wiring until session open uses it.

The harness's AS-1 now allows the clock skew on assertion exp and iat,
so a replay just after exp is refused by its used jti.

Closes #2489.

Co-authored-by: Mecatl <[email protected]>
…dentitybroker

Move the authorization-server client to
internal/identitybroker/adapter/authserverclient so everything that
belongs to the identity broker lives in one tree and can move into its
own service together. The core's no-network depguard rule now excludes
adapter/**; a new rule limits the adapters to the standard library, the
broker core, and golang-jwt; and only internal/app may import them.

Co-authored-by: Mecatl <[email protected]>
…usal codes

Treat 429, 502, 503, and 504 as retryable, never as configuration
faults: a proxy or rate-limit response is a passing outage, not a broker
bug. The design names 503; the others are a deliberate extension.

Report a request that got no response by a fixed kind (timeout, dns,
tls, connection refused, connection closed, other) instead of the
transport error's text, which a custom transport or a malformed status
line could fill with request or server-echoed data.

Show only known OAuth error codes and the design's reason codes; any
other value is reported as unknown, so a server echoing an opaque token
cannot put it into an error.

Co-authored-by: Mecatl <[email protected]>
…icate failures

Address review on #2573:

- server_error and temporarily_unavailable are retryable at any status,
  as design steps (7) and (8) say; the error-reasons table now says so.
- A reason that is not a known code (for example fosite's prose
  error_description) counts as no reason, so the refusal is a
  configuration fault, as the design treats it.
- A cancellation while reading the response returns the context error,
  and a certificate that does not verify returns
  ErrTransportConfiguration, a configuration fault, not a retryable
  ErrNoResponse.
- The AS-1 test decodes the client_assertion the harness received and
  checks its kid against the published key set.

Co-authored-by: Mecatl <[email protected]>
…able

The design's error-reasons table now lists 429, 502, and 504 next to
503, so they are no longer an extension of it.

Co-authored-by: Mecatl <[email protected]>
@ChrisJBurns
ChrisJBurns force-pushed the feat/identitybroker-client-auth branch from da74940 to 0b0c175 Compare October 27, 2026 14:18
@ChrisJBurns
ChrisJBurns merged commit 7d5d79e into main Oct 27, 2026
37 of 40 checks passed
@ChrisJBurns
ChrisJBurns deleted the feat/identitybroker-client-auth branch October 27, 2026 14:41
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Nov 18, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Broker client authentication at AS-1 and AS-2

2 participants