# Choose how a client proves itself

> Pick between a secret and a signed assertion for a confidential client, set it up, and rotate it without downtime.

Every confidential client, such as a web app's server, a back-end service or an agent, proves itself each time it
asks for a token. It can hold a **secret** the realm keeps a hash of, or a **private key** whose public half the realm
holds, signing a fresh assertion each time (`private_key_jwt`). Choose the signed assertion wherever the client can
manage a key: a captured request gives an attacker nothing. This guide is for anyone registering a confidential client.

**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:** for a signed assertion, the client can make and keep a key pair.

## Choose

| | Secret | Signed assertion (`private_key_jwt`) |
|---|---|---|
| The client holds | A shared secret | A private key |
| The realm holds | The secret's hash | The public key only |
| A leak from the realm gives an attacker | Nothing usable | Nothing usable |
| A leak from the client gives an attacker | The secret, until you change it | The key, until you replace it |
| A captured request gives an attacker | The secret | Nothing: each assertion works once, for five minutes |
| Rotating it | Change `clientSecret`, then the client | Publish a new key beside the old one, then drop the old |
| Required for | — | Agents, and clients held to FAPI 2.0 |

## Register with a signed assertion

<Steps>

1. The client makes a key pair and gives you the public key.

2. Register it:

   ```sh
   curl 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=<client-id>' \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "status": "active",
       "tokenEndpointAuthMethod": "private_key_jwt",
       "tokenEndpointAuthSigningAlg": "ES256",
       "jwks": { "keys": [
         { "kty": "EC", "crv": "P-256", "kid": "rb-key-1", "x": "…", "y": "…" }
       ] },
       "grantTypes": ["client_credentials"],
       "scopes": ["reports:read"],
       "allowedResources": ["https://reports.example.com/api"],
       "defaultAudience": "https://reports.example.com/api" }'
   ```

   Public keys only: a key with private parts is refused, and no two keys may share a `kid`. A client that publishes
   its own keys gives `jwksUri` (an `https` address) instead of `jwks`, never both.

3. The client's assertion names its client id as `iss` and `sub`, and the realm's issuer
   `https://foobar.cvyuh.com/oauth2` as `aud`: a single string, not the token address.

4. To rotate later, `PATCH` the client's `jwks` with both keys, move the client to the new one, then `PATCH` with the
   new key only.

</Steps>

## Or register with a secret

Register as in [Let one service call another](../service-to-service/). To change the secret,
`PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/<client-id>` with `{ "clientSecret": "<new secret>" }`.

By default the old secret stops at once. To overlap, set the realm's `clientSecretRotationGraceSecs`: the replaced
secret keeps working for that many seconds, or until its own `clientSecretExpiresAt` if that's sooner.

## Rule a method out for the whole realm

The realm accepts all four methods by default. To accept only some, for example no secrets, or no public clients,
send the methods it should keep accepting. The list replaces the realm's whole 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 '{ "tokenEndpointAuthMethods": ["private_key_jwt"] }'
```

A client registered for a method the realm no longer accepts is refused at the token endpoint.

## Check it worked

- The client gets a token with the method you registered. Exactly one method is accepted per client.
- Any refusal is `invalid_client`, with no hint why. It is on the trail as a `TOKEN_ISSUED` record with result
  `FAILED`, under the client's ID, with the reason. Find it in **Audit → Authentication** in the dashboard.
- A secret change is a `CLIENT_SECRET_ROTATED` record, saying until when the old secret works. Each use of the old
  secret in that time is a `CLIENT_SECRET_PREVIOUS_USED` record: when they stop, the client has moved, and you can
  stop waiting.

<Aside type="note" title="Guessing">
  When the realm's sign-in lockout is on, too many wrong secrets lock the client out for the lockout period, even
  with the right secret. Each counted miss and the lock are on the trail under the client, as
  `AUTH_FAILURE_RECORDED` and `ACCOUNT_LOCKED`.
</Aside>

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `invalid_client` with the right secret | The client is locked out after wrong secrets, its `status` is `inactive`, or it sent the secret by another method than registered | Wait out the lockout; check `status` and `tokenEndpointAuthMethod` |
| `invalid_client` with a signed assertion | `aud` isn't the issuer as a single string, the assertion expires further ahead than `clientAssertionMaxLifetimeSecs` (five minutes) or is reused, or its `kid` isn't registered | Fix the assertion; register the key |
| The client fails at once after a secret change | No overlap was set | Set `clientSecretRotationGraceSecs` before the next change |
| Registering the key is refused | It has private parts, two keys share a `kid`, or both `jwks` and `jwksUri` are given | Send public keys only, unique `kid`s, one of the two |

## Undo it

`PATCH` the client back to its earlier `tokenEndpointAuthMethod` with the matching credential, and the realm's
`tokenEndpointAuthMethods` back to its earlier list.

## Related

- [Client authentication](../../../applications/client-security/client-authentication/): every method in full.
- [Hold a client to FAPI 2.0](../fapi2-clients/): signed assertions and more, with one setting.
