-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
48 lines (37 loc) · 1.59 KB
/
Copy pathmain.py
File metadata and controls
48 lines (37 loc) · 1.59 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
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from starlette.exceptions import HTTPException as StarletteHTTPException
from api.middleware.internal_auth import InternalAuthMiddleware
from api.middleware.locale import get_locale_from_headers
from api.routers.seed_router import seed_router
from src.api.routers.auth_router import auth_router
from src.core import i18n
app = FastAPI(
title="Guardian Auth Service",
docs_url="/swagger",
root_path="/auth",
root_path_in_servers=True,
)
i18n.init()
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request: Request, exc: StarletteHTTPException) -> JSONResponse:
locale = get_locale_from_headers(request.headers)
error_key_map = {
"Invalid credentials": "invalid_credentials",
"Email already registered. Please sign in with password.": "email_exists",
"Google OAuth is not configured": "oauth_not_configured",
"Invalid Google token": "invalid_google_token",
"Google token verification unavailable": "google_unavailable",
"Invalid Google token audience": "invalid_audience",
"Unauthorized internal request": "unauthorized_internal",
}
detail = exc.detail
if isinstance(detail, str) and detail in error_key_map:
detail = i18n.localize_error_message(error_key_map[detail], locale)
return JSONResponse(
status_code=exc.status_code,
content={"detail": detail},
)
app.add_middleware(InternalAuthMiddleware)
app.include_router(auth_router)
app.include_router(seed_router, prefix="/seed")