Skip to content

main

validate_dojo_auth

Python
validate_dojo_auth(
    credentials: HTTPBasicCredentials = Depends(security),
) -> str | None

Checks the provided credentials against the CLI-provided password. Username is ignored (or set to 'mojo'), we just care about the password.

Source code in src/mujoco_mojo/utils/layers/dojo/main.py
Python
def validate_dojo_auth(
    credentials: HTTPBasicCredentials = Depends(security),
) -> str | None:
    """
    Checks the provided credentials against the CLI-provided password. Username is ignored (or set to 'mojo'), we just care about the password.
    """
    if not shared.AUTH_PASSWORD:
        return None

    if not credentials or credentials.password != shared.AUTH_PASSWORD:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect Mojo Password",
            headers={"WWW-Authenticate": "Basic"},
        )
    return credentials.username

auth_exception_handler async

Python
auth_exception_handler(
    request: Request, exc: HTTPException
) -> Response

Catch 401 errors and render our custom HTML page. Crucial: We MUST include the 'WWW-Authenticate' header so the browser knows it should still try to show the login popup first.

Source code in src/mujoco_mojo/utils/layers/dojo/main.py
Python
@dojo_app.exception_handler(status.HTTP_401_UNAUTHORIZED)
async def auth_exception_handler(request: Request, exc: HTTPException) -> Response:
    """
    Catch 401 errors and render our custom HTML page.
    Crucial: We MUST include the 'WWW-Authenticate' header so the browser
    knows it should still try to show the login popup first.
    """
    return shared.templates.TemplateResponse(
        request=request,
        name="error.html",
        context={
            "request": request,
            "status_code": 401,
            "title": "Access Denied",
            "message": "The Mojo Dojo is currently locked. Correct credentials are required to enter.",
            "button_text": "Try Again",
        },
        status_code=status.HTTP_401_UNAUTHORIZED,
        headers={"WWW-Authenticate": "Basic"},
    )