Index
auth
¶
Marianne Dashboard Authentication Module.
Provides authentication middleware and utilities for the dashboard API. Supports API key authentication with optional localhost bypass for development.
Classes¶
AuthMode
¶
Bases: Enum
Authentication modes.
AuthConfig
dataclass
¶
AuthConfig(mode=LOCALHOST_ONLY, api_keys=list(), localhost_bypass=True, excluded_paths=(lambda: ['/health', '/docs', '/openapi.json', '/redoc'])(), header_name='X-API-Key')
Authentication configuration.
Attributes:
| Name | Type | Description |
|---|---|---|
mode |
AuthMode
|
Authentication mode (disabled, api_key, localhost_only) |
api_keys |
list[str]
|
List of valid API key hashes (SHA256). Keys are hashed at load time by from_env() so plaintext is never stored. |
localhost_bypass |
bool
|
Allow localhost to bypass auth when mode is api_key |
excluded_paths |
list[str]
|
Paths that don't require authentication |
header_name |
str
|
Header name for API key |
Functions¶
from_env
classmethod
¶
Create config from environment variables.
Environment variables
MZT_AUTH_MODE: disabled, api_key, or localhost_only MZT_API_KEYS: Comma-separated API keys MZT_LOCALHOST_BYPASS: true/false
Source code in src/marianne/dashboard/auth/__init__.py
AuthMiddleware
¶
Bases: BaseHTTPMiddleware
Authentication middleware for FastAPI.
Handles authentication based on configured mode: - disabled: All requests allowed - api_key: Requires valid API key in header - localhost_only: Only localhost connections allowed
When localhost_bypass is enabled (default), localhost connections bypass API key checks.
Initialize middleware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
ASGIApp
|
ASGI application (FastAPI or Starlette app) |
required |
config
|
AuthConfig | None
|
Authentication configuration |
None
|
Source code in src/marianne/dashboard/auth/__init__.py
Functions¶
dispatch
async
¶
Process authentication for each request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
Incoming request |
required |
call_next
|
RequestResponseEndpoint
|
Next middleware/handler |
required |
Returns:
| Type | Description |
|---|---|
Response
|
Response from handler or 401/403 error |
Source code in src/marianne/dashboard/auth/__init__.py
Functions¶
hash_api_key
¶
Hash an API key for secure storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Plain text API key |
required |
Returns:
| Type | Description |
|---|---|
str
|
SHA256 hash of the key |
verify_api_key
¶
Verify an API key against stored hashes.
Uses constant-time comparison to prevent timing attacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
API key to verify |
required |
hashed_keys
|
list[str]
|
List of valid hashed keys |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if key is valid |
Source code in src/marianne/dashboard/auth/__init__.py
is_localhost
¶
Check if request is from localhost.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
FastAPI request object |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if request is from localhost |