linkrs_server/auth/
error.rs1use thiserror::Error;
6
7use linkrs_core::error::codes::{ErrorCode, PublicError, ToPublicError};
8
9pub type AuthResult<T> = Result<T, AuthError>;
11
12#[derive(Error, Debug, Clone)]
14pub enum AuthError {
15 #[error("Authentication failed: {0}")]
16 AuthenticationFailed(String),
17
18 #[error("Username or password cannot be empty")]
19 EmptyCredentials,
20
21 #[error("Invalid username or password, {0} attempts remaining")]
22 InvalidCredentials(u32),
23
24 #[error("Maximum attempts exceeded")]
25 MaxAttemptsExceeded,
26
27 #[error("Account is locked: {0}")]
28 AccountLocked(String),
29
30 #[error("Authenticator error: {0}")]
31 AuthenticatorError(String),
32}
33
34impl ToPublicError for AuthError {
35 fn to_public_error(&self) -> PublicError {
36 PublicError::new(self.to_error_code(), self.to_public_message())
37 }
38
39 fn to_error_code(&self) -> ErrorCode {
40 match self {
41 AuthError::AuthenticationFailed(_) => ErrorCode::Unauthorized,
42 AuthError::EmptyCredentials => ErrorCode::InvalidInput,
43 AuthError::InvalidCredentials(_) => ErrorCode::Unauthorized,
44 AuthError::MaxAttemptsExceeded => ErrorCode::ResourceExhausted,
45 AuthError::AccountLocked(_) => ErrorCode::Forbidden,
46 AuthError::AuthenticatorError(_) => ErrorCode::InternalError,
47 }
48 }
49
50 fn to_public_message(&self) -> String {
51 self.to_string()
52 }
53}