trading_agent.gateway.access_guard
¶
Checks that a request to the gateway carries the access token.
HTTP requests send the token as Authorization: Bearer <token>. Browsers cannot set headers on a WebSocket, so a WebSocket sends it as the token query parameter instead.
Typical usage example:
guard = AccessGuard(secrets.gateway_access_token) router = fastapi.APIRouter(dependencies=[fastapi.Depends(guard.require_http_token)])
AccessGuard
¶
Compares the token a client presents with the configured one.
Creates a guard for one token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
access_token
|
str
|
The token every request must present. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
The token is empty, which would let anyone in. |
Source code in src/trading_agent/gateway/access_guard.py
is_valid
¶
Checks a presented token in constant time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
presented_token
|
str | None
|
The token the client sent, or None if it sent none. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the token matches. |
Source code in src/trading_agent/gateway/access_guard.py
require_http_token
¶
A FastAPI dependency that rejects HTTP requests without the token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The incoming request. |
required |
Raises:
| Type | Description |
|---|---|
HTTPException
|
The Authorization header is missing or holds the wrong token (status 401). |