auth.py
 1  from typing import Any, Type
 2  
 3  from fastapi import status
 4  
 5  from .api_exception import APIException
 6  from ..utils.docs import responses
 7  
 8  
 9  class InvalidTokenError(APIException):
10      status_code = status.HTTP_401_UNAUTHORIZED
11      detail = "Invalid token"
12      description = "This access token is invalid or the session has expired."
13  
14  
15  class PermissionDeniedError(APIException):
16      status_code = status.HTTP_403_FORBIDDEN
17      detail = "Permission denied"
18      description = "The user is not allowed to use this endpoint."
19  
20  
21  def user_responses(default: type, *args: Type[APIException]) -> dict[int | str, dict[str, Any]]:
22      """api responses for user_auth dependency"""
23  
24      return responses(default, *args, InvalidTokenError)
25  
26  
27  def admin_responses(default: type, *args: Type[APIException]) -> dict[int | str, dict[str, Any]]:
28      """api responses for admin_auth dependency"""
29  
30      return user_responses(default, *args, PermissionDeniedError)
31  
32  
33  def internal_responses(default: type, *args: Type[APIException]) -> dict[int | str, dict[str, Any]]:
34      """api responses for admin_auth dependency"""
35  
36      return responses(default, *args, InvalidTokenError)