# OAuth API

The OAuth API: trade codes for app tokens, refresh and revoke them, with every endpoint's fields, who may call it, its rate limit and a curl example.

## Get the OAuth server's metadata. Authorization server metadata (RFC 8414): where to send people to allow an app, where to trade codes and revoke tokens, and what's supported. OAuth's own shape, not the envelope.

`GET /.well-known/oauth-authorization-server`

Needs no token. Rate limit: 120 a minute, per address. An app calls it with its client id, not an access token.

### Response

It answers in OAuth's own shape, not the envelope.

```bash
curl 'https://api.hematite.gg/v1/.well-known/oauth-authorization-server'
```

Example `data`:

```json
{
  "dataKey": null
}
```

## Revoke a token. OAuth token revocation (RFC 7009): an app gives up a token it holds. A refresh token takes its whole grant with it, an access token only itself. Answers 200 whether or not the token was valid, so it can't be used to test tokens.

`POST /oauth/revoke`

Needs no token. Rate limit: 30 a minute, per address. An app calls it with its client id, not an access token.

### Form (`application/x-www-form-urlencoded`)

| Field | Type | Required | Notes |
|---|---|---|---|
| `token` | `string` | required | The token to give up: a refresh token revokes its whole grant, an access token only itself. |
| `token_type_hint` | `enum` |  | Which kind it is, to look there first. One of `refresh_token`, `access_token`. |
| `client_id` | `string` | required | The app's id. A confidential app may send it in HTTP Basic instead, with its secret. |
| `client_secret` | `string` |  | A confidential app's secret, when it doesn't use Basic. An app in a browser or on a phone has none and sends none. |

### Response

It answers in OAuth's own shape, not the envelope.

```bash
curl -X POST 'https://api.hematite.gg/v1/oauth/revoke' \
  --data-urlencode "token=$REFRESH_TOKEN" \
  --data-urlencode 'token_type_hint=refresh_token' \
  --data-urlencode 'client_id=your-app'
```

Example `data`:

```json
{}
```

## Trade a code or a refresh token for tokens. OAuth 2.1's token endpoint: an authorization code (with its PKCE verifier) or a refresh token for an access token and the next refresh token. Send it form-encoded as OAuth does (JSON works too); it answers in OAuth's shape (access_token, error), not the envelope. A code or refresh token used twice revokes every token of its grant, so a client keeps one refresh in flight at a time: across browser tabs, hold a lock while refreshing (the Web Locks API, navigator.locks.request) or let one tab refresh and hand the new tokens to the others (a BroadcastChannel). One leeway, for a tab that missed the new tokens by a moment: a refresh token shown again by its own app within 30 seconds of its first use is exchanged once more, for another pair; the grant stays, and the refresh token its first use gave stays valid too. A third use, or a second one later than that, revokes the grant.

`POST /oauth/token`

Needs no token. Rate limit: 20 a minute, per address. An app calls it with its client id, not an access token.

### Form (`application/x-www-form-urlencoded`)

| Field | Type | Required | Notes |
|---|---|---|---|
| `grant_type` | `enum` | required | Authorization_code to trade a code, refresh_token for the next tokens. One of `authorization_code`, `refresh_token`. |
| `client_id` | `string` | required | The app's id. A confidential app may send it in HTTP Basic instead, with its secret. |
| `client_secret` | `string` |  | A confidential app's secret, when it doesn't use Basic. An app in a browser or on a phone has none and sends none. |
| `code` | `string` |  | With authorization_code: the code from the redirect. It works once, within a minute. |
| `redirect_uri` | `string` |  | With authorization_code: the redirect_uri the authorization request had, exactly. |
| `code_verifier` | `string` |  | With authorization_code: the PKCE verifier whose SHA-256 was the code_challenge. |
| `refresh_token` | `string` |  | With refresh_token: the latest refresh token. Each works once, and once more within 30 seconds of that for a tab that missed the new one; any other second use revokes the grant. |
| `scope` | `string` |  | With refresh_token: fewer scopes for the new access token, space-separated; never more than were allowed. |

### Response

It answers in OAuth's own shape, not the envelope.

| Field | Type | Present | Notes |
|---|---|---|---|
| `access_token` | `string` |  |  |
| `token_type` | `"Bearer"` |  |  |
| `expires_in` | `number` |  | Seconds until the access token ends. |
| `refresh_token` | `string` |  |  |
| `scope` | `string` |  | Space-separated, as granted. |

```bash
curl -X POST 'https://api.hematite.gg/v1/oauth/token' \
  --data-urlencode 'grant_type=authorization_code' \
  --data-urlencode "code=$CODE" \
  --data-urlencode 'redirect_uri=https://yourapp.example/callback' \
  --data-urlencode "code_verifier=$CODE_VERIFIER" \
  --data-urlencode 'client_id=your-app'
```

Example `data`:

```json
{
  "access_token": "string",
  "token_type": "Bearer",
  "expires_in": 0,
  "refresh_token": "string",
  "scope": "string"
}
```
