-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
generic_builder.rs
1420 lines (1247 loc) · 61.3 KB
/
generic_builder.rs
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use core::marker::PhantomData;
use std::collections::HashMap;
use erased_serde::Serialize;
use serde_json::{Map, Value};
use crate::generic::*;
///The GenericBuilder is created at compile time by specifying a PASETO version and purpose and
///providing a key of the same version and purpose. This structure allows setting [PASETO claims](https://github.com/paseto-standard/paseto-spec/blob/master/docs/02-Implementation-Guide/04-Claims.md),
///your own [custom claims](CustomClaim), an optional [footer](Footer) and in the case of V3/V4 tokens, an optional [implicit
///assertion](ImplicitAssertion).
///
///The intent of the GenericBuilder is to allow the user to wrap basic PASETO standard
///functionality with their own custom business rules or ergonomic API. For most users, the batteries-included
///[paseto builder](crate::prelude::PasetoBuilder) will be all they need. More advanced cases can wrap this
///or the [core](Paseto) struct to accomplish custom functionality.
///
///# Usage
///
///```
///# #[cfg(all(feature = "generic", feature="v2_local"))]
///# {
/// use rusty_paseto::generic::*;
/// let key = PasetoSymmetricKey::<V2, Local>::from(Key::<32>::from(*b"wubbalubbadubdubwubbalubbadubdub"));
/// let footer = Footer::from("some footer");
/// //create a builder, add some claims and then build the token with the key
/// let token = GenericBuilder::<V2, Local>::default()
/// .set_claim(AudienceClaim::from("customers"))
/// .set_claim(SubjectClaim::from("loyal subjects"))
/// .set_claim(IssuerClaim::from("me"))
/// .set_claim(TokenIdentifierClaim::from("me"))
/// .set_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
/// .set_claim(CustomClaim::try_from(("seats", 4))?)
/// .set_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
/// .set_footer(footer)
/// .try_encrypt(&key)?;
/// //now let's decrypt the token and verify the values
/// let json = GenericParser::<V2, Local>::default()
/// .set_footer(footer)
/// .parse(&token, &key)?;
/// assert_eq!(json["aud"], "customers");
/// assert_eq!(json["jti"], "me");
/// assert_eq!(json["iss"], "me");
/// assert_eq!(json["data"], "this is a secret message");
/// assert_eq!(json["exp"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["iat"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["nbf"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["sub"], "loyal subjects");
/// assert_eq!(json["pi to 6 digits"], 3.141526);
/// assert_eq!(json["seats"], 4);
/// # }
/// # Ok::<(),anyhow::Error>(())
/// ```
pub struct GenericBuilder<'a, 'b, Version, Purpose> {
version: PhantomData<Version>,
purpose: PhantomData<Purpose>,
claims: HashMap<String, Box<dyn erased_serde::Serialize + 'b>>,
footer: Option<Footer<'a>>,
implicit_assertion: Option<ImplicitAssertion<'a>>,
}
impl<'a, 'b, Version, Purpose> GenericBuilder<'a, 'b, Version, Purpose> {
pub fn new() -> Self {
Self {
version: PhantomData::<Version>,
purpose: PhantomData::<Purpose>,
claims: HashMap::with_capacity(10),
footer: None,
implicit_assertion: None,
}
}
///Removes a [claim](PasetoClaim) from the internal list of claims by passed key
pub fn remove_claim(&mut self, claim_key: &str) -> &mut Self {
self.claims.remove(claim_key);
self
}
///Allows adding multiple [claims](PasetoClaim) at once by passing a Hashmap of claim keys and values
pub fn extend_claims(&mut self, value: HashMap<String, Box<dyn erased_serde::Serialize>>) -> &mut Self {
self.claims.extend(value);
self
}
///Adds a [claim](PasetoClaim) to the token builder
pub fn set_claim<T: 'b + PasetoClaim + erased_serde::Serialize>(&mut self, value: T) -> &mut Self
where
'b: 'a,
{
let key = value.get_key().to_owned();
// Ignore empty keys
if key.is_empty() {
return self;
}
// Serialize the claim value to serde_json::Value
let mut serialized_value = Vec::new();
let mut serializer = serde_json::Serializer::new(&mut serialized_value);
erased_serde::serialize(&value, &mut serializer).unwrap();
let value_json: serde_json::Value = serde_json::from_slice(&serialized_value).unwrap();
// Handle the special case for Null values
let value = match value_json {
serde_json::Value::Object(mut obj) => {
if obj.len() == 1 && obj.contains_key(&key) {
obj.remove(&key).unwrap()
} else {
serde_json::Value::Object(obj)
}
}
other => other,
};
// Insert the processed claim into the claims map
self.claims.insert(key, Box::new(value));
self
}
///Adds an optional [footer](Footer) to the token builder
pub fn set_footer(&mut self, footer: Footer<'a>) -> &mut Self {
self.footer = Some(footer);
self
}
/// Builds a JSON payload from the claims
///
/// # Returns
/// A `Result` containing the JSON payload as a `String` or a `serde_json::Error`
/// Fixes (issue #39)[https://github.com/rrrodzilla/rusty_paseto/issues/39] reported by @xbb
pub fn build_payload_from_claims(&mut self) -> Result<String, serde_json::Error> {
// Take the claims from the builder, replacing it with an empty HashMap
let claims = std::mem::take(&mut self.claims);
// Serialize each claim to a serde_json::Value
let serialized_claims: HashMap<String, Value> = claims
.into_iter()
.map(|(k, v)| (k, serde_json::to_value(v).unwrap_or(Value::Null)))
.collect();
// Wrap the serialized claims to ensure proper nesting
let wrapped_claims = wrap_claims(serialized_claims);
// Convert the wrapped claims to a JSON string
serde_json::to_string(&wrapped_claims)
}
}
// Wrap claims in an outer JSON object to ensure proper nesting
//
// # Parameters
// - `claims`: A `HashMap` containing the claims as `serde_json::Value`
//
// # Returns
// A `serde_json::Value` representing the wrapped claims
fn wrap_claims(claims: HashMap<String, Value>) -> Value {
// Recursively wrap each claim value
let wrapped: HashMap<String, Value> = claims
.into_iter()
.map(|(k, v)| (k, wrap_value(v)))
.collect();
// Return the wrapped claims as a JSON object
Value::Object(Map::from_iter(wrapped))
}
// Recursively wrap values to ensure all values are valid JSON objects
//
// # Parameters
// - `value`: A `serde_json::Value` to be wrapped
//
// # Returns
// A `serde_json::Value` representing the wrapped value
fn wrap_value(value: Value) -> Value {
match value {
// If the value is an object, check if it's empty
Value::Object(map) => {
if map.is_empty() {
// Wrap empty map as an empty JSON object
Value::Object(Map::new())
} else {
// Recursively wrap each key-value pair in the map
Value::Object(map.into_iter().map(|(k, v)| (k, wrap_value(v))).collect())
}
}
// If the value is an array, recursively wrap each element
Value::Array(arr) => Value::Array(arr.into_iter().map(wrap_value).collect()),
// If the value is null, return it as is
Value::Null => Value::Null,
// For primitive values, return them as is
other => other,
}
}
impl<'a, 'b, Version, Purpose> GenericBuilder<'a, 'b, Version, Purpose>
where
Version: ImplicitAssertionCapable,
{
///Adds an optional [implicit assertion](ImplicitAssertion) to the token builder for V3/V4
///tokens only
pub fn set_implicit_assertion(&mut self, implicit_assertion: ImplicitAssertion<'a>) -> &mut Self {
self.implicit_assertion = Some(implicit_assertion);
self
}
}
impl<Version, Purpose> Default for GenericBuilder<'_, '_, Version, Purpose> {
fn default() -> Self {
Self::new()
}
}
#[cfg(feature = "v1_local")]
impl GenericBuilder<'_, '_, V1, Local> {
/// Given a [PasetoSymmetricKey], attempts to encrypt a (V1, Local) PASETO token from the data and
/// claims provided to the GenericBuilder.
///
/// Returns `Ok(String)` on success, where the String is the encrypted PASETO token, otherwise returns an error.
///
/// # Errors
///
/// Returns [`GenericBuilderError`] for any errors when building the token string
/// for encryption or during ciphertext encryption.
///
/// # Example
///
///
///```
///# #[cfg(all(feature = "generic", feature="v1_local"))]
///# {
/// use rusty_paseto::generic::*;
/// let key = PasetoSymmetricKey::<V1, Local>::from(Key::<32>::from(*b"wubbalubbadubdubwubbalubbadubdub"));
/// let footer = Footer::from("some footer");
/// //create a builder, add some claims and then build the token with the key
/// let token = GenericBuilder::<V1, Local>::default()
/// .set_claim(AudienceClaim::from("customers"))
/// .set_claim(SubjectClaim::from("loyal subjects"))
/// .set_claim(IssuerClaim::from("me"))
/// .set_claim(TokenIdentifierClaim::from("me"))
/// .set_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
/// .set_claim(CustomClaim::try_from(("seats", 4))?)
/// .set_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
/// .set_footer(footer)
/// .try_encrypt(&key)?;
/// //now let's decrypt the token and verify the values
/// let json = GenericParser::<V1, Local>::default()
/// .set_footer(footer)
/// .parse(&token, &key)?;
/// assert_eq!(json["aud"], "customers");
/// assert_eq!(json["jti"], "me");
/// assert_eq!(json["iss"], "me");
/// assert_eq!(json["data"], "this is a secret message");
/// assert_eq!(json["exp"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["iat"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["nbf"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["sub"], "loyal subjects");
/// assert_eq!(json["pi to 6 digits"], 3.141526);
/// assert_eq!(json["seats"], 4);
/// # }
/// # Ok::<(),anyhow::Error>(())
/// ```
pub fn try_encrypt(&mut self, key: &PasetoSymmetricKey<V1, Local>) -> Result<String, GenericBuilderError> {
let mut token_builder = Paseto::<V1, Local>::builder();
let payload = self.build_payload_from_claims()?;
token_builder.set_payload(Payload::from(payload.as_str()));
if let Some(footer) = &self.footer {
token_builder.set_footer(*footer);
}
let random_nonce = Key::<32>::try_new_random()?;
Ok(token_builder.try_encrypt(key, &PasetoNonce::<V1, Local>::from(&random_nonce))?)
}
}
#[cfg(feature = "v2_local")]
impl GenericBuilder<'_, '_, V2, Local> {
/// Given a [PasetoSymmetricKey], attempts to encrypt a (V2, Local) PASETO token from the data and
/// claims provided to the GenericBuilder.
///
/// Returns `Ok(String)` on success, where the String is the encrypted PASETO token, otherwise returns an error.
///
/// # Errors
///
/// Returns [`GenericBuilderError`] for any errors when building the token string
/// for encryption or during ciphertext encryption.
///
/// # Example
///
///
///```
///# #[cfg(all(feature = "generic", feature="v2_local"))]
///# {
/// use rusty_paseto::generic::*;
/// let key = PasetoSymmetricKey::<V2, Local>::from(Key::<32>::from(*b"wubbalubbadubdubwubbalubbadubdub"));
/// let footer = Footer::from("some footer");
/// //create a builder, add some claims and then build the token with the key
/// let token = GenericBuilder::<V2, Local>::default()
/// .set_claim(AudienceClaim::from("customers"))
/// .set_claim(SubjectClaim::from("loyal subjects"))
/// .set_claim(IssuerClaim::from("me"))
/// .set_claim(TokenIdentifierClaim::from("me"))
/// .set_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
/// .set_claim(CustomClaim::try_from(("seats", 4))?)
/// .set_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
/// .set_footer(footer)
/// .try_encrypt(&key)?;
/// //now let's decrypt the token and verify the values
/// let json = GenericParser::<V2, Local>::default()
/// .set_footer(footer)
/// .parse(&token, &key)?;
/// assert_eq!(json["aud"], "customers");
/// assert_eq!(json["jti"], "me");
/// assert_eq!(json["iss"], "me");
/// assert_eq!(json["data"], "this is a secret message");
/// assert_eq!(json["exp"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["iat"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["nbf"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["sub"], "loyal subjects");
/// assert_eq!(json["pi to 6 digits"], 3.141526);
/// assert_eq!(json["seats"], 4);
/// # }
/// # Ok::<(),anyhow::Error>(())
/// ```
pub fn try_encrypt(&mut self, key: &PasetoSymmetricKey<V2, Local>) -> Result<String, GenericBuilderError> {
let mut token_builder = Paseto::<V2, Local>::builder();
let payload = self.build_payload_from_claims()?;
token_builder.set_payload(Payload::from(payload.as_str()));
if let Some(footer) = &self.footer {
token_builder.set_footer(*footer);
}
Ok(token_builder.try_encrypt(key, &PasetoNonce::<V2, Local>::from(&Key::<24>::try_new_random()?))?)
}
}
#[cfg(feature = "v3_local")]
impl GenericBuilder<'_, '_, V3, Local> {
/// Given a [PasetoSymmetricKey], attempts to encrypt a (V3, Local) PASETO token from the data and
/// claims provided to the GenericBuilder.
///
/// Returns `Ok(String)` on success, where the String is the encrypted PASETO token, otherwise returns an error.
///
/// # Errors
///
/// Returns [`GenericBuilderError`] for any errors when building the token string
/// for encryption or during ciphertext encryption.
///
/// # Example
///
///
///```
///# #[cfg(all(feature = "generic", feature="v3_local"))]
///# {
/// use rusty_paseto::generic::*;
/// let key = PasetoSymmetricKey::<V3, Local>::from(Key::<32>::from(*b"wubbalubbadubdubwubbalubbadubdub"));
/// let footer = Footer::from("some footer");
/// let implicit_assertion = ImplicitAssertion::from("some assertion");
/// //create a builder, add some claims and then build the token with the key
/// let token = GenericBuilder::<V3, Local>::default()
/// .set_claim(AudienceClaim::from("customers"))
/// .set_claim(SubjectClaim::from("loyal subjects"))
/// .set_claim(IssuerClaim::from("me"))
/// .set_claim(TokenIdentifierClaim::from("me"))
/// .set_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
/// .set_claim(CustomClaim::try_from(("seats", 4))?)
/// .set_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
/// .set_footer(footer)
/// .set_implicit_assertion(implicit_assertion)
/// .try_encrypt(&key)?;
/// //now let's decrypt the token and verify the values
/// let json = GenericParser::<V3, Local>::default()
/// .set_footer(footer)
/// .set_implicit_assertion(implicit_assertion)
/// .parse(&token, &key)?;
/// assert_eq!(json["aud"], "customers");
/// assert_eq!(json["jti"], "me");
/// assert_eq!(json["iss"], "me");
/// assert_eq!(json["data"], "this is a secret message");
/// assert_eq!(json["exp"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["iat"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["nbf"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["sub"], "loyal subjects");
/// assert_eq!(json["pi to 6 digits"], 3.141526);
/// assert_eq!(json["seats"], 4);
/// # }
/// # Ok::<(),anyhow::Error>(())
/// ```
pub fn try_encrypt(&mut self, key: &PasetoSymmetricKey<V3, Local>) -> Result<String, GenericBuilderError> {
let mut token_builder = Paseto::<V3, Local>::builder();
let payload = self.build_payload_from_claims()?;
token_builder.set_payload(Payload::from(payload.as_str()));
if let Some(footer) = &self.footer {
token_builder.set_footer(*footer);
}
if let Some(implicit_assertion) = &self.implicit_assertion {
token_builder.set_implicit_assertion(*implicit_assertion);
}
let nonce = Key::<32>::try_new_random()?;
let nonce = PasetoNonce::<V3, Local>::from(&nonce);
Ok(token_builder.try_encrypt(key, &nonce)?)
}
}
#[cfg(feature = "v4_local")]
impl GenericBuilder<'_, '_, V4, Local> {
/// Given a [PasetoSymmetricKey], attempts to encrypt a (V4, Local) PASETO token from the data and
/// claims provided to the GenericBuilder.
///
/// Returns `Ok(String)` on success, where the String is the encrypted PASETO token, otherwise returns an error.
///
/// # Errors
///
/// Returns [`GenericBuilderError`] for any errors when building the token string
/// for encryption or during ciphertext encryption.
///
/// # Example
///
///
///```
///# #[cfg(all(feature = "generic", feature="v4_local"))]
///# {
/// use rusty_paseto::generic::*;
/// let key = PasetoSymmetricKey::<V4, Local>::from(Key::<32>::from(*b"wubbalubbadubdubwubbalubbadubdub"));
/// let footer = Footer::from("some footer");
/// let implicit_assertion = ImplicitAssertion::from("some assertion");
/// //create a builder, add some claims and then build the token with the key
/// let token = GenericBuilder::<V4, Local>::default()
/// .set_claim(AudienceClaim::from("customers"))
/// .set_claim(SubjectClaim::from("loyal subjects"))
/// .set_claim(IssuerClaim::from("me"))
/// .set_claim(TokenIdentifierClaim::from("me"))
/// .set_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
/// .set_claim(CustomClaim::try_from(("seats", 4))?)
/// .set_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
/// .set_footer(footer)
/// .set_implicit_assertion(implicit_assertion)
/// .try_encrypt(&key)?;
/// //now let's decrypt the token and verify the values
/// let json = GenericParser::<V4, Local>::default()
/// .set_footer(footer)
/// .set_implicit_assertion(implicit_assertion)
/// .parse(&token, &key)?;
/// assert_eq!(json["aud"], "customers");
/// assert_eq!(json["jti"], "me");
/// assert_eq!(json["iss"], "me");
/// assert_eq!(json["data"], "this is a secret message");
/// assert_eq!(json["exp"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["iat"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["nbf"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["sub"], "loyal subjects");
/// assert_eq!(json["pi to 6 digits"], 3.141526);
/// assert_eq!(json["seats"], 4);
/// # }
/// # Ok::<(),anyhow::Error>(())
/// ```
pub fn try_encrypt(&mut self, key: &PasetoSymmetricKey<V4, Local>) -> Result<String, GenericBuilderError> {
let mut token_builder = Paseto::<V4, Local>::builder();
let payload = self.build_payload_from_claims()?;
token_builder.set_payload(Payload::from(payload.as_str()));
if let Some(footer) = &self.footer {
token_builder.set_footer(*footer);
}
if let Some(implicit_assertion) = &self.implicit_assertion {
token_builder.set_implicit_assertion(*implicit_assertion);
}
let nonce = Key::<32>::try_new_random()?;
let nonce = PasetoNonce::<V4, Local>::from(&nonce);
Ok(token_builder.try_encrypt(key, &nonce)?)
}
}
#[cfg(feature = "v1_public")]
impl GenericBuilder<'_, '_, V1, Public> {
/// Given a [PasetoAsymmetricPrivateKey], attempts to sign a ([V1], [Public]) PASETO token from the data and
/// claims provided to the GenericBuilder with an optional [Footer].
///
/// Returns `Ok(String)` on success, where the String is the signed PASETO token, otherwise returns an error.
///
/// # Errors
///
/// Returns [`GenericBuilderError`] for any errors when building the token string
/// for signing or during signing.
///
/// # Example
///
///```
///# #[cfg(all(feature = "generic", feature="v1_public"))]
///# {
/// # use rusty_paseto::generic::*;
/// //obtain a private key (pk)
/// # let private_key = include_bytes!("../../../tests/v1_public_test_vectors_private_key.pk8");
/// # let pk: &[u8] = private_key;
/// let private_key = PasetoAsymmetricPrivateKey::<V1, Public>::from(pk);
/// let footer = Footer::from("some footer");
/// //sign a public V1 token
/// let token = GenericBuilder::<V1, Public>::default()
/// .set_claim(AudienceClaim::from("customers"))
/// .set_claim(SubjectClaim::from("loyal subjects"))
/// .set_claim(IssuerClaim::from("me"))
/// .set_claim(TokenIdentifierClaim::from("me"))
/// .set_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
/// .set_claim(CustomClaim::try_from(("seats", 4))?)
/// .set_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
/// .set_footer(footer)
/// .try_sign(&private_key)?;
/// //obtain a public key (pubk)
/// # let public_key = include_bytes!("../../../tests/v1_public_test_vectors_public_key.der");
/// # let pubk: &[u8] = public_key;
/// let public_key = PasetoAsymmetricPublicKey::<V1, Public>::from(pubk);
/// //now let's try to verify it
/// let json = GenericParser::<V1, Public>::default()
/// .set_footer(footer)
/// .check_claim(AudienceClaim::from("customers"))
/// .check_claim(SubjectClaim::from("loyal subjects"))
/// .check_claim(IssuerClaim::from("me"))
/// .check_claim(TokenIdentifierClaim::from("me"))
/// .check_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .check_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .check_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .check_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
/// .check_claim(CustomClaim::try_from(("seats", 4))?)
/// .check_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
/// .parse(&token, &public_key)?;
/// // we can access all the values from the serde Value object returned by the parser
/// assert_eq!(json["aud"], "customers");
/// assert_eq!(json["jti"], "me");
/// assert_eq!(json["iss"], "me");
/// assert_eq!(json["data"], "this is a secret message");
/// assert_eq!(json["exp"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["iat"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["nbf"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["sub"], "loyal subjects");
/// assert_eq!(json["pi to 6 digits"], 3.141526);
/// assert_eq!(json["seats"], 4);
/// # }
/// # Ok::<(),anyhow::Error>(())
///```
pub fn try_sign(&mut self, key: &PasetoAsymmetricPrivateKey<V1, Public>) -> Result<String, GenericBuilderError> {
let mut token_builder = Paseto::<V1, Public>::builder();
let payload = self.build_payload_from_claims()?;
token_builder.set_payload(Payload::from(payload.as_str()));
if let Some(footer) = &self.footer {
token_builder.set_footer(*footer);
}
Ok(token_builder.try_sign(key)?)
}
}
#[cfg(feature = "v2_public")]
impl GenericBuilder<'_, '_, V2, Public> {
/// Given a [PasetoAsymmetricPrivateKey], attempts to sign a ([V2], [Public]) PASETO token from the data and
/// claims provided to the GenericBuilder with an optional [Footer].
///
/// Returns `Ok(String)` on success, where the String is the signed PASETO token, otherwise returns an error.
///
/// # Errors
///
/// Returns [`GenericBuilderError`] for any errors when building the token string
/// for signing or during signing.
///
/// # Example
///
///```
///# #[cfg(all(feature = "generic", feature="v1_public"))]
///# {
/// # use rusty_paseto::generic::*;
/// //obtain a key
/// let private_key = Key::<64>::try_from("b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a37741eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2")?;
/// let private_key = PasetoAsymmetricPrivateKey::<V2, Public>::from(&private_key);
/// let public_key = Key::<32>::try_from("1eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2")?;
/// let public_key = PasetoAsymmetricPublicKey::<V2, Public>::from(&public_key);
/// let footer = Footer::from("some footer");
/// //sign a public V2 token
/// let token = GenericBuilder::<V2, Public>::default()
/// .set_claim(AudienceClaim::from("customers"))
/// .set_claim(SubjectClaim::from("loyal subjects"))
/// .set_claim(IssuerClaim::from("me"))
/// .set_claim(TokenIdentifierClaim::from("me"))
/// .set_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
/// .set_claim(CustomClaim::try_from(("seats", 4))?)
/// .set_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
/// .set_footer(footer)
/// .try_sign(&private_key)?;
/// //now let's try to verify it
/// let json = GenericParser::<V2, Public>::default()
/// .set_footer(footer)
/// .check_claim(AudienceClaim::from("customers"))
/// .check_claim(SubjectClaim::from("loyal subjects"))
/// .check_claim(IssuerClaim::from("me"))
/// .check_claim(TokenIdentifierClaim::from("me"))
/// .check_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .check_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .check_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .check_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
/// .check_claim(CustomClaim::try_from(("seats", 4))?)
/// .check_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
/// .parse(&token, &public_key)?;
/// // we can access all the values from the serde Value object returned by the parser
/// assert_eq!(json["aud"], "customers");
/// assert_eq!(json["jti"], "me");
/// assert_eq!(json["iss"], "me");
/// assert_eq!(json["data"], "this is a secret message");
/// assert_eq!(json["exp"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["iat"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["nbf"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["sub"], "loyal subjects");
/// assert_eq!(json["pi to 6 digits"], 3.141526);
/// assert_eq!(json["seats"], 4);
/// # }
/// # Ok::<(),anyhow::Error>(())
///```
pub fn try_sign(&mut self, key: &PasetoAsymmetricPrivateKey<V2, Public>) -> Result<String, GenericBuilderError> {
let mut token_builder = Paseto::<V2, Public>::builder();
let payload = self.build_payload_from_claims()?;
token_builder.set_payload(Payload::from(payload.as_str()));
if let Some(footer) = &self.footer {
token_builder.set_footer(*footer);
}
Ok(token_builder.try_sign(key)?)
}
}
#[cfg(feature = "v3_public")]
impl GenericBuilder<'_, '_, V3, Public> {
/// Given a [PasetoAsymmetricPrivateKey], attempts to sign a ([V3], [Public]) PASETO token from the data and
/// claims provided to the GenericBuilder with an optional [Footer] and an optional
/// [ImplicitAssertion].
///
/// Returns `Ok(String)` on success, where the String is the signed PASETO token, otherwise returns an error.
///
/// # Errors
///
/// Returns [`GenericBuilderError`] for any errors when building the token string
/// for signing or during signing.
///
/// # Example
///
///```
///# #[cfg(all(feature = "generic", feature="v3_public"))]
///# {
/// # use rusty_paseto::generic::*;
/// //obtain a key
/// let private_key = Key::<48>::try_from(
/// "20347609607477aca8fbfbc5e6218455f3199669792ef8b466faa87bdc67798144c848dd03661eed5ac62461340cea96",
/// )?;
/// let private_key = PasetoAsymmetricPrivateKey::<V3, Public>::from(&private_key);
/// let public_key = Key::<49>::try_from(
/// "02fbcb7c69ee1c60579be7a334134878d9c5c5bf35d552dab63c0140397ed14cef637d7720925c44699ea30e72874c72fb",
/// )?;
/// let public_key = PasetoAsymmetricPublicKey::<V3, Public>::try_from(&public_key)?;
/// let footer = Footer::from("some footer");
/// let implicit_assertion = ImplicitAssertion::from("some assertion");
/// //sign a public V3 token
/// let token = GenericBuilder::<V3, Public>::default()
/// .set_claim(AudienceClaim::from("customers"))
/// .set_claim(SubjectClaim::from("loyal subjects"))
/// .set_claim(IssuerClaim::from("me"))
/// .set_claim(TokenIdentifierClaim::from("me"))
/// .set_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
/// .set_claim(CustomClaim::try_from(("seats", 4))?)
/// .set_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
/// .set_footer(footer)
/// .set_implicit_assertion(implicit_assertion)
/// .try_sign(&private_key)?;
/// //now let's try to verify it
/// let json = GenericParser::<V3, Public>::default()
/// .set_footer(footer)
/// .check_claim(AudienceClaim::from("customers"))
/// .set_implicit_assertion(implicit_assertion)
/// .check_claim(SubjectClaim::from("loyal subjects"))
/// .check_claim(IssuerClaim::from("me"))
/// .check_claim(TokenIdentifierClaim::from("me"))
/// .check_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .check_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .check_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .check_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
/// .check_claim(CustomClaim::try_from(("seats", 4))?)
/// .check_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
/// .parse(&token, &public_key)?;
/// // we can access all the values from the serde Value object returned by the parser
/// assert_eq!(json["aud"], "customers");
/// assert_eq!(json["jti"], "me");
/// assert_eq!(json["iss"], "me");
/// assert_eq!(json["data"], "this is a secret message");
/// assert_eq!(json["exp"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["iat"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["nbf"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["sub"], "loyal subjects");
/// assert_eq!(json["pi to 6 digits"], 3.141526);
/// assert_eq!(json["seats"], 4);
/// # }
/// # Ok::<(),anyhow::Error>(())
///```
pub fn try_sign(&mut self, key: &PasetoAsymmetricPrivateKey<V3, Public>) -> Result<String, GenericBuilderError> {
let mut token_builder = Paseto::<V3, Public>::builder();
let payload = self.build_payload_from_claims()?;
token_builder.set_payload(Payload::from(payload.as_str()));
if let Some(footer) = &self.footer {
token_builder.set_footer(*footer);
}
if let Some(implicit_assertion) = &self.implicit_assertion {
token_builder.set_implicit_assertion(*implicit_assertion);
}
Ok(token_builder.try_sign(key)?)
}
}
#[cfg(feature = "v4_public")]
impl GenericBuilder<'_, '_, V4, Public> {
/// Given a [PasetoAsymmetricPrivateKey], attempts to sign a ([V4], [Public]) PASETO token from the data and
/// claims provided to the GenericBuilder with an optional [Footer] and an optional
/// [ImplicitAssertion].
///
/// Returns `Ok(String)` on success, where the String is the signed PASETO token, otherwise returns an error.
///
/// # Errors
///
/// Returns [`GenericBuilderError`] for any errors when building the token string
/// for signing or during signing.
///
/// # Example
///
///```
///# #[cfg(all(feature = "generic", feature="v4_public"))]
///# {
/// # use rusty_paseto::generic::*;
/// //create a key
/// let private_key = Key::<64>::try_from("b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a37741eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2")?;
/// let pk: &[u8] = private_key.as_slice();
/// let private_key = PasetoAsymmetricPrivateKey::<V4, Public>::from(pk);
/// let public_key = Key::<32>::try_from("1eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2")?;
/// let public_key = PasetoAsymmetricPublicKey::<V4, Public>::from(&public_key);
/// let footer = Footer::from("some footer");
/// let implicit_assertion = ImplicitAssertion::from("some assertion");
/// //sign a public V4 token
/// let token = GenericBuilder::<V4, Public>::default()
/// .set_claim(AudienceClaim::from("customers"))
/// .set_claim(SubjectClaim::from("loyal subjects"))
/// .set_claim(IssuerClaim::from("me"))
/// .set_claim(TokenIdentifierClaim::from("me"))
/// .set_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .set_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
/// .set_claim(CustomClaim::try_from(("seats", 4))?)
/// .set_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
/// .set_footer(footer)
/// .set_implicit_assertion(implicit_assertion)
/// .try_sign(&private_key)?;
/// //now let's try to verify it
/// let json = GenericParser::<V4, Public>::default()
/// .set_footer(footer)
/// .set_implicit_assertion(implicit_assertion)
/// .check_claim(AudienceClaim::from("customers"))
/// .check_claim(SubjectClaim::from("loyal subjects"))
/// .check_claim(IssuerClaim::from("me"))
/// .check_claim(TokenIdentifierClaim::from("me"))
/// .check_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .check_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .check_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
/// .check_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
/// .check_claim(CustomClaim::try_from(("seats", 4))?)
/// .check_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
/// .parse(&token, &public_key)?;
/// // we can access all the values from the serde Value object returned by the parser
/// assert_eq!(json["aud"], "customers");
/// assert_eq!(json["jti"], "me");
/// assert_eq!(json["iss"], "me");
/// assert_eq!(json["data"], "this is a secret message");
/// assert_eq!(json["exp"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["iat"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["nbf"], "2019-01-01T00:00:00+00:00");
/// assert_eq!(json["sub"], "loyal subjects");
/// assert_eq!(json["pi to 6 digits"], 3.141526);
/// assert_eq!(json["seats"], 4);
/// # }
/// # Ok::<(),anyhow::Error>(())
///```
pub fn try_sign(&mut self, key: &PasetoAsymmetricPrivateKey<V4, Public>) -> Result<String, GenericBuilderError> {
let mut token_builder = Paseto::<V4, Public>::builder();
let payload = self.build_payload_from_claims()?;
token_builder.set_payload(Payload::from(payload.as_str()));
if let Some(footer) = &self.footer {
token_builder.set_footer(*footer);
}
if let Some(implicit_assertion) = &self.implicit_assertion {
token_builder.set_implicit_assertion(*implicit_assertion);
}
Ok(token_builder.try_sign(key)?)
}
}
#[cfg(all(test, feature = "v4_public"))]
mod generic_v4_public_builders {
use anyhow::Result;
use crate::generic::*;
use crate::generic::claims::*;
#[test]
fn full_generic_v4_public_builder_test() -> Result<()> {
//create a key
let private_key = Key::<64>::try_from("b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a37741eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2")?;
let pk: &[u8] = private_key.as_slice();
let private_key = PasetoAsymmetricPrivateKey::<V4, Public>::from(pk);
let public_key = Key::<32>::try_from("1eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2")?;
let public_key = PasetoAsymmetricPublicKey::<V4, Public>::from(&public_key);
let footer = Footer::from("some footer");
let implicit_assertion = ImplicitAssertion::from("some assertion");
//sign a public V4 token
let token = GenericBuilder::<V4, Public>::default()
.set_claim(AudienceClaim::from("customers"))
.set_claim(SubjectClaim::from("loyal subjects"))
.set_claim(IssuerClaim::from("me"))
.set_claim(TokenIdentifierClaim::from("me"))
.set_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
.set_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
.set_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
.set_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
.set_claim(CustomClaim::try_from(("seats", 4))?)
.set_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
.set_footer(footer)
.set_implicit_assertion(implicit_assertion)
.try_sign(&private_key)?;
//now let's try to verify it
let json = GenericParser::<V4, Public>::default()
.set_footer(footer)
.set_implicit_assertion(implicit_assertion)
.check_claim(AudienceClaim::from("customers"))
.check_claim(SubjectClaim::from("loyal subjects"))
.check_claim(IssuerClaim::from("me"))
.check_claim(TokenIdentifierClaim::from("me"))
.check_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
.check_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
.check_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
.check_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
.check_claim(CustomClaim::try_from(("seats", 4))?)
.check_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
.parse(&token, &public_key)?;
// we can access all the values from the serde Value object returned by the parser
assert_eq!(json["aud"], "customers");
assert_eq!(json["jti"], "me");
assert_eq!(json["iss"], "me");
assert_eq!(json["data"], "this is a secret message");
assert_eq!(json["exp"], "2019-01-01T00:00:00+00:00");
assert_eq!(json["iat"], "2019-01-01T00:00:00+00:00");
assert_eq!(json["nbf"], "2019-01-01T00:00:00+00:00");
assert_eq!(json["sub"], "loyal subjects");
assert_eq!(json["pi to 6 digits"], 3.141526);
assert_eq!(json["seats"], 4);
Ok(())
}
}
#[cfg(all(test, feature = "v3_public"))]
mod generic_v3_public_builders {
use anyhow::Result;
use crate::generic::*;
use crate::generic::claims::*;
#[test]
fn full_generic_v3_public_builder_test() -> Result<()> {
//create a key
let private_key = Key::<48>::try_from(
"20347609607477aca8fbfbc5e6218455f3199669792ef8b466faa87bdc67798144c848dd03661eed5ac62461340cea96",
)?;
let private_key = PasetoAsymmetricPrivateKey::<V3, Public>::from(&private_key);
let public_key = Key::<49>::try_from(
"02fbcb7c69ee1c60579be7a334134878d9c5c5bf35d552dab63c0140397ed14cef637d7720925c44699ea30e72874c72fb",
)?;
let public_key = PasetoAsymmetricPublicKey::<V3, Public>::try_from(&public_key)?;
let footer = Footer::from("some footer");
let implicit_assertion = ImplicitAssertion::from("some assertion");
//sign a public V3 token
let token = GenericBuilder::<V3, Public>::default()
.set_claim(AudienceClaim::from("customers"))
.set_claim(SubjectClaim::from("loyal subjects"))
.set_claim(IssuerClaim::from("me"))
.set_claim(TokenIdentifierClaim::from("me"))
.set_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
.set_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
.set_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
.set_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
.set_claim(CustomClaim::try_from(("seats", 4))?)
.set_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
.set_footer(footer)
.set_implicit_assertion(implicit_assertion)
.try_sign(&private_key)?;
//now let's try to verify it
let json = GenericParser::<V3, Public>::default()
.set_footer(footer)
.set_implicit_assertion(implicit_assertion)
.check_claim(AudienceClaim::from("customers"))
.check_claim(SubjectClaim::from("loyal subjects"))
.check_claim(IssuerClaim::from("me"))
.check_claim(TokenIdentifierClaim::from("me"))
.check_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
.check_claim(NotBeforeClaim::try_from("2019-01-01T00:00:00+00:00")?)
.check_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
.check_claim(CustomClaim::try_from(("data", "this is a secret message"))?)
.check_claim(CustomClaim::try_from(("seats", 4))?)
.check_claim(CustomClaim::try_from(("pi to 6 digits", 3.141526))?)
.parse(&token, &public_key)?;
// we can access all the values from the serde Value object returned by the parser