use bh_jws_utils::SigningAlgorithm;
use crate::{holder::HolderError, verifier::VerifierError, DecodingError};
#[derive(strum_macros::Display, Debug, PartialEq)]
pub enum Error {
#[strum(to_string = "Format error: {0}")]
Format(FormatError),
#[strum(to_string = "Signature error: {0}")]
Signature(SignatureError),
#[strum(to_string = "Decoding error: {0}")]
Decoding(DecodingError),
#[strum(to_string = "Jwt not yet valid: current time is {0}, nbf is {1}")]
JwtNotYetValid(u64, u64),
#[strum(to_string = "Jwt expired, current time is {0}, expiration is {1}")]
JwtExpired(u64, u64),
}
impl bherror::BhError for Error {}
#[derive(strum_macros::Display, Debug, PartialEq, Clone)]
pub enum FormatError {
#[strum(to_string = "Invalid SD-JWT format")]
InvalidSdJwtFormat,
#[strum(to_string = "Provided JWT is not parsable")]
NonParseableJwt,
#[strum(to_string = "Invalid VC schema")]
InvalidVcSchema,
#[strum(to_string = "Invalid Iat format. Iat needs to be a number")]
InvalidIatFormat,
#[strum(to_string = "Invalid disclosure: {0}")]
InvalidDisclosure(String),
}
impl bherror::BhError for FormatError {}
#[derive(strum_macros::Display, Debug, PartialEq, Clone)]
pub enum SignatureError {
#[strum(to_string = "Invalid Jwt signature")]
InvalidJwtSignature,
#[strum(to_string = "Missing signature verifier for algorithm {0}")]
MissingSignatureVerifier(SigningAlgorithm),
#[strum(to_string = "Public key lookup failed")]
PublicKeyLookupFailed,
}
impl bherror::BhError for SignatureError {}
impl Error {
pub(crate) fn to_holder_error(&self) -> HolderError {
match self {
Self::Format(error) => HolderError::Format(error.clone()),
Self::Signature(error) => HolderError::Signature(error.clone()),
Self::Decoding(error) => HolderError::Decoding(error.clone()),
Self::JwtExpired(time, nbf) => HolderError::JwtExpired(*time, *nbf),
Self::JwtNotYetValid(time, exp) => HolderError::JwtNotYetValid(*time, *exp),
}
}
pub(crate) fn to_verifier_error(&self) -> VerifierError {
match self {
Self::Format(error) => VerifierError::Format(error.clone()),
Self::Signature(error) => VerifierError::Signature(error.clone()),
Self::Decoding(error) => VerifierError::Decoding(error.clone()),
Self::JwtExpired(time, nbf) => VerifierError::JwtExpired(*time, *nbf),
Self::JwtNotYetValid(time, exp) => VerifierError::JwtNotYetValid(*time, *exp),
}
}
}
pub type Result<T, E> = bherror::Result<T, E>;