# Dynamic registration

> Developers' tools register their own clients with a one-time token you hand out, within limits you set.

Instead of registering every app yourself, you hand a developer an **initial access token**: a pass that lets their
tool register clients. The tool posts its details to the realm's registration endpoint with that token and gets a
client ID back. You decide how long the token lasts, how many clients it may register, and what those clients may be.

Use it for tools that know only this way of registering. For apps that can publish a document about themselves,
[client metadata documents](../client-metadata-documents/) need no token at all. Either way, a self-registered client
is held to the least it needs: public or key-based, never a secret, never an agent.

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) turns registration on and mints tokens;
  `RealmViewer` can read the settings. Developers then register with the token you hand them.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`).
- **First:** decide which scopes and APIs self-registered clients may reach (`cimdScopes`, `cimdResources`).

## How it works

**1. You mint a token**, as a realm administrator who may change the realm's OAuth2 settings:

```sh
curl https://foobar.cvyuh.com/api/v0/am/clients/v1/registration-tokens \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "lifetimeSecs": 86400, "uses": 3, "redirectHosts": ["expenses.example.com"] }'
```

The answer is `201`. Its `token` is shown this once and never again, so hand it over now:

```json
{
  "id": "6f1c2a9e-3b4d-4e8f-9a7c-1d2e3f4a5b6c",
  "token": "6f1c2a9e-3b4d-4e8f-9a7c-1d2e3f4a5b6c.KQ7mTz…",
  "expiresAt": "<RFC 3339 time>",
  "uses": 3,
  "pins": { "grantTypes": null, "redirectHosts": ["expenses.example.com"], "securityProfile": null }
}
```

The `id` is how you revoke it later. A token can **pin** the grants, the return hosts and the security profile its
clients may have.

**2. The developer's tool registers**, sending the token as a bearer token:

```sh
curl https://foobar.cvyuh.com/oauth2/register \
  -H "Authorization: Bearer 6f1c2a9e-3b4d-4e8f-9a7c-1d2e3f4a5b6c.KQ7mTz…" \
  -H "Content-Type: application/json" \
  -d '{
    "application_type": "web",
    "client_name": "Expense Tracker",
    "redirect_uris": ["https://expenses.example.com/callback"],
    "token_endpoint_auth_method": "none"
  }'
```

The answer is `201`, with the client as the realm stored it:

```json
{
  "client_id": "dcr-3f9a1c7e5b2d4a6f8e0c1b3d5f7a9e2c",
  "client_id_issued_at": 1794393000,
  "application_type": "web",
  "client_name": "Expense Tracker",
  "token_endpoint_auth_method": "none",
  "grant_types": ["authorization_code"],
  "response_types": ["code"],
  "redirect_uris": ["https://expenses.example.com/callback"],
  "scope": "openid profile"
}
```

### What the tool must send

- **`application_type`**, either `web` or `native`.
- **`redirect_uris`**, at least one. A `web` app's must be `https`. A `native` app may also use the device itself
  (`http://127.0.0.1`, `http://[::1]` or `http://localhost`, with any port or path). A redirect's query may carry the
  app's own parameters, never the authorization response's (`code`, `state`, `iss`, `error`, `error_description`,
  `error_uri`).
- **`response_types`**, if sent, holding only `code`.

### What a registered client may be

- **Public or key-based, never a secret:** `none`, or proving itself with keys it names in `jwks` or `jwks_uri`, by a
  signed assertion (`private_key_jwt`) or by its own certificate at the realm's mtls alias
  (`self_signed_tls_client_auth`, [certificate-bound tokens](../../client-security/certificate-bound-tokens/)). Such a client may ask for
  its access tokens to be bound to the certificate, with `"tls_client_certificate_bound_access_tokens": true`.
- **The sign-in and refresh grants only.** A tool that names no `grant_types` gets `authorization_code` alone.
- **Never an agent, never first-party**, and consent is always asked.
- **The security profile the token pins**, or `none`.
- **Signed responses, if it asks.** `id_token_signed_response_alg` and `userinfo_signed_response_alg` are taken when
  they name an algorithm in the realm's `keySettings.algorithm.oauth2.idToken`.
- **Scopes and APIs from the realm's lists.** It gets the scopes it asks for, from those the realm offers, or
  `cimdScopes` if it asks for none. It may get tokens only for the APIs in `cimdResources`. When that list holds
  exactly one, it is the client's default audience. Otherwise each token request names its API with `resource`, or
  is refused.
- **A client ID that starts `dcr-`**, so it never clashes with one you chose.

## Set it up

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

1. Turn registration on with `PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings`, setting
   `registration.enabled` to `true`, and set `cimdScopes` and `cimdResources` for the clients it will make.
2. Mint a token with `POST https://foobar.cvyuh.com/api/v0/am/clients/v1/registration-tokens`, as above, and hand it to the developer.
3. The developer's tool registers at `POST https://foobar.cvyuh.com/oauth2/register`.

To revoke a token, call `DELETE https://foobar.cvyuh.com/api/v0/am/clients/v1/registration-tokens/<id>`; the realm answers `204`.

| Where | Setting | What it does |
|---|---|---|
| Realm | `registration.enabled` | Turns the endpoint on (default off) |
| Realm | `registration.defaultLifetimeSecs`, `maxLifetimeSecs` | Token lifetime: default 3600, cap 86400 |
| Realm | `registration.maxUses` | The most clients one token may register (default 10, up to 100) |
| Realm | `cimdScopes`, `cimdResources` | A registered client's scopes when it asks for none, and the APIs it may get tokens for |
| Token | `lifetimeSecs`, `uses`, `grantTypes`, `redirectHosts`, `securityProfile` | One token's limits and pins |

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `registration.enabled` | off | on or off |
| A token's life (`defaultLifetimeSecs`, `maxLifetimeSecs`) | 3,600 | up to 86,400 |
| Clients one token registers (`maxUses`) | 1 per token; `maxUses` 10 | up to 100 |
| Client IDs | start `dcr-` | fixed |
| Clients in a realm | 1,000, all clients together | ask support to raise it |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| The registration endpoint answers `404` | Registration is off | Set `registration.enabled` to `true` |
| `invalid_token` for a token you just minted | Its uses are spent, it expired, or the tool didn't send it as `Bearer` | Mint a new one; check the `Authorization` header |
| `invalid_client_metadata` | The tool asks for a secret, a grant outside the pin, or a scope the realm doesn't offer | Have the tool ask for `none` or `private_key_jwt` and the sign-in grants only |
| `invalid_redirect_uri` | An address off the pinned hosts, or not `https` | Pin the right hosts, or have the tool use `https` |

## Audit record

`REGISTRATION_TOKEN_MINTED` and `REGISTRATION_TOKEN_REVOKED`, keyed by the token's id (the token itself is never
recorded), and `CLIENT_REGISTERED` for each registration, allowed or refused, keyed by the token that paid for it.
See them in **Audit → Authentication**.

## Good to know

- **Off by default.** While it is off, the endpoint answers `404` and discovery doesn't mention it.
- **No token, no registration.** Only a realm administrator mints tokens. A token lasts an hour by default, a day at
  most, and registers one client by default, up to `maxUses`.
- **A pin refuses, never narrows.** A registration outside a pin is refused, never quietly narrowed.
- **Most refusals spend nothing.** A registration refused for its metadata leaves the token's uses as they were. One
  refused only as the client is saved, such as when the realm already holds its most clients, uses one up: mint a new
  token if a developer runs out this way.
- **No self-management.** The registration answer carries no registration access token, and the client can't read,
  change or delete its own registration. You change or remove it as a realm administrator; see
  [Not offered](../../../../reference/not-offered/#client-self-management).
- **A registered client is a normal client afterwards.** It records which token registered it, and you manage it
  like any other.
- **Every mint, use and revocation is on the [audit trail](../../../../audit-reports/audit-trail/).** The token
  itself is never recorded.

## Reference

### Errors

| Error | When |
|---|---|
| `404` | Registration is off |
| `invalid_token` (401) | No token; not a bearer token; an unknown, revoked or expired token; or one whose uses are spent. The answer carries `WWW-Authenticate: Bearer error="invalid_token"` |
| `invalid_client_metadata` (400) | A field outside what the realm or the token allows: no or a wrong `application_type`, a secret, a method other than `none`, `private_key_jwt` or `self_signed_tls_client_auth`, keys that don't match the method, certificate-bound tokens asked by a client not proving itself by its certificate, a grant outside the pin, a response type other than `code`, a scope the realm doesn't offer, or a signing algorithm it doesn't sign with |
| `invalid_redirect_uri` (400) | No `redirect_uris`; one that isn't `https` (or the device itself, for a `native` app); one whose query carries an authorization response parameter; or one outside the hosts the token pins |

When minting, a `lifetimeSecs` or `uses` past the realm's limits, or a grant pin other than the sign-in and refresh
grants, is refused.

## Related

- [Let developers register their own apps](../../../guides/partners/developers-register-apps/): the task, start to finish.
- [Client metadata documents](../client-metadata-documents/): register with no token, from a published document.
- [Not offered](../../../../reference/not-offered/#client-self-management): why a client can't change its own registration.
- [Security profiles](../../client-security/security-profiles/): what a pinned profile holds a client to.
