1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use core::fmt;
use crate::alloc::String;
#[derive(Debug)]
#[non_exhaustive]
pub enum ParseError {
InvalidTokenStructure,
Base64(base64::DecodeError),
MalformedHeader(serde_json::Error),
UnsupportedContentType(String),
}
impl fmt::Display for ParseError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidTokenStructure => formatter.write_str("Invalid token structure"),
Self::Base64(e) => write!(formatter, "base64 decoding error: {}", e),
Self::MalformedHeader(e) => write!(formatter, "Malformed token header: {}", e),
Self::UnsupportedContentType(ty) => {
write!(formatter, "Unsupported content type: {}", ty)
}
}
}
}
impl From<base64::DecodeError> for ParseError {
fn from(error: base64::DecodeError) -> Self {
Self::Base64(error)
}
}
#[cfg(feature = "std")]
impl std::error::Error for ParseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Base64(e) => Some(e),
Self::MalformedHeader(e) => Some(e),
_ => None,
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ValidationError {
AlgorithmMismatch {
expected: String,
actual: String,
},
MalformedSignature(anyhow::Error),
InvalidSignature,
MalformedClaims(serde_json::Error),
MalformedCborClaims(serde_cbor::error::Error),
NoClaim(Claim),
Expired,
NotMature,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Claim {
Expiration,
NotBefore,
}
impl fmt::Display for Claim {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(match self {
Self::Expiration => "exp",
Self::NotBefore => "nbf",
})
}
}
impl fmt::Display for ValidationError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::AlgorithmMismatch { expected, actual } => write!(
formatter,
"Token algorithm ({actual}) differs from expected ({expected})",
expected = expected,
actual = actual
),
Self::MalformedSignature(e) => write!(formatter, "Malformed token signature: {}", e),
Self::InvalidSignature => formatter.write_str("Signature has failed verification"),
Self::MalformedClaims(e) => write!(formatter, "Cannot deserialize claims: {}", e),
Self::MalformedCborClaims(e) => write!(formatter, "Cannot deserialize claims: {}", e),
Self::NoClaim(claim) => write!(
formatter,
"Claim `{}` requested during validation is not present in the token",
claim
),
Self::Expired => formatter.write_str("Token has expired"),
Self::NotMature => formatter.write_str("Token is not yet ready"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for ValidationError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::MalformedSignature(e) => Some(e.as_ref()),
Self::MalformedClaims(e) => Some(e),
Self::MalformedCborClaims(e) => Some(e),
_ => None,
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum CreationError {
Header(serde_json::Error),
Claims(serde_json::Error),
CborClaims(serde_cbor::error::Error),
}
impl fmt::Display for CreationError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Header(e) => write!(formatter, "Cannot serialize header: {}", e),
Self::Claims(e) => write!(formatter, "Cannot serialize claims: {}", e),
Self::CborClaims(e) => write!(formatter, "Cannot serialize claims into CBOR: {}", e),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for CreationError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Header(e) | Self::Claims(e) => Some(e),
Self::CborClaims(e) => Some(e),
}
}
}