# Backchannel authentication

> An agent asks its business owner before it acts, the owner answers on their Approvals page, and the agent collects a token for what they approved.

Some actions shouldn't happen without a human's yes: a payment, a deletion, a message sent in someone's name.
Backchannel authentication lets an agent ask for that yes. The request goes to the agent's business owner, who sees
it on their **Approvals** page and allows or refuses it. If they allow it, the agent gets a short-lived token acting
for them, and nothing more.

Use it whenever an agent shouldn't act without explicit approval, and when the realm answers `interaction_required`
for a scope that needs approval under [Agent guardrails](../../../gateway/guardrails/agent-guardrails/). To have the realm hold an ordinary token request until the owner answers instead, see
[Or the request waits for the owner](../../../gateway/guardrails/agent-guardrails/#or-the-request-waits-for-the-owner).

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) turns the grant on and sets the client;
  `RealmViewer` can read them. The owner answers as themselves, signed in.
- **Where:** today this is set through the API ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`).
  Owners answer on their **Approvals** page in the dashboard.
- **First:** the agent is registered with a `businessOwner` ([Register an agent](../../guides/agents/register-an-agent/)).

## How it works

**The agent asks,** authenticating as its client, at the realm's backchannel authentication endpoint, with the scope
it wants and a short `binding_message` for the owner:

```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 \
  --data-urlencode "binding_message=Pay invoice 42 to Acme"
```

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

**The owner answers** on their Approvals page, where the request shows the agent, its message, the scopes it asks
for, and when it expires. The page reads them, as the signed-in owner, from
`https://foobar.cvyuh.com/api/v0/am/me/v1/oauth2/approvals`, and answers one by posting `{ "decision": "allow" }` or
`{ "decision": "deny" }` to `https://foobar.cvyuh.com/api/v0/am/me/v1/oauth2/approvals/<id>`
([Call the API](/develop/api/)).

**The agent collects its token.** In **poll** mode it asks the token endpoint every `interval` seconds:

```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…
```

In **ping** mode the realm calls the client's notification endpoint once the owner has answered, with the
`client_notification_token` the client sent as its `Bearer` token, and the client then collects as above.

The token names the owner as `sub` and the agent in `act`. To have the owner approve one exact action rather than a
scope, send it in `authorization_details`: see [Bounded approvals](../bounded-approvals/).

## Set it up

<Steps>

1. Add `urn:openid:params:grant-type:ciba` to the realm's `grantTypes` (in `oauth2Settings`) and to the agent's
   client's. Once it's on, the discovery document publishes `backchannel_authentication_endpoint`.

2. Make sure the agent has a `businessOwner`.

3. Choose how the agent learns the answer: set the client's `backchannelTokenDeliveryMode` to `poll`, or to `ping`
   with a `backchannelClientNotificationEndpoint`:

   ```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" }'
   ```

</Steps>

[Have an agent ask its owner](../../guides/agents/agent-asks-its-owner/) walks through it, including which
scopes need approval for each risk class.

## What the user sees

The business owner opens **Approvals** in the dashboard. Each waiting request shows the agent, its binding message,
the scopes it asks for (or the exact action, with [Bounded approvals](../bounded-approvals/)) and when it expires. They
choose **Allow** or **Deny**. Nothing reaches them by email or push yet, and a request they don't answer in time
expires: the agent is told `expired_token` and must ask again.

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| How long a request waits (`cibaLifetimeSecs`) | 300 seconds | `requested_expiry` can shorten it, never lengthen it |
| Poll interval (`cibaPollIntervalSecs`) | 5 seconds | Grows by 5 seconds after each `slow_down` |
| Requests waiting for one owner | 20 | Fixed |
| `binding_message` | Required | At most 64 printable characters |
| Ping retries | One more try after a timeout or server error | Fixed |
| Refresh tokens | None | Each action needs a new approval |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| Requests are never answered | The owner doesn't know they're waiting: no notification is sent | Tell owners to watch their **Approvals** page, or notify them from your own system when the agent asks |
| `unauthorized_client` at once | The agent has no business owner, the grant isn't on for the realm or the client, or a ping client has no notification endpoint | Name the owner; add the grant to both `grantTypes`; set `backchannelClientNotificationEndpoint` |
| Refused at once after many requests | Twenty requests already wait for this owner | Have the owner answer, or let them expire |
| The owner allowed it, but the agent collects nothing | The agent was quarantined before it collected | Release the agent; ask again |

## Audit record

When the agent collects, the realm writes a `TOKEN_ISSUED` record with the grant type
`urn:openid:params:grant-type:ciba`: issued and keyed by the owner, with the agent among its principals, or refused
(such as `access_denied` after a **Deny**) and keyed by the agent's client. The owner's answer itself isn't a record of
its own yet; a request never collected leaves none.

## Good to know

- **The owner isn't notified yet.** No email or push message is sent: requests wait on the owner's Approvals page,
  so tell owners to watch it.
- **Only an agent's client may ask,** and only for an agent that has a business owner.
- **The business owner is the only approver.** A request may name them with one hint, as a check: `login_hint` by
  their ID or naming attribute, or `id_token_hint` with an ID token this realm issued about them, whatever its
  expiry. A hint naming anyone else is `unknown_user_id`; `login_hint_token` is refused. The technical owner never
  approves, because they may hold far more access than the agent should.
- **Every request carries a `binding_message`,** at most 64 printable characters, which the owner reads to know
  what they're approving.
- **An approval brings an access token only.** Asking for `openid` is `invalid_scope`, and there's no refresh token:
  each new action needs a new approval.
- **The agent must still be active** both when it asks and when the owner answers. A quarantined agent collects
  nothing.
- **Each request is answered once,** within five minutes by default. `requested_expiry` can shorten the wait, never
  lengthen it past the realm's.
- **At most twenty requests wait for one owner at once.** Past that, the agent is refused until some are answered or
  expire.
- **Polling is paced.** A poll inside the interval is told `slow_down`, and the interval grows by five seconds. A
  poll that waits the full interval is answered.
- **The token is short-lived.** It ends with the session the owner approved from, and lives no longer than the
  agent's risk class allows ([Agent guardrails](../../../gateway/guardrails/agent-guardrails/)).
- **A ping goes only to an `https` address at a public host.** One that times out or meets a server error is tried
  once more.

## Reference

### Settings

| Where | Setting | What it does |
|---|---|---|
| Realm and client | `grantTypes` include `urn:openid:params:grant-type:ciba` | Off by default |
| Agent | `businessOwner` | Who approves |
| Client | `backchannelTokenDeliveryMode` | `poll` or `ping` |
| Client | `backchannelClientNotificationEndpoint` | Where a ping goes: `https`, a public host |
| Realm | `cibaLifetimeSecs` (`300`), `cibaPollIntervalSecs` (`5`) | How long a request waits, and the poll pace |

### Errors

| Error | When |
|---|---|
| `unauthorized_client` | Asking: the client isn't an agent's, may not use the grant, its agent has no business owner, or a pinging client has no notification endpoint |
| `invalid_request` | Asking: `binding_message` is missing or too long; more than one hint, a `login_hint_token`, or an `id_token_hint` that isn't this realm's; a ping client sends no `client_notification_token`; `requested_expiry` isn't a positive number |
| `unknown_user_id` | Asking: a hint names someone other than the business owner |
| `invalid_scope` | Asking: a scope the client may not ask for, or `openid` |
| `authorization_pending`, `slow_down` | Collecting: the owner hasn't answered yet, or the agent polled too soon |
| `access_denied` | Collecting: the owner refused |
| `expired_token` | Collecting: the request ran out before the owner answered |

## Related

- [Have an agent ask its owner](../../guides/agents/agent-asks-its-owner/): the task, start to finish.
- [Bounded approvals](../bounded-approvals/): the owner approves one exact action.
- [Agent guardrails](../../../gateway/guardrails/agent-guardrails/): which scopes need approval, by risk class.
- [AI agents](../ai-agents/): owners, risk classes and status.
