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("{0}")]
33 Refused(String),
34
35 #[error("password hashing failure")]
37 PasswordHash(String),
38
39 #[error("database error")]
40 Store(#[from] sqlx::Error),
41
42 #[error("corrupt stored data: {0}")]
45 Data(String),
46}
47
48impl From<AuthError> for CoreError {
49 fn from(err: AuthError) -> Self {
50 match err {
51 AuthError::InvalidCredentials
52 | AuthError::TooManyAttempts
53 | AuthError::SessionInvalid => CoreError::Unauthorized,
54 AuthError::InactiveAccount => CoreError::Forbidden("account is inactive".to_string()),
55 AuthError::PermissionDenied(perm) => CoreError::Forbidden(perm),
56 AuthError::Refused(reason) => CoreError::Forbidden(reason),
59 AuthError::PasswordHash(msg) => CoreError::Internal(msg),
60 AuthError::Store(e) => CoreError::Database(e),
61 AuthError::Data(msg) => CoreError::Internal(msg),
62 }
63 }
64}