-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
66 lines (55 loc) · 1.99 KB
/
Copy pathauth.py
File metadata and controls
66 lines (55 loc) · 1.99 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from typing import Optional
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from src.core.security.providers import (
AuthenticationProvider,
JWTAuthProvider,
)
PUBLIC_PATHS = frozenset(
{
"/health",
"/live",
"/ready",
"/metrics",
"/docs",
"/docs/",
"/redoc",
"/redoc/",
"/openapi.json",
"/api/v1/auth/login",
"/api/v1/auth/register",
"/api/v1/auth/refresh",
}
)
class AuthenticationMiddleware(BaseHTTPMiddleware):
def __init__(self, app, providers: Optional[list[AuthenticationProvider]] = None):
super().__init__(app)
self._providers = providers or [JWTAuthProvider()]
async def dispatch(
self, request: Request, call_next: RequestResponseEndpoint
) -> Response:
if request.method == "OPTIONS":
return await call_next(request)
path = request.url.path.rstrip("/")
if path in PUBLIC_PATHS:
return await call_next(request)
auth_header = request.headers.get("Authorization")
if not auth_header or not auth_header.startswith("Bearer "):
return JSONResponse(
status_code=401,
content={"detail": "Authorization header missing or malformed"},
)
token = auth_header.split(" ", 1)[1]
for provider in self._providers:
result = await provider.authenticate(token, request)
if result is not None:
request.state.user_id = result["user_id"]
request.state.auth_provider = result["provider"]
request.state.token_payload = result.get("payload", {})
response = await call_next(request)
return response
return JSONResponse(
status_code=401,
content={"detail": "Invalid or expired token"},
)