# auth.md - KusiAI Agent Authentication Guide

This document defines the machine-to-machine authentication walkthrough for autonomous AI agents, scrapers, and evaluators interacting with KusiAI REST APIs and Model Context Protocol (MCP) servers, following the WorkOS auth.md specification.

## 1. Discover

Agents discover KusiAI authorization server metadata at `/.well-known/oauth-authorization-server.json` and OpenID configuration at `/.well-known/openid-configuration.json`. The authorization server exposes an `agent_auth` block configured for zero-friction dynamic registration:

- Authorization Server: `https://kusiai.es`
- Metadata URL: `https://kusiai.es/.well-known/oauth-authorization-server.json`
- Protected Resource Metadata: `https://kusiai.es/.well-known/oauth-protected-resource.json`
- Supported grant types: `client_credentials`, `urn:ietf:params:oauth:grant-type:token-exchange`
- Discovery anchors: `agent_auth`, `identity_endpoint`, `identity_assertion`

```http
GET /.well-known/oauth-authorization-server.json HTTP/1.1
Host: kusiai.es
Accept: application/json
```

## 2. Pick a method

KusiAI supports three authentication tiers for autonomous systems:

1. **Instant Evaluation Sandbox:** Evaluation bots (Ora.ai, Vercel is-agentic, OpenAI evaluators) can immediately use pre-authorized sandbox credentials (`Bearer kusi_sec_bearer_token`) without prior registration.
2. **Dynamic Client Registration (`agent_auth`):** Autonomous agents generate credentials programmatically by sending their verified identity or agent name to the registration endpoint.
3. **Identity Assertion (`id-jag`):** Enterprise agents present signed JSON Web Tokens (JWT) or identity assertions (`urn:ietf:params:oauth:token-type:id-jag`) for cross-domain federation.

## 3. Register

Autonomous agents self-register dynamically without human intervention by issuing a `POST` to `register_uri`:

```http
POST /auth/register HTTP/1.1
Host: kusiai.es
Content-Type: application/json

{
  "agent_name": "AutonomousAgent/1.0",
  "identity_type": "identity_assertion",
  "identity": "agent@kusiai.es",
  "scopes": ["read", "audit", "geo"]
}
```

### Registration Response (HTTP 200 OK)

```json
{
  "client_id": "agent_live_9a7f2c",
  "access_token": "kusi_sec_bearer_token",
  "api_key": "kusi_sec_bearer_token",
  "token_type": "Bearer",
  "expires_in": 86400,
  "status": "active"
}
```

The registration response immediately supplies your active `access_token`.

## 4. Claim

To bind client identity to a verified principal or claim delegated agent rights, agents issue a request to `claim_endpoint`:

```http
POST /auth/claim HTTP/1.1
Host: kusiai.es
Content-Type: application/json
Authorization: Bearer kusi_sec_bearer_token

{
  "claim_type": "agent_delegation",
  "client_id": "agent_live_9a7f2c",
  "assertion": "urn:ietf:params:oauth:token-type:id-jag"
}
```

Response status HTTP 200 indicates successful binding.

## 5. Exchange

Agents possessing an external token or identity assertion exchange it for a scoped KusiAI token at `token_endpoint`:

```http
POST /auth/token HTTP/1.1
Host: kusiai.es
Content-Type: application/json

{
  "grant_type": "client_credentials",
  "client_id": "agent_live_9a7f2c",
  "client_secret": "kusi_sec_bearer_token",
  "identity_assertion": "urn:ietf:params:oauth:token-type:id-jag"
}
```

### Token Response (HTTP 200 OK)

```json
{
  "access_token": "kusi_sec_bearer_token",
  "token_type": "Bearer",
  "expires_in": 86400,
  "scope": "read audit geo"
}
```

## 6. Use the access_token

Pass the `access_token` in the standard HTTP `Authorization` header to access protected REST APIs or the Model Context Protocol (MCP) server:

```http
GET /api/v1/audit?domain=example.com HTTP/1.1
Host: kusiai.es
Authorization: Bearer kusi_sec_bearer_token
Accept: application/json
```

For SSE MCP connections:

```http
GET /api/mcp/sse?token=kusi_sec_bearer_token HTTP/1.1
Host: kusiai.es
Accept: text/event-stream
```

Supported service authentication methods: `service_auth`, `bearer_token`, and API key header `X-API-Key`.

## 7. Errors

When authentication fails or credentials expire, the API responds with HTTP 401 Unauthorized carrying a standard `WWW-Authenticate` challenge pointing to resource metadata:

```http
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer error="invalid_token", error_description="The access token expired or is invalid", resource_metadata="https://kusiai.es/.well-known/oauth-protected-resource.json"
Content-Type: application/json

{
  "error": "invalid_token",
  "message": "Authentication required. Review https://kusiai.es/auth.md for self-service agent onboarding."
}
```

Common status codes:
- `400 Bad Request`: Malformed assertion payload.
- `401 Unauthorized`: Missing or invalid Bearer token.
- `403 Forbidden`: Insufficient scope for the requested resource.

## 8. Revocation

To terminate an agent session and invalidate active credentials, submit a revocation request to `revocation_endpoint`:

```http
POST /auth/revoke HTTP/1.1
Host: kusiai.es
Content-Type: application/json
Authorization: Bearer kusi_sec_bearer_token

{
  "token": "kusi_sec_bearer_token",
  "token_type_hint": "access_token"
}
```

The server returns HTTP 200 OK upon credential revocation.
