# Bind a grant to its holder

Bind a grant to the key of the agent that holds it, so a copied grant token is useless. The holder proves the key on every request with a DPoP proof, and every verifier checks it.

Use this when a grant leaves your hands: you pass it to an agent you don't run, or to one whose tokens could be logged or forwarded. A plain grant token works for whoever holds a copy. A bound grant names one key, and works only for the agent that holds that key.

The grant carries the key's thumbprint as `cnf.jkt` (RFC 7800). The agent signs a short DPoP proof (RFC 9449) for each request with that key. Provenance's API, `POST /v1/verify` and the offline verifiers in TypeScript, Python and Go all check the proof. Binding is free on every plan.

**Before you begin:** the agent that will hold the grant needs a P-256 key pair. `generateHolderKey` in `@provenanceauth/rails/mandates` makes one; keep the private key with the agent and send only the public key.

## 1. Make the holder's key

The agent makes its key once and gives you the public half:

```ts
import { generateHolderKey } from "@provenanceauth/rails/mandates";

const holder = await generateHolderKey();
// holder.publicJwk goes to whoever grants; holder.privateJwk stays with the agent.
```

## 2. Grant with `holder_key`

Pass the public key as `holder_key` on [`POST /v1/mandate/grants`](https://provenanceauth.com/docs/api/grants.md#post-v1-mandate-grants), or on a delegation ([`POST /v1/mandate/grants/{id}/delegate`](https://provenanceauth.com/docs/api/grants.md#post-v1-mandate-grants-id-delegate)) to bind a child to its own holder:

**curl**

```bash
curl -s $PROVENANCE_API/v1/mandate/grants \
  -H "Authorization: Bearer prv_test_acct_P9xN2vQ7bT4mW8sYd3cFjLh6gAe1kR5" \
  -H "Content-Type: application/json" \
  -d '{ "device_id": "dev_00000000000000000000000000000001", "purpose": "Hold a seat on the Lisbon flight", "constraints": { "max_amount": 300, "expires_at": "2027-12-31T23:59:59Z" }, "holder_key": { "kty": "EC", "crv": "P-256", "x": "NeVHu4qeD40iEEgSDcTm26Y0xApkjq41T6BuENVl0js", "y": "qS59-GAPcl0bJ-yYFvPJA3omsU9DJFedMw4rou5cXo8" } }'
```

**Node**

```ts
const grant = await provenance.mandate.grants.create({
  deviceId: "dev_00000000000000000000000000000001",
  purpose: "Hold a seat on the Lisbon flight",
  constraints: { maxAmount: 300, expiresAt: "2027-12-31T23:59:59Z" },
  holderKey: holder.publicJwk,
});
```

The grant names the key's thumbprint in `holder_jkt`, and its `mandate_token` carries the same value as `cnf.jkt`. Only the thumbprint is stored. A grant without `holder_key` is a bearer grant: it works for whoever holds the token.

## 3. Sign a proof for each request

The agent signs one proof per request, naming the method, the URL, the time and the grant token, and sends it in the `DPoP` header:

```ts
import { signHolderProof } from "@provenanceauth/rails/mandates";

const url = "https://api.provenanceauth.com/v1/mandate/intents";
const dpop = await signHolderProof(holder.privateJwk, { method: "POST", url, token: mandateToken });
await provenance.mandate.intents.create({ actionDescription: "Hold seat 14C", agentSessionId, deviceId, mandateToken }, { dpop });
```

A proof is good for one request and 60 seconds. Sign a new one for every call, retries included. When a request names the grant by id (`grant_id` on a deal or a tab), sign the proof without `token`.

## 4. Check the proof where you receive the grant

An agent or a store that receives a bound grant checks the proof that came with it. Pass the `DPoP` header and the request it came on:

**Node**

```ts
import { verifyMandate } from "@provenanceauth/rails/mandates";

const seen = new Map<string, number>();
const r = await verifyMandate(mandateToken, {
  jwks,
  need: { amount: 120, action_type: "purchase" },
  holder: {
    proof: req.headers.get("dpop") ?? "",
    method: req.method,
    url: req.url,
    seen: (jti, until) => (seen.has(jti) ? false : (seen.set(jti, until), true)),
  },
});
if (!r.valid) throw new Error(`Not authorized: ${r.reason}`);
```

**Python**

```python
from provenanceauth_verify.mandates import verify_mandate

r = verify_mandate(mandate_token, jwks, need={"amount": 120}, holder={"proof": request.headers.get("DPoP", ""), "method": request.method, "url": request.url, "seen": seen_once})
if not r.valid:
    raise PermissionError(r.reason)
```

**Go**

```go
r := provenanceverify.VerifyMandateHolder(mandateToken, jwks, nil, 0, &provenanceverify.MandateHolder{
	Proof: req.Header.Get("DPoP"), Method: req.Method, URL: fullURL, Seen: seenOnce,
})
if !r.Valid {
	return fmt.Errorf("not authorized: %s", r.Reason)
}
```

`seen` records each proof's `jti` until its window closes, so a proof can't be replayed. Without a store of your own, check online: pass the proof and its request as `presenter` to [`POST /v1/verify`](https://provenanceauth.com/docs/api/verify.md#post-v1-verify), which records the `jti` for you. A checkout assessment reads the proof from the `DPoP` header in the `request` you forward.

The result's `holder_bound` and `holder_jkt` say whether the grant names a key. A bearer grant passes with no proof.

| Reason | What it means |
| --- | --- |
| `dpop_proof_required` | The grant is bound and no proof came with it. Treat the token as a copy. |
| `dpop_proof_invalid` | The proof doesn't check: wrong `typ` or algorithm, a bad signature, another method or URL, an `iat` more than 60 seconds off, or an `ath` for another token. |
| `dpop_proof_replayed` | The proof was already used. |
| `holder_mismatch` | The proof is signed by another key than the one the grant names. |

## Errors you might see

| Code | What to do |
| --- | --- |
| `dpop_proof_required` | Send the proof in the `DPoP` header with every request that uses the grant. |
| `dpop_proof_invalid` | Sign the proof for this exact request: `htm` the method, `htu` the URL without its query, `iat` now, `ath` the grant token's hash. |
| `dpop_proof_replayed` | Sign a new proof with a fresh `jti`. |
| `holder_mismatch` | Sign with the private key whose public key was given as `holder_key`. |
| `invalid_field_value` (`holder_key`) | Send a P-256 public key as a JWK, without `d`. |

## See also

- [Check an agent's grant](https://provenanceauth.com/docs/mandate/guides/check-another-agents-authority.md)
- [Delegate to sub-agents](https://provenanceauth.com/docs/mandate/guides/give-subagents-their-own-limits.md)
- [Verify tokens offline](https://provenanceauth.com/docs/authorize/guides/verify-tokens.md)
