# auth.md — Reformas Integrales Valencia Turia

You are an agent. This service (`https://reformasintegrales-valencia.com.es`) supports **agentic registration** and standard OAuth 2.0 discovery: discover → register → (claim if needed) → exchange for an access_token → call API → handle revocation. Follow the steps in order.

## Service Hosts

- **Resource Server:** `https://reformasintegrales-valencia.com.es`
- **Authorization Server:** `https://reformasintegrales-valencia.com.es`
- **Documentation:** `https://reformasintegrales-valencia.com.es/docs/api`

## Step 1 — Discover

Discovery is two hops. If you received a `401 Unauthorized` response, inspect the `WWW-Authenticate` header for the Protected Resource Metadata URL:

```http
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://reformasintegrales-valencia.com.es/.well-known/oauth-protected-resource"
```

### 1a. Fetch the Protected Resource Metadata (RFC 9728)

```http
GET /.well-known/oauth-protected-resource
Host: reformasintegrales-valencia.com.es
```

Returns:

```json
{
  "resource": "https://reformasintegrales-valencia.com.es",
  "resource_name": "Reformas Integrales Valencia Turia",
  "resource_logo_uri": "https://reformasintegrales-valencia.com.es/images/logo.png",
  "authorization_servers": [
    "https://reformasintegrales-valencia.com.es"
  ],
  "scopes_supported": ["read", "write"],
  "bearer_methods_supported": ["header"]
}
```

### 1b. Fetch the Authorization Server Metadata (RFC 8414)

```http
GET /.well-known/oauth-authorization-server
Host: reformasintegrales-valencia.com.es
```

Inspect the top-level OAuth fields (`issuer`, `token_endpoint`, `revocation_endpoint`, `jwks_uri`, `grant_types_supported`) and the `agent_auth` bootstrap block:

- `agent_auth.skill`: `https://reformasintegrales-valencia.com.es/auth.md`
- `agent_auth.identity_endpoint`: `https://reformasintegrales-valencia.com.es/agent/identity`
- `agent_auth.claim_endpoint`: `https://reformasintegrales-valencia.com.es/agent/identity/claim`
- `agent_auth.events_endpoint`: `https://reformasintegrales-valencia.com.es/agent/event/notify`
- `agent_auth.identity_types_supported`: `["anonymous", "identity_assertion", "service_auth"]`

## Step 2 — Pick a Registration Method

1. **Identity assertion (`identity_assertion` + ID-JAG):** Use when acting on behalf of a verified user with a fresh `urn:ietf:params:oauth:token-type:id-jag` JWT audience-bound to `https://reformasintegrales-valencia.com.es`.
2. **Email claim (`service_auth`):** Use when you have the user's email and can complete the claim ceremony.
3. **Anonymous (`anonymous`):** Use for read-only discovery (`read` scope) without user PII.

## Step 3 — Register

```http
POST /agent/identity
Host: reformasintegrales-valencia.com.es
Content-Type: application/json

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

For `identity_assertion` or `service_auth`, surface `resource_name` (`Reformas Integrales Valencia Turia`) and requested scopes to the user for explicit consent before submitting.

## Step 4 — Claim Ceremony (if required)

When a `claim` block (`user_code`, `verification_uri`, `expires_in`, `interval`) is returned, present `verification_uri` and `user_code` to the user and poll `token_endpoint` using `grant_type=urn:workos:agent-auth:grant-type:claim`.

## Step 5 — Exchange the Assertion for an Access Token

```http
POST /oauth2/token
Host: reformasintegrales-valencia.com.es
Content-Type: application/x-www-form-urlencoded

grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer
&assertion=<identity_assertion>
&resource=https://reformasintegrales-valencia.com.es
```

## Step 6 — Use the Access Token

Send the bearer token in the `Authorization` header:

```http
GET /mcp
Host: reformasintegrales-valencia.com.es
Authorization: Bearer <access_token>
```

Note: Public informational endpoints (`/`, `/blog`, `/llms.txt`, `/openapi.json`, `/.well-known/*`, and Markdown negotiation via `Accept: text/markdown`) are freely readable without authentication.

## Revocation

- **Credential layer (RFC 7009):** `POST /oauth2/revoke` with `token=<access_token>&token_type_hint=access_token`.
- **Registration layer (RFC 8935):** Upstream identity providers deliver Security Event Tokens (`application/secevent+jwt`) to `https://reformasintegrales-valencia.com.es/agent/event/notify`.
