auth_middleware
diapason.server.auth_middleware
¶
API key authentication middleware for the Diapason server.
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.
RateLimitMiddleware
¶
Bases: BaseHTTPMiddleware
Throttle authenticated API traffic per credential and client address.
check_bind_safety(host, *, api_key)
¶
Refuse to bind non-loopback without an API key.
Raises SystemExit if host is not a loopback address and
api_key is empty.
check_cors_safety(host, origins)
¶
Reject credentialed wildcard CORS on a non-loopback listener.
ensure_local_api_key(explicit_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.
est_route_de_gestes(path)
¶
Une route du mode gestes, dont le débit normal est élevé.
est_route_de_transfert(path)
¶
Une route de transfert de fichiers, authentifiée par session.
generate_api_key()
¶
Generate a 256-bit Diapason API key.
websocket_authorized(websocket, expected_key)
¶
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.
websocket_response_subprotocol(websocket)
¶
Select the non-secret Diapason protocol when a browser offers it.