#[derive(Debug, thiserror::Error)]
pub enum JoseError {
#[error("cryptographic error: {0}")]
Crypto(#[from] kryptering::Error),
#[error("invalid token format: {0}")]
InvalidToken(String),
#[error("invalid header: {0}")]
InvalidHeader(String),
#[error("invalid claims: {0}")]
InvalidClaims(String),
#[error("unsupported algorithm: {0}")]
UnsupportedAlgorithm(String),
#[error("key error: {0}")]
Key(String),
#[error("token expired")]
Expired,
#[error("token not yet valid")]
NotYetValid,
#[error("invalid issuer")]
InvalidIssuer,
#[error("invalid audience")]
InvalidAudience,
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("base64 decode error: {0}")]
Base64(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorCategory {
UnsupportedCapability,
InvalidStructure,
PolicyRejection,
AuthenticationFailure,
KeyMaterial,
Cryptographic,
Encoding,
}
impl JoseError {
pub fn category(&self) -> ErrorCategory {
match self {
JoseError::UnsupportedAlgorithm(_) => ErrorCategory::UnsupportedCapability,
JoseError::InvalidToken(_)
| JoseError::InvalidHeader(_)
| JoseError::InvalidClaims(_) => {
if self.is_own_authentication_failure() {
ErrorCategory::AuthenticationFailure
} else {
ErrorCategory::InvalidStructure
}
}
JoseError::Expired
| JoseError::NotYetValid
| JoseError::InvalidIssuer
| JoseError::InvalidAudience => ErrorCategory::PolicyRejection,
JoseError::Key(_) => ErrorCategory::KeyMaterial,
JoseError::Crypto(_) => ErrorCategory::Cryptographic,
JoseError::Json(_) | JoseError::Base64(_) => ErrorCategory::Encoding,
}
}
pub fn is_authentication_failure(&self) -> bool {
match self {
JoseError::Crypto(_) => true,
JoseError::InvalidToken(message) => message == crate::SIGNATURE_VERIFICATION_FAILED,
_ => false,
}
}
fn is_own_authentication_failure(&self) -> bool {
match self {
JoseError::InvalidToken(message) => message == crate::SIGNATURE_VERIFICATION_FAILED,
_ => false,
}
}
}
pub type Result<T> = std::result::Result<T, JoseError>;