1#![deny(warnings)]
2#![warn(unused_extern_crates)]
3#![deny(clippy::todo)]
4#![deny(clippy::unimplemented)]
5#![deny(clippy::unwrap_used)]
6#![deny(clippy::expect_used)]
7#![deny(clippy::panic)]
8#![deny(clippy::unreachable)]
9#![deny(clippy::await_holding_lock)]
10#![deny(clippy::needless_pass_by_value)]
11#![deny(clippy::trivially_copy_pass_by_ref)]
12
13use base64urlsafedata::Base64UrlSafeData;
14use serde::{Deserialize, Serialize};
15use std::collections::BTreeMap;
16use time::{format_description::well_known::Rfc3339, OffsetDateTime};
17use url::Url;
18use utoipa::ToSchema;
19use uuid::Uuid;
20
21pub mod constants;
22pub mod filter;
23pub mod group;
24pub mod user;
25
26pub mod prelude {
27 pub use crate::{
28 constants::*, user::MultiValueAttr, ScimAttr, ScimComplexAttr, ScimEntry, ScimEntryHeader,
29 ScimMeta, ScimValue,
30 };
31}
32
33#[derive(Deserialize, Serialize, Debug, Clone, ToSchema)]
34#[serde(untagged)]
35pub enum ScimAttr {
36 Bool(bool),
37 Integer(i64),
38 Decimal(f64),
39 String(String),
40 #[serde(with = "time::serde::rfc3339")]
44 DateTime(OffsetDateTime),
45 #[schema(value_type = String)]
46 Binary(Base64UrlSafeData),
47 Reference(Url),
48}
49
50impl ScimAttr {
51 pub fn parse_as_datetime(&self) -> Option<Self> {
52 let s = match self {
53 ScimAttr::String(s) => s,
54 _ => return None,
55 };
56
57 OffsetDateTime::parse(s, &Rfc3339)
58 .map(ScimAttr::DateTime)
59 .ok()
60 }
61}
62
63impl From<String> for ScimAttr {
64 fn from(s: String) -> Self {
65 ScimAttr::String(s)
66 }
67}
68
69impl From<bool> for ScimAttr {
70 fn from(b: bool) -> Self {
71 ScimAttr::Bool(b)
72 }
73}
74
75impl From<u32> for ScimAttr {
76 fn from(i: u32) -> Self {
77 ScimAttr::Integer(i as i64)
78 }
79}
80
81impl From<Vec<u8>> for ScimAttr {
82 fn from(data: Vec<u8>) -> Self {
83 ScimAttr::Binary(data.into())
84 }
85}
86
87impl From<OffsetDateTime> for ScimAttr {
88 fn from(odt: OffsetDateTime) -> Self {
89 ScimAttr::DateTime(odt)
90 }
91}
92
93impl From<ScimAttr> for ScimValue {
94 fn from(sa: ScimAttr) -> Self {
95 ScimValue::Simple(sa)
96 }
97}
98
99impl Eq for ScimAttr {}
100
101impl PartialEq for ScimAttr {
102 fn eq(&self, other: &Self) -> bool {
103 match (self, other) {
104 (ScimAttr::String(l), ScimAttr::String(r)) => l == r,
105 (ScimAttr::Bool(l), ScimAttr::Bool(r)) => l == r,
106 (ScimAttr::Decimal(l), ScimAttr::Decimal(r)) => l == r,
107 (ScimAttr::Integer(l), ScimAttr::Integer(r)) => l == r,
108 (ScimAttr::DateTime(l), ScimAttr::DateTime(r)) => l == r,
109 (ScimAttr::Binary(l), ScimAttr::Binary(r)) => l == r,
110 (ScimAttr::Reference(l), ScimAttr::Reference(r)) => l == r,
111 _ => false,
112 }
113 }
114}
115
116pub type ScimComplexAttr = BTreeMap<String, ScimAttr>;
117
118#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
119#[serde(untagged)]
120pub enum ScimValue {
121 Simple(ScimAttr),
122 Complex(ScimComplexAttr),
123 MultiSimple(Vec<ScimAttr>),
124 MultiComplex(Vec<ScimComplexAttr>),
125}
126
127impl ScimValue {
128 pub fn len(&self) -> usize {
129 match self {
130 ScimValue::Simple(_) | ScimValue::Complex(_) => 1,
131 ScimValue::MultiSimple(a) => a.len(),
132 ScimValue::MultiComplex(a) => a.len(),
133 }
134 }
135
136 pub fn is_empty(&self) -> bool {
137 self.len() == 0
138 }
139}
140
141#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
142#[serde(rename_all = "camelCase", deny_unknown_fields)]
143pub struct ScimMeta {
144 pub resource_type: String,
145 #[serde(with = "time::serde::rfc3339")]
146 pub created: OffsetDateTime,
147 #[serde(with = "time::serde::rfc3339")]
148 pub last_modified: OffsetDateTime,
149 pub location: Url,
150 pub version: String,
151}
152
153#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
154#[serde(rename_all = "camelCase")]
155pub struct ScimEntryHeader {
156 pub schemas: Vec<String>,
157 pub id: Uuid,
158 #[serde(skip_serializing_if = "Option::is_none")]
159 pub external_id: Option<String>,
160 #[serde(skip_serializing_if = "Option::is_none")]
161 pub meta: Option<ScimMeta>,
162}
163
164#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
165#[serde(rename_all = "camelCase")]
166pub struct ScimEntry {
167 pub schemas: Vec<String>,
168 pub id: Uuid,
169 #[serde(skip_serializing_if = "Option::is_none")]
170 pub external_id: Option<String>,
171 #[serde(skip_serializing_if = "Option::is_none")]
172 pub meta: Option<ScimMeta>,
173 #[serde(flatten)]
174 pub attrs: BTreeMap<String, ScimValue>,
175}
176
177#[cfg(test)]
178mod tests {
179 use super::*;
180 use crate::constants::RFC7643_USER;
181
182 #[test]
183 fn parse_scim_entry() {
184 let _ = tracing_subscriber::fmt::try_init();
185
186 let u: ScimEntry =
187 serde_json::from_str(RFC7643_USER).expect("Failed to parse RFC7643_USER");
188
189 tracing::trace!(?u);
190
191 let s = serde_json::to_string_pretty(&u).expect("Failed to serialise RFC7643_USER");
192 eprintln!("{s}");
193 }
194
195 use serde::de::{self, Deserialize, Deserializer, Visitor};
199 use std::fmt;
200 use uuid::Uuid;
201
202 #[derive(Debug)]
209 #[allow(dead_code)]
210 enum TestB {
211 Integer(i64),
212 Decimal(f64),
213 MaybeUuid(Uuid, String),
214 String(String),
215 }
216
217 struct TestBVisitor;
218
219 impl Visitor<'_> for TestBVisitor {
220 type Value = TestB;
221
222 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
223 formatter.write_str("cheese")
224 }
225
226 fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
227 where
228 E: de::Error,
229 {
230 Ok(TestB::Decimal(v))
231 }
232
233 fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
234 where
235 E: de::Error,
236 {
237 Ok(TestB::Integer(v as i64))
238 }
239
240 fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
241 where
242 E: de::Error,
243 {
244 Ok(TestB::Integer(v))
245 }
246
247 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
248 where
249 E: de::Error,
250 {
251 Ok(if let Ok(u) = Uuid::parse_str(v) {
252 TestB::MaybeUuid(u, v.to_string())
253 } else {
254 TestB::String(v.to_string())
255 })
256 }
257 }
258
259 impl<'de> Deserialize<'de> for TestB {
260 fn deserialize<D>(deserializer: D) -> Result<TestB, D::Error>
261 where
262 D: Deserializer<'de>,
263 {
264 deserializer.deserialize_any(TestBVisitor)
265 }
266 }
267
268 #[test]
269 fn parse_enum_b() {
270 let x: TestB = serde_json::from_str("10").unwrap();
271 eprintln!("{x:?}");
272
273 let x: TestB = serde_json::from_str("10.5").unwrap();
274 eprintln!("{x:?}");
275
276 let x: TestB = serde_json::from_str(r#""550e8400-e29b-41d4-a716-446655440000""#).unwrap();
277 eprintln!("{x:?}");
278
279 let x: TestB = serde_json::from_str(r#""Value""#).unwrap();
280 eprintln!("{x:?}");
281 }
282
283 #[derive(Serialize, Debug, Deserialize, Clone)]
288 #[serde(rename_all = "lowercase", from = "&str", into = "String")]
289 enum TestC {
290 A,
291 B,
292 Unknown(String),
293 }
294
295 impl From<TestC> for String {
296 fn from(v: TestC) -> String {
297 match v {
298 TestC::A => "A".to_string(),
299 TestC::B => "B".to_string(),
300 TestC::Unknown(v) => v,
301 }
302 }
303 }
304
305 impl From<&str> for TestC {
306 fn from(v: &str) -> TestC {
307 match v {
308 "A" => TestC::A,
309 "B" => TestC::B,
310 _ => TestC::Unknown(v.to_string()),
311 }
312 }
313 }
314
315 #[test]
316 fn parse_enum_c() {
317 let x = serde_json::to_string(&TestC::A).unwrap();
318 eprintln!("{x:?}");
319
320 let x = serde_json::to_string(&TestC::B).unwrap();
321 eprintln!("{x:?}");
322
323 let x = serde_json::to_string(&TestC::Unknown("X".to_string())).unwrap();
324 eprintln!("{x:?}");
325 }
326}