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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
use std::marker::PhantomData;
use serde::de::Visitor;
use crate::{ClaimKind, JWTClaims, RegisteredClaimKind, RegisteredClaims};
struct ClaimsDeserializer<D> {
inner: D,
result: RegisteredClaims,
}
impl<'de, D: serde::de::MapAccess<'de>> ClaimsDeserializer<D> {
fn deserialize_registered_claim(&mut self, kind: RegisteredClaimKind) -> Result<(), D::Error> {
self.result
.insert_any(kind.deserialize_value(&mut self.inner)?);
Ok(())
}
fn into_registered_claims(mut self) -> Result<RegisteredClaims, D::Error> {
while let Some(claim) = self.inner.next_key()? {
match claim {
ClaimKind::Registered(r) => self.deserialize_registered_claim(r)?,
ClaimKind::Unregistered(key) => {
return Err(serde::de::Error::custom(format!(
"unexpected claim `{}`",
key
)))
}
}
}
Ok(self.result)
}
}
impl<'de, D: serde::de::MapAccess<'de>> serde::de::MapAccess<'de> for &mut ClaimsDeserializer<D> {
type Error = D::Error;
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
where
K: serde::de::DeserializeSeed<'de>,
{
loop {
match self.inner.next_key()? {
Some(ClaimKind::Registered(r)) => self.deserialize_registered_claim(r)?,
Some(ClaimKind::Unregistered(key)) => {
break Ok(Some(
seed.deserialize(serde::de::value::StringDeserializer::new(key))?,
))
}
None => break Ok(None),
}
}
}
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
where
V: serde::de::DeserializeSeed<'de>,
{
self.inner.next_value_seed(seed)
}
}
impl<'de, D: serde::de::MapAccess<'de>> serde::Deserializer<'de> for &mut ClaimsDeserializer<D> {
type Error = D::Error;
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>,
{
visitor.visit_map(self)
}
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_map(self)
}
/// Hint that the `Deserialize` type is expecting a `bool` value.
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting an `i8` value.
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting an `i16` value.
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting an `i32` value.
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting an `i64` value.
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting an `i128` value.
///
/// The default behavior unconditionally returns an error.
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
let _ = visitor;
Err(serde::de::Error::custom("i128 is not supported"))
}
/// Hint that the `Deserialize` type is expecting a `u8` value.
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a `u16` value.
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a `u32` value.
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a `u64` value.
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting an `u128` value.
///
/// The default behavior unconditionally returns an error.
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
let _ = visitor;
Err(serde::de::Error::custom("u128 is not supported"))
}
/// Hint that the `Deserialize` type is expecting a `f32` value.
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a `f64` value.
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a `char` value.
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a string value and does
/// not benefit from taking ownership of buffered data owned by the
/// `Deserializer`.
///
/// If the `Visitor` would benefit from taking ownership of `String` data,
/// indicate this to the `Deserializer` by using `deserialize_string`
/// instead.
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a string value and would
/// benefit from taking ownership of buffered data owned by the
/// `Deserializer`.
///
/// If the `Visitor` would not benefit from taking ownership of `String`
/// data, indicate that to the `Deserializer` by using `deserialize_str`
/// instead.
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a byte array and does not
/// benefit from taking ownership of buffered data owned by the
/// `Deserializer`.
///
/// If the `Visitor` would benefit from taking ownership of `Vec<u8>` data,
/// indicate this to the `Deserializer` by using `deserialize_byte_buf`
/// instead.
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a byte array and would
/// benefit from taking ownership of buffered data owned by the
/// `Deserializer`.
///
/// If the `Visitor` would not benefit from taking ownership of `Vec<u8>`
/// data, indicate that to the `Deserializer` by using `deserialize_bytes`
/// instead.
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting an optional value.
///
/// This allows deserializers that encode an optional value as a nullable
/// value to convert the null value into `None` and a regular value into
/// `Some(value)`.
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a unit value.
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_unit()
}
/// Hint that the `Deserialize` type is expecting a unit struct with a
/// particular name.
fn deserialize_unit_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a newtype struct with a
/// particular name.
fn deserialize_newtype_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a sequence of values.
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a sequence of values and
/// knows how many values there are without looking at the serialized data.
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a tuple struct with a
/// particular name and number of fields.
fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
_len: usize,
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting a struct with a particular
/// name and fields.
fn deserialize_struct<V>(
self,
_name: &'static str,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_map(self)
}
/// Hint that the `Deserialize` type is expecting an enum value with a
/// particular name and possible variants.
fn deserialize_enum<V>(
self,
_name: &'static str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type is expecting the name of a struct
/// field or the discriminant of an enum variant.
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
/// Hint that the `Deserialize` type needs to deserialize a value whose type
/// doesn't matter because it is ignored.
///
/// Deserializers for non-self-describing formats may not support this mode.
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Map,
&visitor,
))
}
}
impl<'de, P: serde::Deserialize<'de>> serde::Deserialize<'de> for JWTClaims<P> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
// P::deserialize(PrivateClaimsDeserializer);
struct Visitor<P>(PhantomData<P>);
impl<'de, P: serde::Deserialize<'de>> serde::de::Visitor<'de> for Visitor<P> {
type Value = JWTClaims<P>;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "JWT claims")
}
fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
where
A: serde::de::MapAccess<'de>,
{
let mut claims_deserializer = ClaimsDeserializer {
inner: map,
result: RegisteredClaims::new(),
};
let private_claims = P::deserialize(&mut claims_deserializer)?;
let registered_claims = claims_deserializer.into_registered_claims()?;
Ok(registered_claims.with_private_claims(private_claims))
}
}
deserializer.deserialize_map(Visitor(PhantomData))
}
}