macaroon/
error.rs

1use std::{num, str, string};
2
3/// Represents all of the errors that can arise when creating, deserializing, or verifying macaroons.
4#[derive(Debug)]
5pub enum MacaroonError {
6    /// Only raised by the [`initialize()`](crate::initialize) function, when there is a problem
7    /// initializing the lower-level crypto library
8    InitializationError,
9
10    /// Represents a runtime error in the lower-level cryptographic library, or situations like
11    /// zero-length ciphertext.
12    CryptoError(&'static str),
13
14    /// Can occur when constructing or deserializing a [`Macaroon`](crate::Macaroon) and expected fields are not present.
15    IncompleteMacaroon(&'static str),
16
17    /// Can occur when constructing or deserializing [`Caveat`](crate::Caveat) and expected fields are not present.
18    IncompleteCaveat(&'static str),
19
20    /// Represents a broad category of issues when parsing a macaroon token in any format.
21    DeserializationError(String),
22
23    /// Arises when verifying a [`Macaroon`](crate::Macaroon), when it has any caveat not
24    /// satisfied by any "exact" or "general" satisfiers configured on the
25    /// [`Verifier`](crate::Verifier). Indicates a failure to authenticate the macaroon.
26    CaveatNotSatisfied(String),
27
28    /// Arises when verifying a [`Macaroon`](crate::Macaroon), with a set of discharges configured
29    /// on the verifier, and one or more are not used during the verification process. Indicates a
30    /// failure to authenticate the macaroon.
31    DischargeNotUsed,
32
33    /// Arises when verifying a [`Macaroon`](crate::Macaroon), and the signature does not match
34    /// what is expected. Indicates a failure to authenticate the macaroon.
35    InvalidSignature,
36}
37
38impl From<serde_json::Error> for MacaroonError {
39    fn from(error: serde_json::Error) -> MacaroonError {
40        MacaroonError::DeserializationError(format!("{}", error))
41    }
42}
43
44impl From<string::FromUtf8Error> for MacaroonError {
45    fn from(error: string::FromUtf8Error) -> MacaroonError {
46        MacaroonError::DeserializationError(format!("{}", error))
47    }
48}
49
50impl From<base64::DecodeError> for MacaroonError {
51    fn from(error: base64::DecodeError) -> MacaroonError {
52        MacaroonError::DeserializationError(format!("{}", error))
53    }
54}
55
56impl From<num::ParseIntError> for MacaroonError {
57    fn from(error: num::ParseIntError) -> MacaroonError {
58        MacaroonError::DeserializationError(format!("{}", error))
59    }
60}
61
62impl From<str::Utf8Error> for MacaroonError {
63    fn from(error: str::Utf8Error) -> MacaroonError {
64        MacaroonError::DeserializationError(format!("{}", error))
65    }
66}
67
68impl std::error::Error for MacaroonError {}
69
70impl std::fmt::Display for MacaroonError {
71    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
72        match self {
73            MacaroonError::InitializationError => write!(
74                f,
75                "Failed to initialize cryptographic library for this thread"
76            ),
77            MacaroonError::CryptoError(s) => write!(
78                f,
79                "Error performing lower-level cryptographic function: {}",
80                s
81            ),
82            MacaroonError::IncompleteMacaroon(s) => {
83                write!(f, "Macaroon was missing required field: {}", s)
84            }
85            MacaroonError::IncompleteCaveat(s) => {
86                write!(f, "Caveat was missing required field: {}", s)
87            }
88            MacaroonError::DeserializationError(s) => {
89                write!(f, "Failed to deserialize macaroon: {}", s)
90            }
91            MacaroonError::CaveatNotSatisfied(s) => write!(
92                f,
93                "Macaroon failed to verify because one or more caveats were not satisfied: {}",
94                s
95            ),
96            MacaroonError::DischargeNotUsed => write!(
97                f,
98                "Macaroon failed to verify because one or more discharges were not used"
99            ),
100            MacaroonError::InvalidSignature => write!(
101                f,
102                "Macaroon failed to verify because signature did not match"
103            ),
104        }
105    }
106}