Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Authentication API

Canonical reference: docs/api-reference.md (generated endpoint directory) — this page is a task-oriented summary.

Token-based authentication with RBAC. Mango (Python/Tornado, container zerg-mango) is the identity provider for the whole ZERG platform; Sol’s sol_auth_middleware validates the issued token on every request.

Overview

All auth endpoints live under /api/v1/auth/.... The router is a catch-all ({"/api/v1/auth/[...]", sol_http_auth, []}) that dispatches on the final path segment and HTTP method. Three bare passthrough routes — /auth/login, /auth/logout, /auth/validate — serve the same handlers for the zerg CLI (which talks to the Sol base URL directly); there are no other bare /auth/... endpoints.

Users authenticate with account + password (not username) and receive a token. The token is sent as Authorization: Bearer <token> on subsequent requests. The zerg_session cookie carries the same token for browser / WebSocket clients. Refresh tokens do not exist ( removed the retired /auth/refresh stub). Any unknown auth path — including POST /api/v1/auth/refresh — answers 404 not_found regardless of the verb. Clients re-authenticate with account + password.

Endpoints

Login

POST /api/v1/auth/login

Authenticate and receive a session token.

Request Body:

{
  "account": "alice@example.com",
  "password": "s3cret!"
}
FieldRequiredDescription
accountYes*Account identifier (email or username). email is accepted as a fallback alias.
passwordYesAccount password.

* The handler reads account first, then falls back to email. username is Not accepted.

Response (200):

{
  "token": "zerg_mg_abc123def456",
  "user": {
    "uuid": "user_abc123",
    "email": "alice@example.com",
    "permissions": ["chat:read", "chat:write", "goals:*"]
  }
}
FieldDescription
tokenBearer token. Send as Authorization: Bearer <token> or in the zerg_session cookie.
userUser object returned verbatim from Mango. Exact fields depend on Mango’s user schema; uuid and permissions are always present.

There is no refresh_token field — Mango never issues refresh tokens and the field was dropped from the response in.

Error Responses:

CodeBodyCondition
200{"ok":false,"error":{"code":"auth disabled","message":"auth disabled"}}mango_auth_enabled = false (note: HTTP 200 with an error body — auth is off, so this is not a failure).
400{"ok":false,"error":{"code":"invalid json",...}} / {"ok":false,"error":{"code":"empty body",...}}Malformed request body.
401{"ok":false,"error":{"code":"<reason>",...}}Bad credentials, Mango unreachable, or a Mango client crash (never a 500).

Example:

curl -X POST http://localhost:21434/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{"account": "alice@example.com", "password": "s3cret!"}'

Validate Token

GET /api/v1/auth/validate

Validate the caller’s token and return its identity + permissions. Used internally by Sol and by front-end apps that need to refresh the user object.

Method is GET, not POST. The token is read from the Authorization header, the zerg_session cookie, or the ?token= query parameter.

Response (200):

{
  "valid": true,
  "user_uuid": "user_abc123",
  "token_type": "session",
  "permissions": ["chat:read", "chat:write", "goals:*"]
}
FieldDescription
validAlways true on a 200 response.
user_uuidThe user’s UUID.
token_typeToken type reported by Mango (defaults to "session" if absent).
permissionsList of resource:action permission strings (defaults to []).

Error Responses:

CodeBodyCondition
401{"ok":false,"error":{"code":"missing token","message":"missing token"}}No token found in any location.
401{"ok":false,"error":{"code":"<reason>",...}}Mango rejected the token.
401{"ok":false,"error":{"code":"auth service unavailable",...}}Mango unreachable (Erlang EXIT).
401{"ok":false,"error":{"code":"invalid token","message":"invalid token"}}Mango returned an unexpected shape.

Refresh Endpoints (removed)

There is no refresh endpoint. POST /api/v1/auth/refresh was a stub that always answered 401 “refresh tokens are disabled”; it was **removed in **. Unknown auth segments now answer 404 not_found (the Zerg envelope) for every verb:

{ "ok": false, "error": { "code": "not_found", "message": "not_found" } }

A real refresh flow would ride an end-to-end Mango implementation (Mango currently issues no refresh tokens); until then, re-authenticate via /auth/login.

Logout

POST /api/v1/auth/logout

Invalidate the current token. The token is read from the Authorization header (or cookie / query). If no token is present, the endpoint still succeeds.

Response (200):

{ "ok": true }

Returns HTTP 200 with a JSON body — not 204. The body is always {"ok":true} regardless of whether a token was supplied.

Authentication Delivery

Tokens may be supplied in any of three locations, evaluated in this priority order:

  1. Authorization: Bearer <token> header (preferred; always wins).
  2. zerg_session cookie — set by the browser on login. Required for the WebSocket chat endpoint (/api/v1/chat/:session_id/ws) where custom headers cannot be sent on the upgrade.
  3. ?token=<token> query parameter — deprecated. Still accepted (primarily for SSE consumers that cannot set headers), but logs a warning.

RBAC

Permission Model

Permissions are resource:action strings (e.g. chat:read, goals:*). The admin role is the literal permission "*" in the user’s permission list. The infra router and several admin-gated endpoints check lists:member(<<"*">>, Permissions) directly.

On a successful login, Mango sets:

Set-Cookie: zerg_session=<token>; HttpOnly; Secure; SameSite=Strict; Path=/

The cookie is secondary to the Authorization header — the header takes priority when both are present.

Error Codes Summary

CodeDescription
400Malformed JSON body (login only)
401Invalid credentials, expired/missing token, or auth service unavailable
403Insufficient permissions (raised by RBAC middleware, not the auth router)
404Unknown auth path — including the removed /auth/refresh
405Wrong HTTP method for the path (e.g. POST to /auth/validate)
429Rate limit exceeded (middleware)