# auth.md

How an AI agent registers with **Melatone Skin Clinic** (Battersea, London), gets a credential tied to the person it acts for, uses it, and how that access is withdrawn.

Everything public (treatments, prices, clinic details, pages, the MCP server at `/api/mcp`) needs no credential at all. A credential is only needed to **act for a person**:

| Scope | What it allows | What it never allows |
|---|---|---|
| `clinic:read` | Read the catalogue, prices, clinic details | Anything else |
| `appointments:request` | Send the clinic an appointment request for the person | Booking a time: the clinic confirms every time with the person directly |
| `checkout:write` | Build a skincare basket and get a secure Stripe checkout link | Paying: the person enters their address and pays on Stripe; the agent never sees card details |

There are no customer accounts or passwords on this site, so an agent never asks a person for one.

## Step 1: Discover

1. Protected resource metadata (RFC 9728): `GET https://www.melatoneskinclinic.co.uk/.well-known/oauth-protected-resource`
2. Authorization server metadata (RFC 8414): `GET https://www.melatoneskinclinic.co.uk/.well-known/oauth-authorization-server`

Any protected endpoint called without a token answers `401` with
`WWW-Authenticate: Bearer resource_metadata="https://www.melatoneskinclinic.co.uk/.well-known/oauth-protected-resource"`, so a client can start from the call it wanted to make.

## Step 2: Pick a method

| You are | Method | Grant |
|---|---|---|
| An agent acting for one person who is present (assistant, browser agent, MCP or A2A client) | Person approves on a consent page | `authorization_code` with PKCE (S256), plus `refresh_token` |
| A platform acting for its own users under its own terms (a shopping or booking platform) | Service credential | `client_credentials` |

Prefer the first: the person sees exactly what you will be able to do and says yes themselves.

## Step 3: Register

Dynamic client registration (RFC 7591). No account, no API key, no waiting.

```http
POST /api/oauth/register
Content-Type: application/json

{
  "client_name": "Example Assistant",
  "redirect_uris": ["https://assistant.example/callback"],
  "grant_types": ["authorization_code", "refresh_token"],
  "token_endpoint_auth_method": "none",
  "scope": "clinic:read appointments:request checkout:write",
  "client_uri": "https://assistant.example",
  "contacts": ["ops@assistant.example"]
}
```

`201` returns your `client_id` (valid for one year). `client_name` is shown to the person on the consent page, so use the name they know you by.

* Native and command-line agents may register `http://127.0.0.1/callback`; any port is accepted at authorization time (RFC 8252).
* For `client_credentials`, register `"grant_types": ["client_credentials"]` with `"token_endpoint_auth_method": "client_secret_basic"` (or `client_secret_post`). The response then carries a `client_secret`. Keep it server side.

## Step 4: Get the person's approval

Send the person to:

```
GET /api/oauth/authorize?response_type=code
  &client_id=<client_id>
  &redirect_uri=<registered redirect uri>
  &scope=appointments:request%20checkout:write
  &state=<random>
  &code_challenge=<BASE64URL(SHA256(code_verifier))>
  &code_challenge_method=S256
  &resource=https://www.melatoneskinclinic.co.uk
```

They see who is asking and each permission in plain English, then choose **Allow** or **Do not allow**. You receive `?code=…&state=…&iss=https://www.melatoneskinclinic.co.uk` (check `iss`, RFC 9207). A refusal returns `error=access_denied`. The code lasts 5 minutes and works once.

## Step 5: Exchange for tokens

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

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

Returns `access_token` (Bearer, 1 hour), `refresh_token` (7 days, rotated on each use) and the granted `scope`.

Service clients: `grant_type=client_credentials&scope=…` with their client secret. No refresh token; ask again when it expires.

## Step 6: Use the access token

`Authorization: Bearer <access_token>` on:

| Action | Endpoint | Scope |
|---|---|---|
| Appointment request | `POST /api/agent/appointments` | `appointments:request` |
| Checkout (Universal Commerce Protocol) | `/api/ucp/v1/checkout-sessions` (profile: `/.well-known/ucp`) | `checkout:write` |
| Clinic agent (A2A v1.0, JSON-RPC) | `POST /api/a2a` (card: `/.well-known/agent-card.json`) | per skill |

**Appointment request** body: `first_name`, `phone` or `email`, `consent_to_contact: true` (set it only after the person agreed that the clinic may contact them about this request), and optionally `treatment` (a slug from `/api/v1/treatments`), `preferred_times`, `notes`. Answer: `202` with a reference. The clinic contacts the person to confirm a time, usually the same day in opening hours (open every day except Wednesday). Nothing is booked or charged until they confirm.

**Checkout**: line items use product ids from `GET /api/ucp/v1/products` (the same ids as the clinic's Google Merchant feed). Each session answers `requires_escalation` with a `continue_url`: send the person there to enter their UK delivery address and pay on Stripe. `GET` the session afterwards to see `completed` and the order link. Prices are fixed by the clinic, identical to the website.

## Errors

| Status | Meaning | What to do |
|---|---|---|
| `401 invalid_token` | Missing, expired or invalid token | Refresh, or start again at Step 4 |
| `403 insufficient_scope` | Token lacks the scope named in `WWW-Authenticate` | Ask the person to approve that scope |
| `400 invalid_request` | A field needs fixing | The `errors` list names each field |
| `429` | Too many requests | Wait for `Retry-After` |
| `invalid_client` at the token endpoint | Registration expired or blocked | Register again; if blocked, email the clinic |

## Revocation

* Access tokens expire after **1 hour** and authorization codes after **5 minutes**, so access ends on its own when an agent stops refreshing.
* A person can withdraw an agent's access at any time by emailing **hello@melatoneskinclinic.co.uk** with the agent's name. The clinic blocks that agent's registration, normally within the hour in opening hours; from then on its access tokens, refresh tokens and registration all stop working.
* An agent that wants to give up access simply discards its tokens; nothing is kept on the clinic side.
* Personal details sent with an appointment request are handled under the clinic's privacy policy: https://www.melatoneskinclinic.co.uk/privacy

## Contact

Agent developers: hello@melatoneskinclinic.co.uk. Developer docs: https://www.melatoneskinclinic.co.uk/developers
