1use std::fmt;
2use std::string::FromUtf8Error;
3
4use base64::DecodeError;
5use crypto_common::InvalidLength;
6use digest::MacError;
7use serde_json::Error as JsonError;
8
9use self::Error::*;
10use crate::algorithm::AlgorithmType;
11
12#[derive(Debug)]
13pub enum Error {
14 AlgorithmMismatch(AlgorithmType, AlgorithmType),
15 Base64(DecodeError),
16 Format,
17 InvalidSignature,
18 Json(JsonError),
19 NoClaimsComponent,
20 NoHeaderComponent,
21 NoKeyId,
22 NoKeyWithKeyId(String),
23 NoSignatureComponent,
24 RustCryptoMac(MacError),
25 RustCryptoMacKeyLength(InvalidLength),
26 TooManyComponents,
27 Utf8(FromUtf8Error),
28 Signature(signature::Error),
29 InvalidKey,
30 #[cfg(feature = "openssl")]
31 OpenSsl(openssl::error::ErrorStack),
32}
33
34impl fmt::Display for Error {
35 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
36 match *self {
37 AlgorithmMismatch(a, b) => {
38 write!(f, "Expected algorithm type {:?} but found {:?}", a, b)
39 }
40 NoKeyId => write!(f, "No key id found"),
41 NoKeyWithKeyId(ref kid) => write!(f, "Key with key id {} not found", kid),
42 NoHeaderComponent => write!(f, "No header component found in token string"),
43 NoClaimsComponent => write!(f, "No claims component found in token string"),
44 NoSignatureComponent => write!(f, "No signature component found in token string"),
45 TooManyComponents => write!(f, "Too many components found in token string"),
46 Format => write!(f, "Format"),
47 InvalidSignature => write!(f, "Invalid signature"),
48 Base64(ref x) => write!(f, "{}", x),
49 Json(ref x) => write!(f, "{}", x),
50 Utf8(ref x) => write!(f, "{}", x),
51 RustCryptoMac(ref x) => write!(f, "{}", x),
52 RustCryptoMacKeyLength(ref x) => write!(f, "{}", x),
53 Signature(ref x) => write!(f, "{}", x),
54 InvalidKey => write!(f, "Invalid key"),
55 #[cfg(feature = "openssl")]
56 OpenSsl(ref x) => write!(f, "{}", x),
57 }
58 }
59}
60
61impl std::error::Error for Error {}
62
63macro_rules! error_wrap {
64 ($f:ty, $e:expr) => {
65 impl From<$f> for Error {
66 fn from(f: $f) -> Error {
67 $e(f)
68 }
69 }
70 };
71}
72
73error_wrap!(DecodeError, Base64);
74error_wrap!(JsonError, Json);
75error_wrap!(FromUtf8Error, Utf8);
76error_wrap!(MacError, RustCryptoMac);
77error_wrap!(InvalidLength, RustCryptoMacKeyLength);
78error_wrap!(signature::Error, Signature);
79#[cfg(feature = "openssl")]
80error_wrap!(openssl::error::ErrorStack, Error::OpenSsl);