# Authorization for stend

How an agent gets permission to publish stends on someone's behalf.

Machine-readable equivalents of everything below:

    /.well-known/oauth-protected-resource    RFC 9728
    /.well-known/oauth-authorization-server  RFC 8414

## One method: OAuth 2.1

There is no API key and no long-lived credential to hand over. Access is a
token you obtain yourself, approved by a person in a browser.

stend is its own authorization server. Sign-in goes to `id.everruns.com`;
stend issues its own tokens, scoped to the stend MCP resource.

## The flow

1. **Discover.** Call `POST /mcp` without a token. You get `401` and a
   `WWW-Authenticate` header naming the protected-resource metadata document.
2. **Identify yourself**, either way:
   - **Register.** `POST /register` with `client_name` and `redirect_uris`
     (RFC 7591). Open: no admin approval, no secret issued. You are a public
     client; PKCE takes the place of a client secret.
   - **Or use a URL.** If you host your client metadata at a stable
     HTTPS URL, pass that URL as your `client_id` and skip registration
     (Client ID Metadata Documents). stend fetches it, requires the
     `client_id` inside to equal the URL, and refuses redirects, non-HTTPS
     URLs, IP-literal or private hosts, documents over 5 kB, and any symmetric
     `token_endpoint_auth_method`. Rejections say why.
3. **Authorize.** Send the user to `GET /authorize` with `response_type=code`,
   your `client_id`, a registered `redirect_uri`, and PKCE
   (`code_challenge_method=S256`, required; there is no plain method).
4. **The human decides.** They sign in, then see a consent screen naming your
   client and where the tokens will be sent. They can also switch to a
   different account here. Nothing is issued until they approve.
5. **Exchange.** `POST /token` with `grant_type=authorization_code`, the code,
   your `code_verifier`, `client_id` and the same `redirect_uri` you sent to
   `/authorize`, including its port. Codes are single-use.
6. **Call.** Send `Authorization: Bearer <access_token>` on every `/mcp` call.

## Desktop redirect URIs

Registration and Client ID Metadata Documents accept HTTPS callbacks,
loopback HTTP callbacks, and private-use schemes for installed apps:

- `cursor://anysphere.cursor-mcp/oauth/callback`
- `vscode://mcp/callback` or `claude://oauth/callback`
- `com.example.app:/oauth`
- `http://127.0.0.1:51004/callback`, `http://[::1]:61023/callback`, or
  `http://localhost/callback`

HTTPS and private-use callbacks must match the registered URI exactly. For
loopback HTTP callbacks, only the port may differ at `/authorize`, so a desktop
client can use an available port on each login (RFC 8252). The host, path and
query must still match. Prefer a loopback IP literal over `localhost`.

Every client must use PKCE S256. Private-use schemes are handed to an installed
app by the operating system; the scheme alone does not prove which app receives
the code. PKCE prevents an app that intercepts it from exchanging it for tokens.

## Tokens

- Access tokens last 30 days; refresh tokens 180.
- Refresh with `grant_type=refresh_token`. Tokens rotate: the old refresh token
  is consumed as the new pair is issued, so store what you get back.
- `POST /revoke` (RFC 7009) disconnects you. Post any token of yours
  (`token=<access or refresh token>`, form-encoded or JSON) and every token on
  that grant is revoked at once. An access token you do not revoke stays valid
  for the rest of its 30 days.
- Tokens are opaque. Do not try to parse one.

## Scope

A token grants one thing: managing that user's stends (publishing, reading,
editing and deleting them). It carries no access to anything else in
Everruns, and it cannot read another user's stends.

## What will not work

- Changing a registered callback's scheme, host, path or query. HTTPS and
  private-use callbacks also require the exact registered port.
- Non-loopback HTTP callbacks, URI fragments, and `javascript:`, `data:`,
  `file:`, `vbscript:`, `blob:` or `about:` callbacks are refused at registration.
- Skipping PKCE, or using `plain`: both are refused.
- Replaying an authorization code: it is deleted as it is read.
- Sending the user to `/authorize` with a redirect URI you did not register:
  refused as JSON, never redirected. stend is not an open redirector.
