generated from iotaledger/template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
disclosure.rs
167 lines (153 loc) · 4.8 KB
/
disclosure.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
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
use crate::Error;
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
use std::fmt::Display;
/// Represents an elements constructing a disclosure.
/// Object properties and array elements disclosures are supported.
///
/// See: https://www.ietf.org/archive/id/draft-ietf-oauth-selective-disclosure-jwt-07.html#name-disclosures
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Disclosure {
/// The salt value.
pub salt: String,
/// The claim name, optional for array elements.
pub claim_name: Option<String>,
/// The claim Value which can be of any type.
pub claim_value: Value,
/// The base64url-encoded string.
pub disclosure: String,
}
impl Disclosure {
/// Creates a new instance of [`Disclosure`].
///
/// Use `.to_string()` to get the actual disclosure.
pub fn new(salt: String, claim_name: Option<String>, claim_value: Value) -> Self {
let input = if let Some(name) = &claim_name {
format!("[\"{}\", \"{}\", {}]", &salt, &name, &claim_value.to_string())
} else {
format!("[\"{}\", {}]", &salt, &claim_value.to_string())
};
let encoded = multibase::Base::Base64Url.encode(input);
Self {
salt,
claim_name,
claim_value,
disclosure: encoded,
}
}
/// Parses a Base64 encoded disclosure into a [`Disclosure`].
///
/// ## Error
///
/// Returns an [`Error::InvalidDisclosure`] if input is not a valid disclosure.
pub fn parse(disclosure: String) -> Result<Self, Error> {
let decoded: Vec<Value> = multibase::Base::Base64Url
.decode(&disclosure)
.map_err(|_e| {
Error::InvalidDisclosure(format!(
"Base64 decoding of the disclosure was not possible {}",
disclosure
))
})
.and_then(|data| {
serde_json::from_slice(&data).map_err(|_e| {
Error::InvalidDisclosure(format!(
"decoded disclosure could not be serialized as an array {}",
disclosure
))
})
})?;
if decoded.len() == 2 {
Ok(Self {
salt: decoded
.first()
.ok_or(Error::InvalidDisclosure("invalid salt".to_string()))?
.as_str()
.ok_or(Error::InvalidDisclosure(
"salt could not be parsed as a string".to_string(),
))?
.to_owned(),
claim_name: None,
claim_value: decoded
.get(1)
.ok_or(Error::InvalidDisclosure("invalid claim name".to_string()))?
.clone(),
disclosure,
})
} else if decoded.len() == 3 {
Ok(Self {
salt: decoded
.first()
.ok_or(Error::InvalidDisclosure("invalid salt".to_string()))?
.as_str()
.ok_or(Error::InvalidDisclosure(
"salt could not be parsed as a string".to_string(),
))?
.to_owned(),
claim_name: Some(
decoded
.get(1)
.ok_or(Error::InvalidDisclosure("invalid claim name".to_string()))?
.as_str()
.ok_or(Error::InvalidDisclosure(
"claim name could not be parsed as a string".to_string(),
))?
.to_owned(),
),
claim_value: decoded
.get(2)
.ok_or(Error::InvalidDisclosure("invalid claim name".to_string()))?
.clone(),
disclosure,
})
} else {
Err(Error::InvalidDisclosure(format!(
"deserialized array has an invalid length of {}",
decoded.len()
)))
}
}
/// Reference the actual disclosure.
pub fn as_str(&self) -> &str {
&self.disclosure
}
/// Convert this object into the actual disclosure.
pub fn into_string(self) -> String {
self.disclosure
}
}
impl Display for Disclosure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.disclosure)
}
}
#[cfg(test)]
mod test {
use super::Disclosure;
// Test values from:
// https://www.ietf.org/archive/id/draft-ietf-oauth-selective-disclosure-jwt-07.html#appendix-A.2-7
#[test]
fn test_parsing() {
let disclosure = Disclosure::new(
"2GLC42sKQveCfGfryNRN9w".to_string(),
Some("time".to_owned()),
"2012-04-23T18:25Z".to_owned().into(),
);
let parsed =
Disclosure::parse("WyIyR0xDNDJzS1F2ZUNmR2ZyeU5STjl3IiwgInRpbWUiLCAiMjAxMi0wNC0yM1QxODoyNVoiXQ".to_owned());
assert_eq!(parsed.unwrap(), disclosure);
}
// Test values from:
// https://www.ietf.org/archive/id/draft-ietf-oauth-selective-disclosure-jwt-05.html#section-5.5-25
#[test]
fn test_creating() {
let disclosure = Disclosure::new("lklxF5jMYlGTPUovMNIvCA".to_owned(), None, "US".to_owned().into());
assert_eq!(
"WyJsa2x4RjVqTVlsR1RQVW92TU5JdkNBIiwgIlVTIl0".to_owned(),
disclosure.to_string()
);
}
}