# How it works

> People, agents, sessions as a tree, needs, and approvals: the five ideas the rest of the docs build on.

From `README.md`, `docs/guide.md`, `docs/ARCHITECTURE.md` in tensile-tools/tensile-auth at commit `e66760d` (v0.4.1-7-ge66760d). The HTML page is https://auth.tensile.tools/docs/get-started/how-it-works.

## People

A person signs in with a passkey, or with an email address and a password, and can add TOTP and recovery codes as second factors. The library owns every sign-in route and the browser client: you don't write handlers for sign-in, sign-up, step-up, or sign-out. Sign-up, sign-in, and forgot-password answer the same for every address, nothing locks an account, and a code sent by email never signs anyone in.

A password alone is single-factor. On an account with a passkey or confirmed TOTP, a session that signed in with only a password is never fresh, so a phished password can't change factors, create tokens, or end other sessions.

## Agents

A person creates an agent with `svc.CreateAgent`, and gives it its own token, delegated from the person's session, with `svc.Delegate`. The agent never uses the person's cookie.

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, so a route that only the person should call needs `Human: true`, or checks `p.IsAgent()`.

An agent can also sign itself up. It gets a sandboxed token and a single-use claim code, and waits for a fresh, multi-factor person to claim it. Unclaimed agents are deleted at their claim deadline.

## Sessions are a tree

Every session hangs from the session that delegated it, back to a person's sign-in. Delegation only narrows: a child gets a subset of its parent's scopes, never outlives its parent, and depth is capped.

A session tree: alice signed in with a passkey; Claude Code holds a session delegated from hers; Test runner holds a session delegated from Claude Code's.

Revocation cascades and is immediate: revoking alice's session ends Claude Code's and the Test runner's too. `svc.SessionTree(ctx, user)` returns each device with the agents and tokens delegated from it, for a "Where you're signed in" page.

An OAuth connection is the exception. When alice allows an MCP client such as Claude Code on a consent page, the grant is its own root, not a child of the browser session she approved it from, so signing out of that browser doesn't end it, as with apps you allow on GitHub or Google. The grant ends when it expires or sits unused too long, when she disconnects it on her account page, or when she chooses "Sign out other devices".

## Needs

Each route declares what it needs, and the library decides, enforces, records, and explains. A need can ask for scopes, a level of assurance, a recent proof, or a person:

```go
mux.HandleFunc("DELETE /repos/{id}", authhttp.Need(auth.Need{Assurance: auth.AssuranceMulti, Within: 10 * time.Minute, Human: true}), deleteRepo)
```

A caller who doesn't meet the need gets a challenge that names the need and lists only the factors this account has that would meet it:

```json
{"code": "step_up_required", "title": "Confirm it's you", "fix": "Confirm it's you with a passkey, then try again.",
 "challenge": {"action": "secrets.edit", "assurance": "multi", "within_seconds": 600, "human": true, "satisfy_with": ["passkey"]}}
```

`authhttp.Mux` makes every route declare a rule, and `mux.Check()` refuses to start when something would weaken security.

## Approvals

When your agent tries something risky, it gets a link instead of a yes; you tap your passkey on that exact action, and the agent continues, once, with a signed receipt.

1. **Agent asks.** The route refuses the agent with `human_required` and an approval: a link to show the person, the action's summary, and a hint to wait.
2. **You tap.** The person opens the link and approves with a passkey, which signs the action's digest.
3. **It runs once.** The agent's retry, held open with `Prefer: wait=60`, runs the route once.

## Everything is recorded

Every session start, delegation, rotation, revocation, agent change, and detected token reuse is recorded in the same transaction as the change. Failed sign-ins, throttled attempts, rejected tokens, and denied requests are recorded too. Events are sealed into a hash chain, so `svc.VerifyAudit` detects edits and deletions.
