use std::fmt;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
InvalidSyntax,
InvalidUtf8,
InvalidJson(serde_json::Error),
UnsupportedAlg,
NoSuitableKey,
AlgoMismatch,
InvalidSignature,
ExpMissing,
Expired,
NotValidYet,
ValidationError(String),
InvalidJwk(String),
KeyRejected(aws_lc_rs::error::KeyRejected),
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::InvalidJson(error) => Some(error),
Self::KeyRejected(error) => Some(error),
_ => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidSyntax => {
write!(f, "invalid surface level syntax (wrong number of dots, base64)")
},
Self::InvalidUtf8 => write!(f, "invalid UTF-8"),
Self::InvalidJson(error) => {
write!(f, "invalid JSON")?;
if f.alternate() {
write!(f, ": {error}")?;
}
Ok(())
},
Self::UnsupportedAlg => write!(f, "alg not supported"),
Self::NoSuitableKey => write!(f, "no suitable key found for alg/kid"),
Self::AlgoMismatch => write!(f, "key does not match the given alg"),
Self::InvalidSignature => write!(f, "signature is invalid"),
Self::ExpMissing => write!(f, "`exp` claim required but missing"),
Self::Expired => write!(f, "JWT is expired (`exp`)"),
Self::NotValidYet => write!(f, "JWT is not valid yet (`nbf`)"),
Self::ValidationError(msg) => write!(f, "failed to validate: {msg}"),
Self::InvalidJwk(msg) => write!(f, "invalid JWK: {msg}"),
Self::KeyRejected(err) => {
write!(f, "cryptographic key rejected")?;
if f.alternate() {
write!(f, ": {err}")?;
}
Ok(())
}
}
}
}
impl From<aws_lc_rs::error::KeyRejected> for Error {
fn from(value: aws_lc_rs::error::KeyRejected) -> Self {
Self::KeyRejected(value)
}
}