Skip to content

Register the broker client at AS-1 and point the broker at it #2607

Closed

Description

@ChrisJBurns

Depends on: stacklok/toolhive#7026 (for the last "Done when" item only)

Mecatl change at startup and in AS-1's configuration.

Problem

AS-1 signs users in, but the identity broker cannot convert their sign-ins there:

  • the broker client mecatl is not registered at AS-1;
  • AS-1 has no policy limiting which tokens mecatl may exchange;
  • the broker is not pointed at hosted AS-1.

Session open does not need ID-JAG issuance or child grants (stacklok/toolhive#6898, stacklok/toolhive#6897).

Design

Requirements

  • mecatl's configuration at AS-1. A pre-provisioned confidential delegate client with:

    • a fixed client_id and token_endpoint_auth_method: private_key_jwt;
    • the broker client's public keys as a jwks_file, never jwks_uri. ToolHive reads a delegate's keys only from a file, once, at startup, so hosted AS-1 writes the key set to a private temporary file and removes it after AS-1 is built. The signing algorithm (ES256) is derived from the P-256 key;
    • grant_types = the token-exchange grant URN only;
    • the required scopes;
    • audiences containing the Mecatl API audience (api_audience must be listed there);
    • no client secret and no redirect URIs.

    The issuer the broker uses as the assertion audience equals AS-1's issuer byte for byte, trailing slash included.

  • mecatl's exchange policy at AS-1:

    Build this delegate configuration so Configure the embedded gateway's authorization server as AS-2 #2533 can reuse it for AS-2.

  • The broker reaches AS-1. Startup fills app.Config.IdentityBrokerAS1 with production values for AS-1 hosted in mecak8s: its issuer, its token endpoint, and an in-process HTTP client. That client needs a timeout that really bounds the call, because a session open holds its host session's lock for the whole exchange, including one resend. The in-process transport (as1server.InProcessClient) runs AS-1's handler synchronously, so http.Client.Timeout cannot stop it. Two further rules:

  • Startup order. Create the broker client key, then build AS-1 with that key's public JWK, then the broker. Today buildIdentityBroker (internal/app/identitybroker.go) builds the broker before AS-1 and keeps only its core, dropping the client signer. Keys and gateway entries for the identity broker #2538 owns loading an operator-supplied key.

  • Clock. The echo check at open (echoMatches, internal/identitybroker/sessionopen.go) is exact: the requested and echoed expires_at are both absolute times from the broker's own request. The hard-expiry check counts the expiry as reached one clock skew early (credentialClockSkew, interim 30 seconds, until Keys and gateway entries for the identity broker #2538 adds the setting; credentials-and-lifecycle.md step (6), check 2), so skew fails closed and the broker never presents a grant AS-1 may already have expired (fix(identitybroker): count a session's hard expiry one clock skew early #2658).

  • Assertion checks (credentials-and-lifecycle.md step (5), checks 5 and 6). With v0.60.0, AS-1 caps an assertion's exp at 5 minutes from its clock and refuses a reused jti. The 60-second maximum and the clock-skew allowance wait for Authorization server: allow clock skew on private_key_jwt client assertions toolhive#6954. Until then, an "over-long" assertion means one over 5 minutes. The refusal codes are not uniform:

    • a wrong aud or an unregistered key gets 401 invalid_client;
    • an expired or over-long assertion gets 500 server_error;
    • a reused jti gets 400 jti_known.

    The broker keeps the design's mapping and adds no special case. While ToolHive differs, an expired or over-long assertion surfaces as a retryable UNAVAILABLE (server_error), and a reused jti as INTERNAL (an unknown code). Either way no token is issued, and these cases arise only from a broker bug or clock fault. Phase 1 shortcut, removed by Authorization server: refuse every failed private_key_jwt client assertion with 401 invalid_client toolhive#7028. That issue also tracks requiring the issuer as the only aud value; v0.60.0 also accepts the token endpoint URL. Tests assert that no token is issued, and pin invalid_client only where ToolHive returns it.

  • offline_access. The TUI requests it, but v0.60.0 does not require it on the subject token. Phase 1 shortcut (in Phase 1: Signed-in users' agents call gateway tools with their own access #2451), removed in phase 2 by Authorization server: exchange only access tokens issued with offline_access into refresh tokens toolhive#7029 (Phase 3, Track: ToolHive — The remaining ToolHive changes for audit, sign-out, and schedules #2570). Record it as unenforced, with a skipped test that the remover turns on.

  • Restarts. AS-1's storage, the broker's sessions, and the broker client key are all in memory (phase 1 shortcuts). A restart therefore loses all of them, and the next request that carries a token opens a fresh broker session. State this in the operating guide.

  • Docs. user-docs/features/security-and-execution/caller-identity.md describes the refusals at session open, bearer_rejected and sign_in_required (the text removed in 762eb04 is a starting point).

  • Tests. TestIdentityBrokerAgainstOfflineHarness keeps the harness's AS-1 through the IdentityBrokerAS1 hook, because it injects faults (lost responses, refusals). Add a separate test that starts mecak8s from its flags against the hosted AS-1. Fix the comment in cmd/mecak8s/identity_broker_harness_test.go that still says "No flag sets AS-1's endpoints yet (Host AS-1 in mecak8s #2490)".

How it fits

This is step (3) of the flow: the broker converts the user's sign-in into a refresh token for the session. The broker side exists and is tested against the offline harness; this ticket makes the real AS-1 accept it.

Done when

With mecak8s started from its flags (real startup, not a test hook), the real AS-1 hosted in it, and the offline harness as upstream IdP:

  • A session open converts a TUI sign-in.
  • After a restart, the next request with a fresh sign-in opens a new broker session.
  • AS-1 refuses:

Activity

  1. ChrisJBurns commented on Nov 2, 2026

    @ChrisJBurns
    ContributorAuthor

    Three notes from the #2493 work (PRs #2647, #2648):

    • Leave the test clock unset. feat(identitybroker): prove reopen after hard expiry; reject a token whose session closed mid-conversion #2648 added app.IdentityBrokerAS1.Now so tests can move the broker and its AS-1 client assertions past a hard expiry. When this ticket fills IdentityBrokerAS1 in production, leave Now nil (real time).
    • Clock skew with a real AS-1. The broker and AS-1 will no longer share a clock. Two places compare AS-1's times with the broker's: the echo check at open (the granted expires_at must be in the future and no later than requested) and the session's hard expiry (the held refresh token's expires_at, checked on every lookup). Decide whether either needs a skew allowance, or whether time sync between the two is assumed and documented.
    • Restore the user-docs paragraph. fix(mecak8s): resolve hosted AS-1 before the execution checks; correct identity docs #2647 removed the paragraph in user-docs/features/security-and-execution/caller-identity.md describing bearer_rejected (token already used or subject refused) and sign_in_required (sign-in revoked) at session open, because no deployment converts tokens yet. Once this ticket makes conversion live, bring it back.
  2. 4 remaining items

  3. self-assigned this
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions