generated from iotaledger/template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sd_jwt.rs
100 lines (86 loc) · 5.52 KB
/
sd_jwt.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
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
use std::fmt::Display;
use std::str::FromStr;
use crate::Error;
use crate::Result;
use itertools::Itertools;
/// Representation of an SD-JWT of the format
/// `<Issuer-signed JWT>~<Disclosure 1>~<Disclosure 2>~...~<Disclosure N>~<optional KB-JWT>`.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SdJwt {
/// The JWT part.
pub jwt: String,
/// The disclosures part.
pub disclosures: Vec<String>,
/// The optional key binding JWT.
pub key_binding_jwt: Option<String>,
}
impl SdJwt {
/// Creates a new [`SdJwt`] from its components.
pub fn new(jwt: String, disclosures: Vec<String>, key_binding_jwt: Option<String>) -> Self {
Self {
jwt,
disclosures,
key_binding_jwt,
}
}
/// Serializes the components into the final SD-JWT.
///
/// ## Error
/// Returns [`Error::DeserializationError`] if parsing fails.
pub fn presentation(&self) -> String {
let disclosures = self.disclosures.iter().join("~");
let key_bindings = self.key_binding_jwt.as_deref().unwrap_or("");
format!("{}~{}~{}", self.jwt, disclosures, key_bindings)
}
/// Parses an SD-JWT into its components as [`SdJwt`].
pub fn parse(sd_jwt: &str) -> Result<Self> {
let sd_segments: Vec<&str> = sd_jwt.split('~').collect();
let num_of_segments = sd_segments.len();
if num_of_segments < 2 {
return Err(Error::DeserializationError(
"SD-JWT format is invalid, less than 2 segments".to_string(),
));
}
let includes_key_binding = sd_jwt.chars().next_back().is_some_and(|char| char != '~');
if includes_key_binding && num_of_segments < 3 {
return Err(Error::DeserializationError(
"SD-JWT format is invalid, less than 3 segments with key binding jwt".to_string(),
));
}
let jwt = sd_segments.first().unwrap().to_string();
let disclosures: Vec<String> = sd_segments[1..num_of_segments - 1]
.iter()
.map(|disclosure| disclosure.to_string())
.collect();
let key_binding = includes_key_binding.then(|| sd_segments[num_of_segments - 1].to_string());
Ok(Self {
jwt,
disclosures,
key_binding_jwt: key_binding,
})
}
}
impl Display for SdJwt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&(self.presentation()))
}
}
impl FromStr for SdJwt {
type Err = Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Self::parse(s)
}
}
#[cfg(test)]
mod test {
use crate::SdJwt;
#[test]
fn parse() {
let sd_jwt_str = "eyJhbGciOiAiRVMyNTYifQ.eyJAY29udGV4dCI6IFsiaHR0cHM6Ly93d3cudzMub3JnLzIwMTgvY3JlZGVudGlhbHMvdjEiLCAiaHR0cHM6Ly93M2lkLm9yZy92YWNjaW5hdGlvbi92MSJdLCAidHlwZSI6IFsiVmVyaWZpYWJsZUNyZWRlbnRpYWwiLCAiVmFjY2luYXRpb25DZXJ0aWZpY2F0ZSJdLCAiaXNzdWVyIjogImh0dHBzOi8vZXhhbXBsZS5jb20vaXNzdWVyIiwgImlzc3VhbmNlRGF0ZSI6ICIyMDIzLTAyLTA5VDExOjAxOjU5WiIsICJleHBpcmF0aW9uRGF0ZSI6ICIyMDI4LTAyLTA4VDExOjAxOjU5WiIsICJuYW1lIjogIkNPVklELTE5IFZhY2NpbmF0aW9uIENlcnRpZmljYXRlIiwgImRlc2NyaXB0aW9uIjogIkNPVklELTE5IFZhY2NpbmF0aW9uIENlcnRpZmljYXRlIiwgImNyZWRlbnRpYWxTdWJqZWN0IjogeyJfc2QiOiBbIjFWX0stOGxEUThpRlhCRlhiWlk5ZWhxUjRIYWJXQ2k1VDB5Ykl6WlBld3ciLCAiSnpqTGd0UDI5ZFA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