# Authentication

Two ways in: an API key for scripts and your own tools, and OAuth 2.1 for AI assistants and apps that act for a person.
Both are sent the same way and both are bound to one workspace.

## How a credential is sent

```http
Authorization: Bearer <key or token>
```

Only in that header. A key or token in the URL (a query string) is refused with 400 `credentials_in_url` and is never
logged: if you sent one by mistake, revoke it. A missing, unknown, revoked or no longer allowed credential is 401
`invalid_credentials`. A workspace whose plan does not include the API gets 403 `api_not_in_plan`, and a credential
without the scope a request needs gets 403 `insufficient_scope`.

A credential acts as a person, with that person's current role. If they are removed from the workspace, or their role
changes, the next request already reflects it. A revoked key or a disconnected app stops working on its next request.

## Scopes

| Scope | Lets the credential |
|---|---|
| `read` | See brands, channels, posts and media |
| `write` | Create, change and delete drafts, upload media, submit posts for approval, manage webhooks (owner only), take down published posts |
| `offline_access` | OAuth only: stay connected with a refresh token until the app is disconnected |

`write` also needs a person whose role can edit. There is no scope that approves. Taking down a published post needs the
approver role as well.

## API keys

Workspace owners make keys in the Postaki app, under Settings, "For developers", "API keys and webhooks", "API keys". A key has a name and one
or both of `read` and `write`.

- Format: `pk_live_<8 characters>_<32 characters>`.
- Shown once. Postaki keeps only a hash (SHA-256) and the 8-character prefix that you see in the list.
- Revoke it there and it stops at once. Whatever uses it gets 401.
- Use one key per tool, so you can revoke one without breaking the others.

Keep keys out of source control, browsers and logs.

## OAuth 2.1 for assistants and apps

Postaki is an OAuth 2.1 authorization server. MCP clients find everything from the metadata, so a person only pastes
the server address (see [Agent setup](https://postaki.io/docs/agent-setup/)). If you build an app, this is what it needs.

| Document or endpoint | Address |
|---|---|
| Authorization server metadata (RFC 8414) | `https://app.postaki.io/.well-known/oauth-authorization-server` |
| Protected resource, MCP server (RFC 9728) | `https://app.postaki.io/.well-known/oauth-protected-resource/mcp` |
| Protected resource, REST API | `https://app.postaki.io/.well-known/oauth-protected-resource/api/v1` |
| Authorization | `https://app.postaki.io/oauth/authorize` |
| Token | `https://app.postaki.io/oauth/token` |
| Revocation | `https://app.postaki.io/oauth/revoke` |
| Dynamic client registration | `https://app.postaki.io/oauth/register` |

How the flow works:

- Authorization code flow with **PKCE, method S256 only**. The clients are public (no client secret).
- Scopes: `read`, `write`, `offline_access`.
- The **`resource`** parameter (RFC 8707) says what the token is for: `https://app.postaki.io/api/v1` for the REST API or
  `https://app.postaki.io/mcp` for the MCP server. A token is bound to exactly one of them and each side refuses the
  other's. Without `resource`, a token is for the REST API; the MCP server never accepts such a token.
- The authorization response carries `iss` (RFC 9207) and the metadata says so
  (`authorization_response_iss_parameter_supported`). Check it.
- The person signs in to Postaki, sees the app's name and where it returns to, chooses the workspace to give access to
  and the scopes, and is told that approving posts stays in Postaki. Only workspaces whose plan includes the API are
  offered.
- Access tokens last 1 hour. Refresh tokens last 30 days after the access token expires and are rotated: each refresh
  gives a new one. Using an old refresh token again ends the whole connection (`invalid_grant`).
- A connection ends for good after 90 days, and the app asks for consent again.
- The token endpoint takes form-encoded bodies.

A person sees and disconnects connected apps under Settings, "For developers", "API keys and webhooks", "Connected apps".

### Verified apps and self-registered apps

- **Verified apps** use a Client ID Metadata Document: the `client_id` is an https URL that serves the app's metadata.
  Postaki fetches it (public https addresses only, no redirects) and caches it for an hour. The metadata advertises
  `client_id_metadata_document_supported: true`. The consent page says "Verified by" and the host, and posts the app
  makes are labelled with its name and that host.
- **Self-registered apps** use dynamic client registration (`POST /oauth/register`, public clients only, a limited
  number of registrations an hour per address). The consent page and the post label show only the host the app returns to,
  never the name it chose. A self-registered client with no live connection is deleted after 30 days.

Redirect URIs must be https, or plain http on a loopback address (`localhost`, `127.0.0.1`, `::1`) with any port.

**Reserved names.** A self-registered app cannot call itself by a well-known name: names containing Claude, Anthropic,
ChatGPT, OpenAI, Cursor or Postaki are refused unless every redirect URI is on a loopback address or on that company's
own hosts (`claude.ai`, `claude.com`, `chatgpt.com`, `openai.com`, `cursor.com`). Postaki is never allowed. Names are
printable ASCII, 60 characters at most. This stops an app from posing as an assistant a person trusts.

## Which one to use

- A script, a cron job, Zapier-style tool, Claude Code or Cursor on your own machine: an API key.
- Claude on the web or desktop, ChatGPT, or anything that other people connect to their own account: OAuth. ChatGPT does
  not accept API keys at all.
