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
#![deny(warnings)]
#![warn(unused_extern_crates)]
#![deny(clippy::todo)]
#![deny(clippy::unimplemented)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::expect_used)]
#![deny(clippy::panic)]
#![deny(clippy::unreachable)]
#![deny(clippy::await_holding_lock)]
#![deny(clippy::needless_pass_by_value)]
#![deny(clippy::trivially_copy_pass_by_ref)]

use base64urlsafedata::Base64UrlSafeData;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use url::Url;
use utoipa::ToSchema;
use uuid::Uuid;

pub mod constants;
pub mod filter;
pub mod group;
pub mod user;

pub mod prelude {
    pub use crate::constants::*;
    pub use crate::user::MultiValueAttr;
    pub use crate::{ScimAttr, ScimComplexAttr, ScimEntry, ScimEntryHeader, ScimMeta, ScimValue};
}

#[derive(Deserialize, Serialize, Debug, Clone, ToSchema)]
#[serde(untagged)]
pub enum ScimAttr {
    Bool(bool),
    Integer(i64),
    Decimal(f64),
    String(String),
    // These can't be implicitly decoded because we may not know the intent, but we can *encode* them.
    // That's why "String" is above this because it catches anything during deserialization before
    // this point.
    #[serde(with = "time::serde::rfc3339")]
    DateTime(OffsetDateTime),

    Binary(Base64UrlSafeData),
    Reference(Url),
}

impl ScimAttr {
    pub fn parse_as_datetime(&self) -> Option<Self> {
        let s = match self {
            ScimAttr::String(s) => s,
            _ => return None,
        };

        OffsetDateTime::parse(s, &Rfc3339)
            .map(ScimAttr::DateTime)
            .ok()
    }
}

impl From<String> for ScimAttr {
    fn from(s: String) -> Self {
        ScimAttr::String(s)
    }
}

impl From<bool> for ScimAttr {
    fn from(b: bool) -> Self {
        ScimAttr::Bool(b)
    }
}

impl From<u32> for ScimAttr {
    fn from(i: u32) -> Self {
        ScimAttr::Integer(i as i64)
    }
}

impl From<Vec<u8>> for ScimAttr {
    fn from(data: Vec<u8>) -> Self {
        ScimAttr::Binary(data.into())
    }
}

impl From<OffsetDateTime> for ScimAttr {
    fn from(odt: OffsetDateTime) -> Self {
        ScimAttr::DateTime(odt)
    }
}

impl From<ScimAttr> for ScimValue {
    fn from(sa: ScimAttr) -> Self {
        ScimValue::Simple(sa)
    }
}

impl Eq for ScimAttr {}

impl PartialEq for ScimAttr {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (ScimAttr::String(l), ScimAttr::String(r)) => l == r,
            (ScimAttr::Bool(l), ScimAttr::Bool(r)) => l == r,
            (ScimAttr::Decimal(l), ScimAttr::Decimal(r)) => l == r,
            (ScimAttr::Integer(l), ScimAttr::Integer(r)) => l == r,
            (ScimAttr::DateTime(l), ScimAttr::DateTime(r)) => l == r,
            (ScimAttr::Binary(l), ScimAttr::Binary(r)) => l == r,
            (ScimAttr::Reference(l), ScimAttr::Reference(r)) => l == r,
            _ => false,
        }
    }
}

pub type ScimComplexAttr = BTreeMap<String, ScimAttr>;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
#[serde(untagged)]
pub enum ScimValue {
    Simple(ScimAttr),
    Complex(ScimComplexAttr),
    MultiSimple(Vec<ScimAttr>),
    MultiComplex(Vec<ScimComplexAttr>),
}

impl ScimValue {
    pub fn len(&self) -> usize {
        match self {
            ScimValue::Simple(_) | ScimValue::Complex(_) => 1,
            ScimValue::MultiSimple(a) => a.len(),
            ScimValue::MultiComplex(a) => a.len(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ScimMeta {
    pub resource_type: String,
    #[serde(with = "time::serde::rfc3339")]
    pub created: OffsetDateTime,
    #[serde(with = "time::serde::rfc3339")]
    pub last_modified: OffsetDateTime,
    pub location: Url,
    pub version: String,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ScimEntryHeader {
    pub schemas: Vec<String>,
    pub id: Uuid,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub external_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<ScimMeta>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ScimEntry {
    pub schemas: Vec<String>,
    pub id: Uuid,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub external_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<ScimMeta>,
    #[serde(flatten)]
    pub attrs: BTreeMap<String, ScimValue>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::constants::RFC7643_USER;

    #[test]
    fn parse_scim_entry() {
        let _ = tracing_subscriber::fmt::try_init();

        let u: ScimEntry =
            serde_json::from_str(RFC7643_USER).expect("Failed to parse RFC7643_USER");

        tracing::trace!(?u);

        let s = serde_json::to_string_pretty(&u).expect("Failed to serialise RFC7643_USER");
        eprintln!("{}", s);
    }

    // =========================================================
    // asymmetric serde tests

    use serde::de::{self, Deserialize, Deserializer, Visitor};
    use std::fmt;
    use uuid::Uuid;

    // -> For values, we need to be able to capture and handle "what if it's X" type? But
    // we can't know the "intent" until we hit schema, so we have to preserve the string
    // types as well. In this type, we make this *asymmetric*. When we parse we use
    // this type which has the "maybes" but when we serialise, we use concrete types
    // instead.

    #[derive(Debug)]
    #[allow(dead_code)]
    enum TestB {
        Integer(i64),
        Decimal(f64),
        MaybeUuid(Uuid, String),
        String(String),
    }

    struct TestBVisitor;

    impl<'de> Visitor<'de> for TestBVisitor {
        type Value = TestB;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("cheese")
        }

        fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            Ok(TestB::Decimal(v))
        }

        fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            Ok(TestB::Integer(v as i64))
        }

        fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            Ok(TestB::Integer(v))
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            Ok(if let Ok(u) = Uuid::parse_str(v) {
                TestB::MaybeUuid(u, v.to_string())
            } else {
                TestB::String(v.to_string())
            })
        }
    }

    impl<'de> Deserialize<'de> for TestB {
        fn deserialize<D>(deserializer: D) -> Result<TestB, D::Error>
        where
            D: Deserializer<'de>,
        {
            deserializer.deserialize_any(TestBVisitor)
        }
    }

    #[test]
    fn parse_enum_b() {
        let x: TestB = serde_json::from_str("10").unwrap();
        eprintln!("{:?}", x);

        let x: TestB = serde_json::from_str("10.5").unwrap();
        eprintln!("{:?}", x);

        let x: TestB = serde_json::from_str(r#""550e8400-e29b-41d4-a716-446655440000""#).unwrap();
        eprintln!("{:?}", x);

        let x: TestB = serde_json::from_str(r#""Value""#).unwrap();
        eprintln!("{:?}", x);
    }

    // In reverse when we serialise, we can simply use untagged on an enum.
    // Potentially this lets us have more "scim" types for dedicated serialisations
    // over the generic ones.

    #[derive(Serialize, Debug, Deserialize, Clone)]
    #[serde(rename_all = "lowercase", from = "&str", into = "String")]
    enum TestC {
        A,
        B,
        Unknown(String),
    }

    impl From<TestC> for String {
        fn from(v: TestC) -> String {
            match v {
                TestC::A => "A".to_string(),
                TestC::B => "B".to_string(),
                TestC::Unknown(v) => v,
            }
        }
    }

    impl From<&str> for TestC {
        fn from(v: &str) -> TestC {
            match v {
                "A" => TestC::A,
                "B" => TestC::B,
                _ => TestC::Unknown(v.to_string()),
            }
        }
    }

    #[test]
    fn parse_enum_c() {
        let x = serde_json::to_string(&TestC::A).unwrap();
        eprintln!("{:?}", x);

        let x = serde_json::to_string(&TestC::B).unwrap();
        eprintln!("{:?}", x);

        let x = serde_json::to_string(&TestC::Unknown("X".to_string())).unwrap();
        eprintln!("{:?}", x);
    }
}