Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 44 additions & 25 deletions docs/arch/17-token-exchange-delegation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2080,9 +2080,11 @@ grant_type=urn:ietf:params:oauth:grant-type:token-exchange
```

`audience` must be exactly one target `issuer` and `resource` exactly one of
that target's `resources`, both as written. `scope`, `authorization_details`
and `actor_token` are refused. Unknown parameters, including `sub`, are
ignored. The response is `200` with `Cache-Control: no-store`:
that target's `resources`, both as written. `scope` and `authorization_details`
are refused. `actor_token` and `actor_token_type` are refused too, unless the
client has an actor profile, which must send both (see [Required actor
parameters](#required-actor-parameters)). Unknown parameters, including `sub`,
are ignored. The response is `200` with `Cache-Control: no-store`:

```json
{
Expand All @@ -2102,11 +2104,15 @@ are listed in "Claims and subject".
The server stops at the first check that fails:

1. The client: confidential, with the token-exchange grant, and with both
`exchange_refresh_tokens` and `id_jag.targets`. A client with an actor
profile then gets `400 unauthorized_client` before anything else is
checked (see [Fail-closed guard](#fail-closed-guard)).
2. The request shape, then a target of this client that lists the `resource`.
Nothing is read from the store yet.
`exchange_refresh_tokens` and `id_jag.targets`.
2. The request shape: `subject_token`, `audience`, `resource`, then `scope` and
`authorization_details` (refused). Then the actor parameters: a client
without an actor profile has `actor_token` and `actor_token_type` refused,
and a client with one must send them (see [Required actor
parameters](#required-actor-parameters)). Then a target of this client that
lists the `resource`. A client with an actor profile is then refused with
`400 unauthorized_client`, because the validator call is not built yet. Nothing is read
from the store yet.
3. The grant: an unknown, forged, foreign, login or expired token all get
`grant_invalid`. Then revoked, parent revoked, and `not_issuable` when the
grant does not list the ID-JAG output.
Expand Down Expand Up @@ -2190,7 +2196,8 @@ broker's `subject_token_audiences` is missing there, a target that requires
| `https://toolhive.dev/upstream_subject` | object | Only if the target requires it: `{"iss", "claim", "value"}`. The config calls it `upstream_subject` |

There is no `nbf`, `scope`, `cnf`, `authorization_details` or `act` claim. The
client in `client_id` acts for the user.
client in `client_id` acts for the user. A client with an actor profile gets no
ID-JAG yet (see [Required actor parameters](#required-actor-parameters)).

`sub` is the same for every audience. AS-1 draws it as a random UUID at the
user's first login. With memory storage a restart gives every user a new `sub`,
Expand Down Expand Up @@ -2255,14 +2262,15 @@ An ID-JAG can name the acting agent in its `act` claim (ID-JAG draft -06,
Extensions"). The issuing server cannot see the client's agent registry, so it
cannot check an actor token or find the agent it names. An embedder that holds
such a registry registers an in-process **validator**. For a client with an
actor profile, ID-JAG issuance from an exchange grant will then require an
`actor_token` (RFC 8693 §2.1), pass it to the validator, and build `act` from
the answer.
actor profile, ID-JAG issuance from an exchange grant requires an
`actor_token` (RFC 8693 §2.1), and will pass it to the validator and build `act`
from the answer.

**Status.** Only the configuration and the startup checks exist. Nothing calls
the validator yet. Until the call is added, ID-JAG issuance refuses every
request from a client that has an actor profile (see
[Fail-closed guard](#fail-closed-guard)).
**Status.** The configuration, the startup checks and the request check exist.
Calling the validator and setting `act` are not built yet, so a client with an
actor profile gets no ID-JAG: a request that passes every request check is
refused with `400 unauthorized_client` (see [Required actor
parameters](#required-actor-parameters)).

The validator contract (the request, the result, the refusal reasons and the
rules ToolHive applies when it calls the validator) is in the Go doc comments of
Expand Down Expand Up @@ -2370,26 +2378,37 @@ server builds its per-client policies, it binds each profile to its registered
validator. It does not check the registration again, because `Config.Validate` has
already done so.

#### Fail-closed guard

`checkIDJAGRequest` returns `400 unauthorized_client` ("Actor-token validation
is not available.") for a client that has an actor profile, before it reads any
request parameter. A client the operator configured to require an actor check therefore gets no
ID-JAG until the check is wired in. The change that adds the validator call to issuance
replaces this guard.

**Open.** ToolHive does not check that a SPIFFE agent ID belongs to the trust domain
named by a `spiffe://` `ActIssuer`. A validator that accepts JWT-SVIDs must verify each
SVID against the bundle of the trust domain in its `sub`. That check is the binding,
and it belongs in the embedder's validator.

#### Required actor parameters

A client with an actor profile must send exactly one non-empty `actor_token` and
exactly one `actor_token_type` that equals the profile's token type. The type is
compared exactly, with no case folding. Otherwise the request fails with
`invalid_request` and one of these hints:

- `The 'actor_token' parameter is required, once, for this client.`
- `The 'actor_token_type' parameter must name the token type configured for this client, once.`

`checkIDJAGRequest` makes this check before it looks up the target and before
any grant is read.

The actor token is not validated yet. A request that passes this check and the
target check is refused with `400 unauthorized_client` and the hint `Actor-token
validation is not available.`, so a client the operator configured to require an actor
check gets no ID-JAG until the validator call is built. That call replaces this
refusal.

## Implementation

- `pkg/authserver/server/tokenexchange/actor_validator.go` — `ActorTokenValidator` and its contract, `ActorValidatorRegistration`, `ActIssuerProblem`, `ActorProfile`
- `pkg/authserver/server/tokenexchange/handler.go` — `checkSourcePolicy`, `ConfiguredClientPolicy`, `checkDelegationConsent`, `grantScopes`, `grantAndBoundAudiences`, `externalOrigin`, `IDJAGTarget`, `ConfiguredClientPolicy.IDJAGTargets`, `ConfiguredClientPolicy.ActorProfile`
- `pkg/authserver/server/tokenexchange/refresh_token_handler.go` — `RefreshTokenHandler`, `isRefreshTokenExchange`, `checkLoginToken`, `checkExchangeGrantSubject`
- `pkg/authserver/server/tokenexchange/exchange_grant.go` — the exchange-grant entry type and its validation
- `pkg/authserver/server/tokenexchange/id_jag_mint.go` — the ID-JAG minting core: `checkIDJAGRequest` (including the fail-closed guard for a client with an actor profile), `selectSubjectClaims`, `respondWithIDJAG`, `mintIDJAG`
- `pkg/authserver/server/tokenexchange/id_jag_mint.go` — the ID-JAG minting core: `checkIDJAGRequest` (including the required actor parameters for a client with an actor profile), `selectSubjectClaims`, `respondWithIDJAG`, `mintIDJAG`
- `pkg/authserver/server/tokenexchange/refresh_token_id_jag.go` — `decideIDJAG`, `issueIDJAG`: the grant checks and the Debug line for an ID-JAG
- `pkg/authserver/server/id_jag_seam.go` — `OwnsResourceParameter`, `ReasonDPoPNotSupported`; the token endpoint calls them from `pkg/authserver/server/handlers/token.go`
- `pkg/authserver/server/tokenexchange/multi_issuer_validator.go` — `MultiIssuerTokenValidator`, `TrustedIssuer`, `resolveActorAuthorization`, `ValidateJWKSURL`
Expand Down
2 changes: 1 addition & 1 deletion docs/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ sequenceDiagram

**Responsibilities**:
- Validate JWT token signature and expiration
- Return 401 Unauthorized for a token whose JOSE `typ` header is `oauth-id-jag+jwt` (compared case-insensitively, with or without the `application/` prefix), even when its signature and claims are valid. This applies to tokens from every configured issuer, including external identity providers, and to vMCP incoming authentication. An ID-JAG is not an access token. An issuer that signs ID-JAGs with the same key as its access tokens (an external identity provider today, and the embedded auth server once it issues ID-JAGs) would otherwise have them accepted in place of access tokens (RFC 8725 Section 3.12). Configuring an expected audience is the main defence against one token type being used in place of another; this check is a backup for deployments without one.
- Return 401 Unauthorized for a token whose JOSE `typ` header is `oauth-id-jag+jwt` (compared case-insensitively, with or without the `application/` prefix), even when its signature and claims are valid. This applies to tokens from every configured issuer, including external identity providers, and to vMCP incoming authentication. An ID-JAG is not an access token. An issuer that signs ID-JAGs with the same key as its access tokens (an external identity provider, or the embedded auth server) would otherwise have them accepted in place of access tokens (RFC 8725 Section 3.12). Configuring an expected audience is the main defence against one token type being used in place of another; this check is a backup for deployments without one.
- Extract JWT claims (sub, name, roles, etc.)
- Add claims to request context for downstream middleware
- With the embedded auth server, load the token session's upstream provider credentials (the `tsid` claim) into `Identity.UpstreamTokens` and `Identity.UpstreamIDTokens`, refreshing expired access tokens
Expand Down
88 changes: 64 additions & 24 deletions pkg/authserver/server/tokenexchange/id_jag_mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,21 @@ var idJAGReservedClaims = map[string]struct{}{
// a request. The client gets invalid_request with reasonPolicyDenied.
var errIDJAGPolicyDenied = errors.New("ID-JAG request denied by policy")

// errActorValidationUnavailable marks the refusal of a client with an actor
// profile until issuance calls the validator, so the caller can log it with its
// own reason and not as a malformed request.
var errActorValidationUnavailable = errors.New("actor-token validation is not available")

// idJAGRequestShape is the target and resource of a request that passed
// checkIDJAGRequest.
// checkIDJAGRequest, and, for a client with an actor profile, the presented
// actor token. checkIDJAGRequest checks only that the token is present once;
// it does not validate it. The token type is not carried: checkActorParameters
// forces it to equal the profile's. The actor token is empty for a client
// without a profile.
type idJAGRequestShape struct {
Target IDJAGTarget
Resource string
Target IDJAGTarget
Resource string
ActorToken string
}

// idJAGSubject is the user an ID-JAG is minted for: the sub claim and the
Expand Down Expand Up @@ -167,21 +177,16 @@ func allowAllIDJAGPolicy(context.Context, idJAGPolicyInput) error { return nil }
// and a resource the target does not list all get the same invalid_target
// error. Unknown parameters are ignored.
//
// A client with an actor profile is refused with unauthorized_client before
// any parameter is read, because ID-JAG issuance does not check actor tokens
// yet and must not mint for a client the operator configured to require that
// check. Checking first means no request shape, valid or not, gets past it.
// A client with an actor profile must send actor_token and actor_token_type
// instead of having them refused; see checkIDJAGExtraParameters. The target
// check comes after them. Such a client is then refused with
// unauthorized_client, because issuance does not call the actor validator yet and must not mint for
// a client the operator configured to require that check. A malformed request
// from it still gets invalid_request or invalid_target first.
//
// Every refusal here carries a hint, never a stable reason, so it returns only
// the error to send.
func checkIDJAGRequest(form url.Values, policy ConfiguredClientPolicy) (idJAGRequestShape, error) {
// The change that adds the validateActor call replaces this guard. It
// answers unauthorized_client (400), not server_error, so these refusals
// are not logged as server faults.
if policy.ActorProfile != nil {
return idJAGRequestShape{}, errorsx.WithStack(fosite.ErrUnauthorizedClient.WithHint(
"Actor-token validation is not available."))
}
if form.Get("subject_token") == "" {
return idJAGRequestShape{}, errorsx.WithStack(fosite.ErrInvalidRequest.WithHint(
"The 'subject_token' parameter is required for token exchange."))
Expand All @@ -200,39 +205,74 @@ func checkIDJAGRequest(form url.Values, policy ConfiguredClientPolicy) (idJAGReq
return idJAGRequestShape{}, errorsx.WithStack(server.ErrInvalidTarget.WithHint(
"The 'resource' parameter must have exactly one value when requesting an ID-JAG."))
}
if err := refuseIDJAGParameters(form); err != nil {
actorToken, err := checkIDJAGExtraParameters(form, policy.ActorProfile)
if err != nil {
return idJAGRequestShape{}, err
}
target, ok := policy.IDJAGTargets[audiences[0]]
if !ok || !slices.Contains(target.Resources, resources[0]) {
return idJAGRequestShape{}, errorsx.WithStack(server.ErrInvalidTarget.WithHint(
"The 'audience' and 'resource' parameters do not name a target this client may request an ID-JAG for."))
}
return idJAGRequestShape{Target: target, Resource: resources[0]}, nil
// The change that adds the validateActor call replaces this guard. It
// answers unauthorized_client (400), not server_error, so these refusals
// are not logged as server faults.
if policy.ActorProfile != nil {
return idJAGRequestShape{}, errorsx.WithStack(fosite.ErrUnauthorizedClient.WithHint(
"Actor-token validation is not available.").WithWrap(errActorValidationUnavailable))
}
return idJAGRequestShape{Target: target, Resource: resources[0], ActorToken: actorToken}, nil
}

// refuseIDJAGParameters refuses the parameters an ID-JAG request may not carry
// yet, checking every value of each.
func refuseIDJAGParameters(form url.Values) error {
// checkIDJAGExtraParameters checks the parameters beyond subject_token, audience
// and resource, checking every value of each. scope and authorization_details
// are refused for every client.
//
// Without a profile, actor_token and actor_token_type are refused too, and the
// returned actor token is empty. With a profile they are required instead, by
// checkActorParameters, and the actor token is returned.
func checkIDJAGExtraParameters(form url.Values, profile *ActorProfile) (string, error) {
if len(nonEmptyValues(form["scope"])) > 0 {
return errorsx.WithStack(fosite.ErrInvalidScope.WithHint(
return "", errorsx.WithStack(fosite.ErrInvalidScope.WithHint(
"The 'scope' parameter is not supported when requesting an ID-JAG."))
}
if len(nonEmptyValues(form["authorization_details"])) > 0 {
return errorsx.WithStack(invalidAuthorizationDetailsError().WithHint(
return "", errorsx.WithStack(invalidAuthorizationDetailsError().WithHint(
"The 'authorization_details' parameter is not supported when requesting an ID-JAG."))
}
if profile != nil {
return checkActorParameters(form, profile)
}
if len(nonEmptyValues(form["actor_token"])) > 0 {
return errorsx.WithStack(fosite.ErrInvalidRequest.WithHint(
return "", errorsx.WithStack(fosite.ErrInvalidRequest.WithHint(
"The 'actor_token' parameter is not supported when requesting an ID-JAG."))
}
// actor_token has no non-empty value here, so any actor_token_type is alone
// (RFC 8693 section 2.1).
if len(nonEmptyValues(form["actor_token_type"])) > 0 {
return errorsx.WithStack(fosite.ErrInvalidRequest.WithHint(
return "", errorsx.WithStack(fosite.ErrInvalidRequest.WithHint(
"The 'actor_token_type' parameter must not be given without 'actor_token'."))
}
return nil
return "", nil
}

// checkActorParameters requires the actor token and its type for a client with
// a profile. "Exactly one" counts every value, empty ones included: the
// parameter must appear once, and that value must be non-empty. A repeated
// parameter is refused even when only one of its values is non-empty, since
// fosite reads only the first. actor_token_type must also equal the profile's
// type exactly. Nothing from the request is echoed.
func checkActorParameters(form url.Values, profile *ActorProfile) (string, error) {
tokens := form["actor_token"]
if len(tokens) != 1 || tokens[0] == "" {
return "", errorsx.WithStack(fosite.ErrInvalidRequest.WithHint(
"The 'actor_token' parameter is required, once, for this client."))
}
if types := form["actor_token_type"]; len(types) != 1 || types[0] != profile.ActorTokenType {
return "", errorsx.WithStack(fosite.ErrInvalidRequest.WithHint(
"The 'actor_token_type' parameter must name the token type configured for this client, once."))
}
return tokens[0], nil
}

// selectSubjectClaims picks the subject claims the target requires from the
Expand Down
Loading
Loading