auth_middleware
auth_middleware
¶
API key authentication middleware for the Diapason server.
Classes¶
AuthMiddleware
¶
Bases: BaseHTTPMiddleware
Validates Authorization: Bearer <key> on /v1/* and /api/* routes.
Webhook routes and health checks are exempt — they use per-channel signature verification instead.
Source code in src/diapason/server/auth_middleware.py
RateLimitMiddleware
¶
RateLimitMiddleware(
app,
*,
requests_per_minute: int = 60,
burst_size: int = 10,
enabled: bool = True,
)
Bases: BaseHTTPMiddleware
Throttle authenticated API traffic per credential and client address.
Source code in src/diapason/server/auth_middleware.py
Functions:¶
est_route_de_gestes
¶
est_route_de_transfert
¶
generate_api_key
¶
ensure_local_api_key
¶
Return an API key, creating a permission-restricted local key if needed.
Precedence is an explicit configured value, DIAPASON_API_KEY (with
legacy environment aliases), then <DIAPASON_HOME>/auth/local_api_key.
The generated file and its parent are owner-only and creation is atomic.
Source code in src/diapason/server/auth_middleware.py
check_bind_safety
¶
Refuse to bind non-loopback without an API key.
Raises SystemExit if host is not a loopback address and
api_key is empty.
Source code in src/diapason/server/auth_middleware.py
check_cors_safety
¶
Reject credentialed wildcard CORS on a non-loopback listener.
Source code in src/diapason/server/auth_middleware.py
websocket_authorized
¶
Return True if a WebSocket connection presents the expected key.
AuthMiddleware is a BaseHTTPMiddleware and never sees WebSocket
upgrade requests, so streaming endpoints must check the token themselves
in the handshake before calling websocket.accept().
When expected_key is empty, authentication is disabled (the loopback /
local-only default, matching :class:AuthMiddleware) and all connections
are allowed. The token may be supplied either as a ?token= query
parameter for backwards compatibility, via an Authorization: Bearer
header for programmatic clients, or as a diapason-auth.<key> offered
subprotocol. The desktop uses the last form so access logs never contain
its credential in the request URL.
Source code in src/diapason/server/auth_middleware.py
websocket_response_subprotocol
¶
Select the non-secret Diapason protocol when a browser offers it.