# Sessions and devices

> A session tree people can read, revocation, and sessions bound to a key in the device's hardware.

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

- `svc.SessionTree(ctx, user)` returns each device with the agents and tokens delegated from it, for a "Where you're signed in" page.
- `svc.RevokeOwn(ctx, principal, id)` and `svc.RevokeOwnAgent` end one of the person's own sessions or agents; `svc.RevokeOthers` signs out every other device.
- `pk.Rename` relabels a passkey, and `pk.AcceptedCredentials` feeds the browser client's password-manager sync.

## Session kinds

`GET /auth/sessions`, which the browser client's `sessions()` and `<tensile-sessions>` read, names each session's kind for the person. The app API under `/v1/`, which the SDK's principals and session trees come from, uses the library's names:

| `/auth/sessions` | `/v1/` | The session |
|---|---|---|
| `device` | `human` | A person's sign-in on one browser or device |
| `agent` | `agent` | An agent's session, delegated from the session above it in the tree |
| `token` | `pat` | A personal access token |
| `app` | `oauth` | An OAuth access token; `client` names the OAuth client |
| `other` | An empty string | Any other kind: the refresh session of an OAuth grant that has refresh tokens. It is the grant's root, and the grant's `app` access tokens are its children |

## OAuth grants

An OAuth grant, such as an MCP client's connection, isn't tied to the browser session that approved it: signing out of that browser, or that session expiring, leaves the grant working. A grant ends when one of these happens:

- **It expires.** A grant lives at most 366 days under every built-in preset.
- **It goes unused.** A grant ends after 90 days without use under every built-in preset.
- **The person revokes it.** On the account page, `<tensile-sessions>` revokes a row and everything under it, as `revokeSession(id)` and `svc.RevokeOwn` do. Revoke the grant's root: the `other` row of a grant with refresh tokens, or the `app` row of one without. Revoking only an `app` row under an `other` row ends that access token, and the client gets another with its refresh token.
- **The person signs out everywhere else.** "Sign out other devices", that is `signOutOthers()` or `svc.RevokeOthers`, revokes every OAuth grant, every personal access token, and every other device.
- **An administrator signs the person out.** `svc.RevokeUser`, or `POST /admin/v1/users/{user}/revoke` on the server, revokes every session of the user, grants included.

## Device-bound sessions

Device Bound Session Credentials (DBSC) are opt-in. To turn them on, set `authroutes.Config.DeviceBound`. In browsers that support DBSC, the session is bound to a key in the device's hardware, the cookie lives 10 minutes, and the browser renews it by proving the key, so a stolen cookie stops working within minutes. Other browsers keep a plain cookie, and nothing changes for them.

Turning DBSC off again signs no one out. `authroutes` keeps the refresh route, and each bound browser proves its key one last time and gets a plain cookie for the rest of its session.

Without `authroutes`, pass `authhttp.WithDeviceBound(prefix, lifetime)` and serve `authhttp.DeviceBound(svc)` at that prefix, with refresh public. To turn it off, replace the option with `authhttp.WithDeviceBoundRetired(prefix, lifetime)` and keep the route until the longest session has expired. Removing both signs out every bound browser within 10 minutes.
