# Bind tokens to a key

> Make a client's tokens useless to anyone who copies them, by binding each token to a key only the client holds.

A stolen bearer token works for whoever holds it until it expires. A token bound with
[DPoP](../../../applications/client-security/dpop/) works only beside a fresh proof signed by the client's private key,
which never leaves the client, so a token copied from a log, a proxy or a browser extension can't be used. For public
apps, the refresh token is bound too. This guide is for browser and mobile apps, whose tokens are the easiest to
steal, and any client whose APIs check proofs.

**Before you start:**

- **Who can do it:** a realm administrator, in a group that holds `RealmOwner`
  ([Delegated administration](/platform/realms/delegated-administration/)). `RealmViewer` can read the settings but
  not change them.
- **Where:** today this is done through the API; [Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`.
- **First:** have the client's ID, and a client library that makes DPoP proofs.

## Require bound tokens

<Steps>

1. Require bound tokens for the client:

   ```sh
   curl -X PATCH 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/<client-id>' \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "dpopBoundAccessTokens": true }'
   ```

   Without this, a client that sends a proof still gets a bound token; with it, sending one is compulsory and the
   client can never fall back to a bearer token. An agent's client needs no change: it's held to FAPI 2.0
   by default, which binds its tokens.

2. Leave the realm's nonces on (`oauth2Settings.dpopNonceRequired`) unless a client can't handle the retry. To turn
   them off: `PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings` with `{ "dpopNonceRequired": false }`.

</Steps>

## What the client does

<Steps>

1. It makes a key pair. In a browser, a non-exportable key in the browser's key store.

2. With each token request, it sends a `DPoP` header: a small JWT signed by its key, naming the request's method and
   address.

3. The first request is answered `use_dpop_nonce` with a `DPoP-Nonce` header. It signs a new proof including the
   nonce and sends again. Every answer after that carries the next nonce.

4. Its tokens come back with `token_type: DPoP`, each naming the key.

5. It calls APIs with `Authorization: DPoP <token>` and a fresh proof naming the token. Each proof works once.

</Steps>

## Check it worked

- At the realm's own APIs, such as userinfo, the bound token sent as a plain bearer token is refused.
- Introspection shows the key the token is bound to, in `cnf.jkt`, for an API that checks proofs itself.
- At the gateway, a bound token without a matching proof is refused `invalid_dpop_proof`.

Each bound token's `TOKEN_ISSUED` record names the key it is bound to, in `bound`. Find it in
**Audit → Authentication** in the dashboard.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| Every first token request is refused `use_dpop_nonce` | Expected: the realm hands out a nonce | Retry with a new proof carrying the `DPoP-Nonce` it sent |
| `invalid_dpop_proof` | The proof names another method or address, was used before, is too old, or the clock is off | Sign a fresh proof for each request, with the exact method and address |
| `invalid_dpop_proof`, "this client is issued DPoP-bound tokens only" | The client sent no proof, and bound tokens are now required | Have the client send a `DPoP` header |
| A bound token is refused at an API as a bearer token | Expected: it must be sent as `DPoP` with a proof | Send `Authorization: DPoP <token>` and a proof naming it |

## Undo it

`PATCH` the client with `{ "dpopBoundAccessTokens": false }`. Tokens already bound stay bound until they expire.

## Related

- [DPoP](../../../applications/client-security/dpop/): the standard in full.
- [Bound tokens at the gateway](/gateway/protect-apis/bound-tokens/): how the gateway checks proofs.
- [Hold a client to FAPI 2.0](../fapi2-clients/): binding and more, in one setting.
- [Check a token at your API](../../../../gateway/guides/check-a-token-at-your-api/): introspection and `cnf.jkt`.
