# Access tokens

> Every token the realm issues for an API is a short-lived signed JWT that names its API, its caller and what it allows, so your API can check it on its own.

Whatever grant an app, service or agent uses, what it gets for your API is the same kind of token: a JSON Web Token,
signed with the realm's key, that names the API it is for, who it is about, which client asked for it, and what it
allows. Your API checks the signature against the realm's published keys and reads the claims, with no call to the
realm on each request.

Read this page when you build or configure an API that accepts the realm's tokens, or when you decide how long tokens
should live. Access tokens are short by default, five minutes, because their lifetime bounds how long a revoked
user, client or agent keeps access.

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) sets the lifetime; `RealmViewer` can read
  it. Your API needs no right: it reads the realm's public keys.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`).
- **First:** your API knows its own name, the `resource` clients ask for it by ([Resource indicators](../resource-indicators/)).

## How it works

The token's header names its type, `at+jwt`, and is signed `ES256` with the realm's signing key. The claims, for a
user who signed in to the wiki through the [browser sign-in](../../single-sign-on/authorization-code/):

```json
{
  "iss": "https://foobar.cvyuh.com/oauth2",
  "sub": "3f9b2c71-8d4e-4a6f-b1c2-7e5d9a0f3b48",
  "aud": "https://wiki.example.com/api",
  "exp": <iat + 300>,
  "iat": <unix time>,
  "jti": "0193a7c2-5d1e-7f00-9b3a-6c2d1e0f4a58",
  "client_id": "team-wiki",
  "principal_type": "user",
  "scope": "openid profile wiki:read",
  "sid": "6f1e2d3c-4b5a-4968-8776-a5b4c3d2e1f0"
}
```

Every token carries these:

| Claim | What it says |
|---|---|
| `iss` | The realm that issued it |
| `sub` | Who the token is about: a user's ID, an agent's ID, or the client's own ID |
| `aud` | The API it is for ([Resource indicators](../resource-indicators/)) |
| `exp`, `iat` | When it ends, and when it was issued |
| `jti` | The token's own ID |
| `client_id` | The client that asked for it |
| `principal_type` | What `sub` names: `user`, `agent`, or `oauth2Client` for a service acting as itself. An API never mistakes a client for a user |

Some tokens carry more, depending on how they were issued:

| Claim | When it's there |
|---|---|
| `scope` | The scopes granted, when any were |
| `authorization_details` | The [rich authorization details](../../../authorization/rich-authorization-requests/) granted, when any were |
| `sid` | The sign-in session the token ends with. Absent on a token that outlives the session (`offline_access`) |
| `family` | The [refresh token](../../single-sign-on/refresh-tokens/) family it was issued under |
| `act` | Who acts for `sub`, on a [delegated token](../token-exchange/) |
| `cnf` | The key the token is bound to: `jkt` for a [DPoP](../../client-security/dpop/) key, `x5t#S256` for a [client certificate](../../client-security/certificate-bound-tokens/). A token without it is a bearer token |
| `status` | Where an API checking offline reads whether it was revoked ([Status lists](../../revoke-access/status-lists/)) |
| `mode`, `risk_class`, `autonomy_tier`, `business_owner` | On a token about an [agent](../../../agents/ai-agents/): what a policy decides on beside who it is |

To check a token, your API:

1. Reads the realm's signing keys from `jwks_uri` in the [discovery document](../../registration/discovery/)
   (`https://foobar.cvyuh.com/oauth2/jwks`), and caches them.
2. Checks the header's `typ` is `at+jwt` and the signature verifies with the key the header's `kid` names.
3. Checks `iss` is the realm, `aud` names this API, and `exp` hasn't passed.
4. Decides on `sub`, `principal_type`, `scope` and the rest.

An API that needs to know a token was revoked before it ends can [introspect](../../revoke-access/introspection/) it, or read the
realm's [status list](../../revoke-access/status-lists/). The [gateway](../../../../gateway/) does all of
this for an API you put behind it.

## Set it up

There's nothing to turn on: every grant issues these. The realm's settings set how long they last (see
[Call the API](../../../../develop/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 '{ "accessTokenLifetimeSecs": 300 }'
```

| Where | Setting | What it does |
|---|---|---|
| Realm | `accessTokenLifetimeSecs` | How long access tokens live, from 1 to 86400 seconds (default 300) |
| Realm, `keySettings` | `algorithm.oauth2.accessToken` | The signing algorithm. `ES256`, the only one offered |

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `accessTokenLifetimeSecs` | 300 (five minutes) | 1 to 86,400 |
| Signing algorithm | `ES256` | `ES256` only |
| A token tied to a session | ends by the session's end | fixed |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| Your API refuses tokens after a key rotation | It cached the key set and doesn't know the new `kid` | Fetch the key set again when a token names an unknown `kid` |
| Your API refuses a token as for another audience | The client asked for another API, or none and its default is another | Have the client send `resource` with your API's name |
| Tokens end sooner than `accessTokenLifetimeSecs` | The session ends first, or the agent's risk class caps it | Expected; refresh, or check the agent's guardrails |
| `invalid_token` with `Bearer` | The token is DPoP-bound | Send it as `Authorization: DPoP <token>`, with a proof |

## Audit record

Each token is one `TOKEN_ISSUED` record, under the user or agent it is about, with its `jti` (`tokenId`), its client,
the scopes and API granted, and the key it is bound to. Find one token by its `jti` on the principal's trail
([Read a user's or agent's trail](/audit-reports/audit-trail/read-a-trail/)). A change to the lifetime is in
**Audit → Config**.

## Good to know

- **Only an asymmetric algorithm signs them.** No shared-secret algorithm is offered, because every API that checks
  a token would then hold the key that makes them.
- **A token never outlives its session.** A token tied to a session (`sid`) has its lifetime cut to the session's
  end, and ends with it when the user signs out ([Logout](../../single-sign-on/logout/)).
- **An agent's token can be shorter.** The realm's [agent guardrails](../../../../gateway/guardrails/agent-guardrails/)
  cap its life by the agent's risk class.
- **The token's claims are for the API, not the app.** An app learns who signed in from the
  [ID token and userinfo](../../single-sign-on/openid-connect/), never by reading the access token.
- **The keys rotate.** Fetch the key set again when a token names a `kid` you don't hold
  ([Signing keys](../../../../platform/keys/signing-keys/)).

## Related

- [Set how long tokens live](../../../guides/services/set-token-lifetimes/): the task, start to finish.
- [Resource indicators](../resource-indicators/): how a token comes to name one API.
- [Introspection](../../revoke-access/introspection/): ask the realm whether a token still stands.
- [DPoP](../../client-security/dpop/): make a copied token useless without the client's key.
