INNER CODE UNIT · Python

optional_bearer_auth

shenmintao/marginalia · src/marginalia/main.py:245

async def optional_bearer_auth(request: Request, call_next):
    token = get_settings().marginalia_api_token
    if (
        not token
        or request.method == "OPTIONS"
        or request.url.path in PUBLIC_PROBE_PATHS
    ):
        return await call_next(request)
    auth = request.headers.get("authorization", "")
    prefix = "Bearer "
    if not auth.startswith(prefix) or not compare_digest(auth[len(prefix):], token):
        return JSONResponse(
            {"detail": "missing or invalid bearer token"},
            status_code=401,
            headers={"WWW-Authenticate": "Bearer"},
        )
    return await call_next(request)

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…