# anyseats auth.md

You are an agent. anyseats (https://anyseats.net) is a ticket marketplace for football,
music and live sport. Searching events and prices needs **no authentication**
at all: GET https://anyseats.net/api/v1/events (OpenAPI at https://anyseats.net/api/v1/openapi.json).

This file is for the one thing that does need auth: reading a **customer's own
orders** on their behalf (https://anyseats.net/api/v1/me/orders). It is read-only —
an agent can never buy tickets or change account details. The flow:
discover → register → claim (the user confirms a code) → exchange → use.

If you can open a browser for the user, standard OAuth 2.0 authorization code
+ PKCE works too — see https://anyseats.net/.well-known/oauth-authorization-server
(dynamic client registration at its `registration_endpoint`). The rest of
this file is the browser-free path.

## Step 1 — Discover

A 401 from the API carries the pointer:

    WWW-Authenticate: Bearer realm="anyseats", resource_metadata="https://anyseats.net/.well-known/oauth-protected-resource/api/v1"

    GET https://anyseats.net/.well-known/oauth-protected-resource/api/v1
    GET https://anyseats.net/.well-known/oauth-authorization-server

The authorization server metadata carries `agent_auth`:

- `identity_endpoint` / `register_uri`: https://anyseats.net/agent/identity
- `claim_endpoint` / `claim_uri`: https://anyseats.net/agent/identity/claim
- `token_endpoint`: https://anyseats.net/oauth/token
- `revocation_endpoint`: https://anyseats.net/oauth/revoke
- `events_endpoint`: https://anyseats.net/agent/event/notify
- `identity_types_supported`: which methods below are enabled right now
- `scopes_supported`: `openid email orders:read`

Resource identifier (use it as `resource` / `aud`): https://anyseats.net/api/v1.

## Step 2 — Pick a method

1. You hold an audience-bound ID-JAG from a provider anyseats trusts (only if
   `identity_types_supported` lists `identity_assertion`) → **identity_assertion**.
2. You know the user's email → **service_auth**. Claim ceremony required.
3. Neither → **anonymous**. You get an assertion immediately, but it grants
   no scopes until a user claims it.

Tell the user what anyseats is and that the agent will be able to read their
orders before you send their email anywhere.

## Step 3 — Register

### service_auth

    POST https://anyseats.net/agent/identity
    Content-Type: application/json

    { "type": "service_auth", "login_hint": "user@example.com", "agent_name": "Your agent's name" }

`agent_name` (optional, ≤80 chars) is shown to the user on the claim page.

Response 200:

    {
      "registration_id": "reg_…",
      "registration_type": "service_auth",
      "claim_url": "https://anyseats.net/agent/identity/claim",
      "claim_token": "clm_…",
      "claim_token_expires": "<24h from now>",
      "post_claim_scopes": ["openid", "email", "orders:read"],
      "claim": { "user_code": "123456", "expires_in": 600, "verification_uri": "https://anyseats.net/auth/sign-in?next=…", "interval": 5 }
    }

No identity_assertion yet — go to Step 4. Keep `claim_token` in memory only.

### anonymous

    POST https://anyseats.net/agent/identity
    Content-Type: application/json

    { "type": "anonymous" }

Response 200:

    {
      "registration_id": "reg_…",
      "registration_type": "anonymous",
      "identity_assertion": "<service-signed JWT>",
      "assertion_expires": "<ISO>",
      "pre_claim_scopes": [],
      "claim_url": "https://anyseats.net/agent/identity/claim",
      "claim_token": "clm_…",
      "claim_token_expires": "<24h from now>",
      "post_claim_scopes": ["openid", "email", "orders:read"]
    }

The assertion exchanges at Step 5 straight away, but `pre_claim_scopes` is
empty: nothing on anyseats is readable without a user. Start the ceremony:

    POST https://anyseats.net/agent/identity/claim
    Content-Type: application/json

    { "claim_token": "clm_…", "email": "user@example.com" }

Response 200: `{ "registration_id", "claim_attempt_id": "cla_…", "status": "initiated", "expires_at", "claim_attempt": { user_code, expires_in, verification_uri, interval } }`.
The email binds the registration — only that signed-in user can complete it.

### identity_assertion (ID-JAG)

Only when `identity_types_supported` includes it. Mint the ID-JAG with
`aud` = https://anyseats.net (or the resource), `email_verified: true`, a fresh `jti`,
`exp` ≈ 5 min, and a real `auth_time` (≤ 1 h old).

    POST https://anyseats.net/agent/identity
    Content-Type: application/json

    { "type": "identity_assertion", "assertion_type": "urn:ietf:params:oauth:token-type:id-jag", "assertion": "<ID-JAG>" }

- 200 `{ registration_id, registration_type, identity_assertion, assertion_expires, scopes }` — clean; go to Step 5.
- 401 `interaction_required` + a `claim` block — the email matches an existing
  anyseats account and the user must confirm the link (Step 4).
- 401 `login_required` + `max_age` — re-authenticate the user at your
  provider and mint a fresh ID-JAG.

## Step 4 — Claim ceremony

Show the user, in one message:

> Open this link, sign in to anyseats (or create an account), and enter this
> 6-digit code on the page: **123456**
> <verification_uri>

The page names your agent (`agent_name`) and says exactly what it can do
(read orders and delivery status). The code goes into that page, never back
to you. It expires after `expires_in` seconds (10 minutes); 5 wrong entries
invalidate it.

Poll the token endpoint no faster than `interval` seconds:

    POST https://anyseats.net/oauth/token
    Content-Type: application/x-www-form-urlencoded

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

- `{ "error": "authorization_pending" }` — keep polling.
- `{ "error": "slow_down" }` — add 5 s to your interval.
- `{ "error": "expired_token" }` — the code expired: call
  POST https://anyseats.net/agent/identity/claim again with the same `claim_token` and
  email for a fresh code; if that returns `410 claim_expired`, restart at Step 3.
- 200 — done:

    {
      "access_token": "<JWT>", "token_type": "Bearer", "expires_in": 3600,
      "scope": "openid email orders:read",
      "identity_assertion": "<service-signed JWT>", "assertion_expires": "<ISO>"
    }

For anonymous registrations this assertion supersedes the pre-claim one; the
pre-claim one and any tokens from it stop working.

## Step 5 — Exchange the assertion

    POST https://anyseats.net/oauth/token
    Content-Type: application/x-www-form-urlencoded

    grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=<identity_assertion>&resource=https://anyseats.net/api/v1

Response 200: `{ "access_token", "token_type": "Bearer", "expires_in": 3600, "scope" }`.
No refresh_token — re-exchange the same assertion until it expires (30 days).
`invalid_grant` means the assertion is expired, revoked or superseded: restart at Step 3.

## Step 6 — Use the token

    GET https://anyseats.net/api/v1/me/orders
    GET https://anyseats.net/api/v1/me/orders/{id}
    GET https://anyseats.net/oauth/userinfo
    Authorization: Bearer <access_token>

Orders carry status, event, seat, delivery type and a `url` — the order page
on anyseats.net where tickets are downloaded. Ticket files are not served
through the API.

## Errors

| code | where | do |
|---|---|---|
| `invalid_request` | /agent/identity, /agent/identity/claim | fix the body (or the ID-JAG: signature, aud, exp, jti, verified email) |
| `issuer_not_enabled` | /agent/identity | your provider is not trusted here — use service_auth or anonymous |
| `interaction_required` (401) | /agent/identity | run the claim ceremony from the response |
| `login_required` (401) | /agent/identity | fresh authentication at your provider, new ID-JAG |
| `invalid_claim_token` | /agent/identity/claim | restart at Step 3 |
| `claimed_or_in_flight` | /agent/identity/claim | already claimed — go to Step 5 with the assertion you hold |
| `claim_expired` (410) | /agent/identity/claim | restart at Step 3 |
| `authorization_pending` / `slow_down` / `expired_token` | /oauth/token (claim) | see Step 4 |
| `invalid_grant` | /oauth/token (jwt-bearer) | restart at Step 3 |
| `invalid_target` | /oauth/token | `resource` must be https://anyseats.net/api/v1 |
| `insufficient_scope` (403) | API | you hold a pre-claim token — complete Step 4 |
| `rate_limited` (429) | any | back off |

## Revocation

- Agent-initiated (RFC 7009): POST https://anyseats.net/oauth/revoke with
  `token=<access_token>&token_type_hint=access_token`. 200, idempotent. Your
  assertion still works — re-run Step 5.
- Provider-initiated (RFC 8935): your provider may POST a Security Event Token
  (`application/secevent+jwt`, event
  `https://schemas.workos.com/events/agent/auth/identity/assertion/revoked`)
  to https://anyseats.net/agent/event/notify; every registration for that user is
  revoked and `/oauth/token` starts returning `invalid_grant`. Restart at Step 3.

Contact: https://anyseats.net/contact-us
