# Have an agent ask its owner

> Keep sensitive agent actions behind a user's approval; the agent asks, its business owner approves on their Approvals page, and the agent collects a token for that one action.

Some things an agent should do only after a named user says yes: pay, delete, send. You list the scopes that need
approval for each risk class. An agent of that class asking for one is told `interaction_required`; it then asks its
**business owner**, who approves or refuses on their **Approvals** page. If they approve, the agent gets a short-lived
token acting for the owner.

**Before you start:**

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) sets it up; the business owner answers.
- **Where:** the API, with your session in `$CVYUH_SESSION` ([Call the API](/develop/api/)); `maya` answers on her
  **Approvals** page in the dashboard, with **Allow** or **Deny**.
- **What you need:** `invoice-agent` is registered with `maya` as its business owner (see
  [Register an agent](../register-an-agent/)).

## Put an action behind approval

<Steps>

1. Offer the grant. Read the realm's `grantTypes` from
   `https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings`, then send them back with the
   backchannel grant added:

   ```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 '{ "grantTypes": ["…existing grant types", "urn:openid:params:grant-type:ciba"] }'
   ```

   Do the same on the agent's client, at
   `https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/invoice-agent-client`.

   <Aside type="caution" title="This list replaces the realm's grant types">
     A grant type you leave out is turned off for the whole realm, so send every one you read.
   </Aside>

2. Make sure the agent has a business owner, as in [Register an agent](../register-an-agent/), step 2.

3. Choose how the agent learns the answer. Poll:

   ```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 '{ "backchannelTokenDeliveryMode": "poll" }'
   ```

   Or ping, with the address the realm calls:

   ```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 '{ "backchannelTokenDeliveryMode": "ping",
           "backchannelClientNotificationEndpoint": "https://agent.example.com/ciba" }'
   ```

4. Say which scopes need approval, per risk class:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/agentSettings/agentSettings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "agentGuardrails": {
             "high": { "maxTokenSecs": 120, "requireApproval": ["payments:send"] }
         } }'
   ```

</Steps>

## Check it worked

<Steps>

1. The agent posts to `https://foobar.cvyuh.com/oauth2/bc-authorize`, proving itself, with `scope` and a
   `binding_message`, and optionally `authorization_details` for the owner to approve with them. It may name its
   owner with one hint, `login_hint` (their id or sign-in name) or `id_token_hint`, but only as a check: a hint
   naming anyone else is refused `unknown_user_id`. More than one hint, or a `login_hint_token`, is
   `invalid_request`. It receives an `auth_req_id`, an expiry and a polling interval:

   ```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=payments:send \
     -d login_hint=maya \
     --data-urlencode "binding_message=Pay invoice 4471 to Acme Ltd, EUR 1280"
   ```

   ```json
   { "auth_req_id": "1c266114-a1be-4252-8ad1-04986c5b9ac1", "expires_in": 300, "interval": 5 }
   ```

2. The owner opens **Approvals** in the dashboard and sees the agent, its message (for example *"Pay invoice 4471 to
   Acme Ltd, €1,280"*), the scopes it asks for, and when the request expires. They choose **Allow** or **Deny**.

3. The agent collects. In poll mode it asks the token endpoint until it gets tokens, `access_denied` or
   `expired_token`, waiting the interval between polls:

   ```sh
   curl https://foobar.cvyuh.com/oauth2/token \
     -H "DPoP: <proof>" \
     -d grant_type=urn:openid:params:grant-type:ciba \
     -d auth_req_id=1c266114-a1be-4252-8ad1-04986c5b9ac1 \
     -d client_id=invoice-agent-client \
     -d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \
     -d client_assertion=eyJ…
   ```

   A poll sooner than that is told `slow_down`, and the
   interval grows by five seconds each time; a poll that waits the whole interval is answered. In ping mode it also
   sent a `client_notification_token`; the realm calls its registered address with it when the owner answers, and
   it collects the token once.

4. Each token request carries a DPoP proof, as for any agent's client held to FAPI 2.0. The token names the owner as
   `sub` and the agent in `act`. There's no refresh token: the next action needs a new approval.

</Steps>

If the agent is quarantined or killed while it waits, it collects nothing. When it collects, the realm writes a
`TOKEN_ISSUED` record with the backchannel grant type, issued or refused, on the owner's or the agent's trail.
For what goes wrong most often, see
[Backchannel authentication](../../../agents/backchannel-authentication/#troubleshooting).

<Aside type="note" title="Owners aren't notified yet">
  Nothing but the **Approvals** page tells an owner a request is waiting, so tell owners to watch it.
</Aside>

## Undo it

Send that risk class's entry again, with the scope left out of `requireApproval`.

## Related

- [Backchannel authentication](../../../agents/backchannel-authentication/): every rule a request is held to.
- [Bounded approvals](../../../agents/bounded-approvals/): the owner approves one exact action.
- [Agent guardrails](../../../../gateway/guardrails/agent-guardrails/): limits by risk class.
- [Let users remove agents](../people-remove-agents/).
