# Governed scopes

> Issue a scope only to the users, groups and agents an entitlement grants it to, with broad grants implying narrower ones.

Once an entitlement names a scope, that scope is **governed**: the realm issues it only to someone who holds an
entitlement naming it, directly or through a group. Every other scope is issued as the client and the realm allow, as
before. So a realm that writes no entitlements sees no change, and you opt in one scope at a time, by writing the
entitlement that governs it.

Governed scopes are how a scope such as `invoices:write` stops being something any client can ask for and becomes
something only the finance team holds.

## 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.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`).
- **First:** the entitlement that names the scope; see [Entitlements](../entitlements/).

## How it works

`maya`, a member of the `finance` group, which holds the `invoices` entitlement, asks for `invoices:read` for
`https://app.example.com/api`, and is issued it. `ben`, outside the group, asks for `invoices:read profile` and is
issued `profile` alone, and the token response's `scope` says so.

- **Broad implies narrow.** An entitlement lists every scope it grants, at least one. One that lists `invoices`,
  `invoices:read` and `invoices:write` lets its holder be issued `invoices:read` alone.
- **Scopes for named APIs.** An entitlement may name the `resources` its scopes are for. Its scopes are then issued
  only for a token whose audience is among them. One that names none is for every API.
- **Time-boxed.** A grant may have a window, and so may the entitlement itself. Once either closes, its scopes stop
  being issued. Nothing has to be revoked or swept.
- **Narrowed, not refused.** A governed scope someone lacks is left out of the token, and the response says which
  scopes were issued. The request is refused `invalid_scope` only when nothing it asked for is left.
- **Checked again.** Every refresh, exchange and introspection checks a token's governed scopes again. When a grant
  lapses, a refresh no longer carries its scope, and a token that still holds it reads as inactive.

## Set it up

Write an entitlement (see [Entitlements](../entitlements/)) that names the scope, and grant it. From the moment the entitlement exists,
the scope is governed.

```json
{ "code": "invoices", "scopes": ["invoices", "invoices:read", "invoices:write"],
  "resources": ["https://app.example.com/api"] }
```

## What the user sees

A user who lacks a governed scope still signs in: the token leaves the scope out, and the response's `scope` says
which were issued. Only when nothing they asked for is left is the request refused, with `invalid_scope`.

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| Governed scopes | none, until an entitlement names one | any scope but sign-in and platform scopes |
| Entitlements in a realm | | fewer than 1,000 |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A scope that used to be issued to everyone is now left out | An entitlement now names it, so it's governed | Grant the entitlement to everyone who needs it |
| A token reads inactive at introspection | A grant behind one of its scopes lapsed | Renew the grant; the next sign-in gets a fresh token |

## Audit record

Each token request's `TOKEN_ISSUED` record holds what was asked (`asked.scope`) and what was granted
(`granted.scope`), so a governed scope left out is visible (**Audit → Authentication**).

## Good to know

- **Sign-in and platform scopes can't be governed.** An entitlement can never name a sign-in scope (`openid`,
  `profile`, `email`, `address`, `phone`, `offline_access`) or a scope the realm's own services need
  (`platform:admin`, `keys:admin`, `policies:read`, `revocations:read`, `access:evaluate`, `access:search`,
  `ssf.manage`). Writing one is refused. Governing a sign-in scope would take sign-in away from every client with no
  grant.
- **A client acting as itself** holds no grants, so a governed scope is never issued to it.
- **A lapsed entitlement still governs.** Letting it lapse takes its scopes away, rather than freeing them for
  everyone.
- **Structured permissions are governed the same way.** A type of `authorization_details` that some entitlement names
  is governed like a scope, except that an object no held template admits is refused, never narrowed. See
  [Rich authorization requests](../rich-authorization-requests/).

## Related

- [Entitlements](../entitlements/): writing and granting them.
- [Issuance policy](../issuance-policy/): one policy set asked before every token.
- [Resource indicators](../../applications/tokens/resource-indicators/): how a token names its API.
