Coverage Report

Created: 2026-03-11 07:34

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