# Certificate-bound tokens

> Clients sign in with a TLS client certificate, and their tokens work only over a connection presenting it.

A client that holds a TLS certificate can sign in with it instead of a secret, and have its tokens bound to it. A
bound token works only over a connection presenting that same certificate, so a copy of the token is no use without
the certificate's key. The client registers its public keys, as a key-based client does, and the realm matches the
certificate's key against them: no certificate authority is involved.

Use it for services that already hold TLS certificates, such as those in a mutual-TLS deployment. **What's missing
today:** only self-signed certificates are accepted. The [gateway](/gateway/) checks the binding only when the TLS
front end before it forwards the client certificate ([Bound tokens at the gateway](/gateway/protect-apis/bound-tokens/));
where yours can't, bind tokens with [DPoP](../dpop/) instead.

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`); `RealmViewer` can read the settings but
  not change them. The client then signs in with its own certificate.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`).
- **First:** the client holds a self-signed certificate and can send it without being asked (curl and Go do; some
  SDKs need setting up).

## How it works

While the realm's `mtls` setting is on, each of its hosts has an **mtls alias**: `foobar.cvyuh.com` is
`mtls.foobar.cvyuh.com`. A client connects there with its certificate; browsers on the realm's own host are never
asked for one. The issuer is the same at both hosts.

The client asks for a token at the alias, over a TLS connection presenting the certificate. The certificate is the
client's credential, so the form names the client and nothing else:

```sh
curl https://mtls.foobar.cvyuh.com/oauth2/token \
  --cert payments.crt --key payments.key \
  -d grant_type=client_credentials \
  -d client_id=payments
```

```json
{ "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6ImF0K2p3dCJ9.…", "token_type": "Bearer", "expires_in": 300 }
```

The token names the certificate's thumbprint in `cnf.x5t#S256`, and [introspection](../../revoke-access/introspection/)
shows it too. The client then calls the realm's APIs over the same certificate, with the token as a bearer token.
Over another certificate, or none, the token is refused.

## Set it up

1. Turn the realm's `mtls` on and offer the method. Read the realm's settings first: **a list you send replaces the
   realm's list.**

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "mtls": true, "tokenEndpointAuthMethods": ["…existing methods", "self_signed_tls_client_auth"] }'
   ```

2. Register the client with its keys and the method:

   ```sh
   curl 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=payments' \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{
       "tokenEndpointAuthMethod": "self_signed_tls_client_auth",
       "jwks": { "keys": [ { "kty": "RSA", "n": "…", "e": "AQAB" } ] },
       "tlsClientCertificateBoundAccessTokens": true,
       "grantTypes": ["client_credentials"]
     }'
   ```

3. Have the client ask for its tokens at `https://mtls.foobar.cvyuh.com/oauth2/token`. The alias answers once its
   own certificate is issued, moments after you turn `mtls` on.

## When it fails

| Error | Why | What to do |
|---|---|---|
| The TLS handshake fails | The client sent no certificate | Configure the client to send its certificate without being asked |
| `invalid_client` (401) | The certificate's key isn't one the client registered, or the realm doesn't offer the method | Register the key in `jwks` or `jwksUri`; add the method to the realm's list |
| `invalid_token` (401) | A bound token came over another certificate, or none | Call over the certificate the token was issued to |
| `invalid_grant` (400) | A public client's refresh token came over another certificate | Refresh over the same certificate |
| `400` on save | The client asks for both DPoP and certificate-bound tokens, or has neither or both of `jwks` and `jwksUri` | Choose one binding, and exactly one key source |
| `409` on turning `mtls` on | An alias of one of the realm's hosts is already held by another realm | Contact support |

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `mtls` | off | on or off, for the whole realm |
| `self_signed_tls_client_auth` in `tokenEndpointAuthMethods` | not offered | add it to offer it |
| `tlsClientCertificateBoundAccessTokens` | off | per client |
| Certificates | self-signed, holding a registered EC P-256 or RSA 2,048-bit or larger key | fixed today |

## Audit record

Each token request is a `TOKEN_ISSUED` record under the client, issued or refused with its `error`. Turning `mtls` on
or off, and registering the client, are changes in **Audit → Config**.

## Good to know

- **The alias is the realm's while `mtls` is on.** With it off, the alias answers `404`. A host beginning `mtls.`
  can't be claimed as a host of its own.
- **A token is bound one way.** A client is registered for certificate-bound tokens or for [DPoP](../dpop/), never
  both. A bound token is still `token_type: Bearer`: possession is proved by the connection.
- **Only a client that asks is bound.** Without `tlsClientCertificateBoundAccessTokens`, a client presenting a
  certificate gets a plain bearer token. A public client's refresh tokens are bound too.
- **The discovery document lists the alias** (`mtls_endpoint_aliases`) while `mtls` is on, unless the realm's base URL
  is at a host the realm doesn't claim, such as your own proxy.
- **A [FAPI 2.0](../security-profiles/) client stays on `private_key_jwt` with DPoP.**

## Coming next

- **Certificates from your own certificate authority** (`tls_client_auth`), checked against trust anchors you
  provide.

## Related

- [Client authentication](../client-authentication/): every way a client can prove itself.
- [DPoP](../dpop/): bind tokens to a key with a proof on each request.
- [Introspection](../../revoke-access/introspection/): how an API sees the binding.
- [Discovery document](../../registration/discovery/): where clients find the alias's endpoints.
