Skip to main content

huskarl_core/crypto/verifier/
error.rs

1use snafu::Snafu;
2
3use crate::BoxedError;
4
5/// Errors that could occur during verification.
6#[derive(Debug, Snafu)]
7#[snafu(visibility(pub(crate)))]
8pub enum VerifyError<E: crate::Error> {
9    /// No key matched the requested algorithm/kid pair.
10    #[snafu(display("no matching key"))]
11    NoMatchingKey,
12    /// Multiple keys matched but the token has no `kid` to disambiguate.
13    #[snafu(display("ambiguous key: multiple keys match but token has no kid"))]
14    AmbiguousKeyMatch,
15    /// Signature mismatch, verification failed.
16    #[snafu(display("signature mismatch"))]
17    SignatureMismatch,
18    /// Other kinds of errors that could occur during verification.
19    #[snafu(transparent)]
20    Other {
21        /// The underlying error.
22        source: E,
23    },
24}
25
26impl<E: crate::Error> crate::Error for VerifyError<E> {
27    fn is_retryable(&self) -> bool {
28        match self {
29            VerifyError::NoMatchingKey
30            | VerifyError::AmbiguousKeyMatch
31            | VerifyError::SignatureMismatch => false,
32            VerifyError::Other { source } => source.is_retryable(),
33        }
34    }
35}
36
37/// Errors that could occur while trying to create a verifier.
38#[derive(Debug, Snafu)]
39pub enum CreateVerifierError {
40    /// The key is unsupported.
41    #[snafu(display("Unsupported key"))]
42    UnsupportedKey,
43    /// No JWKS URI was provided to the verifier factory.
44    #[snafu(display("A JWKS URI is required to build a JWS verifier"))]
45    MissingJwksUri,
46    /// Other kinds of errors that may occur while creating a verifier.
47    #[snafu(transparent)]
48    Other {
49        /// The underlying error.
50        source: BoxedError,
51    },
52}
53
54impl crate::Error for CreateVerifierError {
55    fn is_retryable(&self) -> bool {
56        match self {
57            CreateVerifierError::UnsupportedKey | CreateVerifierError::MissingJwksUri => false,
58            CreateVerifierError::Other { source } => source.is_retryable(),
59        }
60    }
61}