# Ajel auth.md

> عاجل — دليل تسجيل الوكلاء الآليين. This document tells software agents how to authenticate to the Ajel (ajel.sa) news API and MCP server.

You are an agent. Ajel is a Saudi Arabic news publisher. Everything here is **public, read-only news**. Authentication is **optional**: it only moves you to a higher rate-limit tier. There are no user accounts, no personal data, and no write operations behind these credentials.

## What is protected

| Resource | URL | Without a token | With a `news:read` token |
| --- | --- | --- | --- |
| Read API (OpenAPI: https://ajel.sa/api/agents/openapi.json) | https://ajel.sa/api/agents/v1/* | Works, low per-IP rate limit | Higher per-client rate limit |
| MCP server (Streamable HTTP) | https://ajel.sa/api/agents/mcp | Works, low per-IP rate limit | Higher per-client rate limit |

The only scope is `news:read` (read public news). Human-readable API docs: https://ajel.sa/api/agents/docs

If you only need a few requests, **skip everything below** and call the API anonymously.

## Step 1 — Discover

- Protected Resource Metadata (RFC 9728): https://ajel.sa/.well-known/oauth-protected-resource
  - MCP-specific: https://ajel.sa/.well-known/oauth-protected-resource/api/agents/mcp
  - Read-API-specific: https://ajel.sa/.well-known/oauth-protected-resource/api/agents/v1
- Authorization Server Metadata (RFC 8414): https://ajel.sa/.well-known/oauth-authorization-server
  - `issuer`: https://ajel.sa
  - The `agent_auth` block in that document lists the registration endpoints below.

## Step 2 — Pick a method

1. **You are an autonomous agent with no human in the loop** → [anonymous registration](#step-3--register-anonymous) (one POST, no email, no human).
2. **You are an OAuth/MCP client app** (e.g. an MCP host acting for a person) → [OAuth authorization code + PKCE](#oauth-client-registration-rfc-7591).
3. **You are a backend service** → register a confidential client and use [client_credentials](#oauth-client-registration-rfc-7591).

Supported `agent_auth.identity_types_supported`: `anonymous` only. `identity_assertion` (ID-JAG) returns `issuer_not_enabled`; `service_auth` returns `service_auth_not_enabled`.

## Step 3 — Register (anonymous)

```http
POST https://ajel.sa/api/agents/auth/register
Content-Type: application/json

{ "type": "anonymous" }
```

Response (200):

```json
{
  "registration_id": "reg_...",
  "registration_type": "anonymous",
  "identity_assertion": "<service-signed JWT>",
  "assertion_expires": "<ISO time, 24 hours>",
  "pre_claim_scopes": ["news:read"],
  "credential_type": "access_token",
  "credential": "<access token, 1 hour>",
  "credential_expires": "<ISO time>",
  "scopes": ["news:read"],
  "token_type": "Bearer",
  "token_endpoint": "https://ajel.sa/api/agents/oauth/token",
  "claim_url": "https://ajel.sa/api/agents/auth/claim",
  "claim_token": "<opaque, 24 hours, single use>",
  "claim_token_expires": "<ISO time>",
  "post_claim_scopes": ["news:read"]
}
```

- `credential` is a ready-to-use access token. Send it as described in Step 6.
- To get another access token later, exchange `identity_assertion` (Step 5). It can be reused until it expires.
- Rate limit: registration is limited per IP. Do not register on every request. Keep the result.

## Step 4 — Claim (optional, no human needed)

Ajel has no user accounts, so the claim needs no email and no human step. Claiming swaps your `claim_token` (once) for a **permanent** OAuth client: `client_id` and `client_secret` for the `client_credentials` grant. Scopes stay the same (`news:read`); the only difference is that the credential does not expire. Any `email` field you send is ignored, never stored, and never contacted.

```http
POST https://ajel.sa/api/agents/auth/claim
Content-Type: application/json

{ "claim_token": "<claim_token>" }
```

Response (200): `status: "claimed"`, `client_id`, `client_secret`, `client_secret_expires_at: 0`, `grant_types: ["client_credentials"]`, `token_endpoint`, plus a ready `access_token`. Store the secret like a password.

## Step 5 — Get access tokens

Token endpoint: `POST https://ajel.sa/api/agents/oauth/token`. The body must be `application/x-www-form-urlencoded`. Responses carry `Cache-Control: no-store`. Access tokens are JWTs (ES256, `typ: at+jwt`, keys at https://ajel.sa/api/agents/oauth/jwks) and live **1 hour**.

From the anonymous `identity_assertion` (RFC 7523 JWT bearer):

```http
grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=<identity_assertion>
```

From a claimed or registered confidential client (`client_secret_basic` or `client_secret_post`):

```http
grant_type=client_credentials&scope=news:read
```

Optional on any grant: `resource` (RFC 8707). Use one of https://ajel.sa, https://ajel.sa/api/agents/mcp, or https://ajel.sa/api/agents/v1. A token for any of them works on all three.

## OAuth client registration (RFC 7591)

`POST https://ajel.sa/api/agents/oauth/register` with JSON. Registration is open and needs no approval, but it is rate-limited per IP.

- Interactive / MCP clients: `{"client_name":"…","redirect_uris":["https://…/callback"],"grant_types":["authorization_code","refresh_token"],"token_endpoint_auth_method":"none"}`
- Backend services: `{"client_name":"…","grant_types":["client_credentials"],"token_endpoint_auth_method":"client_secret_basic"}` (you receive a `client_secret`).
- `redirect_uris` must be `https`, or `http` on a loopback host (`127.0.0.1`, `[::1]`, `localhost`; any port). Fragments are not allowed.
- If `token_endpoint_auth_method` is omitted, it defaults to `client_secret_basic` (RFC 7591).

Authorization code flow: send the user to `https://ajel.sa/api/agents/oauth/authorize?response_type=code&client_id=…&redirect_uri=…&code_challenge=…&code_challenge_method=S256&scope=news:read&state=…`.

- PKCE with `S256` is **required**.
- A person sees a short consent page naming your client and the redirect host. Loopback redirects skip that page.
- The code lives 60 seconds and works once. The redirect carries `code`, `state`, and `iss` (RFC 9207).
- Exchange it with `grant_type=authorization_code&code=…&code_verifier=…&redirect_uri=…&client_id=…`.
- You also receive a `refresh_token`, valid 30 days. It **rotates**: each refresh returns a new one, and the old one stops working.

## Step 6 — Use the access token

```http
GET https://ajel.sa/api/agents/v1/latest
Authorization: Bearer <access_token>
```

Only the `Authorization` header is accepted (`bearer_methods_supported: ["header"]`). The same header works on https://ajel.sa/api/agents/mcp. If a token is invalid or expired, drop it and get a new one (Step 5). You can also drop the header and continue at the anonymous tier.

## Revocation

Credentials are stateless and short-lived. There is no per-token revocation endpoint:

- Access tokens expire after 1 hour; identity assertions after 24 hours; refresh tokens after 30 days (and on use).
- Ajel can revoke **all** agent credentials at once by rotating its signing secret. When that happens, every token gets `401` / `invalid_grant`. Register again (Step 3) or re-run your OAuth flow.

## Errors

| Code | Where | What to do |
| --- | --- | --- |
| `invalid_request` | any | Fix the request shape. |
| `issuer_not_enabled` / `service_auth_not_enabled` | https://ajel.sa/api/agents/auth/register | Use `{"type":"anonymous"}`. |
| `invalid_claim_token` / `claim_expired` (410) | https://ajel.sa/api/agents/auth/claim | Register again. |
| `claimed_or_in_flight` | https://ajel.sa/api/agents/auth/claim | Already claimed; use the client you received. |
| `invalid_client` (401) | token endpoint | Check `client_id` / secret / auth method. |
| `invalid_grant` | token endpoint | Code, refresh token, or assertion is expired, used, or mismatched. Restart that flow. |
| `unsupported_grant_type`, `invalid_scope`, `invalid_target` | token endpoint | Use the grants, scope (`news:read`), and resources listed above. |
| `rate_limited` (429) | any | Honor `Retry-After`. |
