forked from tweedegolf/irmars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessionrequest.rs
More file actions
777 lines (696 loc) · 28.4 KB
/
Copy pathsessionrequest.rs
File metadata and controls
777 lines (696 loc) · 28.4 KB
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
use crate::util::TranslatedString;
use std::{
collections::HashMap,
time::{Duration, SystemTime},
};
use serde::{Deserialize, Serialize};
/// Basic structure of an IRMA disclosure request, a conjunction of disjunctions of inner conjunctions.
/// Examples on how to use this can be found at irma.app/docs
pub type ConDisCon = Vec<Vec<Vec<AttributeRequest>>>;
fn omit_false(value: &bool) -> bool {
!value
}
/// Representation of a request for a single specific attribute
#[derive(Serialize, Deserialize, Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[serde(untagged)]
pub enum AttributeRequest {
/// Request for any value of the named attribute
Simple(String),
/// More complicated request for a value of the named attribute
Compound {
/// Which attribute is requested
#[serde(rename = "type")]
attr_type: String,
/// The required value, if any
#[serde(skip_serializing_if = "Option::is_none")]
value: Option<String>,
/// Is a no-value result is acceptable?
#[serde(rename = "notNull", skip_serializing_if = "omit_false", default)]
not_null: bool,
},
}
impl AttributeRequest {
/// Create an attribute request for an attribute for which we require at least some value.
pub fn non_null(attr_type: String) -> AttributeRequest {
AttributeRequest::Compound {
attr_type,
value: None,
not_null: true,
}
}
/// Create an attribute request where we want a specific value for the attribute to be disclosed
/// This is useful when using IRMA not to learn something about the user, but instead enforcing
/// some sort of access control, such as a minimum age.
pub fn with_value(attr_type: String, value: String) -> AttributeRequest {
AttributeRequest::Compound {
attr_type,
value: Some(value),
not_null: false,
}
}
}
/// Description of an IRMA credential to be issued.
/// The issuing IRMA server requires the private key of the issuer to be present to be able to issue a credential.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub struct Credential {
/// Identifier of the credential to be issued
pub credential: String,
/// Unix timestamp of until when the credential is valid. This is rounded down by the server to the nearest week.
/// When not present, the server will default the credential to be valid for 6 months
#[serde(skip_serializing_if = "Option::is_none", default)]
pub validity: Option<u64>,
/// Values for the attributes in the credential
pub attributes: HashMap<String, String>,
}
/// Builder for an IRMA credential
pub struct CredentialBuilder {
cred: Credential,
}
impl CredentialBuilder {
/// Create a builder for a credential
pub fn new(credential: String) -> CredentialBuilder {
CredentialBuilder {
cred: Credential {
credential,
validity: None,
attributes: HashMap::new(),
},
}
}
/// Set the validity period
pub fn validity_period(mut self, period: Duration) -> Self {
let validity_time = SystemTime::now() + period;
let timestamp = validity_time
.duration_since(SystemTime::UNIX_EPOCH)
.expect("No support for time manipulations before 1-1-1970")
.as_secs();
// we let the server do the rounding
self.cred.validity = Some(timestamp);
self
}
/// Add an indivial attribute
pub fn attribute(mut self, key: String, value: String) -> Self {
self.cred.attributes.insert(key, value);
self
}
/// Create the credential
pub fn build(self) -> Credential {
self.cred
}
}
/// Information common between all types of requests
#[derive(Serialize, Deserialize, Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub struct BaseRequest {
/// Con-dis-con of attributes to be disclosed
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub disclose: ConDisCon,
/// For mobile sessions, URL to redirect user to after completion of the session.
#[serde(rename = "clientReturnUrl", skip_serializing_if = "Option::is_none")]
pub return_url: Option<String>,
/// Have the irma server include the session token in the client return url. (see irma.app/docs for more details)
#[serde(
rename = "augmentReturnUrl",
skip_serializing_if = "omit_false",
default
)]
pub augment_return: bool,
/// Labels for the disjunctions in the disclosure request
#[serde(
skip_serializing_if = "HashMap::is_empty",
default,
deserialize_with = "crate::util::de_int_key"
)]
pub labels: HashMap<usize, TranslatedString>,
/// Host to use in the session QR (`Qr.u`), overriding the IRMA server's default.
/// The server validates this against the requestor's configured `host_perms` allowlist.
/// Requires irmago v0.14.0 or newer on the server side.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub host: Option<String>,
}
/// IRMA session requests
#[derive(Serialize, Deserialize, Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[serde(tag = "@context")]
pub enum IrmaRequest {
/// Request for the disclosure of some set of attributes
#[serde(rename = "https://irma.app/ld/request/disclosure/v2")]
Disclosure {
#[serde(flatten)]
base: BaseRequest,
},
/// Request for the signing of a message
#[serde(rename = "https://irma.app/ld/request/signature/v2")]
Signature {
message: String,
#[serde(flatten)]
base: BaseRequest,
},
/// Request for the issuance of one or more credentials
#[serde(rename = "https://irma.app/ld/request/issuance/v2")]
Issuance {
credentials: Vec<Credential>,
#[serde(flatten)]
base: BaseRequest,
},
}
struct BaseRequestBuilder {
base: BaseRequest,
}
impl BaseRequestBuilder {
fn new() -> BaseRequestBuilder {
BaseRequestBuilder {
base: BaseRequest {
disclose: vec![],
return_url: None,
augment_return: false,
labels: HashMap::new(),
host: None,
},
}
}
fn build(self) -> BaseRequest {
self.base
}
fn add_discons(&mut self, mut discons: ConDisCon) {
self.base.disclose.append(&mut discons);
}
fn add_discon(&mut self, discon: Vec<Vec<AttributeRequest>>) {
self.base.disclose.push(discon);
}
fn add_discon_with_label(
&mut self,
discon: Vec<Vec<AttributeRequest>>,
label: TranslatedString,
) {
let index = self.base.disclose.len();
self.base.disclose.push(discon);
self.base.labels.insert(index, label);
}
fn return_url(&mut self, return_url: String) {
debug_assert!(self.base.return_url.is_none());
self.base.return_url = Some(return_url);
}
fn augmented_return_url(&mut self, return_url: String) {
debug_assert!(self.base.return_url.is_none());
self.base.return_url = Some(return_url);
self.base.augment_return = true;
}
fn host(&mut self, host: String) {
debug_assert!(self.base.host.is_none());
self.base.host = Some(host);
}
}
impl Default for BaseRequestBuilder {
fn default() -> Self {
Self::new()
}
}
/// Build a disclosure request
#[derive(Default)]
pub struct DisclosureRequestBuilder {
base: BaseRequestBuilder,
}
impl DisclosureRequestBuilder {
/// Construct a new builder
pub fn new() -> DisclosureRequestBuilder {
DisclosureRequestBuilder::default()
}
/// Construct the actual request based on the given information
pub fn build(self) -> IrmaRequest {
debug_assert!(!self.base.base.disclose.is_empty());
IrmaRequest::Disclosure {
base: self.base.build(),
}
}
/// Add an additional disjunction to the request
pub fn add_discon(mut self, discon: Vec<Vec<AttributeRequest>>) -> DisclosureRequestBuilder {
self.base.add_discon(discon);
self
}
/// Add multiple additional disjunctions to the request
pub fn add_discons(mut self, discons: ConDisCon) -> DisclosureRequestBuilder {
self.base.add_discons(discons);
self
}
/// Add an additional labeled disjunction to the request
pub fn add_discon_with_label(
mut self,
discon: Vec<Vec<AttributeRequest>>,
label: TranslatedString,
) -> DisclosureRequestBuilder {
self.base.add_discon_with_label(discon, label);
self
}
/// Set a return URL on the request
pub fn return_url(mut self, return_url: String) -> DisclosureRequestBuilder {
self.base.return_url(return_url);
self
}
/// Set an augmented return url on the request
pub fn augmented_return_url(mut self, return_url: String) -> DisclosureRequestBuilder {
self.base.augmented_return_url(return_url);
self
}
/// Set the host to use in the session QR, overriding the IRMA server's default.
/// The server validates this against the requestor's configured `host_perms` allowlist
/// (requires irmago v0.14.0 or newer).
pub fn host(mut self, host: String) -> DisclosureRequestBuilder {
self.base.host(host);
self
}
}
/// Build a signature request
pub struct SignatureRequestBuilder {
message: String,
base: BaseRequestBuilder,
}
impl SignatureRequestBuilder {
/// Construct a new builder for a request to sign the given message
pub fn new(message: String) -> SignatureRequestBuilder {
SignatureRequestBuilder {
message,
base: BaseRequestBuilder::new(),
}
}
/// Construct the actual request based on the given information
pub fn build(self) -> IrmaRequest {
debug_assert!(!self.base.base.disclose.is_empty());
IrmaRequest::Signature {
message: self.message,
base: self.base.build(),
}
}
/// Add an additional disjunction to the request
pub fn add_discon(mut self, discon: Vec<Vec<AttributeRequest>>) -> SignatureRequestBuilder {
self.base.add_discon(discon);
self
}
/// Add multiple additional disjunctions to the request
pub fn add_discons(mut self, discons: ConDisCon) -> SignatureRequestBuilder {
self.base.add_discons(discons);
self
}
/// Add an additional labeled disjunction to the request
pub fn add_discon_with_label(
mut self,
discon: Vec<Vec<AttributeRequest>>,
label: TranslatedString,
) -> SignatureRequestBuilder {
self.base.add_discon_with_label(discon, label);
self
}
/// Set a return URL on the request
pub fn return_url(mut self, return_url: String) -> SignatureRequestBuilder {
self.base.return_url(return_url);
self
}
/// Set an augmented return url on the request
pub fn augmented_return_url(mut self, return_url: String) -> SignatureRequestBuilder {
self.base.augmented_return_url(return_url);
self
}
/// Set the host to use in the session QR, overriding the IRMA server's default.
/// The server validates this against the requestor's configured `host_perms` allowlist
/// (requires irmago v0.14.0 or newer).
pub fn host(mut self, host: String) -> SignatureRequestBuilder {
self.base.host(host);
self
}
}
/// Build a request to issue one or more credentials
#[derive(Default)]
pub struct IssuanceRequestBuilder {
credentials: Vec<Credential>,
base: BaseRequestBuilder,
}
impl IssuanceRequestBuilder {
/// Construct a new builder
pub fn new() -> IssuanceRequestBuilder {
IssuanceRequestBuilder {
credentials: vec![],
base: BaseRequestBuilder::new(),
}
}
/// Construct the actual request based on the given information
pub fn build(self) -> IrmaRequest {
debug_assert!(!self.credentials.is_empty());
IrmaRequest::Issuance {
credentials: self.credentials,
base: self.base.build(),
}
}
/// Add an additional credential to be issued
pub fn add_credential(mut self, credential: Credential) -> IssuanceRequestBuilder {
self.credentials.push(credential);
self
}
/// Add an additional disjunction to the request
pub fn add_discon(mut self, discon: Vec<Vec<AttributeRequest>>) -> IssuanceRequestBuilder {
self.base.add_discon(discon);
self
}
/// Add multiple additional disjunctions to the request
pub fn add_discons(mut self, discons: ConDisCon) -> IssuanceRequestBuilder {
self.base.add_discons(discons);
self
}
/// Add an additional labeled disjunction to the request
pub fn add_discon_with_label(
mut self,
discon: Vec<Vec<AttributeRequest>>,
label: TranslatedString,
) -> IssuanceRequestBuilder {
self.base.add_discon_with_label(discon, label);
self
}
/// Set a return URL on the request
pub fn return_url(mut self, return_url: String) -> IssuanceRequestBuilder {
self.base.return_url(return_url);
self
}
/// Set an augmented return url on the request
pub fn augmented_return_url(mut self, return_url: String) -> IssuanceRequestBuilder {
self.base.augmented_return_url(return_url);
self
}
/// Set the host to use in the session QR, overriding the IRMA server's default.
/// The server validates this against the requestor's configured `host_perms` allowlist
/// (requires irmago v0.14.0 or newer).
pub fn host(mut self, host: String) -> IssuanceRequestBuilder {
self.base.host(host);
self
}
}
/// An IRMA request extended with extra information for the server on how to execute it.
/// (Note: this interface is unstable, and might change significantly in the future)
#[derive(Serialize, Deserialize, Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub struct ExtendedIrmaRequest {
/// How long a session result JWT should be valid once requested, in seconds
#[serde(skip_serializing_if = "Option::is_none")]
pub validity: Option<u64>,
/// How long the session remains available for an IRMA client to connect to
#[serde(skip_serializing_if = "Option::is_none")]
pub timeout: Option<u64>,
/// URL on which to recieve updates as the session status changes
#[serde(rename = "callbackUrl", skip_serializing_if = "Option::is_none")]
pub callback_url: Option<String>,
/// Inner request
pub request: IrmaRequest,
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use maplit::hashmap;
use crate::CredentialBuilder;
use super::{
AttributeRequest, Credential, DisclosureRequestBuilder, IssuanceRequestBuilder,
SignatureRequestBuilder, TranslatedString,
};
#[test]
fn test_attribute_request() {
let attr1 = AttributeRequest::Simple("a.b.c.d".into());
assert_eq!("\"a.b.c.d\"", serde_json::to_string(&attr1).unwrap());
assert_eq!(
attr1,
serde_json::from_str(&serde_json::to_string(&attr1).unwrap()).unwrap()
);
let attr2 = AttributeRequest::non_null("x.y.z.d".into());
assert_eq!(
"{\"type\":\"x.y.z.d\",\"notNull\":true}",
serde_json::to_string(&attr2).unwrap()
);
assert_eq!(
attr2,
serde_json::from_str(&serde_json::to_string(&attr2).unwrap()).unwrap()
);
let attr3 = AttributeRequest::with_value("f.g.h.i".into(), "testvalue".into());
assert_eq!(
"{\"type\":\"f.g.h.i\",\"value\":\"testvalue\"}",
serde_json::to_string(&attr3).unwrap()
);
assert_eq!(
attr3,
serde_json::from_str(&serde_json::to_string(&attr3).unwrap()).unwrap()
);
}
#[test]
fn test_credential() {
let cred1 = CredentialBuilder::new("a.b.c".into())
.attribute("d".into(), "e".into())
.build();
assert_eq!(
"{\"credential\":\"a.b.c\",\"attributes\":{\"d\":\"e\"}}",
serde_json::to_string(&cred1).unwrap()
);
assert_eq!(
cred1,
serde_json::from_str(&serde_json::to_string(&cred1).unwrap()).unwrap()
);
let cred2 = CredentialBuilder::new("a.b.c".into())
.validity_period(Duration::new(300, 0))
.attribute("d".into(), "e".into())
.build();
assert_eq!(
format!(
"{{\"credential\":\"a.b.c\",\"validity\":{},\"attributes\":{{\"d\":\"e\"}}}}",
cred2.validity.unwrap()
),
serde_json::to_string(&cred2).unwrap()
);
assert_eq!(
cred2,
serde_json::from_str(&serde_json::to_string(&cred2).unwrap()).unwrap()
);
}
#[test]
fn test_disclosure_request() {
let req1 = DisclosureRequestBuilder::new()
.add_discon(vec![vec![AttributeRequest::Simple("a.b.c.d".into())]])
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/disclosure/v2\",\"disclose\":[[[\"a.b.c.d\"]]]}", serde_json::to_string(&req1).unwrap());
assert_eq!(
req1,
serde_json::from_str(&serde_json::to_string(&req1).unwrap()).unwrap()
);
let req2 = DisclosureRequestBuilder::new()
.add_discon(vec![vec![AttributeRequest::non_null("x.y.z.w".into())]])
.add_discon_with_label(
vec![vec![AttributeRequest::Simple("a.b.c.d".into())]],
TranslatedString {
en: "en".into(),
nl: "nl".into(),
},
)
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/disclosure/v2\",\"disclose\":[[[{\"type\":\"x.y.z.w\",\"notNull\":true}]],[[\"a.b.c.d\"]]],\"labels\":{\"1\":{\"en\":\"en\",\"nl\":\"nl\"}}}", serde_json::to_string(&req2).unwrap());
assert_eq!(
req2,
serde_json::from_str(&serde_json::to_string(&req2).unwrap()).unwrap()
);
let req3 = DisclosureRequestBuilder::new()
.add_discon(vec![vec![AttributeRequest::Simple("a.b.c.d".into())]])
.return_url("https://example.com".into())
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/disclosure/v2\",\"disclose\":[[[\"a.b.c.d\"]]],\"clientReturnUrl\":\"https://example.com\"}", serde_json::to_string(&req3).unwrap());
assert_eq!(
req3,
serde_json::from_str(&serde_json::to_string(&req3).unwrap()).unwrap()
);
let req4 = DisclosureRequestBuilder::new()
.add_discon(vec![vec![AttributeRequest::Simple("a.b.c.d".into())]])
.augmented_return_url("https://example.com".into())
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/disclosure/v2\",\"disclose\":[[[\"a.b.c.d\"]]],\"clientReturnUrl\":\"https://example.com\",\"augmentReturnUrl\":true}", serde_json::to_string(&req4).unwrap());
assert_eq!(
req4,
serde_json::from_str(&serde_json::to_string(&req4).unwrap()).unwrap()
);
}
#[test]
fn test_host_request() {
// host is omitted from the serialized request when not set
let req1 = DisclosureRequestBuilder::new()
.add_discon(vec![vec![AttributeRequest::Simple("a.b.c.d".into())]])
.build();
assert!(!serde_json::to_string(&req1).unwrap().contains("host"));
// host is serialized as "host" when set, and survives a round-trip
let req2 = DisclosureRequestBuilder::new()
.add_discon(vec![vec![AttributeRequest::Simple("a.b.c.d".into())]])
.host("https://example.com".into())
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/disclosure/v2\",\"disclose\":[[[\"a.b.c.d\"]]],\"host\":\"https://example.com\"}", serde_json::to_string(&req2).unwrap());
assert_eq!(
req2,
serde_json::from_str(&serde_json::to_string(&req2).unwrap()).unwrap()
);
// the host setter is available on every public builder
let req3 = SignatureRequestBuilder::new("testmessage".into())
.add_discon(vec![vec![AttributeRequest::Simple("a.b.c.d".into())]])
.host("https://signature.example.com".into())
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/signature/v2\",\"message\":\"testmessage\",\"disclose\":[[[\"a.b.c.d\"]]],\"host\":\"https://signature.example.com\"}", serde_json::to_string(&req3).unwrap());
assert_eq!(
req3,
serde_json::from_str(&serde_json::to_string(&req3).unwrap()).unwrap()
);
let req4 = IssuanceRequestBuilder::new()
.add_credential(Credential {
credential: "a.b.c".into(),
validity: Some(123456789),
attributes: hashmap![
"d".into() => "e".into(),
],
})
.host("https://issuance.example.com".into())
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/issuance/v2\",\"credentials\":[{\"credential\":\"a.b.c\",\"validity\":123456789,\"attributes\":{\"d\":\"e\"}}],\"host\":\"https://issuance.example.com\"}", serde_json::to_string(&req4).unwrap());
assert_eq!(
req4,
serde_json::from_str(&serde_json::to_string(&req4).unwrap()).unwrap()
);
}
#[test]
fn test_signature_request() {
let req1 = SignatureRequestBuilder::new("testmessage".into())
.add_discon(vec![vec![AttributeRequest::Simple("a.b.c.d".into())]])
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/signature/v2\",\"message\":\"testmessage\",\"disclose\":[[[\"a.b.c.d\"]]]}", serde_json::to_string(&req1).unwrap());
assert_eq!(
req1,
serde_json::from_str(&serde_json::to_string(&req1).unwrap()).unwrap()
);
let req2 = SignatureRequestBuilder::new("testmessage".into())
.add_discon_with_label(
vec![vec![AttributeRequest::Simple("a.b.c.d".into())]],
TranslatedString {
en: "en".into(),
nl: "nl".into(),
},
)
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/signature/v2\",\"message\":\"testmessage\",\"disclose\":[[[\"a.b.c.d\"]]],\"labels\":{\"0\":{\"en\":\"en\",\"nl\":\"nl\"}}}", serde_json::to_string(&req2).unwrap());
assert_eq!(
req2,
serde_json::from_str(&serde_json::to_string(&req2).unwrap()).unwrap()
);
let req3 = SignatureRequestBuilder::new("testmessage".into())
.add_discon(vec![vec![AttributeRequest::Simple("a.b.c.d".into())]])
.return_url("https://example.com".into())
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/signature/v2\",\"message\":\"testmessage\",\"disclose\":[[[\"a.b.c.d\"]]],\"clientReturnUrl\":\"https://example.com\"}", serde_json::to_string(&req3).unwrap());
assert_eq!(
req3,
serde_json::from_str(&serde_json::to_string(&req3).unwrap()).unwrap()
);
let req4 = SignatureRequestBuilder::new("testmessage".into())
.add_discon(vec![vec![AttributeRequest::Simple("a.b.c.d".into())]])
.augmented_return_url("https://example.com".into())
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/signature/v2\",\"message\":\"testmessage\",\"disclose\":[[[\"a.b.c.d\"]]],\"clientReturnUrl\":\"https://example.com\",\"augmentReturnUrl\":true}", serde_json::to_string(&req4).unwrap());
assert_eq!(
req4,
serde_json::from_str(&serde_json::to_string(&req4).unwrap()).unwrap()
);
}
#[test]
fn test_issuance_request() {
let req1 = IssuanceRequestBuilder::new()
.add_credential(Credential {
credential: "a.b.c".into(),
validity: Some(123456789),
attributes: hashmap![
"d".into() => "e".into(),
],
})
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/issuance/v2\",\"credentials\":[{\"credential\":\"a.b.c\",\"validity\":123456789,\"attributes\":{\"d\":\"e\"}}]}", serde_json::to_string(&req1).unwrap());
assert_eq!(
req1,
serde_json::from_str(&serde_json::to_string(&req1).unwrap()).unwrap()
);
let req2 = IssuanceRequestBuilder::new()
.add_discon(vec![vec![AttributeRequest::Simple("x.y.z.w".into())]])
.add_credential(Credential {
credential: "a.b.c".into(),
validity: Some(123456789),
attributes: hashmap![
"d".into() => "e".into(),
],
})
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/issuance/v2\",\"credentials\":[{\"credential\":\"a.b.c\",\"validity\":123456789,\"attributes\":{\"d\":\"e\"}}],\"disclose\":[[[\"x.y.z.w\"]]]}", serde_json::to_string(&req2).unwrap());
assert_eq!(
req2,
serde_json::from_str(&serde_json::to_string(&req2).unwrap()).unwrap()
);
let req3 = IssuanceRequestBuilder::new()
.add_discon_with_label(
vec![vec![AttributeRequest::Simple("x.y.z.w".into())]],
TranslatedString {
en: "en".into(),
nl: "nl".into(),
},
)
.add_credential(Credential {
credential: "a.b.c".into(),
validity: Some(123456789),
attributes: hashmap![
"d".into() => "e".into(),
],
})
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/issuance/v2\",\"credentials\":[{\"credential\":\"a.b.c\",\"validity\":123456789,\"attributes\":{\"d\":\"e\"}}],\"disclose\":[[[\"x.y.z.w\"]]],\"labels\":{\"0\":{\"en\":\"en\",\"nl\":\"nl\"}}}", serde_json::to_string(&req3).unwrap());
assert_eq!(
req3,
serde_json::from_str(&serde_json::to_string(&req3).unwrap()).unwrap()
);
let req4 = IssuanceRequestBuilder::new()
.add_credential(Credential {
credential: "a.b.c".into(),
validity: Some(123456789),
attributes: hashmap![
"d".into() => "e".into(),
],
})
.return_url("https://example.com".into())
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/issuance/v2\",\"credentials\":[{\"credential\":\"a.b.c\",\"validity\":123456789,\"attributes\":{\"d\":\"e\"}}],\"clientReturnUrl\":\"https://example.com\"}", serde_json::to_string(&req4).unwrap());
assert_eq!(
req4,
serde_json::from_str(&serde_json::to_string(&req4).unwrap()).unwrap()
);
let req5 = IssuanceRequestBuilder::new()
.add_credential(Credential {
credential: "a.b.c".into(),
validity: Some(123456789),
attributes: hashmap![
"d".into() => "e".into(),
],
})
.augmented_return_url("https://example.com".into())
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/issuance/v2\",\"credentials\":[{\"credential\":\"a.b.c\",\"validity\":123456789,\"attributes\":{\"d\":\"e\"}}],\"clientReturnUrl\":\"https://example.com\",\"augmentReturnUrl\":true}", serde_json::to_string(&req5).unwrap());
assert_eq!(
req5,
serde_json::from_str(&serde_json::to_string(&req5).unwrap()).unwrap()
);
let req6 = IssuanceRequestBuilder::new()
.add_credential(Credential {
credential: "a.b.c".into(),
validity: None,
attributes: hashmap![
"d".into() => "e".into(),
],
})
.build();
assert_eq!("{\"@context\":\"https://irma.app/ld/request/issuance/v2\",\"credentials\":[{\"credential\":\"a.b.c\",\"attributes\":{\"d\":\"e\"}}]}", serde_json::to_string(&req6).unwrap());
assert_eq!(
req6,
serde_json::from_str(&serde_json::to_string(&req6).unwrap()).unwrap()
);
}
}