# Let an agent act for a user

> Let an agent use a user's access on their behalf, with tokens that name both the user and the agent, within limits you and the user set.

An email assistant, a scheduling agent or a coding agent working in someone's repositories needs the user's access,
not its own. The user signs in to an app as usual; the agent swaps the user's token for one that says *this user,
through this agent*. Every API sees both, and can decide differently for an agent acting for someone than for the
user themselves. The agent can never do more than the user could, or more than you allowed.

**Before you start:**

- **Who can do it:** a realm administrator (a group holding `RealmOwner`).
- **Where:** today this is done through the API, with your session in `$CVYUH_SESSION`
  ([Call the API](/develop/api/)).
- **What you need:** `invoice-agent` and its client `invoice-agent-client` are registered (see
  [Register an agent](../register-an-agent/)). Here the agent acts for `maya`.

## Allow the delegation

<Steps>

1. Read the realm's grant types:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

2. Offer token exchange in the realm: send the `grantTypes` you just read, plus token exchange.

   ```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:ietf:params:oauth:grant-type:token-exchange"] }'
   ```

   <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. Send every one you read in step 1.
   </Aside>

3. Let the agent's client use it: read the client, and send its `grantTypes` plus the same value:

   ```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 '{ "grantTypes": ["client_credentials", "urn:ietf:params:oauth:grant-type:token-exchange"] }'
   ```

4. Let the agent act for the user, limited in time and scope:

   ```sh
   curl -X POST https://foobar.cvyuh.com/api/v0/am/objects/v1/agent/invoice-agent/relationships/actsFor \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{
           "targetId": "maya",
           "targetType": "user",
           "properties": {
             "validFrom": "<RFC 3339 time>",
             "validTo": "<RFC 3339 time>",
             "scopes": ["invoices:read"]
           }
         }'
   ```

5. Allow delegation by policy. The realm's delegation set starts empty, so without a policy every exchange is
   refused. This one allows any agent to use a delegation it holds:

   ```sh
   curl -X POST "https://foobar.cvyuh.com/api/v0/am/objects/v1/policy?id=agents-may-delegate" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{
           "active": true,
           "resources": ["delegation://*/*"],
           "actionValues": { "delegate": true },
           "subject": { "type": "JwtClaim", "claimName": "principal_type", "claimValue": "agent" }
         }'
   ```

6. Link the policy to the `delegation` set and the `delegation` type:

   ```sh
   curl -X POST https://foobar.cvyuh.com/api/v0/am/objects/v1/policy/agents-may-delegate/relationships/policySet \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "targetId": "delegation" }'

   curl -X POST https://foobar.cvyuh.com/api/v0/am/objects/v1/policy/agents-may-delegate/relationships/resourceType \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "targetId": "delegation" }'
   ```

7. Optionally, limit how many agents may stack in one token. The default is 4, and an exchange that would nest
   deeper is refused:

   ```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 '{ "maxActDepth": 2 }'
   ```

</Steps>

## Check it worked

The agent swaps `maya`'s access token at the token endpoint, proving itself with its key and, since an agent's client
is held to FAPI 2.0 by default (unless it attests), a DPoP proof:

```sh
curl https://foobar.cvyuh.com/oauth2/token \
  -H "DPoP: <proof>" \
  -d grant_type=urn:ietf:params:oauth:grant-type:token-exchange \
  -d subject_token=<maya's access token> \
  -d subject_token_type=urn:ietf:params:oauth:token-type:access_token \
  -d scope=invoices:read \
  -d client_id=invoice-agent-client \
  -d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \
  -d client_assertion=eyJ…
```

It gets an access token only, bound to the agent's DPoP key and ending no later than the user's. Its claims name
both:

```json
{
  "sub": "<maya's id>",
  "act": { "sub": "<invoice-agent's id>", "principal_type": "agent", "risk_class": "high", "...": "..." },
  "mode": "delegated",
  "scope": "invoices:read"
}
```

A second agent working for the first appears outside it: `act` names the newest agent, and its own `act` names the
one before.

- **Naming the API.** The agent may name it with `resource`, or with `audience`, which must be one of its client's
  `allowedResources` (else `invalid_target`). Both may be sent only when they name the same one; otherwise the
  request is `invalid_request`.
- **No silent cut.** Asking for more than allowed is `invalid_scope`.
- **Token types.** The realm's access tokens are JWTs, so a token type of `…:token-type:jwt` is taken wherever
  `…:access_token` is, for the token sent and for the one asked for.

The user sees nothing at the exchange. On their **AI agents** page they see every agent allowed to act for them, and
their own audit trail lists every exchange made in their name.

<Aside type="caution" title="Impersonation">
  A token that names only the user, with no `act`, needs three more things: the realm's
  `tokenExchangeImpersonation: true`, `"impersonate": true` on the delegation, and `"impersonate": true` in the
  policy's `actionValues`. Leave it off unless an API can't accept `act`.
</Aside>

## Undo it

Delete the agent's `actsFor` link to the user. The user can do the same themselves: see
[Let users remove agents](../people-remove-agents/).

## Related

- [Token exchange](../../../applications/tokens/token-exchange/): every rule an exchange is held to.
- [AI agents](../../../agents/ai-agents/): the agent identity.
- [Audit trail](../../../../audit-reports/audit-trail/): where each exchange is recorded.
- [Have an agent ask its owner](../agent-asks-its-owner/): approval before a sensitive action.
