# Authentication

Two Bearer token shapes share the `/api/v1/*` surface. Both go in the `Authorization` header. The server dispatches by token prefix.

```
Authorization: Bearer rr_pat_live_<43-chars>          # personal access token
Authorization: Bearer rrk_live_<43-chars>             # organisation API key
```

## Which token should I use?

| You are… | Use | Why |
| --- | --- | --- |
| A human integrating from a desktop tool (Claude Code, Cursor, an MCP client) | **PAT** | Per-user. Every audit row attributes to you directly. Revoke yours without affecting the rest of the org. |
| A scripted automation acting on the org's behalf (CI, webhook fan-out worker, internal back-office tool) | **Organisation API key** | Org-scoped. The audit log records the owner or admin who minted the key as the responsible person, and names the key on every entry. |
| Both at once | Both | Each route accepts either. Mix as needed. |

PATs work on every plan including Free. Organisation API keys require the Business plan or above (the `api_access` feature flag).

## Personal access tokens

**Mint** - `/account/personal-access-tokens` in the app. Plaintext is shown once at creation, never again. Copy it immediately into your secret store.

**Format** - `rr_pat_live_<43 base64url chars>`. Total 55 characters (the 12-character prefix plus 43). The `_live_` segment marks a production token; a future sandbox token may carry `_test_` or similar in its place, so never assume the segment is constant.

**Expiry** - Chosen when the token is made: 30 days, 90 days (the page offers it first), a year, or no expiry. A token past its expiry answers `401 UNAUTHORIZED` saying the date it expired; make a new one. Only the token's holder learns that: the check runs after the token itself matches. Tokens created before expiry dates were introduced never expire. An expired token no longer counts towards the cap below, and the token page marks it **Expired**.

**Lifecycle** - Tokens are user-scoped. If your account is removed from the organisation, your PATs stop working immediately on the next request. If your role, or your permission on a register, drops below what a write needs (for example your permission on a register your script writes to is lowered from write to read), that write answers `403 FORBIDDEN` with subcode `PAT_OWNER_PERMISSION_REVOKED`. A route reserved for admins, such as `/api/v1/audit-log`, refuses a non-admin's token with a message and no subcode (see [Failure modes](#failure-modes)).

**What a token reads** - A PAT acts as you, with your access. An organisation owner or admin reads every register; a member reads the registers they've been added to, as in the app. Reading another register's data (the register, its risks, their reviews, acceptances and treatment actions) answers `403 FORBIDDEN`, and the register list shows only your registers. An organisation key acts as the person who minted it, with their role now: an admin's key reads every register, and if they have since become a member, it reads only their registers (and a write they no longer have permission for answers `API_KEY_CREATOR_PERMISSION_REVOKED`, below). If that person is removed from the organisation, the key stops working altogether (see [Organisation API keys](#organisation-api-keys)).

**What a token can change** - Each PAT has an access level, chosen when it is made and changeable by its owner at any time on the token page. The page names them **Read only** (`read`), **Ask me first** (`propose`) and **Make changes** (`write`, preselected for a new token):

| Access | What it can do |
| --- | --- |
| `read` (Read only) | Read: every `GET`, including exports |
| `propose` (Ask me first) | Read, plus the saves that wait for a person: a draft save (`POST /api/v1/registers/from-document`), a template start (`POST /api/v1/registers/from-template`), a proposal (`POST /api/v1/registers/{id}/proposals`) and a register an AI model generates (`POST /api/v1/registers/from-context`), which is a draft at every level. A draft counts only once someone adopts it in the app, and a proposal only once someone adopts it (see [Drafts and proposals](https://riskregister.com/docs/drafts-and-proposals.md)). A save can still add categories and controls to the workspace's library, and a generation uses the plan's AI allowance |
| `write` (Make changes) | Everything your role allows, made at once. The saves above still wait for a person: a generated register is a draft at this level too |

A request beyond the token's access is a `403` with subcode `INSUFFICIENT_SCOPE` (`details.required`, `details.granted`), and nothing is changed. The level only narrows: your role and register permissions still apply, so a `propose` token can't propose to a register you can't write to. Its owner can change it at any time, on the token's row; the token's next request uses the new level, and the change is in the audit log (`personal_access_token.access_changed`). An assistant's app keeps the list of tools it was given when it connected, and the server can't update it, so after a change reconnect the app, or start a new conversation, so it sees the tools the new level allows. Tokens created before access levels were introduced are `write`. `GET /api/v1/me` says a token's level (`principal.token.access`), and audit rows the token writes record it (`actor.patAccess`). Organisation API keys have no level yet: they can do whatever their creator's role allows, as `write` can, so a register one generates is live.

**Caps** - Each user can hold up to 20 active PATs per org (revoked and expired ones don't count). The cap is enforced atomically; concurrent mints can't slip past it.

**Switched off by Risk Register** - Personal access tokens are on by default on every plan. Risk Register's own staff can switch them off for one organisation (in response to an incident, for example). While they are off, every token for that organisation answers `403 FORBIDDEN` with a message saying so and no subcode. Only Risk Register can switch them back on: contact support.

## Organisation API keys

**Mint** - `/{orgSlug}/settings/api-keys` in the app. Owner or admin role required. Plaintext is shown once at creation.

**Format** - `rrk_live_<43 base64url chars>`. Total 52 characters.

**Lifecycle** - A key belongs to the organisation, but it acts as the person who minted it, with the role they hold now. If that person's role is lowered, the key keeps reading what they can still read, and a write they no longer have permission for answers `403` with subcode `API_KEY_CREATOR_PERMISSION_REVOKED`. If that person is removed from the organisation, the key stops working: every request answers `401 UNAUTHORIZED`, and an owner or admin must mint a new key. To retire a key yourself, revoke it on the API keys page; a revoked key stays in the list for the record.

**Storage** - Plaintext is never persisted. The server stores a SHA-256 hash of the key, plus its first 16 characters so you can tell keys apart in the list. The comparison on every request is constant-time.

**Plan** - Calling `/api/v1/*` with an organisation API key requires the Business plan or above (the `api_access` feature). On the Free or Team plan, the call answers `403 FORBIDDEN` with a message saying programmatic API access needs the Business plan or above, and subcode `FEATURE_NOT_ENABLED` (`details.feature` is `api_access`). Risk Register's staff can enable organisation keys for one organisation without a plan change, and can also switch programmatic access off for one organisation, which refuses its personal access tokens too.

## Failure modes

Every authentication failure answers the same `401 UNAUTHORIZED`, so a caller cannot use the response to find out which tokens exist. The five ways a token fails:

- Missing `Authorization` header.
- Malformed token (wrong length, wrong prefix, characters outside base64url).
- Unknown token (nothing matches it).
- Revoked token.
- Secret mismatch (the start of the token matches a real one, the rest doesn't).

Two more cases answer `401` as well. An organisation key whose creator has been removed from the organisation looks like any other invalid key. A personal access token past its expiry is the one failure that says more: the message gives the date it expired, and only the token's holder can see it, because the check runs after the whole token has matched.

Tier and permission failures surface as `403 FORBIDDEN`. These carry a stable subcode in `details.subcode` that you can dispatch on (see [Errors](https://riskregister.com/docs/errors.md)):

- `ORG_SUSPENDED` - The organisation has been suspended by Risk Register. Affects every API route.
- `API_KEY_CREATOR_PERMISSION_REVOKED` - The person who minted the organisation key has since had their role or register permission lowered, and the write needs more than they now have.
- `PAT_OWNER_PERMISSION_REVOKED` - The same, for a personal access token: its owner no longer has the permission the write needs.
- `INSUFFICIENT_SCOPE` - The token's access level doesn't allow this request (see What a token can change, above).

Two refusals carry a message and no subcode: a personal access token or connected app while Risk Register has switched tokens or programmatic access off for the organisation, and a non-admin's token on a route reserved for admins (the audit log, webhook endpoints, and writes to the control library and scopes). Dispatch on the 403 status for these. Plan refusals carry a subcode, such as `FEATURE_NOT_ENABLED` for an organisation API key, or for scopes, on a plan below Business: see [Errors](https://riskregister.com/docs/errors.md).

## Pre-auth IP failure budget

Both token shapes share a single per-IP failed-auth counter. After 60 failed attempts from one IP in a 60-second window, the next failure returns `429 RATE_LIMITED` regardless of which token shape is being tried. Successful auth never ticks this counter. This is the bot-mitigation backstop; legitimate traffic from behind a NAT is bounded by the per-token limits (1000 reads/min, 100 mutations/min), not by this counter. See [Rate limits](https://riskregister.com/docs/rate-limits.md).

## CORS

`/api/v1/*` does not emit CORS headers. The API is server-to-server only. Calling it from a browser will fail at the CORS preflight. If you're building a browser app on top of Risk Register, sign requests on your server and proxy from the browser.

## TLS

The API is served over HTTPS only.

## See also

- [Getting started](https://riskregister.com/docs/getting-started.md) - first request with `curl`.
- [Errors](https://riskregister.com/docs/errors.md) - the full 401 / 403 / 429 surface.
- [Rate limits](https://riskregister.com/docs/rate-limits.md) - per-token + per-IP budgets.
