# Issuance policy

> Ask one policy set before every token the realm issues, deciding on who it's for, their entitlements, their risk and the scopes about to go out.

An issuance policy is a last check before any token leaves the realm. Name a policy set in the realm's settings, and
the realm asks it at every sign-in and every token request: *may this user, agent or client be issued this token, with
these scopes, for this app?* Anything but an allow refuses.

Use it for rules that cut across every app: no `high`-risk agent gets a token for your finance app, or only holders
of the `contractor` entitlement may use the contractor portal.

## 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 the policies.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`).
- **First:** decide what everyone else may have. Once a set is named, a token no policy allows is refused, so the
  set needs an allow before it needs any deny.

## How it works

With `authorizationSettings.issuancePolicySet` set, the realm asks that set about:

- **the subject**: the user, agent or client the token is for, with their groups, the codes of the
  [entitlements](../entitlements/) they hold, and their risk class;
- **the resource** `client://<client_id>`;
- **the action** `authorize` at the authorization endpoint (before consent), or `issue` at introspection and at every
  token-endpoint grant that issues an access token;
- **the context**: the scopes and the authorization details about to be issued.

The question carries these members, which a policy's subject and conditions can name:

| Member | What it holds |
|---|---|
| `subject.type` | `user`, `agent` or `client` |
| `subject.id` | The user's or agent's id, or the client's `client_id` |
| `subject.properties.claims.entitlements` | The codes of the entitlements they hold |
| `subject.properties.claims.risk_class` | Their risk class, when they have one |
| `subject.properties.groups` | The names of a user's groups |
| `resource.id` | `client://<client_id>` |
| `action.name` | `authorize` or `issue` |
| `context.policySet` | The set's name |
| `context.scopes` | The scopes about to be issued |
| `context.authorizationDetails` | The objects about to be issued |

**Risk.** An agent's `riskClass`, and a user's, on the same scale (`minimal`, `limited`, `high`, `unacceptable`),
reach the policy as the subject's `risk_class`. A risk cohort, such as every agent whose `riskClass` is `high`, can
also be a group that membership conditions keep in step, which a policy then names like any group.

## Set it up

Every realm is born with the resource type `client` (the pattern `client://*`, the actions `authorize` and `issue`)
and an empty policy set for it, `issuance`. The realm asks nothing until you name the set. Each call sends your
session as `$CVYUH_SESSION` ([Call the API](/develop/api/)).

1. Write the policies in the `issuance` set, linked to the `client` type, as on [Policies](../policies/#set-it-up).
   Write the allow first: once the set is named, an empty set refuses every token. This one allows every token, as
   the realm does with no set named:

   ```sh
   curl -X POST "https://foobar.cvyuh.com/api/v0/am/objects/v1/policy?id=issue-by-default" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "active": true,
           "resources": ["client://*"],
           "actionValues": { "authorize": true, "issue": true },
           "subject": { "type": "AuthenticatedUsers" } }'
   ```

   Then write the rule. This one keeps `high`-risk agents away from the app at `https://app.example.com`,
   registered as the client `example-app`; a deny wins over the allow:

   ```sh
   curl -X POST "https://foobar.cvyuh.com/api/v0/am/objects/v1/policy?id=no-high-risk-agents" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "active": true,
           "resources": ["client://example-app"],
           "actionValues": { "authorize": false, "issue": false },
           "subject": { "type": "JwtClaim", "claimName": "risk_class", "claimValue": "high" } }'
   ```

2. Link both policies to the `issuance` set and the `client` type, then check the set before it decides anything:
   ask the decision API as on [Test a policy safely](../../guides/access-policies/test-a-policy-safely/), with
   `"resource": { "type": "client", "id": "client://example-app" }`, `"action": { "name": "issue" }` and
   `"context": { "policySet": "issuance" }`.

3. Name the set in the realm's settings:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/authorizationSettings/authorizationSettings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "issuancePolicySet": "issuance" }'
   ```

## What the user sees

A user refused at sign-in is sent back to the app with `access_denied`, and the app shows its own error. An app or
agent refused at the token endpoint gets `invalid_grant`, or `unauthorized_client` for a client acting as itself,
each with the description "the realm's issuance policy refuses this token". Tokens already issued keep working until
they expire or are revoked.

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `issuancePolicySet` | unset: nothing is asked | any policy set |
| The `issuance` set in a new realm | empty | |
| Actions | `authorize`, `issue` | |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| Every sign-in and token is refused after naming the set | The set holds no allow, or the allow isn't linked to the set and the `client` type | Unset `issuancePolicySet` at once, then add and link the allow |
| A rule about risk never matches | The agent or user has no `riskClass` | Set it on the agent or user |
| A rule about a group never matches an agent | An agent holds no groups | Use `risk_class` or an entitlement code instead |

## Audit record

A refused token request is recorded as `TOKEN_ISSUED` with result `FAILED`, under the client that asked, with the
reason "the realm's issuance policy refuses this token" (**Audit → Authentication**). A refused sign-in at the
authorization endpoint is recorded as the realm's sign-in records it. Changes to the set and its policies are in
**Audit → Config**.

## Good to know

- **Anything but an allow refuses**, and so does a set that holds no policy: `access_denied` at the authorization
  endpoint, otherwise `invalid_grant`, or `unauthorized_client` for a client acting as itself.
- **Unset, nothing is asked.** `issuancePolicySet` is unset by default.
- **Some grants have their own trust settings.** Transaction tokens, identity assertions and cross-domain chaining
  grants are governed by their own trust settings (trusted partners, `issuesTxnTokens`), not by the issuance
  policy.

## Related

- [Common access policies](../../guides/access-policies/common-access-policies/): an issuance rule among workforce
  recipes.
- [Entitlements](../entitlements/): what the subject holds.
- [Policies](../policies/): how to write the rules.
- [Agent guardrails](../../../gateway/guardrails/agent-guardrails/): limits by risk class that need no policy.
