Skip to main content

kanidm_proto/scim_v1/
client.rs

1//! These are types that a client will send to the server.
2use super::{ScimEntryGeneric, ScimEntryGetQuery, ScimMail, ScimOauth2ClaimMapJoinChar};
3use crate::{attribute::Attribute, v1::OutboundMessage};
4use scim_proto::ScimEntryHeader;
5use serde::{Deserialize, Serialize};
6use serde_json::Value as JsonValue;
7use serde_with::{
8    base64, formats, formats::PreferMany, serde_as, skip_serializing_none, OneOrMany,
9};
10use sshkey_attest::proto::PublicKey as SshPublicKey;
11use std::{
12    collections::{BTreeMap, BTreeSet},
13    num::NonZeroU64,
14};
15use time::{format_description::well_known::Rfc3339, OffsetDateTime};
16use url::Url;
17use utoipa::ToSchema;
18use uuid::Uuid;
19
20pub type ScimSshPublicKeys = Vec<ScimSshPublicKey>;
21
22#[derive(Deserialize, Serialize, Debug, Clone)]
23#[serde(deny_unknown_fields, rename_all = "camelCase")]
24pub struct ScimSshPublicKey {
25    pub label: String,
26    pub value: SshPublicKey,
27}
28
29#[derive(Deserialize)]
30#[serde(untagged)]
31enum ScimReferenceAdapter {
32    Complete { uuid: Uuid, value: String },
33    Uuid { uuid: Uuid },
34    UuidX(Uuid),
35    Value { value: String },
36    ValueX(String),
37}
38
39impl From<ScimReferenceAdapter> for ScimReference {
40    fn from(scr: ScimReferenceAdapter) -> Self {
41        match scr {
42            ScimReferenceAdapter::Complete { uuid, value } => ScimReference {
43                uuid: Some(uuid),
44                value: Some(value),
45            },
46            ScimReferenceAdapter::Uuid { uuid } | ScimReferenceAdapter::UuidX(uuid) => {
47                ScimReference {
48                    uuid: Some(uuid),
49                    value: None,
50                }
51            }
52            ScimReferenceAdapter::Value { value } | ScimReferenceAdapter::ValueX(value) => {
53                ScimReference {
54                    uuid: None,
55                    value: Some(value),
56                }
57            }
58        }
59    }
60}
61
62#[serde_as]
63#[skip_serializing_none]
64#[derive(Deserialize, Serialize, Debug, Clone)]
65#[serde(
66    deny_unknown_fields,
67    rename_all = "camelCase",
68    from = "ScimReferenceAdapter"
69)]
70pub struct ScimReference {
71    pub uuid: Option<Uuid>,
72    pub value: Option<String>,
73}
74
75impl<T> From<T> for ScimReference
76where
77    T: AsRef<str>,
78{
79    fn from(value: T) -> Self {
80        ScimReference {
81            uuid: None,
82            value: Some(value.as_ref().to_string()),
83        }
84    }
85}
86
87pub type ScimReferences = Vec<ScimReference>;
88
89#[serde_as]
90#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
91#[serde(transparent)]
92pub struct ScimDateTime {
93    #[serde_as(as = "Rfc3339")]
94    pub date_time: OffsetDateTime,
95}
96
97#[serde_as]
98#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
99#[serde(rename_all = "camelCase")]
100pub struct ScimCertificate {
101    #[serde_as(as = "base64::Base64<base64::UrlSafe, formats::Unpadded>")]
102    pub der: Vec<u8>,
103}
104
105#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
106#[serde(rename_all = "camelCase")]
107pub struct ScimAddress {
108    pub street_address: String,
109    pub locality: String,
110    pub region: String,
111    pub postal_code: String,
112    pub country: String,
113}
114
115#[serde_as]
116#[derive(Deserialize, Serialize, Debug, Clone)]
117#[serde(rename_all = "camelCase")]
118pub struct ScimOAuth2ClaimMap {
119    pub group: Option<String>,
120    pub group_uuid: Option<Uuid>,
121    pub claim: String,
122    pub join_char: ScimOauth2ClaimMapJoinChar,
123    pub values: BTreeSet<String>,
124}
125
126#[serde_as]
127#[derive(Deserialize, Serialize, Debug, Clone)]
128#[serde(rename_all = "camelCase")]
129pub struct ScimOAuth2ScopeMap {
130    pub group: Option<String>,
131    pub group_uuid: Option<Uuid>,
132    pub scopes: BTreeSet<String>,
133}
134
135#[serde_as]
136#[derive(Serialize, Deserialize, Clone, Debug)]
137#[serde(rename_all = "camelCase")]
138pub struct ScimListEntry {
139    pub schemas: Vec<String>,
140    pub total_results: u64,
141    pub items_per_page: Option<NonZeroU64>,
142    pub start_index: Option<NonZeroU64>,
143    pub resources: Vec<ScimEntryGeneric>,
144}
145
146#[serde_as]
147#[derive(Serialize, Debug, Clone)]
148#[serde(rename_all = "snake_case")]
149pub struct ScimEntryApplicationPost {
150    pub name: String,
151    pub displayname: String,
152    pub linked_group: ScimReference,
153}
154
155#[serde_as]
156#[derive(Serialize, Deserialize, Debug, Clone)]
157#[serde(rename_all = "snake_case")]
158pub struct ScimEntryApplication {
159    #[serde(flatten)]
160    pub header: ScimEntryHeader,
161
162    pub name: String,
163    pub displayname: String,
164
165    pub linked_group: super::ScimReference,
166
167    #[serde(flatten)]
168    pub attrs: BTreeMap<Attribute, JsonValue>,
169}
170
171#[serde_as]
172#[derive(Serialize, Deserialize, Clone, Debug)]
173#[serde(rename_all = "camelCase")]
174pub struct ScimListApplication {
175    pub schemas: Vec<String>,
176    pub total_results: u64,
177    pub items_per_page: Option<NonZeroU64>,
178    pub start_index: Option<NonZeroU64>,
179    pub resources: Vec<ScimEntryApplication>,
180}
181
182#[serde_as]
183#[derive(Serialize, Deserialize, Debug, Clone)]
184#[serde(rename_all = "snake_case")]
185pub struct ScimEntryMessage {
186    #[serde(flatten)]
187    pub header: ScimEntryHeader,
188
189    pub message_template: OutboundMessage,
190    pub send_after: ScimDateTime,
191    pub delete_after: ScimDateTime,
192    pub sent_at: Option<ScimDateTime>,
193    pub mail_destination: Vec<ScimMail>,
194
195    #[serde(flatten)]
196    pub attrs: BTreeMap<Attribute, JsonValue>,
197}
198
199#[serde_as]
200#[derive(Serialize, Deserialize, Clone, Debug)]
201#[serde(rename_all = "camelCase")]
202pub struct ScimListFeature {
203    pub schemas: Vec<String>,
204    pub total_results: u64,
205    pub items_per_page: Option<NonZeroU64>,
206    pub start_index: Option<NonZeroU64>,
207    pub resources: Vec<ScimEntryFeature>,
208}
209
210#[serde_as]
211#[derive(Serialize, Deserialize, Debug, Clone)]
212#[serde(rename_all = "snake_case")]
213pub struct ScimEntryFeature {
214    #[serde(flatten)]
215    pub header: ScimEntryHeader,
216
217    pub name: String,
218    pub description: String,
219    #[serde(default)]
220    pub enabled: bool,
221
222    #[serde(flatten)]
223    pub attrs: BTreeMap<Attribute, JsonValue>,
224}
225
226#[serde_as]
227#[derive(Serialize, Deserialize, Clone, Debug)]
228#[serde(rename_all = "camelCase")]
229pub struct ScimListMessage {
230    pub schemas: Vec<String>,
231    pub total_results: u64,
232    pub items_per_page: Option<NonZeroU64>,
233    pub start_index: Option<NonZeroU64>,
234    pub resources: Vec<ScimEntryMessage>,
235}
236
237#[serde_as]
238#[derive(Serialize, Deserialize, Debug, Clone)]
239#[serde(rename_all = "snake_case")]
240pub struct ScimEntrySchemaClass {
241    #[serde(flatten)]
242    pub header: ScimEntryHeader,
243
244    // pub name: String,
245    // pub displayname: String,
246    // pub linked_group: Vec<super::ScimReference>,
247    #[serde(flatten)]
248    pub attrs: BTreeMap<Attribute, JsonValue>,
249}
250
251#[serde_as]
252#[derive(Deserialize, Clone, Debug)]
253#[serde(rename_all = "camelCase")]
254pub struct ScimListSchemaClass {
255    pub schemas: Vec<String>,
256    pub total_results: u64,
257    pub items_per_page: Option<NonZeroU64>,
258    pub start_index: Option<NonZeroU64>,
259    pub resources: Vec<ScimEntrySchemaClass>,
260}
261
262#[serde_as]
263#[derive(Deserialize, Debug, Clone, Serialize)]
264#[serde(rename_all = "snake_case")]
265pub struct ScimEntrySchemaAttribute {
266    #[serde(flatten)]
267    pub header: ScimEntryHeader,
268
269    pub attributename: String,
270    pub description: String,
271    // TODO: To be removed
272    pub multivalue: bool,
273    pub unique: bool,
274    pub syntax: String,
275    // pub linked_group: Vec<super::ScimReference>,
276    #[serde(flatten)]
277    pub attrs: BTreeMap<Attribute, JsonValue>,
278}
279
280#[serde_as]
281#[derive(Deserialize, Clone, Debug)]
282#[serde(rename_all = "camelCase")]
283pub struct ScimListSchemaAttribute {
284    pub schemas: Vec<String>,
285    pub total_results: u64,
286    pub items_per_page: Option<NonZeroU64>,
287    pub start_index: Option<NonZeroU64>,
288    pub resources: Vec<ScimEntrySchemaAttribute>,
289}
290
291#[derive(Serialize, Debug, Clone)]
292pub struct ScimEntryPutKanidm {
293    pub id: Uuid,
294    #[serde(flatten)]
295    pub attrs: BTreeMap<Attribute, Option<super::server::ScimValueKanidm>>,
296}
297
298#[serde_as]
299#[derive(Deserialize, Serialize, Debug, Clone)]
300pub struct ScimStrings(#[serde_as(as = "OneOrMany<_, PreferMany>")] pub Vec<String>);
301
302#[serde_as]
303#[derive(Deserialize, Serialize, Debug, Clone)]
304pub struct ScimUrls(#[serde_as(as = "OneOrMany<_, PreferMany>")] pub Vec<Url>);
305
306#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
307pub struct ScimEntryPostGeneric {
308    /// Create an attribute to contain the following value state.
309    #[serde(flatten)]
310    #[schema(value_type = Object, additional_properties = true)]
311    pub attrs: BTreeMap<Attribute, JsonValue>,
312}
313
314#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
315#[serde(rename_all = "lowercase", tag = "state")]
316pub enum ScimEntryAssertion {
317    /// The entry should be present, with this id/UUID, and
318    /// the content of these attributes must be as shown. If an
319    /// attribute is not present in the assertion, it will not be
320    /// altered. To remove an attribute, set the attribute to "null".
321    Present {
322        id: Uuid,
323        #[schema(value_type = BTreeMap<String, Value>)]
324        #[serde(flatten)]
325        attrs: BTreeMap<Attribute, Option<JsonValue>>,
326    },
327    /// The entry should be absent (removed) from the database. Once
328    /// removed, the entry can not be re-asserted. You will need to create
329    /// a new entry with a unique ID.
330    Absent { id: Uuid },
331}
332
333#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
334pub struct ScimAssertGeneric {
335    /// The ID of this assertion.
336    pub id: Uuid,
337
338    /// A set of assertions about expected entry state.
339    pub assertions: Vec<ScimEntryAssertion>,
340}
341
342#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
343pub struct ScimEntryPutGeneric {
344    // id is only used to target the entry in question
345    pub id: Uuid,
346
347    #[serde(flatten)]
348    /// Non-standard extension - allow query options to be set in a put request. This
349    /// is because a put request also returns the entry state post put, so we want
350    /// to allow putters to adjust and control what is returned here.
351    pub query: ScimEntryGetQuery,
352
353    // external_id can't be set by put
354    // meta is skipped on put
355    // Schemas are decoded as part of "attrs".
356    /// Update an attribute to contain the following value state.
357    /// If the attribute is None, it is removed.
358    #[schema(value_type = BTreeMap<String, Value>)]
359    #[serde(flatten)]
360    pub attrs: BTreeMap<Attribute, Option<JsonValue>>,
361}
362
363impl TryFrom<ScimEntryPutKanidm> for ScimEntryPutGeneric {
364    type Error = serde_json::Error;
365
366    fn try_from(value: ScimEntryPutKanidm) -> Result<Self, Self::Error> {
367        let ScimEntryPutKanidm { id, attrs } = value;
368
369        let attrs = attrs
370            .into_iter()
371            .map(|(attr, value)| {
372                if let Some(v) = value {
373                    serde_json::to_value(v).map(|json_value| (attr, Some(json_value)))
374                } else {
375                    Ok((attr, None))
376                }
377            })
378            .collect::<Result<_, _>>()?;
379
380        Ok(ScimEntryPutGeneric {
381            id,
382            attrs,
383            query: Default::default(),
384        })
385    }
386}