ruma_client_api/uiaa/
auth_params.rs1use std::collections::BTreeMap;
4
5use serde::{Deserialize, Serialize};
6
7mod params_serde;
8
9#[derive(Clone, Debug, Default, Deserialize, Serialize)]
17#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
18pub struct LoginTermsParams {
19 pub policies: BTreeMap<String, PolicyDefinition>,
21}
22
23impl LoginTermsParams {
24 pub fn new(policies: BTreeMap<String, PolicyDefinition>) -> Self {
26 Self { policies }
27 }
28}
29
30#[derive(Clone, Debug, Serialize)]
32#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
33pub struct PolicyDefinition {
34 pub version: String,
36
37 #[serde(flatten)]
43 pub translations: BTreeMap<String, PolicyTranslation>,
44}
45
46impl PolicyDefinition {
47 pub fn new(version: String, translations: BTreeMap<String, PolicyTranslation>) -> Self {
49 Self { version, translations }
50 }
51}
52
53#[derive(Clone, Debug, Deserialize, Serialize)]
55#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
56pub struct PolicyTranslation {
57 pub name: String,
59
60 pub url: String,
64}
65
66impl PolicyTranslation {
67 pub fn new(name: String, url: String) -> Self {
69 Self { name, url }
70 }
71}
72
73#[derive(Clone, Debug, Deserialize, Serialize)]
77#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
78pub struct OAuthParams {
79 pub url: String,
84}
85
86impl OAuthParams {
87 pub fn new(url: String) -> Self {
89 Self { url }
90 }
91}
92
93#[cfg(test)]
94mod tests {
95 use ruma_common::canonical_json::assert_to_canonical_json_eq;
96 use serde_json::{from_value as from_json_value, json};
97
98 use super::{LoginTermsParams, PolicyDefinition, PolicyTranslation};
99
100 #[test]
101 fn serialize_login_terms_params() {
102 let privacy_definition = PolicyDefinition::new(
103 "1".to_owned(),
104 [
105 (
106 "en-US".to_owned(),
107 PolicyTranslation::new(
108 "Privacy Policy".to_owned(),
109 "http://matrix.local/en-US/privacy".to_owned(),
110 ),
111 ),
112 (
113 "fr-FR".to_owned(),
114 PolicyTranslation::new(
115 "Politique de confidentialité".to_owned(),
116 "http://matrix.local/fr-FR/privacy".to_owned(),
117 ),
118 ),
119 ]
120 .into(),
121 );
122 let params = LoginTermsParams::new([("privacy".to_owned(), privacy_definition)].into());
123
124 assert_to_canonical_json_eq!(
125 params,
126 json!({
127 "policies": {
128 "privacy": {
129 "en-US": {
130 "name": "Privacy Policy",
131 "url": "http://matrix.local/en-US/privacy",
132 },
133 "fr-FR": {
134 "name": "Politique de confidentialité",
135 "url": "http://matrix.local/fr-FR/privacy",
136 },
137 "version": "1",
138 },
139 },
140 })
141 );
142 }
143
144 #[test]
145 fn deserialize_login_terms_params() {
146 let json = json!({
148 "policies": {
149 "privacy": {
150 "en-US": {
151 "name": "Privacy Policy",
152 "url": "http://matrix.local/en-US/privacy",
153 },
154 "fr-FR": {
155 "name": "Politique de confidentialité",
156 "url": "http://matrix.local/fr-FR/privacy",
157 },
158 },
159 },
160 });
161
162 from_json_value::<LoginTermsParams>(json).unwrap_err();
163
164 let json = json!({
166 "policies": {
167 "privacy_policy": {
168 "en": {
169 "name": "Privacy Policy",
170 "url": "https://example.org/somewhere/privacy-1.2-en.html"
171 },
172 "fr": {
173 "name": "Politique de confidentialité",
174 "url": "https://example.org/somewhere/privacy-1.2-fr.html"
175 },
176 "foo": "bar",
178 "version": "1.2",
179 },
180 "terms_of_service": {
182 "version": "1.2",
183 }
184 }
185 });
186
187 let params = from_json_value::<LoginTermsParams>(json).unwrap();
188
189 assert_eq!(params.policies.len(), 2);
190
191 let policy = params.policies.get("privacy_policy").unwrap();
192 assert_eq!(policy.version, "1.2");
193 assert_eq!(policy.translations.len(), 2);
194 let translation = policy.translations.get("en").unwrap();
195 assert_eq!(translation.name, "Privacy Policy");
196 assert_eq!(translation.url, "https://example.org/somewhere/privacy-1.2-en.html");
197 let translation = policy.translations.get("fr").unwrap();
198 assert_eq!(translation.name, "Politique de confidentialité");
199 assert_eq!(translation.url, "https://example.org/somewhere/privacy-1.2-fr.html");
200
201 let policy = params.policies.get("terms_of_service").unwrap();
202 assert_eq!(policy.version, "1.2");
203 assert_eq!(policy.translations.len(), 0);
204 }
205}