Skip to content

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'])(), public_orchestra=False, 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

public_orchestra bool

Serve the orchestra view (/orchestra, its API and the static assets the page loads) to every client, whatever the mode (GH #468, #387). The orchestra projection carries only score names, sheet numbers, instruments, outcomes and times; this is the one surface meant for a public hostname. Off by default.

header_name str

Header name for API key

Methods:
from_env classmethod
from_env()

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 MZT_PUBLIC_ORCHESTRA: true/false (default false)

Source code in src/marianne/dashboard/auth/__init__.py
@classmethod
def from_env(cls) -> AuthConfig:
    """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
        MZT_PUBLIC_ORCHESTRA: true/false (default false)
    """
    mode_str = os.getenv("MZT_AUTH_MODE", "localhost_only")
    mode = AuthMode(mode_str.lower())

    api_keys_str = os.getenv("MZT_API_KEYS", "")
    raw_keys = [k.strip() for k in api_keys_str.split(",") if k.strip()]
    # Hash keys immediately so plaintext is never stored in the config object
    hashed_keys = [hash_api_key(k) for k in raw_keys]

    localhost_bypass = os.getenv("MZT_LOCALHOST_BYPASS", "true").lower() == "true"
    public_orchestra = os.getenv("MZT_PUBLIC_ORCHESTRA", "false").lower() == "true"

    return cls(
        mode=mode,
        api_keys=hashed_keys,
        localhost_bypass=localhost_bypass,
        public_orchestra=public_orchestra,
    )

AuthMiddleware

AuthMiddleware(app, config=None)

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
def __init__(self, app: ASGIApp, config: AuthConfig | None = None):
    """Initialize middleware.

    Args:
        app: ASGI application (FastAPI or Starlette app)
        config: Authentication configuration
    """
    super().__init__(app)
    self.config = config or AuthConfig.from_env()
Methods:
dispatch async
dispatch(request, call_next)

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
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
    """Process authentication for each request.

    Args:
        request: Incoming request
        call_next: Next middleware/handler

    Returns:
        Response from handler or 401/403 error
    """
    # Check if path is excluded from auth
    path = request.url.path
    if self._is_excluded_path(path):
        return await call_next(request)
    if self.config.public_orchestra and is_public_orchestra_path(path):
        return await call_next(request)

    # Check authentication based on mode
    if self.config.mode == AuthMode.DISABLED:
        return await call_next(request)

    if self.config.mode == AuthMode.LOCALHOST_ONLY:
        if not is_localhost(request):
            return JSONResponse(
                status_code=status.HTTP_403_FORBIDDEN,
                content={"detail": "Access restricted to localhost"},
            )
        return await call_next(request)

    if self.config.mode == AuthMode.API_KEY:
        # Check localhost bypass
        if self.config.localhost_bypass and is_localhost(request):
            return await call_next(request)

        # Verify API key
        api_key = request.headers.get(self.config.header_name)
        if not api_key:
            return JSONResponse(
                status_code=status.HTTP_401_UNAUTHORIZED,
                content={"detail": "API key required"},
                headers={"WWW-Authenticate": "ApiKey"},
            )

        # Keys are already hashed at load time (from_env)
        if not verify_api_key(api_key, self.config.api_keys):
            return JSONResponse(
                status_code=status.HTTP_401_UNAUTHORIZED,
                content={"detail": "Invalid API key"},
            )

        return await call_next(request)

    # Unknown mode - deny by default
    return JSONResponse(
        status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
        content={"detail": "Invalid authentication configuration"},
    )

Functions:

hash_api_key

hash_api_key(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

Source code in src/marianne/dashboard/auth/__init__.py
def hash_api_key(key: str) -> str:
    """Hash an API key for secure storage.

    Args:
        key: Plain text API key

    Returns:
        SHA256 hash of the key
    """
    return hashlib.sha256(key.encode()).hexdigest()

verify_api_key

verify_api_key(key, hashed_keys)

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
def verify_api_key(key: str, hashed_keys: list[str]) -> bool:
    """Verify an API key against stored hashes.

    Uses constant-time comparison to prevent timing attacks.

    Args:
        key: API key to verify
        hashed_keys: List of valid hashed keys

    Returns:
        True if key is valid
    """
    key_hash = hash_api_key(key)
    return any(hmac.compare_digest(key_hash, stored_hash) for stored_hash in hashed_keys)

is_public_orchestra_path

is_public_orchestra_path(path)

Whether path belongs to the orchestra surface a public hostname serves.

Source code in src/marianne/dashboard/auth/__init__.py
def is_public_orchestra_path(path: str) -> bool:
    """Whether ``path`` belongs to the orchestra surface a public hostname serves."""
    return any(path == p or path.startswith(p + "/") for p in ORCHESTRA_PUBLIC_PATHS)

is_loopback_bind

is_loopback_bind(host)

Whether binding host keeps the listener on this machine only.

Source code in src/marianne/dashboard/auth/__init__.py
def is_loopback_bind(host: str) -> bool:
    """Whether binding ``host`` keeps the listener on this machine only."""
    candidate = host.strip().strip("[]").lower()
    if candidate in ("localhost", ""):
        return candidate == "localhost"
    try:
        return ipaddress.ip_address(candidate).is_loopback
    except ValueError:
        return False

exposure_warning

exposure_warning(host, config=None)

The startup warning a non-loopback bind deserves, or None (GH #387).

An operator who binds 0.0.0.0 with the default localhost_only mode gets a listener that answers every interface and then refuses every non-local client with 403: exposed, and useless. api_key with the localhost bypass still on, or with no keys loaded, is the same surface. Only api_key with keys and the bypass off, or disabled chosen on purpose, is silent here; disabled is still named so the choice is visible in the terminal.

Source code in src/marianne/dashboard/auth/__init__.py
def exposure_warning(host: str, config: AuthConfig | None = None) -> str | None:
    """The startup warning a non-loopback bind deserves, or None (GH #387).

    An operator who binds ``0.0.0.0`` with the default ``localhost_only``
    mode gets a listener that answers every interface and then refuses
    every non-local client with 403: exposed, and useless. ``api_key`` with
    the localhost bypass still on, or with no keys loaded, is the same
    surface. Only ``api_key`` with keys and the bypass off, or ``disabled``
    chosen on purpose, is silent here; ``disabled`` is still named so the
    choice is visible in the terminal.
    """
    if is_loopback_bind(host):
        return None
    cfg = config or AuthConfig.from_env()
    mode = cfg.mode.value
    if cfg.mode == AuthMode.LOCALHOST_ONLY:
        if cfg.public_orchestra:
            return (
                f"Binding {host} with MZT_AUTH_MODE={mode} and "
                "MZT_PUBLIC_ORCHESTRA=true: /orchestra, /api/orchestra/* and "
                "/static are served to every client; every other path refuses "
                "non-local clients with 403. This is the public-fleet shape; "
                "nothing else is exposed."
            )
        return (
            f"Binding {host} with MZT_AUTH_MODE={mode}: the dashboard will answer "
            "on every interface and refuse every non-local client with 403. "
            "For network exposure set MZT_AUTH_MODE=api_key, MZT_API_KEYS=<keys> "
            "and MZT_LOCALHOST_BYPASS=false; to publish only the orchestra view "
            "set MZT_PUBLIC_ORCHESTRA=true; otherwise bind 127.0.0.1."
        )
    if cfg.mode == AuthMode.API_KEY:
        problems = []
        if not cfg.api_keys:
            problems.append("MZT_API_KEYS is empty, so no client can authenticate")
        if cfg.localhost_bypass:
            problems.append(
                "MZT_LOCALHOST_BYPASS is true, so anything that reaches the "
                "loopback interface (a reverse proxy, a tunnel) skips the key check"
            )
        if problems:
            return (
                f"Binding {host} with MZT_AUTH_MODE=api_key: "
                + "; ".join(problems)
                + ". Set MZT_API_KEYS and MZT_LOCALHOST_BYPASS=false before exposing it."
            )
        return None
    return (
        f"Binding {host} with MZT_AUTH_MODE=disabled: every client on the network "
        "can read workspaces, logs and artifacts and control scores. Only do this "
        "behind an authenticating proxy."
    )

is_localhost

is_localhost(request)

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

Source code in src/marianne/dashboard/auth/__init__.py
def is_localhost(request: Request) -> bool:
    """Check if request is from localhost.

    Args:
        request: FastAPI request object

    Returns:
        True if request is from localhost
    """
    if not request.client:
        return False
    client_host = request.client.host
    if not client_host:
        return False
    # Include testclient which uses special addresses
    localhost_addresses = {"127.0.0.1", "::1", "localhost", "testclient"}
    # Also check for loopback pattern
    if client_host.startswith("127."):
        return True
    return client_host in localhost_addresses