# Authentication — pro.makeup

> pro.makeup uses scoped Bearer tokens for server-to-server access. There is
> no public OAuth flow today; tokens are issued by the operator on request.
> This document describes the format, scopes, and lifecycle. A formal OAuth 2.1
> + PKCE flow is on the roadmap (Phase C / D of the agent-readiness plan in
> `orank.md`).

## Scheme

```
Authorization: Bearer pmk_<id>_<secret>
```

Token format: `pmk_` prefix, 16-character hex id, underscore, 43-character
base64url secret. Tokens are 60+ characters total.

The server verifies an HMAC-SHA256 of the plaintext token against
`api_tokens.token_hash`. Plaintext tokens never round-trip through the
database.

## Available scopes

| Scope                    | What it permits                                                          |
|--------------------------|--------------------------------------------------------------------------|
| `listings:read`          | Read business profiles via `/api/business/by-slug/{slug}`, `/by-phone`, `/{id}` |
| `owner:read`             | Read owner-private fields on a business profile                          |
| `verify:initiate`        | Start a verification flow                                                |
| `verify:send-otp`        | Trigger OTP delivery (SMS / voice / email)                               |
| `verify:confirm`         | Confirm an OTP code                                                      |
| `listing-events:write`   | Emit listing analytics events (`POST /api/listing-events`)               |
| `business:owner-update`  | Update owner-controlled fields on a business profile                     |

Tokens carry an array of scopes. Each protected endpoint requires exactly
one scope; the server returns `403` if the token is valid but missing the
required scope.

## How to request a token

Email **`support@pro.makeup`** with:

- The integration name and use case
- The scopes you need (the smallest subset that works)
- The contact for ongoing communications
- The expected request volume (rough order of magnitude is fine)

You will receive a token **once** — store it securely. It cannot be retrieved
again. If lost, request a fresh one; the lost token will be revoked.

## Lifecycle

- **Issuance:** out-of-band by the operator (see above). Tokens are minted
  with `scripts/mint-api-token.ts` on the server.
- **Expiry:** optional, set at issuance.
- **Revocation:** instant — revoked tokens immediately fail with `401`.
- **Rotation:** request a new token; the old one continues to work until
  revoked, enabling overlap.

## Error responses

### Missing or invalid Bearer token

```http
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="pro.makeup", scope="<required-scope>", resource_metadata="https://pro.makeup/.well-known/oauth-protected-resource"
Content-Type: application/json

{ "error": "Missing bearer token" }
```

The `resource_metadata` parameter follows RFC 9728 and points to a JSON
document describing the resource server (scopes supported, bearer methods,
etc.). See `/.well-known/oauth-protected-resource`.

### Valid token, wrong scope

```http
HTTP/1.1 403 Forbidden
Content-Type: application/json

{ "error": "Missing scope: listings:read" }
```

### Other failures

- `401 "Token expired"` — token's `expires_at` is in the past
- `401 "Token revoked"` — token's `revoked_at` is set
- `401 "Invalid token format"` — token doesn't start with `pmk_`
- `500 "API token verification unavailable"` — server misconfiguration (the
  `API_TOKEN_HMAC_SECRET` env var is missing; not the caller's fault)

## Security notes

- Tokens are HMAC-hashed at rest; the plaintext is shown to the operator
  exactly once at issuance.
- Use only over TLS (`https://pro.makeup`). The server does not accept
  HTTP requests in production.
- Treat tokens like passwords — do not commit them to source control or
  log them in plaintext. The `last_used_at` field is updated server-side on
  every successful verification for audit purposes.

## Roadmap

- **Phase C** (orank.md): publish an OpenAPI 3.1 spec at
  `/api/openapi.json` and a `/developers` portal covering all public
  endpoints with request / response examples.
- **Phase D**: MCP server at `/api/mcp` with hybrid auth (open reads,
  scoped writes). The server-card will reference this same resource
  metadata document.
- **Future:** full OAuth 2.1 + PKCE issuer with self-serve token minting,
  per-client app registration, and short-lived access tokens with refresh.

## Contact

`support@pro.makeup`
