# auth.md

> How an AI agent registers and authenticates to **Restivity**. Restivity
> implements OAuth 2.1 (authorization code + PKCE) with RFC 7591 Dynamic Client
> Registration. Protected resources: `https://restivity.ai/api/v1` (REST) and
> `https://restivity.ai/api/mcp` (MCP, Streamable HTTP). This file follows the `auth.md`
> convention (https://github.com/workos/auth.md).

## Step 1: Discover

### 1a. Fetch the Protected Resource Metadata (RFC 9728)

`GET` <https://restivity.ai/.well-known/oauth-protected-resource>. It advertises the protected
resource (`https://restivity.ai/api/v1`) and its authorization server.

### 1b. Fetch the Authorization Server Metadata (RFC 8414)

`GET` <https://restivity.ai/.well-known/oauth-authorization-server> (alias
<https://restivity.ai/.well-known/openid-configuration>). It returns the endpoints below, the
supported scopes, `code_challenge_methods_supported: ["S256"]`,
`grant_types_supported: ["authorization_code", "refresh_token"]`, and an
`agent_auth` block (`skill`, `register_uri`, `revocation_uri`).

| Purpose | Method | Endpoint |
| --- | --- | --- |
| Dynamic Client Registration (RFC 7591) | `POST` | <https://restivity.ai/api/oauth/register> |
| Authorization | redirect | <https://restivity.ai/oauth/authorize> |
| Token | `POST` | <https://restivity.ai/api/oauth/token> |
| Token revocation (RFC 7009) | `POST` | <https://restivity.ai/api/oauth/revoke> |
| JWKS (token-verification keys) | `GET` | <https://restivity.ai/api/oauth/jwks> |

## Step 2: Register

Restivity supports RFC 7591 Dynamic Client Registration. Public clients receive
no secret and MUST use PKCE; confidential clients may authenticate with
`client_secret_basic`.

`scope` is optional: a client registered without it may ask for any scope of
the registry (see Scopes); the user approves each one on the consent screen, and
a token carries only the scopes the user approved. `offline_access` is accepted and
ignored, here and at the authorization endpoint: every authorization-code grant
returns a refresh token. Any other scope outside the registry is refused.

A `client_name` that imitates Claude, Anthropic, ChatGPT, OpenAI, Cursor or Restivity is refused with
`invalid_client_metadata`, unless every redirect URI is on the domains of the vendor it
names (`claude.ai`, `claude.com` for Claude and Anthropic; `chatgpt.com`, `openai.com` for ChatGPT
and OpenAI) or every redirect URI is a native app's loopback callback (plain http on
`127.0.0.1`, `[::1]` or `localhost`). Restivity is reserved for Restivity's own clients, and
Cursor has no domain here, so its name is accepted on loopback callbacks only. A metadata
field sent as `null` counts as omitted.

```http
POST https://restivity.ai/api/oauth/register
Content-Type: application/json

{
  "client_name": "My Agent",
  "redirect_uris": ["https://my-agent.example/callback"],
  "grant_types": ["authorization_code", "refresh_token"],
  "response_types": ["code"],
  "token_endpoint_auth_method": "none",
  "scope": "tasks:read tasks:write"
}
```

Response (`201 Created`). Store the `client_id`:

```json
{
  "client_id": "dcr_8f3a1c2e-...",
  "client_id_issued_at": 1748600000,
  "redirect_uris": ["https://my-agent.example/callback"],
  "grant_types": ["authorization_code", "refresh_token"],
  "response_types": ["code"],
  "token_endpoint_auth_method": "none",
  "scope": "tasks:read tasks:write"
}
```

### Or: a Client ID Metadata Document (CIMD)

Instead of registering, a client may use the HTTPS URL of its own Client ID
Metadata Document as `client_id` (draft-ietf-oauth-client-id-metadata-document;
`client_id_metadata_document_supported: true` in the metadata above). At the
authorization endpoint the server fetches that URL (`GET`, no redirect
followed, a 5-second timeout, `200` with a JSON content type, at most
5120 bytes) and requires `client_id` equal to that URL, a `client_name`, and
`redirect_uris` that include the `redirect_uri` of the request. Neither the URL
nor any URL the server may fetch from the document (`jwks_uri`, `logo_uri`, `client_uri`, `tos_uri`, `policy_uri`)
may resolve to a private or special-use address; its `redirect_uris` follow the
registration rule: https, or plain http on a loopback address for a native app. A
valid document is cached for its `Cache-Control: max-age`, between 5 minutes and 24 hours; a refused one is
never cached.

A document that lists `none` (in `token_endpoint_auth_method` or
`token_endpoint_auth_methods_supported`), or names no method at all, is a public
client: no secret, PKCE `S256` as for every client, even when it also lists
`private_key_jwt`. A document that lists `private_key_jwt` without `none` is a
confidential client: it must name its `jwks_uri` (https; an inline `jwks` is not
accepted) and its `token_endpoint_auth_signing_alg`, one of `RS256`, `PS256`, `ES256` (never
`none`, never an HS algorithm). Every request it makes to the token and the
revocation endpoints carries a client assertion (RFC 7523):
`client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer` and
`client_assertion`, a JWT signed with that algorithm by a key of its `jwks_uri`, whose
`iss` and `sub` are the `client_id`, whose `aud` is the issuer `https://restivity.ai` or the
token endpoint `https://restivity.ai/api/oauth/token` (at the revocation endpoint also `https://restivity.ai/api/oauth/revoke`), with an
`exp` at most 600 seconds ahead, `iat` and `nbf` (when present) not in the
future, 60 seconds of clock skew allowed, and a `jti` that is used once. The server
reads the keys again at most every 10 minutes, and at once for a `kid` it does not
know. PKCE still applies to the code.

Its `client_name` follows the same rule, anchored on the host of
the `client_id` instead of the redirect URIs. Dynamic Client Registration remains available.

## Step 3: Authorize

Redirect the user to the authorization endpoint with a PKCE challenge. `plain`
is NOT supported: use `S256`.

```
https://restivity.ai/oauth/authorize?response_type=code
  &client_id=<client_id>
  &redirect_uri=<redirect_uri>
  &scope=tasks:read%20tasks:write
  &state=<opaque>
  &code_challenge=<base64url-sha256(verifier)>
  &code_challenge_method=S256
```

## Step 4: Token

Exchange the authorization code (with the PKCE verifier) for tokens, and renew
with the refresh token.

```http
POST https://restivity.ai/api/oauth/token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=<code>&redirect_uri=<redirect_uri>&client_id=<client_id>&code_verifier=<verifier>
```

Response (`200 OK`):

```json
{
  "access_token": "<ES256 JWT>",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "<refresh_token>",
  "scope": "tasks:read tasks:write"
}
```

A code is single-use. Exchanging it a second time answers `400` `invalid_grant`
("Authorization code has already been used.") and, when the second exchange carries
the same `client_id`, `redirect_uri` and a matching `code_verifier`, revokes every
token the first exchange issued (RFC 6749 §4.1.2). If the answer of an exchange is lost,
start again from Step 3 instead of sending the code a second time.

Renewal: `grant_type=refresh_token&refresh_token=<token>&client_id=<client_id>`.
A `private_key_jwt` metadata-document client adds `client_assertion_type` and
`client_assertion` (Step 2) to both requests.
Access tokens are ES256-signed JWTs verified against the JWKS above.

## Step 5: Use the credential

Send `Authorization: Bearer <access_token>` to:

- **REST**: <https://restivity.ai/api/v1> (OpenAPI at <https://restivity.ai/api/v1/openapi.json>).
- **MCP**: <https://restivity.ai/api/mcp> (Streamable HTTP).

## Scopes

Through the scopes below an agent can read and change your tasks, projects, custom statuses, teams, focus sessions, comments, tags and profile, and read your earnings.

`resource:action` where resource ∈ {tasks, projects, pomodoro, comments, tags} and action ∈ {read, write, delete}, plus `profile:read`, `profile:write` and `earnings:read`.
Account deletion is intentionally NOT an agent scope (no `profile:delete`):
route the user to the UI. Full registry:

- `tasks:read`
- `tasks:write`
- `tasks:delete`
- `projects:read`
- `projects:write`
- `projects:delete`
- `pomodoro:read`
- `pomodoro:write`
- `pomodoro:delete`
- `comments:read`
- `comments:write`
- `comments:delete`
- `tags:read`
- `tags:write`
- `tags:delete`
- `profile:read`
- `profile:write`
- `earnings:read`

## Plan requirement

A valid token is necessary, not sufficient. The connection needs a Restivity Pro or Team plan; a paid Team seat also unlocks it on a free personal plan. A free account can sign in, and the tools then answer that its plan does not include agent access.
A free account completes the OAuth flow normally; the two surfaces then answer
differently:

- REST https://restivity.ai/api/v1 answers HTTP 403 `upgrade_required` (application/problem+json with `upgrade_url`), with `type: <https://restivity.ai/errors/upgrade_required>`.
- MCP https://restivity.ai/api/mcp accepts `initialize`, `ping` and notifications (the `initialize` `instructions` open with the refusal's message), then answers every other request with HTTP 200 and a JSON-RPC error whose `data.type` is `upgrade_required` (code `-32003`, with `data.upgrade_url`), never an HTTP 403.

Show the upgrade link; do not restart the OAuth flow. The `upgrade_url` is `https://restivity.ai/pricing`; neither
answer is a `401`.

## Rate limits

Per user, per UTC hour, by HTTP verb:

- read: 1000/hour (GET, HEAD)
- write: 200/hour (POST, PATCH, PUT)
- delete: 50/hour (DELETE)

Every agent response carries `X-RateLimit-Tier: <tier>:<count>/<limit>`.
Over budget the API answers `429` `rate_limited` with `Retry-After`
(seconds to the top of the hour); back off until then.

The token and revocation endpoints (`POST /api/oauth/token`, `POST /api/oauth/revoke`)
share one gate: 600 requests per source IP per minute, both counted together.
Over budget it answers `429` with the OAuth error `slow_down` and `Retry-After`
(seconds to the next minute): wait, then retry the same request; do not
re-run the authorization flow.

Each authenticated client also has a budget across both endpoints:
6000 requests per client per hour. A confidential client (one that
authenticates with its secret) past it is refused with `429` `slow_down` and
`Retry-After` (seconds to the end of the hour); the refused request consumes
nothing, so the same refresh token or code is still valid when you retry.

A public client (PKCE, no secret) is never refused on that per-client count.
Each person it acts for has a budget instead: 300 requests per user per hour on the token endpoint,
counted only for a request that carries a valid authorization code or refresh
token. Past it the answer is the same `429` `slow_down` with `Retry-After` to the
end of the hour, and nothing is consumed. Revocation never counts toward it.

## Errors

- OAuth endpoints return RFC 6749 error objects: `invalid_request`,
  `invalid_client`, `invalid_grant`, `unauthorized_client`,
  `unsupported_grant_type`, `invalid_scope`.
- The protected resource returns `401` with a `WWW-Authenticate: Bearer`
  challenge when the token is missing, expired or revoked (`invalid_token`).
- A token without the scope a call needs: REST answers `403` `insufficient_scope` (problem+json with `scope_required`, and a `WWW-Authenticate: Bearer error="insufficient_scope"` challenge that names the scope);
  an MCP tool answers a tool error (`isError`) whose `code` is `insufficient_scope`, with `required_scope`.
- REST errors use RFC 7807 `application/problem+json`; MCP tools return
  structured errors (`not_found`, `forbidden`, `validation_error`,
  `conflict_recurring_master`, `overlapping_active_session`, …).

## Revocation

`POST` <https://restivity.ai/api/oauth/revoke> (RFC 7009) with `token=<access_or_refresh_token>`
to revoke a credential. A confidential client authenticates with HTTP Basic
(`client_id:client_secret`); a public client sends its `client_id` in the
form. A client can revoke only its own tokens; the answer is `200` with an
empty body whether or not the token existed. Users can also revoke a
connected agent from the Restivity UI.

An access token stops working when it is revoked: when the user disconnects the
app, when you revoke it (or the refresh token it was issued with), when its refresh
token is replayed, when the authorization code it came from is exchanged a second time,
when an administrator suspends the account or disables your client.
The API then answers `401` `invalid_token` within 60 seconds of the
revocation, never later. On that `401`, refresh; if the refresh is refused with
`invalid_grant`, run the authorization flow again.

## Agent skills

Skills: the published how-to texts, one per job; the index, with each skill's URL and digest, is https://restivity.ai/.well-known/agent-skills/index.json
- `restivity-automation`: driving the REST API and the MCP tools: connecting, the tool catalog, the task / today / search / batch / recurrence workflows
- `restivity-daily-plan`: running the person's day (plan my day, what do we close today, add this to today, recap): one card per calendar day, its subtasks, log and recap (`profile_get` / `tasks_create` / `tasks_get` / `tasks_update` / `comments_create` / `comments_list`)
- `restivity-focus-flow`: actually WORKING a task, not only reading or editing it: status, pomodoro work sessions, findings filed on the way, a closing comment (`focus_start` / `focus_finish`)
- `restivity-task-updates`: writing ON a task: a comment, a closing summary, a progress note (`comments_create`)
- `restivity-task-writing`: CREATING or REWRITING a task: a title that states the fact, then Esempio reale → Causa → Proposta → (Consiglio) → Dove vive → Trovato da → Stima, each section a paragraph that opens with its bold label and a period (`tasks_create` / `tasks_update`)

## Related surfaces

- Agent capabilities & workflows: <https://restivity.ai/.well-known/agent-skills/index.json>
- MCP server card: <https://restivity.ai/.well-known/mcp/server-card.json>
- API catalog: <https://restivity.ai/.well-known/api-catalog>
- Human API docs: <https://restivity.ai/docs/api>
- LLM index: <https://restivity.ai/llms.txt>
