jwt_compact_frame/
error.rs1#[cfg(feature = "ciborium")]
4use core::convert::Infallible;
5use core::fmt;
6
7use crate::alloc::String;
8
9#[cfg(feature = "ciborium")]
10pub type CborDeError<E = anyhow::Error> = ciborium::de::Error<E>;
11#[cfg(feature = "ciborium")]
12pub type CborSerError<E = Infallible> = ciborium::ser::Error<E>;
13
14#[derive(Debug)]
16#[non_exhaustive]
17pub enum ParseError {
18 InvalidTokenStructure,
23 InvalidBase64Encoding,
25 MalformedHeader(serde_json::Error),
27 UnsupportedContentType(String),
34 ClaimsTooLarge,
36 SignatureTooLarge,
38 SignedDataTooLarge,
40}
41
42impl fmt::Display for ParseError {
43 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
44 match self {
45 Self::InvalidTokenStructure => formatter.write_str("invalid token structure"),
46 Self::InvalidBase64Encoding => write!(formatter, "invalid base64 decoding"),
47 Self::MalformedHeader(err) => write!(formatter, "malformed token header: {err}"),
48 Self::UnsupportedContentType(ty) => {
49 write!(formatter, "unsupported content type: {ty}")
50 },
51 Self::ClaimsTooLarge => {
52 write!(formatter, "claims too large")
53 },
54 Self::SignatureTooLarge => {
55 write!(formatter, "signature too large")
56 },
57 Self::SignedDataTooLarge => {
58 write!(formatter, "signed data too large")
59 },
60 }
61 }
62}
63
64#[cfg(feature = "std")]
65impl std::error::Error for ParseError {
66 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
67 match self {
68 Self::MalformedHeader(err) => Some(err),
69 _ => None,
70 }
71 }
72}
73
74#[derive(Debug)]
76#[non_exhaustive]
77pub enum ValidationError {
78 AlgorithmMismatch {
80 expected: String,
82 actual: String,
84 },
85 InvalidSignatureLen {
87 expected: usize,
89 actual: usize,
91 },
92 MalformedSignature(anyhow::Error),
94 InvalidSignature,
96 MalformedClaims(serde_json::Error),
98 #[cfg(feature = "ciborium")]
100 #[cfg_attr(docsrs, doc(cfg(feature = "ciborium")))]
101 MalformedCborClaims(CborDeError),
102 NoClaim(Claim),
104 Expired,
106 NotMature,
108}
109
110#[derive(Debug, Clone, PartialEq, Eq)]
112#[non_exhaustive]
113pub enum Claim {
114 Expiration,
116 NotBefore,
118}
119
120impl fmt::Display for Claim {
121 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
122 formatter.write_str(match self {
123 Self::Expiration => "exp",
124 Self::NotBefore => "nbf",
125 })
126 }
127}
128
129impl fmt::Display for ValidationError {
130 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
131 match self {
132 Self::AlgorithmMismatch { expected, actual } => {
133 write!(formatter, "token algorithm ({actual}) differs from expected ({expected})")
134 },
135 Self::InvalidSignatureLen { expected, actual } => {
136 write!(formatter, "invalid signature length: expected {expected} bytes, got {actual} bytes")
137 },
138 Self::MalformedSignature(err) => write!(formatter, "malformed token signature: {err}"),
139 Self::InvalidSignature => formatter.write_str("signature has failed verification"),
140 Self::MalformedClaims(err) => write!(formatter, "cannot deserialize claims: {err}"),
141 #[cfg(feature = "ciborium")]
142 Self::MalformedCborClaims(err) => write!(formatter, "cannot deserialize claims: {err}"),
143 Self::NoClaim(claim) => {
144 write!(formatter, "claim `{claim}` requested during validation is not present in the token")
145 },
146 Self::Expired => formatter.write_str("token has expired"),
147 Self::NotMature => formatter.write_str("token is not yet ready"),
148 }
149 }
150}
151
152#[cfg(feature = "std")]
153impl std::error::Error for ValidationError {
154 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
155 match self {
156 Self::MalformedSignature(err) => Some(err.as_ref()),
157 Self::MalformedClaims(err) => Some(err),
158 #[cfg(feature = "ciborium")]
159 Self::MalformedCborClaims(err) => Some(err),
160 _ => None,
161 }
162 }
163}
164
165#[derive(Debug)]
167#[non_exhaustive]
168pub enum CreationError {
169 Header(serde_json::Error),
171 Claims(serde_json::Error),
173 #[cfg(feature = "ciborium")]
175 #[cfg_attr(docsrs, doc(cfg(feature = "ciborium")))]
176 CborClaims(CborSerError),
177}
178
179impl fmt::Display for CreationError {
180 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
181 match self {
182 Self::Header(err) => write!(formatter, "cannot serialize header: {err}"),
183 Self::Claims(err) => write!(formatter, "cannot serialize claims: {err}"),
184 #[cfg(feature = "ciborium")]
185 Self::CborClaims(err) => write!(formatter, "cannot serialize claims into CBOR: {err}"),
186 }
187 }
188}
189
190#[cfg(feature = "std")]
191impl std::error::Error for CreationError {
192 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
193 match self {
194 Self::Header(err) | Self::Claims(err) => Some(err),
195 #[cfg(feature = "ciborium")]
196 Self::CborClaims(err) => Some(err),
197 }
198 }
199}