use crate::error::ServiceError;
use std::fmt;
#[derive(Debug)]
pub enum AuthError {
InvalidApiKey,
InvalidTenant(String),
InvalidSession(String),
AuthorizationBackend(String),
AuthorizationBackendUnavailable(String),
MissingTenant,
MissingPermission,
}
impl fmt::Display for AuthError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AuthError::InvalidApiKey => write!(f, "invalid api key"),
AuthError::InvalidTenant(msg) => write!(f, "invalid tenant: {msg}"),
AuthError::InvalidSession(msg) => write!(f, "invalid session: {msg}"),
AuthError::AuthorizationBackend(msg) => {
write!(f, "authorization backend error: {msg}")
}
AuthError::AuthorizationBackendUnavailable(msg) => {
write!(f, "authorization backend unavailable: {msg}")
}
AuthError::MissingTenant => write!(f, "missing tenant"),
AuthError::MissingPermission => write!(f, "permission denied"),
}
}
}
impl std::error::Error for AuthError {}
impl From<AuthError> for ServiceError {
fn from(err: AuthError) -> Self {
match err {
AuthError::InvalidApiKey
| AuthError::InvalidTenant(_)
| AuthError::InvalidSession(_)
| AuthError::MissingTenant => ServiceError::Unauthenticated(err.to_string()),
AuthError::MissingPermission => ServiceError::PermissionDenied(err.to_string()),
AuthError::AuthorizationBackend(msg)
| AuthError::AuthorizationBackendUnavailable(msg) => ServiceError::Internal(msg),
}
}
}