diff options
Diffstat (limited to 'net/alcoholic_jwt')
-rw-r--r-- | net/alcoholic_jwt/src/lib.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/net/alcoholic_jwt/src/lib.rs b/net/alcoholic_jwt/src/lib.rs index 297bf2a99085..1b50a9117712 100644 --- a/net/alcoholic_jwt/src/lib.rs +++ b/net/alcoholic_jwt/src/lib.rs @@ -84,6 +84,8 @@ use openssl::rsa::Rsa; use openssl::sign::Verifier; use serde::de::DeserializeOwned; use serde_json::Value; +use std::error::Error; +use std::fmt::{self, Display}; use std::time::{Duration, SystemTime, UNIX_EPOCH}; #[cfg(test)] @@ -219,6 +221,38 @@ pub enum ValidationError { InvalidClaims(Vec<&'static str>), } +impl Error for ValidationError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + match self { + ValidationError::InvalidBase64(e) => Some(e), + ValidationError::OpenSSL(e) => Some(e), + ValidationError::JSON(e) => Some(e), + ValidationError::InvalidComponents + | ValidationError::InvalidJWK + | ValidationError::InvalidSignature + | ValidationError::InvalidClaims(_) => None, + } + } +} + +impl Display for ValidationError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ValidationError::InvalidComponents => { + f.write_str("Invalid number of token components in JWT") + } + ValidationError::InvalidBase64(_) => f.write_str("Invalid Base64 encoding in JWT"), + ValidationError::InvalidJWK => f.write_str("JWK decoding failed"), + ValidationError::InvalidSignature => f.write_str("JWT signature validation failed"), + ValidationError::OpenSSL(e) => write!(f, "SSL error: {}", e), + ValidationError::JSON(e) => write!(f, "JSON error: {}", e), + ValidationError::InvalidClaims(errs) => { + write!(f, "Invalid claims: {}", errs.join(", ")) + } + } + } +} + type JWTResult<T> = Result<T, ValidationError>; impl From<ErrorStack> for ValidationError { |