> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agentonboard.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Testing & Production

> Verify your integration with six curl checks, and understand JWKS caching, 1-hour refresh, fallback behavior, and key rotation.

## Integration verification with curl

Run these six `curl` checks against your local or staging server before publishing `auth.md`.

First, mint a 5-minute test token using [`POST /api/v1/token/exchange`](https://docs.agentonboard.xyz/api/token-exchange) and your `aon_live_...` API Key:

```bash theme={null}
TOKEN=$(curl -s -X POST https://agentonboard.xyz/api/v1/token/exchange \
  -H "Authorization: Bearer aon_live_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{"audience":"notes.com"}' \
  | jq -r '.token')
```

### 1. Unknown email returns `403 ACCOUNT_REQUIRED`

Before your email exists in your app's database, call a protected endpoint:

```bash theme={null}
curl -i -X POST https://notes.com/api/notes \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"title": "Test", "body": "Checking 403"}'
```

**Expected:** HTTP `403` with `{"code": "ACCOUNT_REQUIRED", ...}`.
If you receive `401`, fix your status mapping so agents follow your `403` account-setup instructions instead of re-minting tokens.

### 2. Signup creates once and is idempotent on retry

If you implemented `POST /api/signup` with `createAgentAccount`:

```bash theme={null}
# First call creates the user
curl -i -X POST -H "Authorization: Bearer $TOKEN" https://notes.com/api/signup
# Expected: 201 with {"user": "...", "created": true}

# Retry the protected endpoint
curl -i -X POST https://notes.com/api/notes \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"title": "First note", "body": "Created by an agent"}'
# Expected: 200 or 201

# Second signup call returns the existing user without duplicating
curl -i -X POST -H "Authorization: Bearer $TOKEN" https://notes.com/api/signup
# Expected: 201 with {"user": "...", "created": false}
```

Check your database table to confirm only one user row exists for that email.

### 3. Tampered or wrong-Domain tokens return `401` without touching your DB

Corrupt the last character of the JWT signature and call both routes:

```bash theme={null}
BROKEN="${TOKEN%?}A"
curl -i -X POST -H "Authorization: Bearer $BROKEN" https://notes.com/api/notes
curl -i -X POST -H "Authorization: Bearer $BROKEN" https://notes.com/api/signup
```

**Expected:** HTTP `401` with `{"code": "INVALID_SIGNATURE", ...}` on both routes, with zero database queries executed.

Also test:

* **No `Authorization` header:** Expect `401 MALFORMED_TOKEN`.
* **Wrong Domain:** Mint a token with `{"audience": "other-domain.com"}` and send it to your endpoint. Expect `401 AUDIENCE_MISMATCH`.

### 4. Expired tokens return `401 EXPIRED`

Wait 5 minutes (plus 5 seconds for clock tolerance) and replay the original `$TOKEN`:

```bash theme={null}
curl -i -X POST -H "Authorization: Bearer $TOKEN" https://notes.com/api/notes
```

**Expected:** HTTP `401` with `{"code": "EXPIRED", ...}`.

### 5. Database exceptions return `500 RESOLVER_ERROR` without leaking secrets

Temporarily throw `new Error("db-secret-test-string")` inside `resolveAccount` and call your endpoint.

**Expected:** HTTP `500` with `{"code": "RESOLVER_ERROR", ...}`, and confirm `"db-secret-test-string"` never appears in the HTTP response body. Remove the test throw afterward.

### 6. `auth.md` matches your live responses

Fetch both discovery paths and verify:

```bash theme={null}
curl -i https://notes.com/auth.md
curl -i https://notes.com/.well-known/auth.md
```

* Both return `200 OK` over HTTPS with no login redirect.
* The hostname under `## Audience` matches the `audience` option in your backend code.
* The `403` row in `## When a request is rejected` matches your live behavior (human connect URL or `POST /api/signup`).

***

## JWKS caching and production runtime

This section documents how `@agentonboard/sdk` ([`agentonboard/js-sdk`](https://github.com/agentonboard/js-sdk)) and `agentonboard-sdk` ([`agentonboard/agentonboard-python-sdk`](https://github.com/agentonboard/agentonboard-python-sdk)) fetch, cache, and rotate public JWKS keys in production.

### How key resolution works per request

On each request, the SDK decodes the JWT header to read `kid`, resolves the matching RSA public key, verifies the signature and claims, and runs your `resolveAccount` callback.

```mermaid theme={null}
flowchart TD
    A["Incoming JWT"] --> B["Decode JWT Header (kid)"]
    B --> C{"Is kid in local cache and cache age <= 1 hour?"}
    C -- "Yes" --> D["Verify RS256 signature locally (Zero network calls)"]
    C -- "No (Cold cache, >1h old, or unknown kid past 30s cooldown)" --> E["Fetch /.well-known/jwks.json (5s timeout)"]
    E -- "Fetch succeeded & passed shape check" --> F["Update cache & verify signature"]
    E -- "Fetch failed or timed out" --> G{"Does process hold a last-known-good key set?"}
    G -- "Yes (Warm process)" --> H["Verify against last-known-good key set"]
    G -- "No (Cold start during outage)" --> I["Return 401 KEY_SOURCE_UNAVAILABLE"]
```

The SDK uses your runtime's global `fetch` (TypeScript) or `httpx` (Python) with zero filesystem or socket dependencies.

> [!WARNING]
>
> **Local verification refreshes JWKS once per hour**
>
> Verification does not call AgentOnboard per request, and a cold process fetches `/.well-known/jwks.json` on its first verification. Long-running processes refresh the cached key set once per hour (`CACHE_MAX_AGE_MS = 3_600_000`).

### Caching and last-known-good fallback

Every fetched JWKS response is shape-checked before caching:

* It must contain a `keys` array of valid RSA public keys (`2048` bits minimum) with unique `kid` strings.
* Malformed JSON, non-RSA keys, or duplicate `kid` values are treated as a failed fetch.

When a JWKS fetch fails or times out, a warm process **falls back to the last validated key set held in memory**.

> [!WARNING]
>
> **Cold starts have no fallback key set**
>
> `KEY_SOURCE_UNAVAILABLE` fires only when a JWKS fetch fails **and** the process has no cached key set in memory (such as the first request after a deploy during a JWKS outage). Warm instances continue verifying with cached keys.

Cache bounds and timers:

* **Cache TTL:** `1 hour` (`3,600,000 ms`).
* **Unknown `kid` refetch cooldown:** `30 seconds` in TypeScript (`60 seconds` in Python). Prevents a flood of forged tokens with fake `kid` values from triggering a flood of JWKS requests.
* **Maximum cached key sources:** `8` distinct issuer URLs per process (LRU-evicted).

### Key rotation

When AgentOnboard rotates Signing Keys:

1. New tokens arrive with a new `kid` in the header.
2. Your server sees the unknown `kid` and refetches `/.well-known/jwks.json` once the 30-second cooldown has elapsed.
3. The new public key loads into memory and verification continues without dropping requests.

Once an old `kid` is removed from `/.well-known/jwks.json`, tokens signed by that retired key fail with `UNKNOWN_KEY`.

### Operational edge cases

> [accordion] 5-second latency spike when refreshing an expired cache during a JWKS outage
>
> When the 1-hour cache expires and `/.well-known/jwks.json` is unreachable or slow, the request triggering the refresh waits up to the **5-second fetch timeout** before falling back to the last-known-good key set.
>
> If your serverless function or API gateway enforces a strict 3-second request timeout, that 5-second wait will time out the request. To avoid this:
>
> 1. Set route timeouts on agent-protected endpoints above 5 seconds, or
> 2. Warm the verifier at process startup, or
> 3. Pass a custom `fetchJwks` callback with a shorter `AbortSignal.timeout(1500)`.

> [accordion] Alerting on KEY\_SOURCE\_UNAVAILABLE and UNKNOWN\_KEY
>
> * Alert on `KEY_SOURCE_UNAVAILABLE`: a cold-start instance could not reach `/.well-known/jwks.json`.
> * Alert on spikes in `UNKNOWN_KEY`: indicates a key rotation, a staging environment missing `AON_ISSUER`, or forged tokens.

> [accordion] Why custom fetchJwks errors are sanitized
>
> If your custom `fetchJwks` callback throws, the SDK discards the raw error message and returns a generic `KEY_SOURCE_UNAVAILABLE` description so internal proxy URLs or credentials never leak into `401` bodies. Log errors inside `fetchJwks` before rethrowing.

### Fixed cryptographic constants

These parameters are pinned in the SDK:

| Property | Value |
| - | - |
| Accepted algorithm | `RS256` only |
| Required JWT claims | `exp`, `jti`, `sub`, `email`, `aud`, `iss` |
| Required JWT header | `kid` |
| Minimum RSA key size | `2048 bits` (smaller keys fail with `KEY_SOURCE_UNAVAILABLE`) |
| Default clock-skew tolerance | `5 seconds` (`clockToleranceSeconds`) |
| Token lifetime | `5 minutes` (`300 seconds`) |
| Max cached key sources per process | `8` (LRU) |

***

## Production deployment checklist

* [ ] `audience` is hardcoded in your server source and matches `## Audience` in `auth.md`.
* [ ] `AON_ISSUER` (or `issuer`) is set on any non-production environment verifying against a non-production issuer.
* [ ] Route timeouts on protected endpoints allow at least 5 seconds for cold-start or hourly JWKS fetches.
* [ ] `ACCOUNT_REQUIRED` returns HTTP **403** (not 401).
* [ ] Regular routes use `verifyAgentAccount` (`requireAgentAccount`), and `createAgentAccount` is only used on an explicit `POST /api/signup` route if you opt into agent signup.
* [ ] Application logs record `code` and `jti`, never raw JWTs or `Authorization` headers.
* [ ] Monitoring alerts on `KEY_SOURCE_UNAVAILABLE` and spikes in `UNKNOWN_KEY`.
* [ ] Both `https://<your-domain>/auth.md` and `https://<your-domain>/.well-known/auth.md` return `200 OK` over HTTPS and match your live error codes.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.