# Client attestation

> Only approved builds of an app or agent get tokens, because each instance proves which software it is, not only that it holds a key.

With attestation, a party you trust vouches for a client's instance: a vendor's backend or a platform's attestation
service, called the **attester**, signs a statement that this instance holds a particular key. The instance presents
the statement with a fresh proof signed by that key. A key copied to another machine, or a statement replayed, is not
enough to get a token.

Use it where only approved builds of an app or an agent may get tokens. It is one of the
[client authentication](../client-authentication/) methods, `attest_jwt_client_auth`, and refresh tokens issued
under it stay with the instance that earned them.

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) trusts the attesters and sets the client's
  method; `RealmViewer` can read both. The instance then authenticates itself.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`).
- **First:** each attester's issuer name and keys, and an app or agent that can fetch an attestation and sign proofs.

## How it works

The client sends two JWTs in two headers with its token request:

- the **attestation**, from the attester, in `OAuth-Client-Attestation`;
- the **proof**, signed by the instance's own key, in `OAuth-Client-Attestation-PoP`.

The attestation's claims name the attester, the client and the instance's public key:

```json
{
  "iss": "https://attester.example.com",
  "sub": "field-app",
  "exp": <unix time>,
  "cnf": { "jwk": { "kty": "EC", "crv": "P-256", "x": "…", "y": "…" } }
}
```

The proof's claims name the realm, and carry the realm's challenge:

```json
{
  "aud": "https://foobar.cvyuh.com/oauth2",
  "iat": <unix time>,
  "jti": "4c1e9a7b-2f3d-4e5a-8b6c-9d0e1f2a3b4c",
  "challenge": "eyJhbGciOi…"
}
```

**1. The first request has no challenge yet**, so the realm answers with one:

```sh
curl https://foobar.cvyuh.com/oauth2/token \
  -H "OAuth-Client-Attestation: eyJhbGciOiJFUzI1NiIsInR5cCI6Im9hdXRoLWNsaWVudC1hdHRlc3RhdGlvbitqd3QifQ.…" \
  -H "OAuth-Client-Attestation-PoP: eyJhbGciOiJFUzI1NiIsInR5cCI6Im9hdXRoLWNsaWVudC1hdHRlc3RhdGlvbi1wb3Arand0In0.…" \
  -d grant_type=client_credentials \
  -d scope=orders.read
```

```http
HTTP/1.1 400 Bad Request
OAuth-Client-Attestation-Challenge: eyJhbGciOi…
Cache-Control: no-store

{
  "error": "use_attestation_challenge",
  "error_description": "the proof of possession must carry the server's challenge"
}
```

**2. The client signs a new proof** with that challenge in it, and sends the request again. The answer is a normal
token response.

A challenge is good for `dpopNonceLifetimeSecs` (300 seconds by default). The realm seals it rather than storing it.

## Set it up

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

1. Turn the method on and name the attesters the realm trusts, with
   `PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings`: add `attest_jwt_client_auth` to
   `tokenEndpointAuthMethods`, and list each attester in `clientAttesters`. 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. On the client (`https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client`), set `tokenEndpointAuthMethod` to `attest_jwt_client_auth` and
   name its attesters in `clientAttesters`.

| Where | Setting | What it does |
|---|---|---|
| Realm | `tokenEndpointAuthMethods` includes `attest_jwt_client_auth` | Off by default |
| Realm | `clientAttesters` | The attesters the realm trusts, by `iss`, each by its keys (`jwks`) or an `https` `jwksUri` |
| Realm | `tokenEndpointAuthSigningAlgs` | The algorithms both JWTs may be signed with (`ES256`, `RS256` by default) |
| Realm | `clientAssertionMaxLifetimeSecs`, `clockSkewSecs` | How old a proof may be (300) and the clock tolerance (60) |
| Realm | `dpopNonceLifetimeSecs` | How long a challenge is good for (300) |
| Client | `tokenEndpointAuthMethod` `attest_jwt_client_auth`, `clientAttesters` | This client authenticates by attestation, from these attesters (at least one, and only with this method) |
| Client | `securityProfile` | Anything but `fapi2`. An attesting agent's client that names none is written `oauth2.1` |

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `attest_jwt_client_auth` in `tokenEndpointAuthMethods` | not offered | add it to offer it |
| `clientAssertionMaxLifetimeSecs`: how old a proof may be | 300 | 1 to 3,600 |
| `clockSkewSecs` | 60 | 0 to 300 |
| `dpopNonceLifetimeSecs`: how long a challenge is good for | 300 | 10 to 3,600 |
| Uses per proof | one | fixed |
| Security profile | anything but `fapi2` | fixed |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `use_attestation_challenge` on the first try | That's the design: the proof needs the realm's challenge | Retry with the value from `OAuth-Client-Attestation-Challenge` |
| `invalid_client` for a new build | Its attester isn't in the realm's `clientAttesters`, or not in the client's | Trust the attester on both |
| `invalid_grant` on refresh after scaling out | Another instance used the refresh token | Each instance refreshes only its own tokens |
| Saving the client is refused | It names `fapi2`, or attesters without the method | Choose `oauth2.1` or `none`; set the method |

## Audit record

Each token request is a `TOKEN_ISSUED` record under the client (or its agent), issued or refused with its `error`.
Changes to `clientAttesters` and the client are in **Audit → Config**.

## Good to know

- **Only attesters the realm trusts, and only those the client names.** A statement from any other is refused
  `invalid_client`.
- **The statement is about this client.** Its `sub` is the client ID, it carries an `iat` no further ahead than the
  realm's clock skew, it hasn't expired (allowing the skew), and the key it vouches for, in `cnf.jwk`, is a public
  key. The client is named by that `sub`: a `client_id` sent beside it must match it.
- **Each JWT says what it is.** The attestation's `typ` is `oauth-client-attestation+jwt`, and the proof's is
  `oauth-client-attestation-pop+jwt`. Both are signed with an algorithm in the realm's `tokenEndpointAuthSigningAlgs`.
- **The proof is signed by that key, fresh, and used once.** It is addressed to the realm (`aud` is the issuer, as a
  single string). Fresh means its `iat` isn't ahead of the realm's clock by more than the skew, and is no older than
  `clientAssertionMaxLifetimeSecs` plus the skew.
- **A refresh token stays with its instance.** A refresh token issued to an attesting client refreshes only when the
  same instance authenticates, by the same key. Another instance of the same client is refused `invalid_grant`, and
  the token stays usable by its own instance.
- **The two headers travel together, once each.** Either header without the other, either sent twice, or a value
  that isn't a single token is refused `invalid_request`.
- **Not under FAPI 2.0.** FAPI 2.0 doesn't allow authentication by attestation, so a client authenticating this way
  can't hold the `fapi2` [profile](../security-profiles/). Any other profile takes it, including `none`. An attesting
  agent's client that names `fapi2` is refused when it is saved ([AI agents](../../../agents/ai-agents/)).
- **Not offered to self-registered clients.** [Dynamic registration](../../registration/dynamic-registration/) and
  [client metadata documents](../../registration/client-metadata-documents/) don't offer the method.
- **An agent's `attestationType` and `attestation` fields are recorded only**, never checked.

## Coming next

- **Access tokens bound to the instance's key.** Today the refresh token is bound to the attested instance; the
  access tokens it buys are not yet bound to that key.

## Reference

### Errors

| Error | When |
|---|---|
| `use_attestation_challenge` (400) | The proof carries no challenge, or one that is no longer current. Retry with the one in `OAuth-Client-Attestation-Challenge` |
| `invalid_client` (401) | The attester isn't trusted or isn't one the client names; a signature, a `typ`, a time, the `sub` or the key doesn't hold; or the proof was used before |
| `invalid_request` (400) | A header missing, sent twice or not a single token; a `client_id` that isn't the attestation's `sub`; or another client authentication method beside it |
| `invalid_grant` (400) | A refresh token issued to another instance of this client |

## Related

- [Client authentication](../client-authentication/): every method a client can use.
- [AI agents](../../../agents/ai-agents/): agents that authenticate by attestation.
- [DPoP](../dpop/): bind access tokens to a key the client holds.
- [Workload identity](../../../agents/workload-identity/): agents that prove themselves with their platform's identity.
