Skip to main content

kanidm_proto/scim_v1/
synch.rs

1use serde::{Deserialize, Serialize};
2use serde_with::{base64, formats, serde_as};
3use utoipa::ToSchema;
4use uuid::Uuid;
5
6use scim_proto::{user::MultiValueAttr, ScimEntry, ScimEntryHeader};
7use serde_with::skip_serializing_none;
8
9#[serde_as]
10#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
11pub enum ScimSyncState {
12    Refresh,
13    Active {
14        #[serde_as(as = "base64::Base64<base64::UrlSafe, formats::Unpadded>")]
15        cookie: Vec<u8>,
16    },
17}
18
19#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
20pub enum ScimSyncRetentionMode {
21    /// No actions are to be taken - only update or create entries in the
22    /// entries set.
23    Ignore,
24    /// All entries that have their uuid present in this set are retained.
25    /// Anything not present will be deleted.
26    Retain(Vec<Uuid>),
27    /// Any entry with its UUID in this set will be deleted. Anything not
28    /// present will be retained.
29    Delete(Vec<Uuid>),
30}
31
32#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ToSchema)]
33pub struct ScimSyncRequest {
34    pub from_state: ScimSyncState,
35    pub to_state: ScimSyncState,
36
37    // These entries are created with serde_json::to_value(ScimSyncGroup) for
38    // example. This is how we can mix/match the different types.
39    pub entries: Vec<ScimEntry>,
40
41    pub retain: ScimSyncRetentionMode,
42}
43
44impl ScimSyncRequest {
45    pub fn need_refresh(from_state: ScimSyncState) -> Self {
46        ScimSyncRequest {
47            from_state,
48            to_state: ScimSyncState::Refresh,
49            entries: Vec::default(),
50            retain: ScimSyncRetentionMode::Ignore,
51        }
52    }
53}
54
55pub const SCIM_SCHEMA_SYNC_1: &str = "urn:ietf:params:scim:schemas:kanidm:sync:1:";
56pub const SCIM_SCHEMA_SYNC_ACCOUNT: &str = "urn:ietf:params:scim:schemas:kanidm:sync:1:account";
57pub const SCIM_SCHEMA_SYNC_GROUP: &str = "urn:ietf:params:scim:schemas:kanidm:sync:1:group";
58pub const SCIM_SCHEMA_SYNC_PERSON: &str = "urn:ietf:params:scim:schemas:kanidm:sync:1:person";
59pub const SCIM_SCHEMA_SYNC_OAUTH2_ACCOUNT: &str =
60    "urn:ietf:params:scim:schemas:kanidm:sync:1:oauth2_account";
61pub const SCIM_SCHEMA_SYNC_POSIXACCOUNT: &str =
62    "urn:ietf:params:scim:schemas:kanidm:sync:1:posixaccount";
63pub const SCIM_SCHEMA_SYNC_POSIXGROUP: &str =
64    "urn:ietf:params:scim:schemas:kanidm:sync:1:posixgroup";
65
66pub const SCIM_ALGO: &str = "algo";
67pub const SCIM_DIGITS: &str = "digits";
68pub const SCIM_SECRET: &str = "secret";
69pub const SCIM_STEP: &str = "step";
70
71#[derive(Serialize, Deserialize, Debug, Clone)]
72pub struct ScimTotp {
73    /// maps to "label" in kanidm.
74    pub external_id: String,
75    pub secret: String,
76    pub algo: String,
77    pub step: u32,
78    pub digits: u32,
79}
80
81#[derive(Serialize, Deserialize, Debug, Clone)]
82pub struct ScimSshPubKey {
83    pub label: String,
84    pub value: String,
85}
86
87#[skip_serializing_none]
88#[derive(Serialize, Deserialize, Debug, Clone)]
89#[serde(rename_all = "snake_case")]
90pub struct ScimSyncPerson {
91    #[serde(flatten)]
92    pub entry: ScimEntryHeader,
93
94    pub name: String,
95    pub displayname: String,
96    pub gidnumber: Option<u32>,
97    pub password_import: Option<String>,
98    pub unix_password_import: Option<String>,
99    #[serde(default, skip_serializing_if = "Vec::is_empty")]
100    pub totp_import: Vec<ScimTotp>,
101    pub loginshell: Option<String>,
102    #[serde(default, skip_serializing_if = "Vec::is_empty")]
103    pub mail: Vec<MultiValueAttr>,
104    #[serde(default, skip_serializing_if = "Vec::is_empty")]
105    pub ssh_publickey: Vec<ScimSshPubKey>,
106    pub account_valid_from: Option<String>,
107    pub account_expire: Option<String>,
108    pub oauth2_account_provider: Option<Uuid>,
109    pub oauth2_account_unique_user_id: Option<String>,
110}
111
112impl TryInto<ScimEntry> for ScimSyncPerson {
113    type Error = serde_json::Error;
114
115    fn try_into(self) -> Result<ScimEntry, Self::Error> {
116        serde_json::to_value(self).and_then(serde_json::from_value)
117    }
118}
119
120pub struct ScimSyncPersonBuilder {
121    inner: ScimSyncPerson,
122}
123
124impl ScimSyncPerson {
125    pub fn builder(
126        id: Uuid,
127        external_id: String,
128        name: String,
129        displayname: String,
130    ) -> ScimSyncPersonBuilder {
131        ScimSyncPersonBuilder {
132            inner: ScimSyncPerson {
133                entry: ScimEntryHeader {
134                    schemas: vec![
135                        SCIM_SCHEMA_SYNC_ACCOUNT.to_string(),
136                        SCIM_SCHEMA_SYNC_PERSON.to_string(),
137                    ],
138                    id,
139                    external_id: Some(external_id),
140                    meta: None,
141                },
142                name,
143                displayname,
144                gidnumber: None,
145                password_import: None,
146                unix_password_import: None,
147                totp_import: Vec::with_capacity(0),
148                loginshell: None,
149                mail: Vec::with_capacity(0),
150                ssh_publickey: Vec::with_capacity(0),
151                account_valid_from: None,
152                account_expire: None,
153                oauth2_account_provider: None,
154                oauth2_account_unique_user_id: None,
155            },
156        }
157    }
158}
159
160impl ScimSyncPersonBuilder {
161    pub fn set_password_import(mut self, password_import: Option<String>) -> Self {
162        self.inner.password_import = password_import;
163        self
164    }
165
166    pub fn set_unix_password_import(mut self, unix_password_import: Option<String>) -> Self {
167        self.inner.unix_password_import = unix_password_import;
168        self
169    }
170
171    pub fn set_totp_import(mut self, totp_import: Vec<ScimTotp>) -> Self {
172        self.inner.totp_import = totp_import;
173        self
174    }
175
176    pub fn set_mail(mut self, mail: Vec<MultiValueAttr>) -> Self {
177        self.inner.mail = mail;
178        self
179    }
180
181    pub fn set_ssh_publickey(mut self, ssh_publickey: Vec<ScimSshPubKey>) -> Self {
182        self.inner.ssh_publickey = ssh_publickey;
183        self
184    }
185
186    pub fn set_login_shell(mut self, loginshell: Option<String>) -> Self {
187        self.inner.loginshell = loginshell;
188        self
189    }
190
191    pub fn set_account_valid_from(mut self, account_valid_from: Option<String>) -> Self {
192        self.inner.account_valid_from = account_valid_from;
193        self
194    }
195
196    pub fn set_account_expire(mut self, account_expire: Option<String>) -> Self {
197        self.inner.account_expire = account_expire;
198        self
199    }
200
201    pub fn set_gidnumber(mut self, gidnumber: Option<u32>) -> Self {
202        self.inner.gidnumber = gidnumber;
203        if self.inner.gidnumber.is_some() {
204            self.inner.entry.schemas = vec![
205                SCIM_SCHEMA_SYNC_ACCOUNT.to_string(),
206                SCIM_SCHEMA_SYNC_PERSON.to_string(),
207                SCIM_SCHEMA_SYNC_POSIXACCOUNT.to_string(),
208            ];
209        } else {
210            self.inner.entry.schemas = vec![
211                SCIM_SCHEMA_SYNC_ACCOUNT.to_string(),
212                SCIM_SCHEMA_SYNC_PERSON.to_string(),
213            ];
214        }
215        self
216    }
217
218    pub fn set_oauth2_account_provider(mut self, maybe_provider: Option<(Uuid, String)>) -> Self {
219        if let Some((provider, unique_user_id)) = maybe_provider {
220            self.inner
221                .entry
222                .schemas
223                .push(SCIM_SCHEMA_SYNC_OAUTH2_ACCOUNT.to_string());
224            self.inner.oauth2_account_provider = Some(provider);
225            self.inner.oauth2_account_unique_user_id = Some(unique_user_id);
226        } else {
227            self.inner
228                .entry
229                .schemas
230                .retain(|x| x != SCIM_SCHEMA_SYNC_OAUTH2_ACCOUNT);
231            self.inner.oauth2_account_provider = None;
232            self.inner.oauth2_account_unique_user_id = None;
233        }
234        self
235    }
236
237    pub fn build(self) -> ScimSyncPerson {
238        self.inner
239    }
240}
241
242#[derive(Serialize, Deserialize, Debug, Clone)]
243pub struct ScimExternalMember {
244    pub external_id: String,
245}
246
247#[skip_serializing_none]
248#[derive(Serialize, Deserialize, Debug, Clone)]
249#[serde(rename_all = "snake_case")]
250pub struct ScimSyncGroup {
251    #[serde(flatten)]
252    pub entry: ScimEntryHeader,
253
254    pub name: String,
255    pub description: Option<String>,
256    pub gidnumber: Option<u32>,
257    #[serde(default, skip_serializing_if = "Vec::is_empty")]
258    pub member: Vec<ScimExternalMember>,
259}
260
261impl TryInto<ScimEntry> for ScimSyncGroup {
262    type Error = serde_json::Error;
263
264    fn try_into(self) -> Result<ScimEntry, Self::Error> {
265        serde_json::to_value(self).and_then(serde_json::from_value)
266    }
267}
268
269#[derive(Serialize, Deserialize, Debug, Clone)]
270#[serde(rename_all = "camelCase")]
271pub struct ScimSyncGroupBuilder {
272    inner: ScimSyncGroup,
273}
274
275impl ScimSyncGroup {
276    pub fn builder(id: Uuid, external_id: String, name: String) -> ScimSyncGroupBuilder {
277        ScimSyncGroupBuilder {
278            inner: ScimSyncGroup {
279                entry: ScimEntryHeader {
280                    schemas: vec![SCIM_SCHEMA_SYNC_GROUP.to_string()],
281                    id,
282                    external_id: Some(external_id),
283                    meta: None,
284                },
285                name,
286                description: None,
287                gidnumber: None,
288                member: Vec::with_capacity(0),
289            },
290        }
291    }
292}
293
294impl ScimSyncGroupBuilder {
295    pub fn set_description(mut self, desc: Option<String>) -> Self {
296        self.inner.description = desc;
297        self
298    }
299
300    pub fn set_gidnumber(mut self, gidnumber: Option<u32>) -> Self {
301        self.inner.gidnumber = gidnumber;
302        if self.inner.gidnumber.is_some() {
303            self.inner.entry.schemas = vec![
304                SCIM_SCHEMA_SYNC_GROUP.to_string(),
305                SCIM_SCHEMA_SYNC_POSIXGROUP.to_string(),
306            ];
307        } else {
308            self.inner.entry.schemas = vec![SCIM_SCHEMA_SYNC_GROUP.to_string()];
309        }
310        self
311    }
312
313    pub fn set_members<I>(mut self, member_iter: I) -> Self
314    where
315        I: Iterator<Item = String>,
316    {
317        self.inner.member = member_iter
318            .map(|external_id| ScimExternalMember { external_id })
319            .collect();
320        self
321    }
322
323    pub fn build(self) -> ScimSyncGroup {
324        self.inner
325    }
326}