# Entitlements

> Name what a user, a group or an agent holds, for as long as you choose, and let it decide the scopes and permissions their tokens may carry.

An entitlement is a named bundle of access, such as *invoices* or *small payments*, that you grant to a user, a group or
an agent. It decides which scopes their tokens may carry, and which structured permissions, such as a payment of at
most 500 EUR. Grant it to a group and every member holds it; give the grant an end date and it lapses by itself.

Use entitlements for *what someone holds*. For *which calls are allowed* on an API, write [policies](../policies/);
a policy can then require a scope only an entitlement grants.

## Before you begin

- **Who can do it:** a realm administrator, in a group that holds `RealmOwner`
  ([Delegated administration](/platform/realms/delegated-administration/)). `RealmViewer` can read entitlements and grants.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`).

## How it works

An entitlement is an `entitlement` object: a unique `code`; the `scopes` it grants; optionally the
`authorizationDetails` it grants, as templates (a `type`, and a bound on each field its holder may ask for);
optionally the `resources` its scopes are for; and optionally its own window (`validFrom`, `validTo`).

Someone holds it through a **`grants`** link from a user, a group or an agent to the entitlement, with its own
`validFrom`, `validTo` and `grantedBy`. A group's grants reach its members.

The entitlements someone holds right now are worked out whenever they're read, from the links whose window is open and
whose entitlement's own window is open. Nothing is stored or swept when one lapses.

What they decide:

- **The scopes a token carries.** Once an entitlement names a scope, that scope is issued only to its holders: see
  [Governed scopes](../entitlement-scopes/).
- **The structured permissions a token carries.** An `authorization_details` object is issued only when a template its
  holder holds admits it: see [Rich authorization requests](../rich-authorization-requests/).
- **What the issuance policy sees.** The codes of the entitlements held reach the realm's
  [issuance policy](../issuance-policy/), so a policy can name them.

## Set it up

Each call sends your session as `$CVYUH_SESSION` ([Call the API](/develop/api/)).

1. Write the entitlement. Its id is its code:

   ```sh
   curl -X POST "https://foobar.cvyuh.com/api/v0/am/objects/v1/entitlement?id=invoices" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "code": "invoices", "scopes": ["invoices", "invoices:read", "invoices:write"],
           "resources": ["https://app.example.com/api"] }'
   ```

2. Grant it to a group (or a user, or an agent) through their `grants` link, with an optional window:

   ```sh
   curl -X POST https://foobar.cvyuh.com/api/v0/am/objects/v1/group/finance/relationships/grants \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "targetId": "invoices", "properties": { "validTo": "<RFC 3339 time>" } }'
   ```

3. Check what a holder has:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/group/finance/relationships/grants \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

To take a grant back, delete the link: `DELETE` the same address followed by `/invoices`. To end it at a set time, give it a `validTo`.

## What the user sees

Nothing at sign-in. A user who lacks a governed scope gets a token without it, and the app's call that needs it is
refused by the API. A user whose grant lapses loses the scope at the app's next refresh.

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| Entitlements in a realm | | fewer than 1,000; at 1,000, token requests are refused `503` |
| `scopes` on an entitlement | | at least one |
| `validFrom`, `validTo` on an entitlement or a grant | open | any time |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A token comes back without a scope it asked for | The scope is governed, and the user holds no entitlement that grants it, or the grant's window is closed | Grant the entitlement, or check `validFrom` and `validTo` |
| A client acting as itself never gets a scope | Clients acting as themselves hold no grants | Use a scope no entitlement names for that client |
| Writing an entitlement is refused | It names a sign-in or platform scope | Leave those scopes out |
| Every token request answers `503` | The realm holds 1,000 entitlements or more | Remove unused ones |

## Audit record

Writing an entitlement, and adding or removing a grant, is recorded in **Audit → Config**
([Change history](/audit-reports/audit-trail/change-history/)), with the grant's `grantedBy`. Each token records what
was asked and what was granted in its `TOKEN_ISSUED` record (**Audit → Authentication**), so a scope left out shows
there.

## Good to know

- **A lapsed entitlement still governs its scopes.** Letting it lapse takes its scopes from everyone, rather than
  freeing them for anyone.
- **Lapsing takes effect on the next check.** Every refresh, exchange and introspection checks a token's governed
  scopes again, so a token whose grant has lapsed reads as inactive at introspection.
- **A client acting as itself holds no grants**, so a governed scope is never issued to it.
- **No sign-in or platform scopes.** An entitlement can't name `openid`, `profile`, `email`, `address`, `phone`,
  `offline_access`, or the scopes the realm's own services need: writing one is refused. See
  [Governed scopes](../entitlement-scopes/#good-to-know).
- **Fewer than 1000 per realm.** At 1000 or more, the realm won't work out a token's scopes from a partial list: a
  token request is refused `temporarily_unavailable` (`503`) instead.

## Reference

### Fields

| Where | Field | What it sets |
|---|---|---|
| `entitlement` | `code`, `scopes` | Its unique code, and the scopes it grants. Both required |
| `entitlement` | `authorizationDetails` | The structured permissions it grants, as templates |
| `entitlement` | `resources` | The APIs its scopes are for. None means every API |
| `entitlement` | `validFrom`, `validTo` | When the entitlement itself is open |
| `grants` link on a user, group or agent | `validFrom`, `validTo`, `grantedBy` | When this holder holds it, and who granted it |

## Related

- [Governed scopes](../entitlement-scopes/): how an entitlement decides a token's scopes.
- [Rich authorization requests](../rich-authorization-requests/): bounds on payments and other exact actions.
- [Issuance policy](../issuance-policy/): one more check before every token.
- [Policies](../policies/): who may call what, on the API itself.
- [Agents acting for users](../../agents/acting-for-users/): the most an agent's token may hold when it acts for a user.
