# Access tokens and the claims hook

The API accepts an access token from one authorisation server in place of an
API key. This page is for whoever operates the deployment: what has to be
configured, what the authorisation server is told, and the one route that
exists for it.

## What is accepted

Set `SKERRY_OAUTH_ISSUER` to the authorisation server's issuer and a bearer
value shaped like a JWT is read as an access token (`lib/oauth.ts`). It must
be signed by a key the issuer publishes, have `typ: at+jwt`, name this API in
`aud`, live no longer than `SKERRY_OAUTH_MAX_TOKEN_SECONDS` (an hour unless
set), and carry:

| claim | meaning |
|---|---|
| `skerry_partner` | the partner the token acts in |
| `skerry_mode` | `sandbox` or `live` |
| `skerry_agent` | optional: the registered agent the caller acts as |
| `scope` | the scopes, space separated; `*` is ignored |
| `cnf.jkt` | optional: the token is bound to a key and each call needs a DPoP proof |

`GET /.well-known/oauth-protected-resource` (RFC 9728) names the
authorisation server in `authorization_servers`, this API's resource
identifier, and every scope a token may carry. With no issuer configured it
answers 404 and the API takes API keys only, as before.

### Resource identifiers

A token is issued for one resource, and its `aud` must contain a resource
identifier this API answers to. By default that is the API's own origin.
`SKERRY_OAUTH_RESOURCES` is a comma-separated list of them, for an API that
is reached at more than one address:
the MCP server is a second address in front of the same API, and the MCP
specification requires a token used there to be issued for the MCP server's
own address. With

```text
SKERRY_OAUTH_RESOURCES=https://api.skerry.xyz,https://mcp.skerry.xyz
```

a token whose `aud` holds either address is accepted, and nothing else is.
The agent tools' identifier is the bare host, `https://mcp.skerry.xyz`, with
no path: it is the `resource` their own metadata document announces.
The first identifier in the list is the one the API names for itself.

The identity provider must register each identifier as a resource, and
issue a token for exactly the one the client asked for with the `resource`
parameter (RFC 8707). The claims endpoint below answers for any identifier
on the list and refuses others with `invalid_target`.

RFC 9728 gives a metadata document one `resource`, the address it is
published for. The document served here names the API's own identifier in
`resource` and lists every accepted identifier in
`skerry_accepted_resources`, an additional member. An MCP server publishes
its own metadata document at its own address, naming itself as `resource`
and the same authorisation server.

A DPoP proof is checked against the API's own origin (`SKERRY_API_ORIGIN`),
whichever resource the token was issued for.

The metadata the API reads from the authorisation server is `issuer` and
`jwks_uri`, from `/.well-known/openid-configuration` or
`/.well-known/oauth-authorization-server`. Keys of type RSA (RS256), P-256
(ES256) and Ed25519 (EdDSA) are used.

## The claims hook

`POST /oauth/claims` is called by the authorisation server, not by partners,
and is not part of the partner API. Like the card processor's event routes
under `/hooks`, it is authenticated by a signature instead of a key and is
left out of the reference.

The authorisation server does not know what a Skerry partner is. Each time it
is about to issue a token for this API, and when it draws its consent screen,
it asks here who the token is for and puts the answer in the token.

**Configuration.** `SKERRY_OAUTH_HOOK_SECRET`, the same value the
authorisation server signs with. Unset, the route answers 503 and no token
can be issued for this API.

**Request.** The header `Praecise-Signature: t=<ms>,v1=<hex>` carries the
HMAC-SHA256, keyed with the secret, of `<t>.<raw body>`: the scheme of this
API's own webhooks. `t` must be within five minutes of the API's clock.

```json
{
  "account": { "id": "…", "email": "person@example.com", "email_verified": true },
  "client_id": "https://host.example/oauth/client.json",
  "scopes": ["payments:read", "customers:read"],
  "resource": "https://api.skerry.xyz",
  "requested": { "skerry_partner": "…", "skerry_mode": "sandbox" }
}
```

`account` is `null` when a client acts alone, and `requested.skerry_agent`
then says which agent it acts as. `resource` must be one of this API's
resource identifiers.

**Answer.**

| status | body | when |
|---|---|---|
| 200 | `{ claims, scopes, label, requested }` | the token may be issued with these claims and scopes |
| 200 | `{ choices: [{ label, detail, requested }] }` | the person can act for more than one partner or environment and the request did not say which |
| 400 | `{ error: "invalid_request" \| "invalid_target", error_description }` | the request is not a claims request, or is for another resource |
| 401 | `{ error: "invalid_signature", … }` | the signature or its timestamp was not accepted |
| 403 | `{ error: "access_denied", error_description }` | a refusal; the description says why |
| 503 | `{ error: "temporarily_unavailable", … }` | no secret is configured |

Every answer also carries `request_id`.

**How it decides** (`lib/pure/oauth-claims.ts`):

- A request with `requested.skerry_partner` equal to `personal` (or to the
  id of Skerry's own developer account, which a renewal sends back) is a
  person asking as themselves (personal.md). The address must
  be verified; no roster is consulted. The answer is always claims, never
  choices: Skerry's own account, and live where the platform and that
  account are open for it, otherwise sandbox. Scopes are the ones asked for
  that a person may hold (`PERSON_SCOPES`). Such a token is accepted at
  `/personal/...` only.
- Otherwise a person is the console member whose email address equals the account's,
  ignoring case. The address must be verified and the member not disabled.
- They may act for the active partners of their account: in sandbox, and in
  live only where a live key could be minted, which is the partner verified
  and approved for live, and live open on the platform.
- One possibility is answered with claims. More than one is answered with the
  choices, unless `requested` narrows it to one.
- Scopes are the ones asked for that this API knows, never `*`. A member with
  the role `member` is not given `keys:write`, `agents:write`,
  `policies:write`, `approvals:write` or `controls:write`.
- `skerry_agent` is put in a token only for a client the agent's record
  lists, and the scopes are then narrowed to that approval's.

## Clients that act as an agent

An agent that runs with no person present uses the authorisation server's
client credentials grant. It is refused unless the agent's record approves
that client:

```text
POST /agents/{id}/oauth-clients     { "client_id": "ledger-agent", "scopes": ["payments:read"] }
DELETE /agents/{id}/oauth-clients?client_id=ledger-agent
```

Both need `agents:write`. `scopes` is the most a token for that client and
agent may carry: only scopes the calling key holds, never `*`. Approving a
client again replaces its scopes. An agent's own credential may not approve
or withdraw a client. Approvals are recorded per authorisation server, in
`agent_oauth_client` (`db/023_oauth.sql`), and each change is in the audit
trail.

A token issued this way is the agent on every call: it is subject to the
agent's status, and it can only move money under a spend policy.

## Rolling it out

1. Apply `db/023_oauth.sql`.
2. Set `SKERRY_OAUTH_ISSUER` and `SKERRY_OAUTH_HOOK_SECRET`.
3. On the authorisation server, register this API's origin as the resource
   and set the same secret. Where tokens are also issued for an MCP server
   in front of the API, set `SKERRY_OAUTH_RESOURCES` to both addresses and
   register each as a resource.

A deployment that sets none of this behaves exactly as it did.
