use laterite_core::CoreError;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum AuthError {
#[error("invalid credentials")]
InvalidCredentials,
#[error("too many attempts")]
TooManyAttempts,
#[error("session invalid")]
SessionInvalid,
#[error("account is inactive")]
InactiveAccount,
#[error("permission denied: {0}")]
PermissionDenied(String),
#[error("password hashing failure")]
PasswordHash(String),
#[error("database error")]
Store(#[from] sqlx::Error),
#[error("corrupt stored data: {0}")]
Data(String),
}
impl From<AuthError> for CoreError {
fn from(err: AuthError) -> Self {
match err {
AuthError::InvalidCredentials
| AuthError::TooManyAttempts
| AuthError::SessionInvalid => CoreError::Unauthorized,
AuthError::InactiveAccount => CoreError::Forbidden("account is inactive".to_string()),
AuthError::PermissionDenied(perm) => CoreError::Forbidden(perm),
AuthError::PasswordHash(msg) => CoreError::Internal(msg),
AuthError::Store(e) => CoreError::Database(e),
AuthError::Data(msg) => CoreError::Internal(msg),
}
}
}