# Bounded approvals

> An owner approves one exact, structured action an agent will take, such as one payment of 100 EUR, and the agent's token allows that and nothing wider.

A free-text approval ("Pay invoice 42 to Acme") tells the owner what the agent says it will do. A bounded approval
makes the token say it too. The agent describes the action exactly, the owner sees that action whole on their
**Approvals** page, and the token the agent receives allows that action and nothing broader.

Use it for agents that pay, delete, send or sign on someone's behalf, where an approval must mean one thing.

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) writes the entitlement, grants it and sets
  the accepted action types; `RealmViewer` can read them. The owner approves as themselves.
- **Where:** today this is set through the API ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`).
  Owners approve on their **Approvals** page in the dashboard.
- **First:** [backchannel authentication](../backchannel-authentication/) works for the agent, and the action's type
  is set up as a [rich authorization request](../../authorization/rich-authorization-requests/) type.

## How it works

An agent asking its owner through [backchannel authentication](../backchannel-authentication/) adds the action in
`authorization_details`, in the same structured form as
[rich authorization requests](../../authorization/rich-authorization-requests/). The realm checks
it against what the owner could grant before the request ever reaches them.

The same holds for a token request an agent asks the realm to hold for its owner, with `completion_mode=deferred`
([Or the request waits for the owner](../../../gateway/guardrails/agent-guardrails/#or-the-request-waits-for-the-owner)):
its action is checked against the owner's entitlements in the same way, and waits on the same Approvals page.

## Set it up

<Steps>

1. Give the owner an entitlement that bounds the action, and grant it to `maya`. Here, payments in euros of at most
   500. Each call sends your session as `$CVYUH_SESSION` ([Call the API](/develop/api/)):

   ```sh
   curl -X POST "https://foobar.cvyuh.com/api/v0/am/objects/v1/entitlement?id=payments-small" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "code": "payments-small", "scopes": ["pay"],
           "authorizationDetails": [
             { "type": "payment_initiation",
               "instructedAmount.currency": { "in": ["EUR"] },
               "instructedAmount.amount": { "max": 500 } } ] }'

   curl -X POST https://foobar.cvyuh.com/api/v0/am/objects/v1/user/maya/relationships/grants \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "targetId": "payments-small" }'
   ```

2. Let the realm and the agent's client accept the action's type, by listing it in their
   `authorizationDetailsTypes`, each with its schema at the realm (see
   [Rich authorization requests](../../authorization/rich-authorization-requests/#how-it-works)).

3. The agent asks, naming the action and a short `binding_message`:

   ```sh
   curl https://foobar.cvyuh.com/oauth2/bc-authorize \
     -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 "binding_message=Pay invoice 42 to Acme" \
     --data-urlencode 'authorization_details=[{ "type": "payment_initiation",
       "instructedAmount": { "currency": "EUR", "amount": 100 } }]'
   ```

   It's within the owner's template, so the request waits for them. The same request for 900 EUR is refused at once.

4. The owner approves on their Approvals page, which shows the payment whole beside the binding message.

5. The agent collects its token as for any backchannel request. The token response returns the approved array as
   `authorization_details`, and the access token carries it in its `authorization_details` claim.

</Steps>

## What the user sees

On their **Approvals** page the owner sees the binding message and, beside it, the action whole: here, a payment of
100 EUR. They choose **Allow** or **Deny** for the whole action. An action outside what they could grant never
reaches them.

## Limits and defaults

The limits of [Backchannel authentication](../backchannel-authentication/#limits-and-defaults) apply: a request waits
300 seconds by default, at most twenty wait for one owner, and the `binding_message` is still required. What an owner
may approve is bounded by the `authorizationDetails` templates of the entitlements they hold.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `invalid_authorization_details` at once | The action is outside the owner's entitlements, isn't a type the realm or the client accepts, or fails its schema | Grant the owner an entitlement that covers it; list the type on the realm and the client |
| Approved, but collecting is refused | The owner no longer holds the entitlement that allowed it, or the agent asked for more than was approved | Grant it again (the approval stays to collect); ask for no more than was approved |

## Audit record

Collecting writes a `TOKEN_ISSUED` record with the scopes and API granted, or a refusal keyed by the agent's client.
The approved action itself isn't on the record yet: read it from the token's `authorization_details` claim. Changes to entitlements and grants are in the change history: **Audit → Config**.

## Good to know

- **The owner can only approve what they could grant.** An action the owner's entitlements don't allow is refused
  `invalid_authorization_details` at once, and never reaches their Approvals page.
- **All or nothing.** The owner approves the whole action or refuses it. An owner who wants less refuses, and the
  agent asks again.
- **The token carries exactly what was approved.** When collecting, the agent may ask for part of what was approved,
  never more.
- **It's checked again when collected.** If the owner no longer holds the entitlement that allowed the action, the
  token is refused, not issued without it, and the approval stays to collect.
- **The binding message is still required** beside the action: it's the short line the owner reads first.
- **The token stands only while the action is still granted,** as every rich authorization request's does.

## Coming next

A user approving, in the same way, an agent that acts for them rather than one they own.

## Reference

### Settings

| Where | Setting | What it does |
|---|---|---|
| Realm | `authorizationDetailsTypes` | The action types the realm accepts, each with its schema |
| Client | `authorizationDetailsTypes` | The types this agent's client may ask for |
| Entitlement | `authorizationDetails` | The actions an owner holding it may approve, as bounded templates |

### Errors

The only error of its own is `invalid_authorization_details`: the action isn't one the owner could grant, isn't a
type the realm accepts or the client may ask for, fails its type's schema, or, when collecting, asks for more than
was approved. The rest are those of [Backchannel authentication](../backchannel-authentication/#errors).

## Related

- [Backchannel authentication](../backchannel-authentication/): how an agent asks, and how it collects.
- [Rich authorization requests](../../authorization/rich-authorization-requests/): action types,
  schemas and entitlements.
- [Have an agent ask its owner](../../guides/agents/agent-asks-its-owner/): the task, start to finish.
