# Rich authorization requests

> Grant exact permissions, such as one payment of up to 500 EUR to a named account, in place of broad scopes.

A scope such as `payments:send` says what *kind* of thing a token may do, and nothing about how much. With rich
authorization requests, a client asks for exactly what it needs in a structured `authorization_details` field: one
payment, of a set amount, to a named account. The user sees those objects at consent, the token carries them, and the
API checks them.

Use them for payments, health records and any action where *what exactly* matters more than *which kind*, and
wherever a scope would grant more than the one action a client needs. They work at the realm, in an agent's
[bounded approvals](../../agents/bounded-approvals/), and in the [policies](../policies/) the gateway enforces.

## 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 settings and templates.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`).

## How it works

You set three things: which types of object the realm accepts, which a client may ask for, and, through an
entitlement (see [Entitlements](../entitlements/)), how far each holder may go.

**The realm accepts a type**, with the JSON Schema its objects must satisfy, in `authorizationDetailsTypes`:

```json
{ "authorizationDetailsTypes": {
    "payment_initiation": { "type": "object", "required": ["type"],
      "properties": { "type": { "type": "string" }, "instructedAmount": { "type": "object" } } }
} }
```

**The client lists the types it may ask for:** `"authorizationDetailsTypes": ["payment_initiation"]`.

**An entitlement bounds what is granted**, through a template. Each key is a field's dotted path, and each value its
bound: one of a list (`in`), a range (`min`, `max`), a subset of a list (`subsetOf`) or an exact value (`equals`):

```json
{ "code": "payments-small", "scopes": ["pay"],
  "authorizationDetails": [
    { "type": "payment_initiation",
      "instructedAmount.currency": { "in": ["EUR"] },
      "instructedAmount.amount": { "max": 500 } }
  ] }
```

**The client asks**, here an agent's client under the client-credentials grant, for an agent that holds the
entitlement. The objects go as a URL-encoded JSON array:

```sh
curl https://foobar.cvyuh.com/oauth2/token \
  -H "DPoP: <proof>" \
  -d grant_type=client_credentials \
  -d client_id=invoice-agent-client \
  -d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \
  -d client_assertion=eyJ… \
  -d scope=pay \
  --data-urlencode 'authorization_details=[{ "type": "payment_initiation",
    "instructedAmount": { "currency": "EUR", "amount": 100 } }]'
```

The token response
returns the same array as `authorization_details`, and the access token carries it as its `authorization_details`
claim. The same request for 900 EUR is refused `invalid_authorization_details`.

**The gateway can require them.** A policy's `OAuth2AuthorizationDetails` condition names the objects a call needs,
in the same bounds: a token carrying one passes, and any other is refused. The condition constrains only the fields it
names, so an object may carry more. It doesn't compare the objects with the request's body.

## Set it up

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

1. Add the type and its schema to the realm's `authorizationDetailsTypes`:

   ```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 '{ "authorizationDetailsTypes": {
             "payment_initiation": { "type": "object", "required": ["type"],
               "properties": { "type": { "type": "string" },
                               "instructedAmount": { "type": "object" } } } } }'
   ```

2. Add the type to each client's `authorizationDetailsTypes` that may ask for it:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/invoice-agent-client \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "authorizationDetailsTypes": ["payment_initiation"] }'
   ```

3. Write an entitlement whose `authorizationDetails` holds a template for the type, and grant it, as on
   [Entitlements](../entitlements/#set-it-up).

## What the user sees

At consent, the user sees every object whole, field by field, and allows or denies the request as a whole. A request with `authorization_details` always asks, even where consent would be
remembered.

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| Types the realm accepts (`authorizationDetailsTypes`) | none | each with its JSON Schema |
| Types a client may ask for | none | within the realm's |
| Remembered consent for objects | never | |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `invalid_authorization_details` naming a type | The realm doesn't accept the type, or the client may not ask for it | Add the type to the realm, then to the client |
| `invalid_authorization_details` for a valid-looking object | No held template admits it, or it has a field the template doesn't bound | Widen the template, or ask for less |
| An exchanged token has no objects | Token exchange doesn't carry them yet | See What's next |

## Audit record

The `TOKEN_ISSUED` record of each token request holds what was asked; a refusal holds the error and its reason
(**Audit → Authentication**). Changes to accepted types and templates are in **Audit → Config**.

## Good to know

- **Refused, never reduced.** An object outside what's granted fails the whole request, naming the object. The realm
  never quietly issues a smaller one.
- **Only what's accepted.** An object must name a `type` the realm accepts and this client may ask for, and satisfy
  its type's schema. A value that isn't a JSON array is refused too.
- **A template admits only what it bounds.** A field the template doesn't bound is refused, so an object can't widen
  itself by adding one. `min` and `max` read a number, or a decimal string such as `"123.50"`. Any other bound is
  refused when the template is written. A type no template governs is issued as asked.
- **Consent shows every object whole**, and a request with `authorization_details` always asks the user, even where
  consent would be remembered or implied.
- **The objects travel with the grant.** The code carries them, and they're checked against the grants again when
  it's redeemed. A token request may name a subset of them, never add one. A refresh token carries them too, and its
  requests narrow the same way.
- **A token stands only while its objects are still granted.** At
  [introspection](../../applications/revoke-access/introspection/), a token whose objects a held template no longer admits
  is inactive.
- **Which grants take them.** The authorization code grant; client credentials, for a client acting as itself (which
  holds no entitlements, so gets only types no template governs) or as its agent; and
  [backchannel authentication](../../agents/backchannel-authentication/), where the objects are checked
  against the owner's grants when the agent asks, so the owner never sees one they couldn't grant. The device and
  jwt-bearer grants take none. A request for an identity assertion that carries `authorization_details` is refused `invalid_request`
  (see [Issuing identity assertions](../../applications/tokens/identity-assertion-issuing/)).
- **Discovery lists the types** the realm accepts, as `authorization_details_types_supported`.

## Reference

### Errors

| Error | When |
|---|---|
| `invalid_authorization_details` | The value isn't a JSON array; an object names no `type`, a type the realm doesn't accept or the client may not ask for, or fails its schema; no held template admits it; or a token request names an object the code, refresh token or approval doesn't hold |
| `invalid_request` | `authorization_details` sent on a request for an identity assertion |

### Fields

| Where | Field | What it sets |
|---|---|---|
| Realm, `oauth2Settings` | `authorizationDetailsTypes` | The types the realm accepts, each with its JSON Schema. None by default |
| Client | `authorizationDetailsTypes` | The types this client may ask for, within the realm's. None by default |
| Entitlement | `authorizationDetails` | The templates it grants: a type, and a bound on each field |

## Coming next

- **Remembered consent** to the same objects, and **objects carried through token exchange** (today an exchanged
  token holds no `authorization_details`): see [What's next for authorization](../whats-next/).

## Related

- [Bounded approvals](../../agents/bounded-approvals/): an agent's owner approves one exact action.
- [Entitlements](../entitlements/): where templates are granted.
- [Policies](../policies/): requiring objects at the gateway.
