# Bound tokens at the gateway

> The gateway takes a token bound to a key only with a fresh proof from that key, and one bound to a certificate only over that certificate, so a copied token is useless at your APIs.

A bearer token works for whoever holds it. A bound token works only for the client that holds its key: the realm
binds it to a key ([DPoP](/workforce-identity/applications/client-security/dpop/)) or a client certificate
([Certificate-bound tokens](/workforce-identity/applications/client-security/certificate-bound-tokens/)), and the
[gateway](/gateway/) checks the binding on every request. A token copied out of an agent's environment, a
log or a proxy is refused at your APIs as it is at the realm.

Agents' clients are held to the FAPI 2.0 profile ([Security profiles](/workforce-identity/applications/client-security/security-profiles/)) unless they name
another or attest, so a non-attesting agent's tokens are bound by default. Require binding at the gateway, and it refuses every unbound
token too.

## Before you begin

- **The gateway running** in front of your API: see [Run the gateway](/gateway/protect-apis/run-the-gateway/). Its
  image isn't published yet: ask your Cvyūh contact for it.
- **To bind a client's tokens in the realm**, a realm administrator in a group holding `RealmOwner`, with the API.
  Everything on the gateway's side is a setting you give it when it starts.
- **For certificate-bound tokens**, a TLS front end that checks client certificates and forwards the one it saw.

## How it works

A *proof* is a short signed token the client makes for each request with its private key. It names the request's
method and address and the access token it comes with. A client presents a bound token under the `DPoP` scheme, with
its proof in the `DPoP` header:

```http
GET /orders/42 HTTP/1.1
Host: api.example.com
Authorization: DPoP eyJhbGciOiJFUzI1NiIsInR5cCI6ImF0K2p3dCJ9.eyJjbmYiOnsiamt0Ijoi…
DPoP: eyJ0eXAiOiJkcG9wK2p3dCIsImFsZyI6IkVTMjU2IiwiandrIjp7…
```

The proof must name this request's method and address and this token, be at most a minute old, and use `ES256`,
`PS256` or `RS256`. A proof is taken once: the gateway refuses a proof it has already taken. With several replicas, see
[Run it in production](/gateway/protect-apis/run-in-production/#several-replicas).

### Nonces

With a nonce key set, a proof must also carry a nonce the gateway issued. The first request without one is
answered with the nonce to use:

```http
HTTP/1.1 401 Unauthorized
WWW-Authenticate: DPoP error="use_dpop_nonce", algs="ES256 PS256 RS256", resource_metadata="https://api.example.com/.well-known/oauth-protected-resource"
DPoP-Nonce: <nonce>
```

The client makes a new proof carrying the nonce, and sends the request again. A nonce lasts five minutes, and every
allowed answer to a `DPoP` request carries the next one, so a client meets the refusal once, not each time a nonce
ages out. Give every replica the same key file, and a nonce one replica gives is good at all of them. Treat a nonce as
an opaque string.

A nonce also cures a wrong clock: a proof that carries a fresh nonce is taken even when its time is up to 24 hours
off, since the nonce, not the client's clock, shows it's fresh.

### Certificate-bound tokens

A token bound to a client certificate carries the certificate's thumbprint (`cnf.x5t#S256`). The gateway takes it
only when the request came over that certificate. TLS ends in front of the gateway, so the front end forwards the
client certificate in the `x-forwarded-client-cert` header. With the forwarded-certificate setting on (see
[Gateway configuration](/gateway/configuration/#tokens-and-callers)), the gateway reads it. Turn it on only where every
request reaches the gateway through that front end. Off, no request carries a certificate, and every
certificate-bound token is refused.

## Set it up

1. Bind the client's tokens in the realm: an agent's client is bound by default; for any other, set
   `dpopBoundAccessTokens: true` on the client, or follow [Bind tokens to a key](/workforce-identity/guides/services/bind-tokens-to-a-key/).
2. Optionally, set `DPOP_REQUIRED=true` on the gateway, to refuse every token bound to no key.
3. Optionally, set `DPOP_NONCE_KEY` to a key file, the same on every replica, to require nonces.
4. If clients address the API at another origin than its audience, set `PUBLIC_ORIGIN` to it: a proof names the
   address the client used.
5. For certificate-bound tokens, have your TLS front end forward the client certificate, and turn on the
   forwarded-certificate setting.

## Good to know

- **Bound means bound.** A token bound to a key (`cnf.jkt`) is taken only under `DPoP`, beside a proof signed by that
  key. Sent as a plain bearer token, it's refused `invalid_token`. A token bound to no key, sent under `DPoP`, is
  refused too.
- **One `DPoP` header.** A request carrying more than one is refused.
- **Required, if you say so.** With `DPOP_REQUIRED`, the challenge is `DPoP` alone, and the gateway's metadata
  says proofs are required.
- **Without nonces, the minute is strict.** A proof outside its minute is refused `invalid_dpop_proof`. With nonces,
  it's answered `use_dpop_nonce`, so the client can retry.
- **Behind your own proxy, the next nonce must be relayed.** An allowed `ext_authz` check carries `DPoP-Nonce`, and
  the proxy has to pass it on to the client, as a header it copies from the check onto the answer. A proxy that
  copies the check's headers only onto the request to your API never relays it, so behind it the client gets its
  nonce from the `401` each time one ages out.
- **Running more than one replica?** Give every replica the same `DPOP_NONCE_KEY`, and keep each client on one
  replica: see [Run it in production](/gateway/protect-apis/run-in-production/#several-replicas).

## Limits and defaults

| What | Default | Can it change? |
|---|---|---|
| A proof's age | At most a minute, with 5 seconds of clock difference allowed | No |
| A proof's algorithms | `ES256`, `PS256`, `RS256` | No |
| A nonce's life | Five minutes | No |
| A proof's clock with a fresh nonce | Up to 24 hours off | No |
| Binding required | Off | `DPOP_REQUIRED` |
| Nonces required | Off | `DPOP_NONCE_KEY` |

## What the client sees

A client that gets the binding wrong meets a `401` with a `DPoP` challenge, and the reason in `error`. A client that
needs a nonce gets it in `DPoP-Nonce` on that `401`, and retries once. The errors, and what the client does about each,
are under [Errors](#errors).

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A bound token is `401 invalid_token` | It was sent as `Bearer` | Send it as `Authorization: DPoP <token>`, with a proof in `DPoP` |
| Every proof is `401 invalid_dpop_proof` | The proof's `htu` names another address than the gateway expects: clients call an origin other than the audience's | Set `PUBLIC_ORIGIN` to the origin clients call |
| Proofs fail now and then, without nonces | The client's clock is more than a few seconds off | Turn on nonces: a proof with a fresh nonce is taken whatever the client's clock says |
| Behind your proxy, clients meet `use_dpop_nonce` every few minutes | Your proxy doesn't copy `DPoP-Nonce` from the check onto its answer | Copy it: see [Put the gateway behind your proxy](/gateway/guides/put-the-gateway-behind-your-proxy/) |
| Every certificate-bound token is `401 invalid_token` | The forwarded-certificate setting is off, or the front end doesn't forward the certificate | Turn the setting on, and forward the certificate in `x-forwarded-client-cert` |

## Audit record

The realm records each bound token it issues as `TOKEN_ISSUED`, with the key it's bound to. The gateway's refusals
aren't on the audit trail yet: each is a warning in the gateway's own log, with the path and why the proof failed. See
[Logs and tracing](/gateway/configuration/#logs-and-tracing).

## Reference

### Errors

| Status and reason | What it means | What the client does |
|---|---|---|
| `401 invalid_token` | A bound token came as a bearer token, an unbound one came under `DPoP`, an unbound one came where `DPOP_REQUIRED` is set, or a certificate-bound token came without its certificate or over another | Present the token as its binding requires |
| `401 invalid_dpop_proof` | The proof is missing, there's more than one `DPoP` header, or the proof doesn't name this request and token, is too old, uses another algorithm, is signed by another key, or was used before | Make a new proof for this request |
| `401 use_dpop_nonce` | The proof needs the gateway's fresh nonce | Retry with the nonce in `DPoP-Nonce` |
| `503 dpop_proofs_full` | The gateway is holding too many recent proofs | Try again shortly |

### Settings

| Where | Setting | What it does |
|---|---|---|
| Client | `securityProfile` | Absent, `fapi2` for an agent's client, whose tokens are bound (`oauth2.1` for one that attests) |
| Client | `dpopBoundAccessTokens` | Issue this client only bound tokens (default off) |
| Gateway | `DPOP_REQUIRED` | Refuse every token bound to no key (default off) |
| Gateway | `DPOP_NONCE_KEY` | The key nonces are sealed with (unset, no nonce) |
| Gateway | `PUBLIC_ORIGIN` | The origin a proof names for this API (unset, the audience's) |
| Gateway | The forwarded-certificate setting | Read the client certificate your TLS front end forwards, for certificate-bound tokens (default off) |

## Related

- [Bind tokens to a key](/workforce-identity/guides/services/bind-tokens-to-a-key/): the task, start to finish.
- [DPoP](/workforce-identity/applications/client-security/dpop/): how the realm issues bound tokens.
- [Certificate-bound tokens](/workforce-identity/applications/client-security/certificate-bound-tokens/): binding to a client certificate.
- [Token swap](/gateway/protect-apis/token-swap/): a bound caller stays bound all the way to the API.
