# Let developers register their own apps

> Hand a developer a token their tool registers its own client with, within the limits and pins you choose.

Outside developers building against your realm shouldn't need a realm administrator for every app. You mint an
**initial access token** and give it to the developer; their tool registers its own client with the standard
registration call, only within what the token allows. The client is a normal client afterwards, and records which
token registered it.

**Before you start:**

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) turns registration on and mints tokens.
- **Where:** today this is done through the API; [Call the API](../../../../develop/api/) shows how to get a session.
- **What you need:** the host names the developer's app returns to.

## Hand out a registration token

<Steps>

1. Turn registration on in the realm. It takes all four fields:

   ```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 '{ "registration": { "enabled": true, "defaultLifetimeSecs": 3600,
                         "maxLifetimeSecs": 86400, "maxUses": 10 } }'
   ```

2. Mint a token, pinned to what the developer's app may be:

   ```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,
       "grantTypes": ["authorization_code", "refresh_token"],
       "redirectHosts": ["expenses.example.com"],
       "securityProfile": "oauth2.1"
     }'
   ```

   The answer holds the `token`, shown once, and its `id`. A token lasts an hour by default and a day at most, and
   registers one client by default and ten at most; the realm's `registration` settings set those limits.

3. Give the token to the developer. Their tool registers its client:

   ```sh
   curl https://foobar.cvyuh.com/oauth2/register \
     -H "Authorization: Bearer <initial access token>" \
     -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" }'
   ```

   It receives a client id such as `dcr-…`. It never receives a secret: a registered client is public, or proves
   itself with keys it names.

</Steps>

## What a registered client gets

- **Sign-in and refresh only.** Registered clients get the sign-in and refresh grants, never an agent or a
  first-party role, and never have consent implied.
- **Scopes.** The scopes it asks for, from the realm's scopes, or the realm's `cimdScopes` if it asks for none.
- **APIs.** The realm's `cimdResources`. When the realm lists exactly one, that one is the client's default, and a
  token request may leave `resource` out. With more than one, each request names its API with `resource`, or is
  refused.
- **No self-management.** The tool can register only. It can't read, change or delete its registration afterwards;
  a realm administrator does that, as for any client.

## Check it worked

Read the new client as you would any other:

```sh
curl 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/<client-id>' \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

A registration outside the token's pins, with an expired or revoked token, or with none at all, is refused.

On the audit trail, minting is `REGISTRATION_TOKEN_MINTED` and each registration, made or refused, is
`CLIENT_REGISTERED`, both keyed by the token's id; the token itself is never recorded. Look for them under
**Audit → Authentication** in the dashboard.

| Symptom | Cause | Fix |
|---|---|---|
| Minting is refused `404` | Registration isn't on in the realm | Turn it on, as in step 1, with all four fields |
| Minting is refused `400` | The lifetime or the use count asked for is past the realm's `registration` limits | Ask for less, or raise the limits |
| The developer's registration is refused | Its redirect host, grant types or security profile fall outside the token's pins, or the token is used up, expired or revoked | Mint a token whose pins match the app |

## Undo it

Revoke a token before it's used up, so nothing more registers with it:

```sh
curl -X DELETE 'https://foobar.cvyuh.com/api/v0/am/clients/v1/registration-tokens/<id>' \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

Revoking is recorded as `REGISTRATION_TOKEN_REVOKED`. Clients already registered stay; change or deactivate them with `PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/<client-id>`.

## Related

- [Dynamic registration](../../../applications/registration/dynamic-registration/): every rule a registration is held
  to.
- [Accept apps identified by a URL](../apps-identified-by-url/): apps that need no registration at all.
- [Choose how a client proves itself](../../services/choose-client-authentication/): keys in place of a secret.
