MCPcopy Create free account
hub / github.com/dbunt1tled/python-fast-api / dispatch

Method dispatch

src/core/http/middleware/auth_bearer.py:37–71  ·  view source on GitHub ↗
(self, request: Request, call_next: RequestResponseEndpoint)

Source from the content-addressed store, hash-verified

35 self.logger = logging.getLogger(__name__)
36
37 async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
38 request.state.is_authenticated = False
39 if request.method == "OPTIONS" or AuthBearer._is_excluded_path(request.url.path):
40 return await call_next(request)
41
42 authorization = request.headers.get("Authorization")
43 if not authorization:
44 raise UnauthorizedException(error_no=ErrorNo.AUTHORIZATION_HEADER_NOT_FOUND, message="Unauthorized!")
45
46 try:
47 scheme, token = authorization.split(" ", 1)
48 if scheme.lower() != "bearer":
49 raise UnauthorizedException(error_no=ErrorNo.AUTHORIZATION_BEARER_FORMAT_ERROR, message="Unauthorized!")
50 except ValueError as e:
51 self.logger.error(str(e), exc_info=e)
52 raise UnauthorizedException(error_no=ErrorNo.AUTHORIZATION_BEARER_INVALID, message="Unauthorized!")
53
54 payload = self.hash_service.verify_token(token)
55 if not payload:
56 raise UnauthorizedException(
57 error_no=ErrorNo.AUTHORIZATION_BEARER_TOKEN_INVALID_OR_EXPIRED, message="Unauthorized!"
58 )
59
60 user = await self.user_service.get_by_id(int(payload.subject))
61 if not user:
62 raise UnauthorizedException(error_no=ErrorNo.AUTHORIZATION_USER_NOT_FOUND, message="Unauthorized!")
63 if user.status != UserStatus.ACTIVE:
64 raise UnauthorizedException(error_no=ErrorNo.AUTHORIZATION_USER_NOT_ACTIVE, message="Unauthorized!")
65 if user.session != payload.session:
66 raise UnauthorizedException(error_no=ErrorNo.AUTHORIZATION_USER_SESSION_INVALID, message="Unauthorized!")
67
68 request.state.user = user
69 request.state.is_authenticated = True
70
71 return await call_next(request)
72
73 @staticmethod
74 def _is_excluded_path(path: str) -> bool:

Callers

nothing calls this directly

Calls 5

_is_excluded_pathMethod · 0.80
verify_tokenMethod · 0.80
errorMethod · 0.45
get_by_idMethod · 0.45

Tested by

no test coverage detected