# Give a client tokens for one API

> Make each token name the one API it's for, so a token leaked from one API can't be used at another.

A token names its API in `aud`, and an API that checks `aud` refuses tokens meant for any other. So if one API is
compromised or logs tokens carelessly, the tokens taken there are useless against the rest. This guide is for any
client that calls more than one of your APIs, and every API that checks tokens.

**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:** have the client's ID and the audience of each API it calls.

## Set it up

<Steps>

1. List the APIs the client may call, and the one it gets when it names none:

   ```sh
   curl -X PATCH 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/<client-id>' \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "allowedResources": ["https://billing.example.com/api",
                            "https://reports.example.com/api"],
       "defaultAudience": "https://billing.example.com/api" }'
   ```

   The list you send replaces the client's whole list, so name every API it may call. Each address is an absolute
   `https` URL with no fragment. `defaultAudience` must be one of `allowedResources`;
   without it, a request naming no API is refused.

2. Optionally, require every token request in the realm to name its API:

   ```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 '{ "requireResource": true }'
   ```

3. Have the client name the API in each token request: `resource=https://billing.example.com/api`.

4. Have each API check that `aud` holds its own address, or ask the realm, as in
   [Check a token at your API](../../../../gateway/guides/check-a-token-at-your-api/).

</Steps>

## Check it worked

- A request naming an API not on the client's list is refused `invalid_target`.
- A request naming none gets a token for the default API.
- On a refresh, the client may ask for fewer of its granted APIs,
  never more.
- Each token's `TOKEN_ISSUED` record shows the APIs asked for and the audience granted. Find it in
  **Audit → Authentication** in the dashboard.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `invalid_target` | The API named isn't on the client's list, or none was named with `requireResource` on and no `defaultAudience` | Add it to `allowedResources`, or set `defaultAudience` |
| Saving the client is refused | `defaultAudience` isn't one of `allowedResources`, or an address isn't an absolute `https` URL | Fix the list |
| An API accepts tokens meant for another | It doesn't check `aud` | Have it check `aud`, as in step 4 |

## Undo it

`PATCH` the client with the earlier `allowedResources` and `defaultAudience`, and the realm with
`{ "requireResource": false }`.

## Related

- [Resource indicators](../../../applications/tokens/resource-indicators/): the standard in full.
- [Refresh tokens](../../../applications/single-sign-on/refresh-tokens/): what a refresh may ask for.
- [Token swap](/gateway/protect-apis/token-swap/): a gateway that swaps a token for each API behind it.
