Skip to main content

Agent authentication

The browser holds its session in an HttpOnly cookie, which a script cannot read and which the agent, on another origin, never receives. The agent still has to know who is asking, so that it can apply the user's roles and limits and count the user's usage. FINOS Workspace solves this with a short-lived agent token. The browser asks the API for one, sends it to the agent, and the agent asks the API who the token belongs to. This page describes the token, the calls that the agent makes to the API, and what each party refuses. The code is in backend-api/backend/api/auth_helpers.py, backend-api/backend/routers/app/index.py, frontend/src/lib/state/agentToken.ts and agent/src/auth/.

The agent token​

The browser asks for a token with POST /app/copilot/token. The request must carry the session cookie and the header X-Workspace-Client: web, which only the web app sends. The API answers with the token and its lifetime in seconds:

{ "token": "<the token>", "expires_in": <lifetime in seconds> }

The token is 32 random bytes in URL-safe base64. It lives for 30 minutes, or for the time that is left of the session when that is shorter. The API stores only a hash of the token, together with the user and the session that it belongs to, in the shared Redis-compatible store, and it keeps a list of a session's tokens so that it can end them together.

The web app keeps the token in memory and never writes it to browser storage. It sends the token in the header Authorization: Bearer <token> on its requests to the default agent, which is the agent at the address that the web app's urls.ai setting names. It asks for a new token when less than 20 minutes of the current one remain. When the agent answers 401, the web app mints a new token and repeats the request once.

A token stops working when the session that it came from ends. The API checks on every request that the session still exists and that the user is not deleted, and signing out, deleting the user and ending a session also delete the token's record at once.

What the API accepts a token for​

The API accepts an agent token on three routes and nowhere else.

RouteWho calls itPurpose
GET /app/userThe agentTo learn who a token belongs to. The answer is the user's uuid and profile fields, without the user's connected sources.
POST /app/copilot/authorizeThe agentTo ask whether the user's roles allow a model and the tools of the request.
PUT /app/usageThe agentTo add to the user's use of the agent.

A request that carries the header X-Workspace-Client is always authenticated with the cookie and never with a token, so a token that is sent to GET /app/user together with that header is refused. A token sent to any other route is refused as well. In both cases the answer is the status 401 with the detail "Could not validate credentials". Every request must also come from an allowed origin, so the agent sends the header Origin with the value of its WORKSPACE_ORIGIN setting, which has to be one of the API's BACKEND_CORS_ORIGINS.

The routes POST /app/copilot/authorize and PUT /app/usage change what a user may do or has used, so they have a second check. The agent must send the header X-Metering-Authorization: Bearer <metering token>, where the metering token is a JWT, signed with the API's JWT_SECRET, that the operator creates with python -m scripts.create_metering_token and gives to the agent as AGENT_METERING_TOKEN. The API requires the token to expire, and it expires after 365 days unless --days says otherwise. A request without the header or with a wrong token receives the status 403, and the detail says "Usage can only be reported by the Agent service". When the token has expired the detail says so and names the command that mints a new one. Until the token is replaced the agent cannot answer questions, because it cannot check the user.

What the agent asks​

For every request to an endpoint under /v1, the agent reads the bearer token and asks the API who it belongs to. In the default mode, AGENT_AUTH=workspace, the steps are these:

  1. A request without a token, or with a token longer than 4,096 characters, is refused with 401 and WWW-Authenticate: Bearer.
  2. The agent looks the token up in its cache. The cache holds the user of a token for AGENT_AUTH_CACHE_SECONDS, which is 30 seconds by default, and it remembers a token that the API refused for at most 10 seconds. The cache stores a hash of the token, never the token.
  3. When the token is not in the cache, the agent calls GET /app/user. Calls for the same token that arrive together share one answer, and the agent makes at most 32 of these calls at once and queues up to 256 more. A 401 or 403 from the API means the token is refused. An unreachable API means the status 503.
  4. For every request to /v1/query, the agent calls POST /app/copilot/authorize with the agent's id, the model and the tools of the request. The answer says whether the model is allowed, which tools the user's roles deny, and which capabilities are switched off for the user, such as web search. The agent leaves the denied tools out, refuses a denied model, and tells the model which capabilities are off.
  5. When a question starts, and not when it continues, the agent calls PUT /app/usage to count one use. The API answers 422 with a message when the user has reached the daily limit, and the agent shows the message as the answer. When a request ends, the agent reports its tokens with the same route.

Because the agent caches who a token belongs to, a user whose session has ended can still use the agent for up to the cache time. Set AGENT_AUTH_CACHE_SECONDS to 0 to check every request.

With AGENT_AUTH=none the agent does not ask anyone. Every request is anonymous, nothing is counted and no role is applied. That mode is for a development machine.

Calls from an agent that is not the built-in agent​

Any agent that implements the query protocol can use the same mechanism. The agent token identifies the user to the API, and the metering token identifies the agent. An agent that the user connects with its own agents.json and headers is authenticated with the headers that the user entered, and the web app does not send it an agent token.