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
use crate::ScimEntryHeader;
use base64urlsafedata::Base64UrlSafeData;
use std::fmt;
use url::Url;
use uuid::Uuid;

use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Name {
    // The full name including all middle names and titles
    formatted: Option<String>,
    family_name: Option<String>,
    given_name: Option<String>,
    middle_name: Option<String>,
    honorific_prefix: Option<String>,
    honorific_suffix: Option<String>,
}

/*
// https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.5
//
// https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
// Same as locale?
#[derive(Serialize, Deserialize, Debug, Clone)]
enum Language {
    en,
}
*/

// https://datatracker.ietf.org/doc/html/rfc5646
#[allow(non_camel_case_types)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Locale {
    en,
    #[serde(rename = "en-AU")]
    en_AU,
    #[serde(rename = "en-US")]
    en_US,
    de,
    #[serde(rename = "de-DE")]
    de_DE,
}

impl fmt::Display for Locale {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Locale::en => write!(f, "en"),
            Locale::en_AU => write!(f, "en-AU"),
            Locale::en_US => write!(f, "en-US"),
            Locale::de => write!(f, "de"),
            Locale::de_DE => write!(f, "de-DE"),
        }
    }
}

#[allow(non_camel_case_types)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Timezone {
    #[serde(rename = "Australia/Brisbane")]
    australia_brisbane,
    #[serde(rename = "America/Los_Angeles")]
    america_los_angeles,
}

impl fmt::Display for Timezone {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Timezone::australia_brisbane => write!(f, "Australia/Brisbane"),
            Timezone::america_los_angeles => write!(f, "America/Los_Angeles"),
        }
    }
}

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct MultiValueAttr {
    #[serde(rename = "type")]
    pub type_: Option<String>,
    pub primary: Option<bool>,
    pub display: Option<String>,
    #[serde(rename = "$ref")]
    pub ref_: Option<Url>,
    pub value: String,
}

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Photo {
    #[serde(rename = "type")]
    type_: Option<String>,
    primary: Option<bool>,
    display: Option<String>,
    #[serde(rename = "$ref")]
    ref_: Option<Url>,
    value: Url,
}

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Binary {
    #[serde(rename = "type")]
    type_: Option<String>,
    primary: Option<bool>,
    display: Option<String>,
    #[serde(rename = "$ref")]
    ref_: Option<Url>,
    value: Base64UrlSafeData,
}

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Address {
    #[serde(rename = "type")]
    type_: Option<String>,
    primary: Option<bool>,
    formatted: Option<String>,
    street_address: Option<String>,
    locality: Option<String>,
    region: Option<String>,
    postal_code: Option<String>,
    country: Option<String>,
}

/*
#[derive(Serialize, Deserialize, Debug, Clone)]
enum Membership {
    Direct,
    Indirect,
}
*/

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Group {
    #[serde(rename = "type")]
    type_: Option<String>,
    #[serde(rename = "$ref")]
    ref_: Url,
    value: Uuid,
    display: String,
}

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct User {
    #[serde(flatten)]
    entry: ScimEntryHeader,
    // required, must be unique, string.
    user_name: String,
    // Components of the users name.
    name: Option<Name>,
    // required, must be unique, string.
    display_name: Option<String>,
    nick_name: Option<String>,
    profile_url: Option<Url>,
    title: Option<String>,
    user_type: Option<String>,
    preferred_language: Option<Locale>,
    locale: Option<Locale>,
    // https://datatracker.ietf.org/doc/html/rfc6557
    // How can we validate this? https://docs.rs/iana-time-zone/0.1.51/iana_time_zone/fn.get_timezone.html
    timezone: Option<Timezone>,
    active: bool,
    password: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    emails: Vec<MultiValueAttr>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    phone_numbers: Vec<MultiValueAttr>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    ims: Vec<MultiValueAttr>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    photos: Vec<Photo>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    addresses: Vec<Address>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    groups: Vec<Group>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    entitlements: Vec<MultiValueAttr>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    roles: Vec<MultiValueAttr>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    x509certificates: Vec<Binary>,
}

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

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

        let u: User = 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);
    }
}