Skip to content

Self-improving AI Employees for everyone

3.7 KB
Raw Blame
"""Authentication middleware for route protection."""

import logging
from fastapi import Request
from fastapi.responses import JSONResponse
from starlette.middleware.base import BaseHTTPMiddleware

from core.auth_cookies import get_session_token
from core.container import container
from services.ws_handler_registry import is_public_ingress_path

logger = logging.getLogger(__name__)

# Public routes that don't require authentication
PUBLIC_PATHS = frozenset(
    [
        "/health",
        "/health/ready",  # readiness probe for the desktop shell splash / CI
        "/api/desktop/shutdown",  # desktop shell; gated by X-Desktop-Token, not the cookie
        "/docs",
        "/openapi.json",
        "/redoc",
        "/api/auth/status",
        "/api/auth/login",
        "/api/auth/register",
        "/api/auth/logout",
        "/ws/internal",  # Internal WebSocket for Temporal workers
        "/api/browser/owner/command",  # Owner route verifies scoped signed forwarding itself.
    ]
)

# Path prefixes that are public. ``/mcp/`` is the CLI-agent MCP server, which
# enforces its own per-batch bearer-token auth (cookies don't apply to it), so
# it bypasses this cookie gate.
PUBLIC_PREFIXES = ("/webhook/", "/mcp/")


class AuthMiddleware(BaseHTTPMiddleware):
    """Middleware to protect routes requiring authentication."""

    async def dispatch(self, request: Request, call_next):
        path = request.url.path

        # Allow public paths
        if self._is_public_path(path):
            return await call_next(request)

        # Static SPA shell, built assets, and client-side routes carry no
        # secrets and must load BEFORE login (the SPA renders the login page
        # itself). When the container serves the client on a single port, any
        # GET/HEAD that is not an API or WebSocket call is the SPA — treat it
        # as public. The protected surface (/api/*, /ws/*) stays gated below.
        if request.method in ("GET", "HEAD") and not path.startswith(("/api/", "/ws/")):
            return await call_next(request)

        # Get settings
        settings = container.settings()

        # Check if auth is disabled (VITE_AUTH_ENABLED=false)
        if settings.vite_auth_enabled and settings.vite_auth_enabled.lower() == "false":
            # Auth disabled - set anonymous user and allow request
            request.state.user_id = 0
            request.state.user_email = "anonymous"
            request.state.is_owner = True
            return await call_next(request)

        # Auth enabled - check token
        token = get_session_token(request.cookies, settings)

        if not token:
            return JSONResponse(status_code=401, content={"detail": "Not authenticated"})

        # Verify token
        user_auth = container.user_auth_service()
        payload = user_auth.verify_token(token)

        if not payload:
            return JSONResponse(status_code=401, content={"detail": "Invalid or expired session"})

        # Attach user info to request state for downstream handlers
        request.state.user_id = payload.get("sub")
        request.state.user_email = payload.get("email")
        request.state.is_owner = payload.get("is_owner", False)

        return await call_next(request)

    def _is_public_path(self, path: str) -> bool:
        """Check if path is public (no auth required)."""
        # Exact match
        if path in PUBLIC_PATHS:
            return True

        # Prefix match
        for prefix in PUBLIC_PREFIXES:
            if path.startswith(prefix):
                return True

        # Plugin routes that authenticate each request themselves
        # (services.ws_handler_registry.register_public_ingress_path).
        return is_public_ingress_path(path)