AAnvio
API

Authentication

How the REST API authenticates requests — disabled by default, JWT-based when enabled.

4 min read

Anvio's REST API (apps/api) ships with authentication disabled by default. This is deliberate: Anvio is local-first, and a single developer running anvio chat or hitting POST /api/sessions on localhost should never need to log in first. Auth becomes relevant only once you expose the API to more than one user (Level 3 in the progressive enhancement model).

This page documents the REST API's auth layer (AuthProvider, JWT). It is a different concern from Runtime OAuth, which authenticates outbound connections to vendor CLIs (Claude Code, Cursor, Codex) and channel platforms (Slack, GitHub). See Security → Authentication for the full picture across both layers.

Where auth is configured

Auth is a workspace-level setting in workspace/anvio.yaml, read by createPlatform() in packages/platform/src/index.ts:

spec:
  auth:
    enabled: false   # default
    # provider: jwt  # or oauth2, none

The default workspace ships with auth.enabled: false. This is not an API middleware toggle — it selects which AuthProvider implementation packages/auth/src/index.ts constructs:

providerClassBehavior
none (default)NoAuthProviderenabled = false. Every request resolves to a fixed anonymous context (local-user, roles ['user']). No token is checked.
jwtJwtAuthProviderenabled = true. Verifies a Bearer token with jsonwebtoken against JWT_SECRET.
oauth2OAuth2AuthProviderenabled = true. Stub — authenticate() throws 'OAuth2 auth requires provider-specific token exchange (Phase 2+)'.
// packages/core/src/ports/auth.port.ts
export interface AuthContext {
  userId: string;
  email?: string;
  roles: string[];
  provider: string;
}

export interface AuthProvider {
  readonly enabled: boolean;
  authenticate(token?: string): Promise<AuthContext | null>;
  getDefaultContext(): AuthContext;
}

provider: oauth2 is a stub in the current source (packages/auth/src/index.ts). Calling authenticate() on it always throws. Do not select it expecting a working OAuth2 code-exchange flow — use jwt for real API authentication today.

How a request is authenticated

Every controller that needs a user identity (SessionsController) resolves it the same way:

// apps/api/src/sessions.controller.ts
private async resolveAuth(authHeader?: string) {
  const { auth } = this.appService.platform;
  if (!auth.enabled) return auth.getDefaultContext();
  const token = authHeader?.replace('Bearer ', '');
  const ctx = await auth.authenticate(token);
  return ctx ?? auth.getDefaultContext();
}

Key detail: even with auth.enabled: true, an invalid or missing token does not reject the request — authenticate() returning null falls back to getDefaultContext() (the local-user identity). There is no @UseGuards / 401 response in the current controllers. Treat this as advisory identity resolution, not an access-control gate, until you've verified otherwise for your deployment.

Rendering diagram…

JWT token shape

JwtAuthProvider.authenticate() expects a standard JWT signed with JWT_SECRET, with this payload shape:

{
  "sub": "user-id-or-uuid",
  "email": "user@example.com",
  "roles": ["user"]
}
  • sub maps to AuthContext.userId.
  • roles defaults to ['user'] if omitted.
  • The secret is read from process.env.JWT_SECRET, with a fallback literal 'dev-secret' if unset (see createAuthProvider call site in packages/platform/src/index.ts).

JWT_SECRET falls back to the hardcoded string dev-secret if the environment variable is not set. Always set a strong, unique JWT_SECRET before enabling auth.enabled: true outside local development. See Environment Variables.

Sending an authenticated request

POST /api/sessions HTTP/1.1
Host: localhost:3000
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Content-Type: application/json

{ "agentName": "architect" }

If auth.enabled: false (the default), the Authorization header is ignored entirely — you can omit it.

Environment variables

VariableUsed byNotes
JWT_SECRETJwtAuthProviderOnly read when spec.auth.enabled: true in anvio.yaml. Defaults to dev-secret if unset — override this.

See the full list in Environment Variables.

What is NOT implemented

TODO: verify against apps/api/src — there is currently no /auth/login or /auth/register REST endpoint. Older internal docs (docs/15-api-reference.md) reference these paths, but no AuthController exists in apps/api/src. Token issuance (signing a JWT with sub/email/roles) is expected to happen outside the API process (e.g. your own identity provider), then handed to clients as a Bearer token. Do not build against /auth/login until a controller actually ships.

Edit on GitHub

Last updated on

On this page