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 #[cfg(feature = "openssl")]
29 OpenSsl(openssl::error::ErrorStack),
30}
31
32impl fmt::Display for Error {
33 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
34 match *self {
35 AlgorithmMismatch(a, b) => {
36 write!(f, "Expected algorithm type {:?} but found {:?}", a, b)
37 }
38 NoKeyId => write!(f, "No key id found"),
39 NoKeyWithKeyId(ref kid) => write!(f, "Key with key id {} not found", kid),
40 NoHeaderComponent => write!(f, "No header component found in token string"),
41 NoClaimsComponent => write!(f, "No claims component found in token string"),
42 NoSignatureComponent => write!(f, "No signature component found in token string"),
43 TooManyComponents => write!(f, "Too many components found in token string"),
44 Format => write!(f, "Format"),
45 InvalidSignature => write!(f, "Invalid signature"),
46 Base64(ref x) => write!(f, "{}", x),
47 Json(ref x) => write!(f, "{}", x),
48 Utf8(ref x) => write!(f, "{}", x),
49 RustCryptoMac(ref x) => write!(f, "{}", x),
50 RustCryptoMacKeyLength(ref x) => write!(f, "{}", x),
51 #[cfg(feature = "openssl")]
52 OpenSsl(ref x) => write!(f, "{}", x),
53 }
54 }
55}
56
57impl std::error::Error for Error {}
58
59macro_rules! error_wrap {
60 ($f:ty, $e:expr) => {
61 impl From<$f> for Error {
62 fn from(f: $f) -> Error {
63 $e(f)
64 }
65 }
66 };
67}
68
69error_wrap!(DecodeError, Base64);
70error_wrap!(JsonError, Json);
71error_wrap!(FromUtf8Error, Utf8);
72error_wrap!(MacError, RustCryptoMac);
73error_wrap!(InvalidLength, RustCryptoMacKeyLength);
74#[cfg(feature = "openssl")]
75error_wrap!(openssl::error::ErrorStack, Error::OpenSsl);