# Cross-domain chaining

> A user's or an agent's authority crosses from one realm or organization to another, keeping who acts for whom, with each side's rules held at its own hop.

Cross-domain chaining carries a caller's authority into another domain: a partner, or another of your realms. An app
or agent holding a token here trades it for a short **chaining grant** addressed to the other domain, and presents
the grant there for that domain's own token. The grant carries the chain of who acts for whom, so the other side sees
the agent acting for the user, and each domain still applies its own rules at its own hop.

Use it when an agent acting for a user here must call a partner's API and the partner must see both, or when two
realms must pass a caller's authority across without the user signing in again. The realm plays both sides: it
**issues** grants to the partners you list, and **redeems** grants from the partners you trust. For a user alone, with
only their ID token, use [issuing identity assertions](../identity-assertion-issuing/) instead.

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) lists the partners on each side;
  `RealmViewer` can read them. Clients then issue and redeem grants themselves.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`).
- **First:** each partner's issuer name and keys, and an agreement on which side issues and which redeems.

## How it works

**1. The client asks for a grant**, by a token exchange that asks for a JWT **and** names an `audience`:

```sh
curl https://foobar.cvyuh.com/oauth2/token \
  -d grant_type=urn:ietf:params:oauth:grant-type:token-exchange \
  -d requested_token_type=urn:ietf:params:oauth:token-type:jwt \
  -d subject_token=eyJ0eXAiOiJhdCtqd3Qi… \
  -d subject_token_type=urn:ietf:params:oauth:token-type:access_token \
  -d audience=https://auth.partner.example/ \
  -d scope=travel:read \
  -d client_id=invoice-agent-client \
  -d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \
  -d client_assertion=eyJ…
```

```json
{ "access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6…",
  "issued_token_type": "urn:ietf:params:oauth:token-type:jwt",
  "token_type": "N_A", "expires_in": 60 }
```

The grant is in `access_token`, as RFC 8693 names the field, though it isn't an access token here. Without an
`audience`, the same exchange issues an ordinary access token of this realm's instead: see
[token exchange](../token-exchange/).

The grant is signed `RS256`, with no `typ` in its header. It names the user (`sub`), the partner (`aud`) and the
client by its ID at the partner (`client_id`). It carries the token's `act`, `principal_type`, `mode` and
`risk_class` as they are, and `resource` and `scope` when there are any.

**2. The client presents the grant** at the other domain, under the JWT bearer grant. Where the other domain is
another realm, that is:

```sh
curl https://foobar.cvyuh.com/oauth2/token \
  -d grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer \
  -d assertion=eyJhbGciOiJSUzI1NiIsImtpZCI6… \
  -d client_id=partner-invoice-agent \
  -d client_assertion_type=… \
  -d client_assertion=eyJ…
```

The answer is a normal token response about the linked user there. The other domain's chain comes across marked as
that domain's: each hop carries its `iss`, so a resource here never mistakes another domain's agent for one of yours.
A token with a chain is marked `mode` `delegated`.

## Set it up

Set the grants on the realm
(`PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings`) and the client (`https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client`; see [Call the API](../../../../develop/api/)), then
create the objects for each partner:

| Where | Setting | What it does |
|---|---|---|
| Realm and client | `grantTypes` include token exchange (to issue) or `urn:ietf:params:oauth:grant-type:jwt-bearer` (to redeem) | Off by default. Both need a confidential client |
| `oauth2AssertionAudience` | `issues` includes `chaining`, and `clients` names the client | A partner you issue grants to. Its `lifetimeSecs` can shorten a grant, never past 60 |
| `oauth2TrustedIssuer` | `accepts` includes `chaining` | A partner whose grants you redeem |
| `foreignIdentity` | `link` and `personId`, both required; `person` | One user's link to the partner, as for the [identity assertion grant](../identity-assertion-grant/) |
| Realm | `maxActDepth` | How many agents a chain may hold, across both domains (default 4, 1 to 16) |

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| A chaining grant's life | 60 seconds | an audience's `lifetimeSecs` can shorten it; never past the token it came from |
| `maxActDepth`: agents in the chain, both domains together | 4 | 1 to 16 |
| Redemptions per grant | one | fixed |
| What a redemption buys | an access token, no refresh token | fixed |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `invalid_target` when issuing | The audience doesn't issue `chaining`, or is inactive | Add `chaining` to its `issues` |
| `invalid_grant` when redeeming every grant from a partner | Its trusted issuer doesn't accept `chaining`, or its keys or `issuer` don't match | Check the `oauth2TrustedIssuer` |
| `invalid_grant` for an agent's redemption | Your agent may not act for the user here, or the chain is too deep | Link the agent to the user and allow it in your delegation policy; shorten the chain |
| `interaction_required` | Your agent's guardrail holds the scope for its owner | Have the owner approve, or ask for less |

## Audit record

Issuing a grant is a `TOKEN_EXCHANGED` record keyed by the user, with the token it came from as its parent.
Redeeming one is a `TOKEN_ISSUED` record with `grantType` `urn:ietf:params:oauth:grant-type:jwt-bearer`, naming the
partner in `assertionIssuer`. Both, allowed or refused, are on the user's trail
([Read a user's or agent's trail](/audit-reports/audit-trail/read-a-trail/)) and in **Audit → Authentication**.

## Good to know

When the realm issues a grant:

- **The token exchanged must be this realm's access token**, issued for the client exchanging it, and still active.
- **The partner must be an assertion audience that issues chaining grants** (`issues` includes `chaining`), and the
  client must be named on it. An audience that doesn't issue them answers as an unknown one.
- **No actor token.** The grant carries the subject token's own chain, so an `actor_token` is refused.
- **It never widens.** The grant carries no more scope than the token had, nor than the audience allows. With no
  `scope` asked, it carries the token's scopes within the audience's.
- **It is short-lived:** at most a minute, and never past the token it was made from.

When the realm redeems a grant:

- **It must come from a partner you trust for chaining.** The grant must come from a trusted issuer whose `accepts`
  includes `chaining`, be signed with one of the realm's `tokenEndpointAuthSigningAlgs`, be addressed to this realm,
  name the redeeming client, be fresh, and be used once.
- **The user is found only through a link you made**, as for an [identity assertion](../identity-assertion-grant/).
- **Only an untyped grant is redeemed this way.** An access token, an ID token or an identity assertion presented in
  its place is refused.
- **Your own rules hold at your hop.** When an agent of yours presents the grant, it acts in front of that chain only
  where it may act for the user here, under your own delegation rules. It is held to its
  [guardrails](../../../../gateway/guardrails/agent-guardrails/), and the whole chain, both domains' hops together,
  to `maxActDepth`.
- **An access token only**, never a refresh token. It is held to the realm's
  [governed scopes](../../../authorization/entitlement-scopes/). Every redemption is on the
  [audit trail](../../../../audit-reports/audit-trail/).

## Reference

### Errors

| Error | Side | When |
|---|---|---|
| `invalid_request` | Issuing | The subject token isn't this realm's access token, isn't for this client, is no longer active or has expired; an `actor_token` is sent; a parameter is repeated |
| `invalid_target` | Issuing | The audience is unknown, inactive or doesn't issue chaining grants; a `resource` it doesn't govern |
| `access_denied` | Issuing | The audience doesn't name this client |
| `invalid_grant` | Redeeming | The grant is malformed, carries a header `typ` (other than an identity assertion's, which is redeemed as one), comes from an issuer you don't trust for it, or doesn't verify; it isn't addressed here, has expired, names another client or was already redeemed; it names no linked, active user; your agent may not act for the user; the chain is deeper than `maxActDepth` |
| `invalid_scope`, `invalid_target` | Redeeming | As for any grant: a scope or a resource the client may not have |
| `interaction_required` | Redeeming | Your agent's guardrail needs its owner's approval for a scope |

## Related

- [Token exchange](../token-exchange/): an agent acts for a user within one realm.
- [Issuing identity assertions](../identity-assertion-issuing/): vouch for a user alone to a partner.
- [Identity assertion grant](../identity-assertion-grant/): trusted issuers and foreign identity links.
- [Transaction tokens](../transaction-tokens/): carry a request's identity inside your own services.
