# Tokens and agents

> Personal access tokens, agents, and delegated tokens that only narrow what the person can do.

From `docs/guide.md`, `README.md` in tensile-tools/tensile-auth at commit `e66760d` (v0.4.1-7-ge66760d). The HTML page is https://auth.tensile.tools/docs/guides/tokens-and-agents.

**Go**
```go
pat, _, err := svc.CreatePAT(ctx, principal, auth.PATSpec{Name: "CI deploys", Scopes: []string{"repo:read"}})
agent, err := svc.CreateAgent(ctx, principal, auth.AgentSpec{Name: "Claude Code", Scopes: []string{"repo:read"}})
tok, _, err := svc.Delegate(ctx, principal, auth.DelegateSpec{Agent: agent.ID, Name: "Claude Code, task 14"})
```

**Next.js**
```ts
// app/api/agents/route.ts
import { auth } from '@/lib/auth'

export const POST = auth.route(async () => {
  await auth.require({ action: 'agent.create', human: true })
  const me = auth.as(await auth.subject())
  const agent = await me.agents.create({ name: 'My agent', scopes: [] })
  const { token } = await me.agents.delegate(agent.id, { scopes: [], ttl: '1h' })
  return Response.json({ agent: agent.id, token }, { status: 201 })
})
```

`Delegate` gives the token only scopes that both the parent and the agent's `AgentSpec.Scopes` allow; asking for more is `scope_exceeded`.

> **An agent's `Principal.User` is the human it acts for.** A scope check alone lets the agent do anything its human can within those scopes. In every handler that uses the user's identity for something only the person should do, such as accepting an invitation, creating an account-level resource like an API key or a household, or listing everything the person has, refuse agents: declare the route with a need that has `Human: true`, or check `p.IsAgent()` in the handler.
>
> ```go
> mux.HandleFunc("POST /invites/{id}/accept", authhttp.Need(auth.Need{Human: true}), acceptInvite)
>
> // Or, in a handler that agents may call for other things:
> p, _ := auth.FromContext(r.Context())
> if p.IsAgent() {
> 	// Require records the refusal and answers human_required.
> 	authhttp.Error(w, r, svc.Require(r.Context(), p, auth.Need{Action: "household.create", Human: true}))
> 	return
> }
> ```

`examples/agents` creates an agent for one household, delegates its token from the person's session, refuses it in every handler that only people may call, and revokes it with `svc.RevokeOwnAgent`.

To let agents sign themselves up, pass `auth.WithSignup(auth.SignupPolicy{...})`. An unclaimed agent holds only `SignupPolicy.Scopes`, its sandbox, until a person claims it. Your app serves three routes:

1. **Sign-up**, `authhttp.Public`: call `svc.RegisterAgent`. Return the agent's token, a claim link, and the four pairing words from `auth.PairingWords(code)`. An empty agent type is stored as `unknown`.
2. **The claim page**, for a signed-in person: show `svc.PeekClaim(ctx, principal, code)`, with the same pairing words, then call `svc.ClaimAgent` with the scopes the person grants. Claiming needs a multi-factor sign-in from the last 10 minutes.
3. **Pickup**, `authhttp.Public`: the agent sends its old token in the request body, and the route calls `svc.CollectClaimWait`. The old token can't go in an `Authorization` header: the claim revoked it, so the mux refuses it before your handler runs.

`ratelimit.DefaultRules` allows each client 5 sign-ups an hour. `docs/design/agent-signup.md`, in the library repository, covers the claim window, quotas, expiry, and type verification.

## Tokens, leaks, and DPoP

Tokens are opaque and checksummed (`tss_…`, `tsa_…`, `tsp_…`, `tso_…`, `tsr_…`), so secret scanners can detect them, and `leakscan` revokes a token when a scanner reports it. Only their SHA-256 is stored, and the `Token` type redacts itself everywhere.

With DPoP (RFC 9449), an agent's token works only with a fresh proof signed by the agent's private key, so a leaked token alone gets nowhere. `auth.RequireAgentKeys` refuses to create unbound agent sessions.
