# auth.md

PFoS-iD (https://id.pfos.school) supports agentic registration. The resource
server and the authorization server are both https://id.pfos.school.
Treat https://id.pfos.school/.well-known/oauth-protected-resource as
authoritative if anything here conflicts with it.

PFoS-iD issues no credentials before a human approves the claim. Every agent ID has a human owner and a registered public key.

## Step 1 — Discover

- Protected resource metadata: https://id.pfos.school/.well-known/oauth-protected-resource
- Authorization server metadata: https://id.pfos.school/.well-known/openid-configuration (look at `agent_auth`)
- Registration endpoint: `POST https://id.pfos.school/agent/identity`
- Claim endpoint: `POST https://id.pfos.school/agent/identity/claim`
- Token endpoint: `POST https://id.pfos.school/oauth2/token`

## Step 2 — Pick a method

- `service_auth`: you know the email address of the person who will own the agent. Send it as `login_hint`. PFoS-iD returns a code for that person right away.
- `anonymous`: you do not know the owner yet. Register first, then name the owner at the claim endpoint (Step 4).
- `identity_assertion` (bring your own assertion) is not supported.

Use only a method listed in `identity_types_supported` in `agent_auth`. The operator can turn a method off: it then leaves the metadata, and the registration endpoint answers 403 (`anonymous_not_enabled` or `service_auth_not_enabled`). If the metadata has no `agent_auth`, registration with auth.md is turned off.

## Step 3 — Register

Create a key pair (EC P-256 for ES256, or Ed25519 for EdDSA) and keep the private key. Send only the public JWK. A public key is required: PFoS-iD binds every token to it.

```http
POST /agent/identity HTTP/1.1
Host: id.pfos.school
Content-Type: application/json

{
  "type": "service_auth",
  "login_hint": "owner@example.com",
  "agent": {
    "name": "helper",
    "display_name": "Calendar helper",
    "purpose": "Summarize calendar events into short links",
    "requested_scopes": [
      "me:read"
    ],
    "modes": [
      "self"
    ],
    "jwk": {
      "kty": "EC",
      "crv": "P-256",
      "x": "…",
      "y": "…",
      "kid": "run-1"
    },
    "links": {
      "website": "https://example.com/helper"
    }
  }
}
```

- `name` follows the PFoS-iD @handle rules (a-z, 0-9 and _, starting with a letter). The full name is `@<owner handle>/<name>`.
- `display_name`: 1-50 characters. `purpose`: 1-200 characters.
- `modes`: `self` (the agent acts as itself) and/or `delegate` (the agent acts for its owner through the device authorization grant). An auth.md registration always includes `self`.
- `links` (optional): `a2a_card`, `mcp`, `website`, `web_bot_auth_directory` (https URLs). They appear on the public profile.
- Limits: 10 registrations per hour per IP address, 5 per day per `login_hint`, and 5 waiting registrations per person.

Scopes an agent ID can get:

- `openid`: Confirm that you signed in with PFoS-iD. (delegate mode only)
- `profile`: Read your display name and @handle. (delegate mode only)
- `email`: Read your email address. (delegate mode only)
- `offline_access`: Stay connected while you are away (refresh tokens). (delegate mode only)
- `me:read`: Read your PFoS-iD profile (handle, display name).
- `agents:read`: Read your agent IDs and connected agents.
- `links:read`: Read your link history on s.pfos.school, the PFOS short URL service.
- `links:write`: Create short links on s.pfos.school in your name, and remove links from your history.

`service_auth` response (200):

```json
{
  "registration_id": "6f0c…",
  "registration_type": "service_auth",
  "claim_token": "ct_…",
  "claim_token_expires": "2026-11-14T12:00:00Z",
  "post_claim_scopes": [
    "me:read"
  ],
  "claim": {
    "user_code": "482913",
    "expires_in": 600,
    "verification_uri": "https://id.pfos.school/claim?claim_attempt_token=ca_…",
    "interval": 5
  }
}
```

`anonymous` response (200). `pre_claim_scopes` is always empty:

```json
{
  "registration_id": "6f0c…",
  "registration_type": "anonymous",
  "pre_claim_scopes": [],
  "claim_url": "https://id.pfos.school/agent/identity/claim",
  "claim_token": "ct_…",
  "claim_token_expires": "2026-11-14T12:00:00Z",
  "post_claim_scopes": [
    "me:read"
  ]
}
```

The `claim_token` is returned only once. The whole registration expires after 24 hours.

## Step 4 — Claim ceremony

For `anonymous`, name the owner first (for `service_auth`, call this only to get a new code after the old one expired):

```http
POST /agent/identity/claim HTTP/1.1
Host: id.pfos.school
Content-Type: application/json

{
  "claim_token": "ct_…",
  "email": "owner@example.com"
}
```

```json
{
  "registration_id": "6f0c…",
  "claim_attempt_id": "a1b2…",
  "status": "initiated",
  "expires_at": "2026-11-13T12:10:00Z",
  "claim_attempt": {
    "user_code": "482913",
    "expires_in": 600,
    "verification_uri": "https://id.pfos.school/claim?claim_attempt_token=ca_…",
    "interval": 5
  }
}
```

Show `verification_uri` and `user_code` to the human. The human signs in to PFoS-iD, checks the request and enters the code. PFoS-iD does not email the code.
The code is valid for 10 minutes and accepts 5 wrong tries. If it expires, call the claim endpoint again within the registration window.

Poll the token endpoint every 5 seconds or slower, with a DPoP proof signed by the key you registered:

```http
POST /oauth2/token HTTP/1.1
Host: id.pfos.school
Content-Type: application/x-www-form-urlencoded
DPoP: eyJ…

grant_type=urn:workos:agent-auth:grant-type:claim&claim_token=ct_…
```

| Result | Response |
| --- | --- |
| Waiting for the human | 400 `{"error": "authorization_pending"}` |
| Polling too fast | 400 `{"error": "slow_down"}` (add 5 seconds to your interval) |
| The code expired | 400 `{"error": "expired_token"}` (call the claim endpoint again) |
| The registration expired | 400 `{"error": "invalid_grant"}` |
| The human did not approve | 400 `{"error": "access_denied"}` |
| Approved | 200 (below) |

```json
{
  "access_token": "…",
  "token_type": "DPoP",
  "expires_in": 600,
  "scope": "me:read",
  "identity_assertion": "eyJ…",
  "assertion_expires": "2027-11-13T12:00:00Z",
  "agent_id": "0b7e…",
  "client_id": "0b7e…",
  "agent_name": "owner_handle/helper"
}
```

The approved response is returned once. Keep the `identity_assertion`: it is your credential.

## Step 5 — Exchange the assertion

Access tokens last 10 minutes and there is no refresh token. When one expires, exchange the assertion again (jwt-bearer grant), with a DPoP proof signed by the same key:

```http
POST /oauth2/token HTTP/1.1
Host: id.pfos.school
Content-Type: application/x-www-form-urlencoded
DPoP: eyJ…

grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=eyJ…&resource=https://id.pfos.school
```

- The assertion is a JWT signed by PFoS-iD (`typ: agent-identity+jwt`, ES256, keys at https://id.pfos.school/keys/site-jwks.json). It lasts at most 365 days.
- It works only while the agent and its owner are active. An agent expires after 180 days without getting a token (the owner may set a shorter limit).
- `resource` is `https://id.pfos.school` (account API) or `https://id.pfos.school/mcp` (MCP server). Without it you get a token for `https://id.pfos.school`.
- For s.pfos.school (the PFOS short URL service), ask for `resource=https://s.pfos.school` with `links:read` or `links:write` (the owner must have allowed the scope). That token carries only the app's scopes, so get a separate token for the PFoS-iD APIs.
- For the s.pfos.school QR code designer (s.pfos.school/qr), ask for `resource=https://s.pfos.school/qr` with `links:write` (the owner must have allowed the scope). Its tokens work only for the two calls of the QR code designer, POST /api/v1/public/links and POST /api/v1/qr; use a token for https://s.pfos.school for anything else. That token carries only the app's scopes, so get a separate token for the PFoS-iD APIs.

## Step 6 — Use the access_token

Call the account API (https://id.pfos.school/api/openapi.json) or the MCP server with `Authorization: DPoP <access_token>` and a fresh DPoP proof that has `ath`. For example:

```http
GET /api/v1/me HTTP/1.1
Host: id.pfos.school
Authorization: DPoP eyJ…
DPoP: eyJ…
```

Self-mode tokens carry `principal: "agent"`, `agent_id` and `owner`. Delegate-mode tokens carry `principal: "human"` (the owner) and `act` (the agent).

## Keys and DPoP

PFoS-iD extension:

- Accepted keys: EC P-256 (ES256) and Ed25519 (EdDSA) public JWKs. Never send private key members (`d`).
- An agent can have up to 5 keys. Use a different `kid` for each running instance so the owner can tell them apart. The owner adds and removes keys on PFoS-iD.
- DPoP (RFC 9449) is required for every agent token request. Proofs are valid for 5 minutes and each `jti` can be used once.
- An agent created by its owner on PFoS-iD gets an enrollment code (`ec_…`, 10 minutes, one use). Send it with your public JWK to `POST https://id.pfos.school/agent/enroll` `{"enrollment_code": "ec_…", "jwk": {…}}`, then use `client_credentials` with `private_key_jwt` (`aud` must be `https://id.pfos.school`) and DPoP.
- Delegate mode uses the device authorization grant (`POST https://id.pfos.school/oauth2/device/code` with `private_key_jwt`). Only the owner can approve it at https://id.pfos.school/device.

## Errors

| Where | Errors |
| --- | --- |
| `/agent/identity` | `invalid_request`, `anonymous_not_enabled`, `service_auth_not_enabled`, `invalid_jwk`, `rate_limited` (429, with Retry-After) |
| `/agent/identity/claim` | `invalid_request`, `invalid_claim_token`, `claimed_or_in_flight` (409), `claim_expired` (410), `rate_limited` (429) |
| `/oauth2/token` | `invalid_request`, `invalid_grant`, `invalid_client`, `unsupported_grant_type`, `authorization_pending`, `slow_down`, `expired_token`, `access_denied`, `invalid_dpop_proof`, `rate_limited` (429) |

Error bodies are JSON: `{"error": "…", "error_description": "…"}`.

## Revocation

- Revoke access tokens with RFC 7009 at `POST https://id.pfos.school/oauth2/revoke`.
- The owner can suspend or delete the agent at any time. Then tokens stop working at PFoS-iD within a minute, and the assertion can no longer be exchanged.

## Rules for agents

- PFoS-iD issues no credentials before a human approves the claim.
- Never ask a human for their PFoS-iD email login code.
- Agents cannot manage accounts (email, @ID, login methods, other agents).
- An agent ID cannot get `agents:register`, so it cannot create other agent IDs.
