1use bh_jws_utils::SigningAlgorithm;
17
18use crate::{holder::HolderError, verifier::VerifierError, DecodingError};
19
20#[derive(strum_macros::Display, Debug, PartialEq)]
22pub enum Error {
23 #[strum(to_string = "Format error: {0}")]
25 Format(FormatError),
26
27 #[strum(to_string = "Signature error: {0}")]
29 Signature(SignatureError),
30
31 #[strum(to_string = "Decoding error: {0}")]
33 Decoding(DecodingError),
34
35 #[strum(to_string = "Jwt not yet valid: current time is {0}, nbf is {1}")]
37 JwtNotYetValid(u64, u64),
38
39 #[strum(to_string = "Jwt expired, current time is {0}, expiration is {1}")]
41 JwtExpired(u64, u64),
42}
43
44impl bherror::BhError for Error {}
45
46#[derive(strum_macros::Display, Debug, PartialEq, Clone)]
48pub enum FormatError {
49 #[strum(to_string = "Invalid SD-JWT format")]
51 InvalidSdJwtFormat,
52
53 #[strum(to_string = "Provided JWT is not parsable")]
55 NonParseableJwt,
56
57 #[strum(to_string = "Invalid VC schema")]
59 InvalidVcSchema,
60
61 #[strum(to_string = "Invalid Iat format. Iat needs to be a number")]
63 InvalidIatFormat,
64
65 #[strum(to_string = "Invalid disclosure: {0}")]
67 InvalidDisclosure(String),
68}
69
70impl bherror::BhError for FormatError {}
71
72#[derive(strum_macros::Display, Debug, PartialEq, Clone)]
74pub enum SignatureError {
75 #[strum(to_string = "Invalid Jwt signature")]
77 InvalidJwtSignature,
78
79 #[strum(to_string = "Missing signature verifier for algorithm {0}")]
82 MissingSignatureVerifier(SigningAlgorithm),
83
84 #[strum(to_string = "Public key lookup failed")]
86 PublicKeyLookupFailed,
87}
88
89impl bherror::BhError for SignatureError {}
90
91impl Error {
92 pub(crate) fn to_holder_error(&self) -> HolderError {
93 match self {
94 Self::Format(error) => HolderError::Format(error.clone()),
95 Self::Signature(error) => HolderError::Signature(error.clone()),
96 Self::Decoding(error) => HolderError::Decoding(error.clone()),
97 Self::JwtExpired(time, nbf) => HolderError::JwtExpired(*time, *nbf),
98 Self::JwtNotYetValid(time, exp) => HolderError::JwtNotYetValid(*time, *exp),
99 }
100 }
101
102 pub(crate) fn to_verifier_error(&self) -> VerifierError {
103 match self {
104 Self::Format(error) => VerifierError::Format(error.clone()),
105 Self::Signature(error) => VerifierError::Signature(error.clone()),
106 Self::Decoding(error) => VerifierError::Decoding(error.clone()),
107 Self::JwtExpired(time, nbf) => VerifierError::JwtExpired(*time, *nbf),
108 Self::JwtNotYetValid(time, exp) => VerifierError::JwtNotYetValid(*time, *exp),
109 }
110 }
111}
112
113pub type Result<T, E> = bherror::Result<T, E>;