-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddlewares.py
36 lines (28 loc) · 1.27 KB
/
middlewares.py
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
from fastapi import Request, Response
from loguru import logger
from starlette.middleware.base import (
BaseHTTPMiddleware, RequestResponseEndpoint)
from fastapi.responses import JSONResponse
from schemas.base import ErrorResponseBody, HTTPException
from utils.utils import getReqPath, verifyToken
from conf.config import config
class Middleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
logger.debug("Request: " + str(request.__dict__))
response = await call_next(request)
logger.debug("Response: " + str(response.__dict__))
return response
class AuthMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
try:
if getReqPath(request) not in config.ignore_auth_urls:
authorization = request.headers.get("authorization")
verifyToken(authorization)
except HTTPException as e:
return JSONResponse(
status_code=e.status_code,
content=ErrorResponseBody(e.status_code, e.message).__dict__,
headers=e.headers
)
response = await call_next(request)
return response