feat(net/alcoholic_jwt): Implement Error for ValidationError
This allows using it unchanged in contexts where working with &dyn Error is desirable, such as when using anyhow. Change-Id: Ide34025e432204546b2c80b14b870af42dbfbb44 Reviewed-on: https://cl.tvl.fyi/c/depot/+/5615 Tested-by: BuildkiteCI Reviewed-by: tazjin <tazjin@tvl.su> Autosubmit: grfn <grfn@gws.fyi>
This commit is contained in:
parent
e20f7695fa
commit
d377830281
1 changed files with 34 additions and 0 deletions
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue