# Issuing identity assertions

> The realm vouches for one of its users to a partner's authorization server you name, so an app here reaches the partner's apps on their behalf.

When your users sign in here and also use a partner's apps, the realm can vouch for them to the partner. An app here
trades the user's ID token for a signed **identity assertion** addressed to that partner; the partner redeems it at its
own authorization server and grants the app access there for the same user, without them signing in again.

Use it when an enterprise app or MCP client here must call a partner's API for the user signed in, at a partner that
runs its own authorization server and trusts your realm as its identity provider. The realm issues one only to a
partner you list as an **assertion audience**, and only to an app you name on it. The reverse direction, redeeming a
partner's assertions here, is the [identity assertion grant](../identity-assertion-grant/).

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) lists the partner and the apps that may
  ask; `RealmViewer` can read them. The app then asks for assertions itself.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`).
- **First:** the partner's authorization server trusts your realm as an identity provider, and has given each of your
  apps a client ID there.

## How it works

**1. You list the partner** as an `oauth2AssertionAudience`, naming each app here that may ask, with the app's own
client ID at the partner:

```json
{ "audience": "https://auth.partner.example/", "status": "active", "issues": ["idJag"],
  "clients": { "expense-app": "partner-expenses" },
  "resources": ["https://api.partner.example/travel"], "scopes": ["travel:read", "travel:book"],
  "lifetimeSecs": 120 }
```

**2. The app exchanges the user's ID token**, authenticating as itself:

```sh
curl https://foobar.cvyuh.com/oauth2/token \
  -u 'expense-app:<secret>' \
  -d grant_type=urn:ietf:params:oauth:grant-type:token-exchange \
  -d requested_token_type=urn:ietf:params:oauth:token-type:id-jag \
  -d subject_token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9… \
  -d subject_token_type=urn:ietf:params:oauth:token-type:id_token \
  -d audience=https://auth.partner.example/ \
  -d resource=https://api.partner.example/travel \
  -d scope=travel:read
```

```json
{ "access_token": "eyJ0eXAiOiJvYXV0aC1pZC1qYWcrand0Ii…",
  "issued_token_type": "urn:ietf:params:oauth:token-type:id-jag",
  "token_type": "N_A", "expires_in": 120 }
```

The assertion is in `access_token`, as RFC 8693 names the field, though it isn't an access token: `token_type` is
`N_A`. There is no refresh token.

**3. The app presents the assertion** at the partner's authorization server, under the partner's own rules.

The assertion is signed `RS256`, as the standard requires, with the header `typ` `oauth-id-jag+jwt`, under a key
published in the realm's key set. It names the user by their ID here (`sub`), the partner (`aud`), the app by its ID at
the partner (`client_id`), and how and when they signed in (`auth_time`, `amr`, `acr`). It carries `resource` and
`scope` when there are any. The user's email goes in only when the audience asks for it. When the app proves a DPoP
key on the exchange, the assertion carries that key's thumbprint as `cnf.jkt`.

## Set it up

These steps use the admin API; [Call the API](../../../../develop/api/) shows how to get a session.

1. Add `urn:ietf:params:oauth:grant-type:token-exchange` to the realm's `grantTypes`
   (`PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings`) and to the app's (`https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client`).
   The app must be confidential. Read the realm's settings first with `GET` on the same address, and send back the whole list with the new value
   added: **a list you send replaces the realm's list.**
2. Create an `oauth2AssertionAudience` object for the partner, with the fields below.

| `oauth2AssertionAudience` field | What it sets |
|---|---|
| `audience` | The partner authorization server's issuer, exactly as it names itself |
| `displayName` | A name for the partner, for you |
| `issues` | What the realm issues to it: `idJag`, `chaining` or both (default `["idJag"]`) |
| `clients` | Each app here that may ask, mapped to its client ID at the partner |
| `resources`, `scopes` | What an assertion to this partner may carry |
| `lifetimeSecs` | 300 at most, and by default |
| `includeEmail` | Puts the user's email in the assertion. Off by default |
| `status` | Only an active audience is issued to |

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `lifetimeSecs` on an audience | 300 | up to 300, never past the access-token lifetime, the ID token or the session |
| `issues` | `["idJag"]` | `idJag`, `chaining` or both |
| `includeEmail` | off | on or off |
| Subject | a live ID token issued to the asking app | fixed |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `invalid_target` for a partner you listed | The audience is inactive, doesn't issue `idJag`, or the `audience` sent differs from its issuer by a character | Check `status`, `issues` and the exact `audience` |
| `access_denied` | The app isn't named in the audience's `clients` | Add the app, mapped to its client ID at the partner |
| `invalid_request` for a user who just signed in | The app sent its access token, or the ID token has expired | Send the current ID token with the ID-token `subject_token_type` |
| The partner refuses the assertion | Its client ID for your app differs from the one mapped, or it doesn't hold your realm's keys | Correct the mapping in `clients`; have the partner read your [Discovery document](../../registration/discovery/) |

## Audit record

Each request, issued or refused, is a `TOKEN_EXCHANGED` record keyed by the user, with its `reason` when refused,
on the user's trail ([Read a user's or agent's trail](/audit-reports/audit-trail/read-a-trail/)) and in
**Audit → Authentication**. Changes to assertion audiences are in **Audit → Config**.

## Good to know

- **The subject must be a live ID token for this app.** It must be an ID token this realm issued to the exchanging
  app, unexpired, about a user who is still active, from a session still open. An access token, a refresh token or
  another app's ID token is refused.
- **The audience must be one partner you list and keep active**, with `issues` including `idJag`. An unknown,
  inactive or non-issuing one is refused with the same answer, so a refusal never tells a caller which partners you
  trust. Naming two is refused.
- **The app must be named on that audience.** An app that isn't named is refused.
- **It carries only what the audience allows:** the resources it governs, and scopes within its ceiling. Anything
  asked beyond the ceiling is dropped, and the response returns the `scope` it did issue. A request that names no
  `scope` gets an assertion with none.
- **No actor and no details.** An `actor_token` or `authorization_details` in the request is refused. This is a known
  limit: the identity-assertion draft lets an assertion carry `authorization_details`, and this realm carries scopes
  and resources only.
- **It is short-lived.** At most five minutes, and never longer than the realm's access-token lifetime: an audience
  may shorten it, never lengthen it. It never outlives the ID token it was made from, or that sign-in's session: an ID
  token with 30 seconds left buys an assertion that ends no later. Once the user has signed out, the ID token buys
  none.
- **Discovery says it, without naming partners.** While the realm offers token exchange, the
  [discovery document](../../registration/discovery/) says it issues assertions
  (`identity_chaining_requested_token_types_supported`) and how they are signed
  (`id_jag_signing_alg_values_supported`).
- **Every exchange is recorded.** Issued or refused, it is on the user's
  [audit trail](../../../../audit-reports/audit-trail/).
- **Users only.** An agent acting for a user is not issued an assertion. It carries its authority across with
  [cross-domain chaining](../cross-domain-chaining/) instead.

## Reference

### Errors

| Error | When |
|---|---|
| `invalid_request` | The subject isn't an ID token, isn't this app's, has expired, is no longer active, or its session has ended; `subject_token_type` isn't the ID-token type; no `audience`, or a parameter repeated; an `actor_token` or `authorization_details` is sent |
| `invalid_target` | The audience is unknown, inactive or doesn't issue identity assertions; a `resource` it doesn't govern |
| `access_denied` | The audience doesn't name this app |
| `unauthorized_client` | A public app, or one without the token-exchange grant |

## Related

- [Identity assertion grant](../identity-assertion-grant/): redeem a partner's assertions here.
- [Cross-domain chaining](../cross-domain-chaining/): the same idea for an agent acting for a user.
- [Token exchange](../token-exchange/): the exchange this builds on.
