# Introspection

> An API asks the realm whether a token is still good right now, and what it says.

Introspection lets an API ask the realm about a token and get the answer as it stands now, not as it was when the
token was issued. A token revoked a second ago, or one whose user was disabled, reads inactive at once. If the token
is active, the answer says who it is about, which client holds it, its scopes, its audience, when it expires and the
key it is bound to.

Use it for an API that wants the realm's answer on every request rather than checking signatures itself, and for a
refresh token, which only the realm can read. Behind the [gateway](../../../../gateway/)
it isn't needed: the gateway keeps its own revocation list.

## Before you begin

- **Who can do it:** your API calls introspection with its own client credentials. A realm administrator (a group
  holding `RealmOwner`) registers the API's client and lists what it serves; `RealmViewer` can read it.
- **Where:** today the client is set up through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`).
- **First:** the API has a confidential client of its own, with `servesResources` naming its audience.

## How it works

The API authenticates as its own client, as at the token endpoint, and posts the token to
`https://foobar.cvyuh.com/oauth2/introspect`:

```sh
curl https://foobar.cvyuh.com/oauth2/introspect \
  -d token=eyJhbGciOiJFUzI1NiIsInR5cCI6ImF0K2p3dCJ9… \
  -d client_id=orders-api \
  -d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \
  -d client_assertion=eyJ…
```

For an active access token, the answer is the token's claims, with `active` and `token_type` added:

```json
{
  "active": true,
  "iss": "https://foobar.cvyuh.com/oauth2",
  "sub": "5b0e3c4a-8f1d-4e2b-9a6c-7d3f1e0b2c48",
  "client_id": "orders-web",
  "aud": "https://orders.example.com/api",
  "scope": "orders.read",
  "exp": <unix time>,
  "cnf": { "jkt": "0ZcOCORZNYy-DWpqq30jZyJGHTN0d2HglBV3uiguA4I" },
  "token_type": "DPoP"
}
```

`token_type` is `DPoP` for a token bound to a key (it carries `cnf`), else `Bearer`. For a refresh token, the answer
is `active`, `client_id`, `sub`, `scope`, `aud` and `exp`, with `cnf` when the token is bound to a key and `acr` when
the sign-in had one. Anything else, or a token this caller may not see, is answered `{"active": false}`.

A token is inactive when:

- it has expired, or been revoked;
- any token it was exchanged from (through [token exchange](../../tokens/token-exchange/)) has been revoked;
- its refresh family, session or client grant was revoked;
- its user, client or any agent in it may no longer act;
- a governed scope it holds ([governed scopes](../../../authorization/entitlement-scopes/)) is
  no longer granted to whom it is about, or the realm's issuance policy set no longer allows it.

### A signed answer

Send `Accept: application/token-introspection+jwt`, and the answer comes as a JWT signed by the realm, of type
`token-introspection+jwt`. It is signed with the client's `introspectionSignedResponseAlg`, else `ES256` for a `fapi2`
client, else the first of the realm's `keySettings.algorithm.oauth2.introspection` (`RS256` by default). Its claims
are `iss` (the realm), `aud` (the caller's `client_id`), `iat`, and `token_introspection`, which holds the answer
above.

## Set it up

On the API's own client (`https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client`; see [Call the API](../../../../develop/api/)),
list the audiences it serves:

| Where | Setting | What it does |
|---|---|---|
| The API's client | `servesResources` | The audiences this API is, so it may introspect their tokens. Empty by default |
| The API's client | `introspectionSignedResponseAlg` | The algorithm its signed answers use, one of the realm's. A `fapi2` client may name only `ES256` or `PS256` |
| Realm | `clockSkewSecs` | The clock tolerance a revocation is kept beyond a token's expiry (default 60) |
| Realm, `keySettings` | `algorithm.oauth2.introspection` | The algorithms a signed answer may use (`RS256` and `ES256` by default); listed in the [discovery document](../../registration/discovery/) |

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `servesResources` | empty: the API learns only of its own tokens | absolute URIs |
| `clockSkewSecs`: how long past `exp` a revocation is kept | 60 | 0 to 300 |
| `introspectionSignedResponseAlg` | none: plain JSON | one of the realm's algorithms (`ES256`, `PS256` under `fapi2`) |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| Every token reads `{"active": false}` | The API's client doesn't list the tokens' audience in `servesResources` | Add the exact audience your API's tokens carry |
| `invalid_client` | The API calls without authenticating, or its client is public | Authenticate as a confidential client |
| A refresh token reads inactive to your API | Only the client it was issued to may see a refresh token | Introspect refresh tokens from the app that holds them |

## Audit record

Introspection writes no record per call. Revocations it reflects are `TOKEN_REVOKED` records
([Revoke tokens](../revocation/)).

## Good to know

- **The caller authenticates first.** A public client can't introspect: it's refused `invalid_client`.
- **Only two callers learn anything:** the client the token was issued to, and an API whose client lists the token's
  audience in `servesResources`. Anyone else gets `{"active": false}`, the same as for a token that doesn't exist. A
  refresh token is shown only to the client it was issued to.
- **`token_type_hint` only orders the search.** `refresh_token` looks among refresh tokens first, then access tokens.
  Any other hint, or none, looks the other way round. A wrong hint never makes a token inactive.
- **A revoked token is kept revoked** until its `exp` plus the realm's clock tolerance (`clockSkewSecs`), so it
  never reads active again while it could still verify.
- **Access and refresh tokens only.** A [transaction token](../../tokens/transaction-tokens/) answers `{"active": false}`.

## Reference

### Errors

| Error | When |
|---|---|
| `invalid_client` | The caller doesn't authenticate, or is a public client |
| `invalid_request` | No `token` is sent, or a parameter is sent twice |

A token that is unknown, expired, revoked or another's is never an error: it's `{"active": false}`.

## Related

- [Check a token at your API](../../../../gateway/guides/check-a-token-at-your-api/): the task,
  start to finish.
- [Access tokens](../../tokens/access-tokens/): check a token yourself, against the realm's keys.
- [Status lists](../status-lists/): learn of revoked tokens without asking each time.
- [Revoke tokens](../revocation/): how a token is revoked.
