jsonwebtoken/
errors.rs

1use std::error::Error as StdError;
2use std::fmt;
3use std::result;
4use std::sync::Arc;
5
6/// A crate private constructor for `Error`.
7pub(crate) fn new_error(kind: ErrorKind) -> Error {
8    Error(Box::new(kind))
9}
10
11/// A type alias for `Result<T, jsonwebtoken::errors::Error>`.
12pub type Result<T> = result::Result<T, Error>;
13
14/// An error that can occur when encoding/decoding JWTs
15#[derive(Clone, Debug, Eq, PartialEq)]
16pub struct Error(Box<ErrorKind>);
17
18impl Error {
19    /// Return the specific type of this error.
20    pub fn kind(&self) -> &ErrorKind {
21        &self.0
22    }
23
24    /// Unwrap this error into its underlying type.
25    pub fn into_kind(self) -> ErrorKind {
26        *self.0
27    }
28}
29
30/// The specific type of an error.
31///
32/// This enum may grow additional variants, the `#[non_exhaustive]`
33/// attribute makes sure clients don't count on exhaustive matching.
34/// (Otherwise, adding a new variant could break existing code.)
35#[non_exhaustive]
36#[derive(Clone, Debug)]
37pub enum ErrorKind {
38    /// When a token doesn't have a valid JWT shape
39    InvalidToken,
40    /// When the signature doesn't match
41    InvalidSignature,
42    /// When the secret given is not a valid ECDSA key
43    InvalidEcdsaKey,
44    /// When the secret given is not a valid RSA key
45    InvalidRsaKey(String),
46    /// We could not sign with the given key
47    RsaFailedSigning,
48    /// When the algorithm from string doesn't match the one passed to `from_str`
49    InvalidAlgorithmName,
50    /// When a key is provided with an invalid format
51    InvalidKeyFormat,
52
53    // Validation errors
54    /// When a claim required by the validation is not present
55    MissingRequiredClaim(String),
56    /// When a token’s `exp` claim indicates that it has expired
57    ExpiredSignature,
58    /// When a token’s `iss` claim does not match the expected issuer
59    InvalidIssuer,
60    /// When a token’s `aud` claim does not match one of the expected audience values
61    InvalidAudience,
62    /// When a token’s `sub` claim does not match one of the expected subject values
63    InvalidSubject,
64    /// When a token’s `nbf` claim represents a time in the future
65    ImmatureSignature,
66    /// When the algorithm in the header doesn't match the one passed to `decode` or the encoding/decoding key
67    /// used doesn't match the alg requested
68    InvalidAlgorithm,
69    /// When the Validation struct does not contain at least 1 algorithm
70    MissingAlgorithm,
71
72    // 3rd party errors
73    /// An error happened when decoding some base64 text
74    Base64(base64::DecodeError),
75    /// An error happened while serializing/deserializing JSON
76    Json(Arc<serde_json::Error>),
77    /// Some of the text was invalid UTF-8
78    Utf8(::std::string::FromUtf8Error),
79    /// Something unspecified went wrong with crypto
80    Crypto(::ring::error::Unspecified),
81}
82
83impl StdError for Error {
84    fn cause(&self) -> Option<&dyn StdError> {
85        match &*self.0 {
86            ErrorKind::InvalidToken => None,
87            ErrorKind::InvalidSignature => None,
88            ErrorKind::InvalidEcdsaKey => None,
89            ErrorKind::RsaFailedSigning => None,
90            ErrorKind::InvalidRsaKey(_) => None,
91            ErrorKind::ExpiredSignature => None,
92            ErrorKind::MissingAlgorithm => None,
93            ErrorKind::MissingRequiredClaim(_) => None,
94            ErrorKind::InvalidIssuer => None,
95            ErrorKind::InvalidAudience => None,
96            ErrorKind::InvalidSubject => None,
97            ErrorKind::ImmatureSignature => None,
98            ErrorKind::InvalidAlgorithm => None,
99            ErrorKind::InvalidAlgorithmName => None,
100            ErrorKind::InvalidKeyFormat => None,
101            ErrorKind::Base64(err) => Some(err),
102            ErrorKind::Json(err) => Some(err.as_ref()),
103            ErrorKind::Utf8(err) => Some(err),
104            ErrorKind::Crypto(err) => Some(err),
105        }
106    }
107}
108
109impl fmt::Display for Error {
110    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
111        match &*self.0 {
112            ErrorKind::InvalidToken
113            | ErrorKind::InvalidSignature
114            | ErrorKind::InvalidEcdsaKey
115            | ErrorKind::ExpiredSignature
116            | ErrorKind::RsaFailedSigning
117            | ErrorKind::MissingAlgorithm
118            | ErrorKind::InvalidIssuer
119            | ErrorKind::InvalidAudience
120            | ErrorKind::InvalidSubject
121            | ErrorKind::ImmatureSignature
122            | ErrorKind::InvalidAlgorithm
123            | ErrorKind::InvalidKeyFormat
124            | ErrorKind::InvalidAlgorithmName => write!(f, "{:?}", self.0),
125            ErrorKind::MissingRequiredClaim(c) => write!(f, "Missing required claim: {}", c),
126            ErrorKind::InvalidRsaKey(msg) => write!(f, "RSA key invalid: {}", msg),
127            ErrorKind::Json(err) => write!(f, "JSON error: {}", err),
128            ErrorKind::Utf8(err) => write!(f, "UTF-8 error: {}", err),
129            ErrorKind::Crypto(err) => write!(f, "Crypto error: {}", err),
130            ErrorKind::Base64(err) => write!(f, "Base64 error: {}", err),
131        }
132    }
133}
134
135impl PartialEq for ErrorKind {
136    fn eq(&self, other: &Self) -> bool {
137        format!("{:?}", self) == format!("{:?}", other)
138    }
139}
140
141// Equality of ErrorKind is an equivalence relation: it is reflexive, symmetric and transitive.
142impl Eq for ErrorKind {}
143
144impl From<base64::DecodeError> for Error {
145    fn from(err: base64::DecodeError) -> Error {
146        new_error(ErrorKind::Base64(err))
147    }
148}
149
150impl From<serde_json::Error> for Error {
151    fn from(err: serde_json::Error) -> Error {
152        new_error(ErrorKind::Json(Arc::new(err)))
153    }
154}
155
156impl From<::std::string::FromUtf8Error> for Error {
157    fn from(err: ::std::string::FromUtf8Error) -> Error {
158        new_error(ErrorKind::Utf8(err))
159    }
160}
161
162impl From<::ring::error::Unspecified> for Error {
163    fn from(err: ::ring::error::Unspecified) -> Error {
164        new_error(ErrorKind::Crypto(err))
165    }
166}
167
168impl From<::ring::error::KeyRejected> for Error {
169    fn from(_err: ::ring::error::KeyRejected) -> Error {
170        new_error(ErrorKind::InvalidEcdsaKey)
171    }
172}
173
174impl From<ErrorKind> for Error {
175    fn from(kind: ErrorKind) -> Error {
176        new_error(kind)
177    }
178}
179
180#[cfg(test)]
181mod tests {
182    use wasm_bindgen_test::wasm_bindgen_test;
183
184    use super::*;
185
186    #[test]
187    #[wasm_bindgen_test]
188    fn test_error_rendering() {
189        assert_eq!(
190            "InvalidAlgorithmName",
191            Error::from(ErrorKind::InvalidAlgorithmName).to_string()
192        );
193    }
194}