Coverage for netrun_errors / auth.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-12-15 18:37 +0000

1""" 

2Authentication-related exceptions for Netrun Systems. 

3 

4All authentication exceptions return HTTP 401 Unauthorized status codes 

5with specific error codes for different authentication failure scenarios. 

6""" 

7 

8from typing import Any, Dict, Optional 

9 

10from fastapi import status 

11 

12from .base import NetrunException 

13 

14 

15class InvalidCredentialsError(NetrunException): 

16 """ 

17 Raised when user provides invalid email/password combination. 

18 

19 Status Code: 401 Unauthorized 

20 Error Code: AUTH_INVALID_CREDENTIALS 

21 """ 

22 

23 def __init__( 

24 self, 

25 message: str = "Invalid email or password", 

26 details: Optional[Dict[str, Any]] = None, 

27 correlation_id: Optional[str] = None, 

28 ): 

29 super().__init__( 

30 status_code=status.HTTP_401_UNAUTHORIZED, 

31 error_code="AUTH_INVALID_CREDENTIALS", 

32 message=message, 

33 details=details, 

34 correlation_id=correlation_id, 

35 ) 

36 

37 

38class TokenExpiredError(NetrunException): 

39 """ 

40 Raised when authentication token has expired. 

41 

42 Status Code: 401 Unauthorized 

43 Error Code: AUTH_TOKEN_EXPIRED 

44 """ 

45 

46 def __init__( 

47 self, 

48 message: str = "Authentication token has expired", 

49 details: Optional[Dict[str, Any]] = None, 

50 correlation_id: Optional[str] = None, 

51 ): 

52 super().__init__( 

53 status_code=status.HTTP_401_UNAUTHORIZED, 

54 error_code="AUTH_TOKEN_EXPIRED", 

55 message=message, 

56 details=details, 

57 correlation_id=correlation_id, 

58 ) 

59 

60 

61class TokenInvalidError(NetrunException): 

62 """ 

63 Raised when authentication token is malformed or invalid. 

64 

65 Status Code: 401 Unauthorized 

66 Error Code: AUTH_TOKEN_INVALID 

67 """ 

68 

69 def __init__( 

70 self, 

71 message: str = "Authentication token is invalid", 

72 details: Optional[Dict[str, Any]] = None, 

73 correlation_id: Optional[str] = None, 

74 ): 

75 super().__init__( 

76 status_code=status.HTTP_401_UNAUTHORIZED, 

77 error_code="AUTH_TOKEN_INVALID", 

78 message=message, 

79 details=details, 

80 correlation_id=correlation_id, 

81 ) 

82 

83 

84class TokenRevokedError(NetrunException): 

85 """ 

86 Raised when authentication token has been explicitly revoked. 

87 

88 Status Code: 401 Unauthorized 

89 Error Code: AUTH_TOKEN_REVOKED 

90 """ 

91 

92 def __init__( 

93 self, 

94 message: str = "Authentication token has been revoked", 

95 details: Optional[Dict[str, Any]] = None, 

96 correlation_id: Optional[str] = None, 

97 ): 

98 super().__init__( 

99 status_code=status.HTTP_401_UNAUTHORIZED, 

100 error_code="AUTH_TOKEN_REVOKED", 

101 message=message, 

102 details=details, 

103 correlation_id=correlation_id, 

104 ) 

105 

106 

107class AuthenticationRequiredError(NetrunException): 

108 """ 

109 Raised when endpoint requires authentication but none provided. 

110 

111 Status Code: 401 Unauthorized 

112 Error Code: AUTH_REQUIRED 

113 """ 

114 

115 def __init__( 

116 self, 

117 message: str = "Authentication is required to access this resource", 

118 details: Optional[Dict[str, Any]] = None, 

119 correlation_id: Optional[str] = None, 

120 ): 

121 super().__init__( 

122 status_code=status.HTTP_401_UNAUTHORIZED, 

123 error_code="AUTH_REQUIRED", 

124 message=message, 

125 details=details, 

126 correlation_id=correlation_id, 

127 )