# Authentication

> Set up 10xGraph API authentication: open by default, JWT with JWT_SECRET_KEY and JWT_ALGORITHM, custom BaseAuth classes, plus roles, permissions and reach.

Source: https://10xgraph.com/docs/reference/api-cli/auth
Last updated: 2026-11-25

By default, the 10xGraph API accepts all requests without authentication (suitable for local development). For production, set the `auth` key to JWT or to a custom `BaseAuth` class to verify identity, and add roles to control what each user may do and how far their data reaches. This page is the reference. See [Set up API authentication](/docs/server/auth) for a walk-through.

## No authentication (default)

```json
{
  "agent": "graph.react:app",
  "auth": null
}
```

All endpoints are publicly accessible, no thread has an owner, and `GET /v1/me` answers `{"auth": "off"}`. `"auth": {"method": "none"}` is the same. RBAC keys with auth off refuse to boot.

## JWT authentication

Set `auth` to `"jwt"` in `10xgraph.json`:

```json
{
  "agent": "graph.react:app",
  "auth": "jwt"
}
```

Install the JWT extra:

```bash
pip install "10xgraph-api[jwt]"
```

Set the required environment variables:

```bash
JWT_SECRET_KEY=your-secret-key
JWT_ALGORITHM=HS256  # optional, default HS256
```

Startup stops when PyJWT is missing, `JWT_ALGORITHM` is not an allowed algorithm, or, in production, an HS secret is shorter than 32 bytes. A missing `JWT_SECRET_KEY` answers 500 `JWT_SETTINGS_NOT_CONFIGURED` on the first request.

Send `Authorization: Bearer <token>`. The resolved `Identity` becomes `config["user"]`.

### Required JWT claims

The built-in backend requires:

| Claim | Notes |
|---|---|
| `exp` | Expiry time, required. No token is treated as permanently valid; a missing `exp` is rejected with 401. |
| `user_id` | Identity key. The `sub` claim is not accepted; you must use `user_id`. It must be a non-empty string or a whole number (not a boolean). A number is stored as a string. Anything else, such as an empty or whitespace-only string, a boolean, a float, a list or an object, is rejected with 401. |

`JWT_ISSUER` and `JWT_AUDIENCE` are both off when unset or empty. With `JWT_ISSUER` unset, the issuer is not checked: a token with any `iss`, or none, is accepted. With it set, the token must carry `iss` and it must match exactly. With `JWT_AUDIENCE` unset, a token without `aud` is accepted, but a token that carries an `aud` claim is refused. With it set, the token must carry `aud` and it must match. A failure of either check answers 401 `INVALID_TOKEN`. Set both in production when the same signing key or identity provider also issues tokens for other services.

Other claims (`roles`, `scopes`, `email`, custom fields) are kept in `identity.claims`. The claims named in `auth.roles_claim`, `auth.permissions_claim` and `auth.tenant_claims` also drive permissions, reach and tenant values.

Two claims are also read by the server:

- `scope` (a space-separated string, as in OAuth) and `scopes` (a list of strings, or a space-separated string). Both only **narrow** the permissions the roles grant: the effective set is the granted set intersected with the permission-shaped entries (`*` or anything containing `:`). A scope matches by exact name or wildcard. Plain OAuth scopes such as `openid profile` are ignored, so they do not narrow. A token with no permission-shaped scope is not narrowed. A scope claim of the wrong type (a number, an object, or a list that holds non-strings) is rejected with 401.
- `jti`, the token ID. When `cache_store.token_revocation` is on in `10xgraph.json`, a revoked `jti` is refused. The check runs even when `user_id` is missing or unusual. See [Token revocation](#token-revocation).

### Rotating the signing key

Set `JWT_SECRET_KEYS` to a JSON list so tokens signed with an old key keep working while new tokens use a new one. Each entry is an object with a `key` and an optional `kid`, or a plain string (a key with no `kid`). `JWT_SECRET_KEY` keeps working unchanged and is tried too.

```bash
JWT_SECRET_KEY=the-new-secret
JWT_SECRET_KEYS='[{"kid": "2026-09", "key": "the-old-secret"}, "an-older-secret-without-kid"]'
```

A token whose header names a `kid` is checked against the keys with that `kid`, then the keys without one. A token with no `kid` is tried against every key. The first key that verifies wins. Remove an old key from the list once its tokens have expired.

### Limiting token age

Set `JWT_MAX_TOKEN_AGE_SECONDS` to refuse tokens older than that many seconds, counted from the `iat` claim, even when `exp` is far away. A token with no `iat` is refused. Both settings are off by default.

### Status codes for JWT failures

| Status | `error_code` | Cause |
|---|---|---|
| 401 | `INVALID_TOKEN` | No credential was presented (missing Authorization header, missing WebSocket auth, missing query param) |
| 401 | `EXPIRED_TOKEN` | Token `exp` is in the past, or the token is older than `JWT_MAX_TOKEN_AGE_SECONDS` by its `iat` |
| 401 | `INVALID_TOKEN` | Signature or structure is invalid, `user_id` is missing or malformed, or a `scope` or `scopes` claim has the wrong type |
| 401 | `INVALID_TOKEN` (message "Invalid token, please login again") | A configured `tenant_claims` claim is missing, or the roles or permissions claim is malformed |
| 401 | `INVALID_TOKEN` | The `user_id` starts with the reserved prefix `__system__`, or `cache_store.token_revocation` is on and the token has no usable `jti` (message "Invalid token, jti missing") |
| 401 | `REVOKED_TOKEN` | The token, or all of the user's tokens, were revoked |
| 500 | `JWT_SETTINGS_NOT_CONFIGURED` | `JWT_SECRET_KEY` is unset, or `JWT_ALGORITHM` is not an allowed value, at request time |

On WebSocket routes, errors become a clean close with code 1008 instead of an HTTP response.

Client action: refresh the token on `EXPIRED_TOKEN`; sign in again on `INVALID_TOKEN` or `REVOKED_TOKEN`.

## Sending auth over WebSocket

WebSocket routes check two places in order for a credential:

1. **`Authorization: Bearer <token>`**: for non-browser clients.
2. **`Sec-WebSocket-Protocol: 10xgraph-bearer, <token>`**: preferred for browsers (the token stays in headers, never in URLs or logs). The server echoes the sentinel back on accept, which browsers require.
A `?token=` query parameter is not accepted: a token in the URL would land in access logs, proxy logs and trace spans, so it is ignored.

HTTP routes read the `Authorization` header only. The subprotocol is honoured on WebSocket connections and nowhere else.

Example:

```javascript
const ws = new WebSocket(
  "ws://localhost:8000/v1/graph/ws",
  ["10xgraph-bearer", token],  // sentinel, then raw JWT
);
```

The subprotocol offer must start with the sentinel, followed by the token. With anything else, no credential is found and the handshake is refused with close code `1008`.

`agentflow-bearer` is accepted as a deprecated alias until 2.0.

## Custom authentication backend

Provide your own `BaseAuth` subclass when you need integration with an internal identity system:

```json
{
  "agent": "graph.react:app",
  "auth": {
    "method": "custom",
    "path": "graph.auth:MyAuthBackend"
  }
}
```

The `path` accepts four forms: `auth_app:Cls` (a top-level module in the project folder), `auth_app.py:Cls`, `pkg.mod:Cls` (a dotted package path) and `pkg/mod.py:Cls` (a file path). A path that does not exist raises an error naming it.

Subclass `BaseAuth` and implement `authenticate`. The method is `async def` (the server awaits it) and receives the connection, the response (or `None`) and the credential:

```python
from typing import Any
from fastapi import HTTPException, Response
from fastapi.security import HTTPAuthorizationCredentials
from starlette.requests import HTTPConnection
from tenxgraph_api import BaseAuth

class MyAuthBackend(BaseAuth):
    async def authenticate(
        self,
        request: HTTPConnection,  # HTTP request or WebSocket
        response: Response | None,
        credential: HTTPAuthorizationCredentials | None,  # bearer token or None
    ) -> dict[str, Any] | None:
        """Return the verified claims, or raise to reject.

        - Return a dict with at least 'user_id' to authenticate.
        - Returning None or {} answers 401 "Authentication required".
        - Raise UserAccountError (401) or HTTPException (your status) to reject.
        - On WebSocket: either becomes a clean 1008 close.
        """
        if credential is None:
            raise HTTPException(status_code=401, detail="Missing credentials")
        claims = my_identity_service.verify(credential.credentials)
        return {
            "user_id": claims["sub"],
            "email": claims.get("email"),
            "roles": claims.get("roles", []),
            "scopes": claims.get("scopes", []),
        }
```

> **`authenticate` must be `async def`**
>
> `authenticate` must be `async def`; the server awaits it. For non-Bearer schemes (API keys, cookies), ignore `credential` and read `request.headers` directly.

Override `downstream_credential(connection, credential, claims)` to change the credential runs use to call other services; the default returns the bearer token. See [Calling services as the user](/docs/guides/call-services-as-the-user).

## Authorization (roles, permissions, reach)

Authorization decides what an authenticated user may do and whose data they can see. It sits under the same `auth` key as authentication.

### The `auth` object

```json
{
  "auth": {
    "method": "jwt",
    "path": null,
    "roles_claim": "roles",
    "permissions_claim": null,
    "tenant_claims": ["org_id"],
    "roles": {
      "member": {"permissions": ["graph:*", "threads:*", "store:*", "files:*"], "reach": "own"},
      "org_admin": {"inherits": ["member"], "permissions": ["admin:read"], "reach": "tenant"}
    },
    "default_role": "member",
    "system_identities": {"nightly": {"roles": ["member"], "tenant": {"org_id": "acme"}}}
  }
}
```

| Key | Type | Meaning |
|---|---|---|
| `method` | `none` \| `jwt` \| `custom` | Which class verifies the credential. Default `none`. `null` and `"jwt"` are accepted as shortcuts. |
| `path` | string | `"module:Class"` of a `BaseAuth` subclass. Only with `custom`, and required there. |
| `roles` | object | Role name to `{permissions, inherits, reach}`. `reach` is required, `permissions` and `inherits` are lists. |
| `roles_claim` | string | Claim holding the caller's role names (list or space-separated string). Defaults to `roles` when `roles` is set. Refused without `roles`. |
| `permissions_claim` | string | Claim holding permissions directly. Cannot be combined with `roles`. A missing claim gives no permissions. |
| `tenant_claims` | list of strings | Claims that name the caller's tenant. Each must be a non-empty string or whole number on every token, else 401. |
| `default_role` | string | Role for a token with no roles claim. Must be in `roles`. |
| `system_identities` | object | Name to `{roles, tenant}`, for background tasks. |

With no RBAC keys, every authenticated user gets the default permission set (every permission except `admin:*`) and reach `own`.

### Permissions

Permissions are `resource:action` from one registry. A role naming anything else refuses to boot. `resource:*` and `*` are wildcards.

| Resource | Actions |
|---|---|
| `graph` | `read`, `invoke`, `stream`, `stop`, `fix` |
| `threads` | `read`, `write`, `delete` |
| `store` | `read`, `write`, `delete` |
| `files` | `read`, `upload`, `delete` |
| `config` | `read` |
| `tasks` | `trigger`, `read`, `cancel` |
| `admin` | `read`, `write` |
| `tools` | A tool name (1 to 128 of `A-Z a-z 0-9 _ - .`) or `*`. Checked by `ToolGuard`, never by a route. The default set holds none, so grant `tools:<name>` or `tools:*` in a role or token claim. See [Zero-trust agents](/docs/security/zero-trust-agents#on-the-api-server) |

The route guards:

| Routes | Permission |
|---|---|
| `POST /v1/graph/invoke` | `graph:invoke` |
| `POST /v1/graph/stream`, `/v1/graph/ws`, `/v1/graph/live`, `POST /v1/ag-ui` | `graph:stream` |
| `GET /v1/graph`, `GET /v1/graph/tools`, `GET /v1/graph:StateSchema`, `GET /v1/observability/{thread_id}` | `graph:read` |
| `POST /v1/graph/stop` | `graph:stop` |
| `POST /v1/graph/fix` | `graph:fix` |
| Thread, state and message reads | `threads:read` |
| Thread state and message writes | `threads:write` |
| Thread, state and message deletes | `threads:delete` |
| Store search, get and list | `store:read` |
| Store create and update | `store:write` |
| Store delete and forget | `store:delete` |
| File downloads, info, urls and list | `files:read` |
| File upload | `files:upload` |
| File delete | `files:delete` |
| Server config read | `config:read` |
| `POST /v1/tasks/{name}`, `PATCH /v1/tasks/{schedule_id}` | `tasks:trigger` |
| `GET /v1/tasks/{id}` | `tasks:read` |
| `POST /v1/tasks/{id}/cancel` | `tasks:cancel` |
| `GET /v1/me` | Authentication only |

A refusal answers 403 `Missing required permission: <resource>:<action>`.

### Reach

| Reach | Rows a caller's queries match |
|---|---|
| `own` | This `user_id`, within their tenant values |
| `tenant` | Any user, with the caller's tenant values. Needs `tenant_claims` and tenant columns on the checkpointer |
| `all` | No filter |

A caller with several roles gets the union of their permissions and the widest reach. Reach widens reads, lists, deletes and stop only. Runs and state or message writes always stay `own`. A thread, message, memory, file or run outside the caller's reach answers 404. Token claims cannot name a reach.

### Resolving a token

1. `authenticate` returns claims with a `user_id` (a string, or a whole number stored as a string).
2. Tenant values are read from each `tenant_claims` claim.
3. With `roles`, the roles come from `roles_claim` (names not in `roles` are ignored), or from `default_role` when the claim is absent. With `permissions_claim`, the permissions come from that claim. Otherwise the default set applies.
4. Token `scope` and `scopes` narrow the result.
5. A `__system__` user id is refused (401). A revoked token is refused (401).

The result is an `Identity` (`user_id`, `kind`, `tenant`, `roles`, `permissions`, `reach`, `claims`), stored as `config["user"]`.

### `GET /v1/me`

Returns the resolved caller. Needs only authentication.

```json
{"data": {"user_id": "u-42", "tenant": {"org_id": "acme"}, "roles": ["member"], "permissions": ["graph:invoke", "graph:stream"], "reach": "own"}}
```

An identity with no permission restriction reports `["*"]`. With auth off the answer is `{"auth": "off"}`.

### Token revocation

Revoke a token (by `jti`) or all of a user's tokens by calling `revoke_token` or `revoke_user` from your own code in the server process (a tool, a custom `BaseAuth` class or a hook). There is no CLI command. The revocations live in the API's cache store, so with Redis they reach every worker at once and the server needs no restart. Turn the check on inside `cache_store` in `10xgraph.json`. It needs `auth` and a cache store the server can reach; with `token_revocation` on, an unreachable cache store stops the boot:

```json
{
  "auth": "jwt",
  "cache_store": {"type": "redis", "token_revocation": true}
}
```

With `token_revocation` on, every token must carry a `jti` claim (a non-empty string or an integer). A token without it answers 401 `INVALID_TOKEN` ("Invalid token, jti missing"), because one token is revoked by its `jti`. A custom `BaseAuth` class must return `jti` in its claims too. With revocation off, `jti` stays optional. A revoked token answers 401 `REVOKED_TOKEN`, or a 1008 close on a WebSocket.

See [Set up API authentication](/docs/server/auth) for revoking from your own code.

The accepted values of `cache_store.token_revocation` are listed under [cache_store](/docs/reference/api-cli/configuration#cache_store).

Revocation fails closed: if the cache store raises an error, requests that need the check answer `503`. With an in-memory cache store in production, or `WEB_CONCURRENCY` above 1, the server warns that revocations only reach this process; set `cache_store.type` to `redis` (or a shared custom store).

### `BaseAuth.authorize`

```python
from tenxgraph_api import BaseAuth

class MyAuth(BaseAuth):
    async def authenticate(self, request, response, credential): ...

    async def authorize(self, identity, permission, resource=None) -> bool:
        """True to allow, False for 403."""
        return await super().authorize(identity, permission, resource)
```

- The default returns `identity.can(permission)`.
- `permission` is `resource:action`. `resource` is the thread id, memory id, namespace or file id from the path or the JSON body, the task name for a background task, or `None`.
- It takes no request, so the jobs worker calls it too (`tasks:trigger`).
- If you override it and `roles` is set, the roles apply only when you call `super().authorize`. Boot warns about it.
- An `HTTPException` keeps its status; any other exception fails the request. Nothing counts as allowed on an error.

### Boot-time checks

The same validator runs at boot, `10xgraph config`, `10xgraph audit` and `10xgraph build`.

| Setup | Result |
|---|---|
| RBAC key with `auth` off | Refused |
| Top-level `authorization` or `graph_introspection` key | Refused |
| `custom` without a valid `path`, or `path` with another method | Refused |
| `permissions_claim` with `roles` or `roles_claim` | Refused |
| `roles_claim` without `roles` | Refused |
| `default_role`, `inherits` or a system identity role naming an unknown role | Refused |
| Inheritance cycle, unknown permission, missing or unknown `reach` | Refused |
| Role with reach `tenant` and no `tenant_claims` | Refused |
| `tenant_claims` and checkpointer or store tenant columns differ | Refused in production, warning otherwise |
| `authorize` overridden with `roles` set | Warning |
| Role with reach `all` | Warning listing the roles |
| `MODE=production` with auth off | Warning |

### Ownership and data isolation

Storage enforces ownership by the caller's reach; a thread, message, memory or file outside it answers 404. See [Multi-tenant data](/docs/guides/multi-tenant-data).

## Public paths

`/ping` and `/health/ready` need no credentials; every other route needs `RequirePermission`.
