File size: 986 Bytes
0c8f7e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from fastapi import Request, status
from fastapi.responses import JSONResponse
from loguru import logger
import os


auth_tokens = os.getenv("AUTH_TOKENS", "").split(",") if os.getenv("AUTH_TOKENS") else []

async def auth_middleware(request: Request, call_next):
    # skip authentication if the auth_tokens list is empty
    if not len(auth_tokens):
        return await call_next(request)
    # authenticate all requests except the /health endpoint
    if request.url.path != "/health":
        auth_token = request.headers.get("Authorization")
        logger.bind(
            path=request.url.path,
            method=request.method,
            auth_token=auth_token,
        ).debug("Received request")
        if not auth_token or auth_token not in auth_tokens:
            return JSONResponse(
                status_code=status.HTTP_401_UNAUTHORIZED,
                content={"error": "Unauthorized"},
            )
    response = await call_next(request)
    return response