use super::crypto::{grant_verifier, pop_verifier};
use super::persistence::grant::GrantStatus;
use crate::client_server::auth::SignupServiceError;
#[derive(Debug, thiserror::Error)]
pub enum AuthServiceError {
#[error("Invalid grant: {0}")]
InvalidGrant(#[from] grant_verifier::Error),
#[error("Invalid PoP proof: {0}")]
InvalidPopProof(#[from] pop_verifier::Error),
#[error("User not found")]
UserNotFound,
#[error("User already exists")]
UserAlreadyExists,
#[error("Grant not found")]
GrantNotFound,
#[error("Token required")]
SignupTokenRequired,
#[error("Invalid token")]
InvalidSignupToken,
#[error("Token already used")]
SignupTokenAlreadyUsed,
#[error("PoP nonce already used")]
NonceReplay,
#[error("Grant has been revoked")]
GrantRevoked,
#[error("Grant has expired")]
GrantExpired,
#[error("Invalid signup grant: {0}")]
InvalidSignupGrant(String),
#[error("Session not found")]
SessionNotFound,
#[error("Session has expired")]
SessionExpired,
#[error("Grant does not belong to authenticated user")]
GrantOwnershipMismatch,
#[error("Root capability required")]
RootCapabilityRequired,
#[error("Internal error: {0}")]
Internal(#[from] sqlx::Error),
}
impl From<GrantStatus> for AuthServiceError {
fn from(status: GrantStatus) -> Self {
match status {
GrantStatus::Revoked => Self::GrantRevoked,
GrantStatus::Expired => Self::GrantExpired,
}
}
}
impl From<SignupServiceError> for AuthServiceError {
fn from(error: SignupServiceError) -> Self {
match error {
SignupServiceError::UserAlreadyExists => Self::UserAlreadyExists,
SignupServiceError::SignupTokenRequired => Self::SignupTokenRequired,
SignupServiceError::InvalidSignupToken => Self::InvalidSignupToken,
SignupServiceError::SignupTokenAlreadyUsed => Self::SignupTokenAlreadyUsed,
SignupServiceError::Internal(e) => Self::Internal(e),
}
}
}