1use laterite_core::CoreError;
4use thiserror::Error;
5
6#[derive(Debug, Error)]
7pub enum AuthError {
8 #[error("invalid credentials")]
11 InvalidCredentials,
12
13 #[error("too many attempts")]
15 TooManyAttempts,
16
17 #[error("session invalid")]
19 SessionInvalid,
20
21 #[error("account is inactive")]
23 InactiveAccount,
24
25 #[error("permission denied: {0}")]
27 PermissionDenied(String),
28
29 #[error("password hashing failure")]
31 PasswordHash(String),
32
33 #[error("database error")]
34 Store(#[from] sqlx::Error),
35
36 #[error("corrupt stored data: {0}")]
39 Data(String),
40}
41
42impl From<AuthError> for CoreError {
43 fn from(err: AuthError) -> Self {
44 match err {
45 AuthError::InvalidCredentials
46 | AuthError::TooManyAttempts
47 | AuthError::SessionInvalid => CoreError::Unauthorized,
48 AuthError::InactiveAccount => CoreError::Forbidden("account is inactive".to_string()),
49 AuthError::PermissionDenied(perm) => CoreError::Forbidden(perm),
50 AuthError::PasswordHash(msg) => CoreError::Internal(msg),
51 AuthError::Store(e) => CoreError::Database(e),
52 AuthError::Data(msg) => CoreError::Internal(msg),
53 }
54 }
55}