# Resource indicators

> Each token names the one API it is meant for, so an API never accepts a token meant for another.

Every token the realm issues names the API it is for, and only that API accepts it. A token stolen from one API can't
be replayed against another. The client names the API by its URL in the `resource` parameter when it asks for a
token; when it names none, the realm uses the client's default.

Use it whenever a client calls more than one API: give it one token per API. If a client calls a single API, set that
API as its default audience and the client need not name it.

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) lists each client's APIs; `RealmViewer` can
  read them. The client names an API on each request.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`).
- **First:** give each API a name: an `https` URL, such as `https://app.example.com/api`.

## How it works

The client names the API in the token request:

```sh
curl https://foobar.cvyuh.com/oauth2/token \
  -d grant_type=client_credentials \
  -d client_id=orders-sync \
  -d scope=orders.read \
  -d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \
  -d client_assertion=eyJ… \
  --data-urlencode resource=https://orders.example.com/api
```

The token's audience, its `aud` claim, is `https://orders.example.com/api`. `resource` may be repeated to name
several APIs in one request, and the token's `aud` then names each of them. Each `resource` must be an absolute URI
with no fragment (no `#…` part).

The same parameter works in every grant: the [browser sign-in](../../single-sign-on/authorization-code/), a
[refresh](../../single-sign-on/refresh-tokens/), and [token exchange](../token-exchange/), where the API may also be named with
`audience`.

## Set it up

List each client's APIs on the client
(`https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client`; see [Call the API](../../../../develop/api/)), and set the realm's rules with
`PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings`:

| Where | Setting | What it does |
|---|---|---|
| Client | `allowedResources` | The APIs this client may get tokens for: each an `https` URI with no fragment. Empty by default |
| Client | `defaultAudience` | The API used when the client names none. Absent by default, so a request naming none is refused |
| Client | `servesResources` | For an API's own client: the audiences it may [introspect](../../revoke-access/introspection/) |
| Realm | `requireResource` | Refuse any request that names no API (default off) |
| Realm | `cimdResources` | The APIs a [metadata-document client](../../registration/client-metadata-documents/) may name. Empty by default |

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `allowedResources` | empty | absolute `https` URIs with no fragment |
| `defaultAudience` | none: a request naming no API is refused | one of `allowedResources` |
| `requireResource` | off | on or off |
| `cimdResources` | empty | absolute URIs |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `invalid_target` on a request that names no API | The client has no `defaultAudience`, or the realm sets `requireResource` | Set `defaultAudience`, or have the client send `resource` |
| `invalid_target` naming a known API | It isn't on this client's `allowedResources`, or differs by a trailing slash or case | Add the exact URI the client sends |
| Your API refuses the token | The token's `aud` is another API | Check which `resource` the client sent |

## Audit record

Each token request's `TOKEN_ISSUED` record shows what was asked (`asked.resource`) beside what was granted
(`granted.audience`), so a refused `invalid_target` shows the API the client named. See them in
**Audit → Authentication**.

## Good to know

- **A client may name only the APIs on its allowed list.** Anything else is refused as `invalid_target`.
- **Every token has an audience.** A client with no default that names none is refused `invalid_target`.
- **A refresh may name fewer APIs than the sign-in did, never more.**
- **With `openid`, the token is also good at the realm's own userinfo endpoint.**
- **In a token exchange,** the API is named with `resource`, or with `audience` naming the same registered API. Both
  together must name the same API, or the request is refused `invalid_request`.
- **A metadata-document client** with no registration of its own may name only the realm's `cimdResources`. When
  that list holds exactly one API, it's the client's default audience.

## Reference

### Errors

| Error | When |
|---|---|
| `invalid_target` | A `resource` isn't an absolute URI, has a fragment, or isn't on the client's `allowedResources` |
| `invalid_target` | The request names no resource, and the realm sets `requireResource` |
| `invalid_target` | The request names no resource, and the client has no `defaultAudience` |
| `invalid_target` | A refresh names an API the sign-in didn't |

## Related

- [Give a client tokens for one API](../../../guides/services/tokens-for-one-api/): the task, start
  to finish.
- [Access tokens](../access-tokens/): what the token says, `aud` among it.
- [Introspection](../../revoke-access/introspection/): an API asks about the tokens meant for it.
