scim_v2 0.4.0

A crate that provides utilities for working with the System for Cross-domain Identity Management (SCIM) version 2.0 protocol. (rfc7642, rfc7643, rfc7644)
Documentation
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use serde::{Deserialize, Serialize};

use crate::utils::error::SCIMError;
use crate::{ENTERPRISE_USER_SCHEMA, GROUP_SCHEMA, USER_SCHEMA};

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Meta {
    #[serde(rename = "resourceType", skip_serializing_if = "Option::is_none")]
    pub resource_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created: Option<String>,
    #[serde(rename = "lastModified", skip_serializing_if = "Option::is_none")]
    pub last_modified: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub location: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Schema {
    pub id: String,
    pub name: String,
    pub description: String,
    pub attributes: Vec<Attributes>,
    pub meta: Meta,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Attributes {
    pub name: String,
    pub r#type: String,
    #[serde(rename = "multiValued")]
    pub multi_valued: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub required: Option<bool>,
    #[serde(rename = "canonicalValues", skip_serializing_if = "Option::is_none")]
    pub canonical_values: Option<Vec<String>>,
    #[serde(rename = "caseExact", skip_serializing_if = "Option::is_none")]
    pub case_exact: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mutability: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub returned: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uniqueness: Option<String>,
    #[serde(rename = "subAttributes", skip_serializing_if = "Option::is_none")]
    pub sub_attributes: Option<Vec<SubAttributes>>,
    #[serde(rename = "referenceTypes", skip_serializing_if = "Option::is_none")]
    pub reference_types: Option<Vec<String>>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SubAttributes {
    pub name: String,
    pub r#type: String,
    #[serde(rename = "multiValued")]
    pub multi_valued: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub required: Option<bool>,
    #[serde(rename = "canonicalValues", skip_serializing_if = "Option::is_none")]
    pub canonical_values: Option<Vec<String>>,
    #[serde(rename = "caseExact", skip_serializing_if = "Option::is_none")]
    pub case_exact: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mutability: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub returned: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uniqueness: Option<String>,
    #[serde(rename = "referenceTypes", skip_serializing_if = "Option::is_none")]
    pub reference_types: Option<Vec<String>>,
}

/// Retrieves a list of `Schema` instances based on the provided schema names.
///
/// This function takes a vector of schema names as input and attempts to retrieve the corresponding `Schema` instances.
/// It uses a predefined list of schema contents to match the input schema names.
///
/// # Parameters
///
/// * `schema_names` - A vector of string slices that represent the names of the schemas to retrieve.
///
/// # Returns
///
/// This function returns a `Result<Vec<Schema>, SCIMError>`. If the function succeeds, it returns `Ok(Vec<Schema>)`
/// where `Vec<Schema>` is a vector of the retrieved `Schema` instances. If the function fails, it returns `Err(SCIMError)`
/// where `SCIMError` is the error that occurred.
///
/// # Errors
///
/// This function will return an error if any of the provided schema names do not match any of the predefined schema contents.
/// The error will be of type `SCIMError::SchemaNotFound` and will contain the name of the schema that could not be found.
///
/// # Examples
///
/// ```rust
/// use scim_v2::models::scim_schema::get_schemas;
///
/// let schemas = get_schemas(vec!["user", "group"]);
/// match schemas {
///     Ok(schemas) => println!("Successfully retrieved schemas: {:?}", schemas),
///     Err(e) => println!("Error retrieving schemas: {}", e),
/// }
/// ```
pub fn get_schemas(schema_names: Vec<&str>) -> Result<Vec<Schema>, SCIMError> {
    let mut schemas = Vec::new();

    let schema_contents = [
        ("user", USER_SCHEMA),
        ("enterprise_user", ENTERPRISE_USER_SCHEMA),
        ("group", GROUP_SCHEMA),
    ]
    .iter()
    .cloned()
    .collect::<std::collections::HashMap<_, _>>();

    for schema_name in schema_names {
        if let Some(schema_content) = schema_contents.get(schema_name) {
            let schema: Schema = serde_json::from_str(schema_content)?;
            schemas.push(schema);
        } else {
            return Err(SCIMError::SchemaNotFound(schema_name.to_string()));
        }
    }
    Ok(schemas)
}

/// Converts a JSON string into a `Schema` struct.
///
/// This method attempts to parse a JSON string to construct a `Schema` object. It's useful for scenarios where
/// you receive a JSON representation of a user from an external source (e.g., a web request) and you need to
/// work with this data in a strongly-typed manner within your application.
///
/// # Errors
///
/// Returns `SCIMError::DeserializationError` if the provided JSON string cannot be parsed into a `Schema` object.
///
/// # Examples
///
/// ```rust
/// use scim_v2::models::scim_schema::Schema;
///
/// let schema_json = r#"{
///   "id": "urn:ietf:params:scim:schemas:core:2.0:ResourceType",
///   "name": "ResourceType",
///   "description": "Specifies the schema that describes a SCIM resource type",
///   "attributes": [
///     {
///       "name": "id",
///       "type": "string",
///       "multiValued": false,
///       "description": "The resource type's server unique id. May be the same as the 'name' attribute.",
///       "required": false,
///       "caseExact": false,
///       "mutability": "readOnly",
///       "returned": "default",
///       "uniqueness": "none"
///     },
///     {
///       "name": "name",
///       "type": "string",
///       "multiValued": false,
///       "description": "The resource type name.  When applicable, service providers MUST specify the name, e.g., 'User'.",
///       "required": true,
///       "caseExact": false,
///       "mutability": "readOnly",
///       "returned": "default",
///       "uniqueness": "none"
///     },
///     {
///       "name": "description",
///       "type": "string",
///       "multiValued": false,
///       "description": "The resource type's human-readable description.  When applicable, service providers MUST specify the description.",
///       "required": false,
///       "caseExact": false,
///       "mutability": "readOnly",
///       "returned": "default",
///       "uniqueness": "none"
///     },
///     {
///       "name": "endpoint",
///       "type": "reference",
///       "referenceTypes": [
///         "uri"
///       ],
///       "multiValued": false,
///       "description": "The resource type's HTTP-addressable endpoint relative to the Base URL, e.g., '/Users'.",
///       "required": true,
///       "caseExact": false,
///       "mutability": "readOnly",
///       "returned": "default",
///       "uniqueness": "none"
///     },
///     {
///       "name": "schema",
///       "type": "reference",
///       "referenceTypes": [
///         "uri"
///       ],
///       "multiValued": false,
///       "description": "The resource type's primary/base schema URI.",
///       "required": true,
///       "caseExact": true,
///       "mutability": "readOnly",
///       "returned": "default",
///       "uniqueness": "none"
///     },
///     {
///       "name": "schemaExtensions",
///       "type": "complex",
///       "multiValued": false,
///       "description": "A list of URIs of the resource type's schema extensions.",
///       "required": true,
///       "mutability": "readOnly",
///       "returned": "default",
///       "subAttributes": [
///         {
///           "name": "schema",
///           "type": "reference",
///           "referenceTypes": [
///             "uri"
///           ],
///           "multiValued": false,
///           "description": "The URI of a schema extension.",
///           "required": true,
///           "caseExact": true,
///           "mutability": "readOnly",
///           "returned": "default",
///           "uniqueness": "none"
///         },
///         {
///           "name": "required",
///           "type": "boolean",
///           "multiValued": false,
///           "description": "A Boolean value that specifies whether or not the schema extension is required for the resource type.  If true, a resource of this type MUST include this schema extension and also include any attributes declared as required in this schema extension. If false, a resource of this type MAY omit this schema extension.",
///           "required": true,
///           "mutability": "readOnly",
///           "returned": "default"
///         }
///       ]
///     }
///   ]
/// }"#;
/// match Schema::try_from(schema_json) {
///     Ok(schema) => println!("Successfully converted JSON to Schema: {:?}", schema),
///     Err(e) => println!("Error converting from JSON to Schema: {}", e),
/// }
/// ```
impl TryFrom<&str> for Schema {
    type Error = SCIMError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        serde_json::from_str(value).map_err(SCIMError::DeserializationError)
    }
}

impl Schema {
    /// Serializes the `Schema` instance to a JSON string, using the custom SCIMError for error handling.
    ///
    /// # Returns
    ///
    /// This method returns a `Result<String, SCIMError>`, where `Ok(String)` contains
    /// the JSON string representation of the `Schema` instance, and `Err(SCIMError)` contains
    /// the custom error encountered during serialization.
    ///
    /// # Examples
    ///
    /// ```
    /// use scim_v2::models::scim_schema::{Schema, Attributes, Meta};
    ///
    ///     let user = Schema {
    ///     id: "urn:ietf:params:scim:schemas:core:2.0:User".to_string(),
    ///     name: "User".to_string(),
    ///     description: "User Account".to_string(),
    ///     attributes: vec![
    ///         Attributes {
    ///             name: "userName".to_string(),
    ///             r#type: "string".to_string(),
    ///             multi_valued: false,
    ///             description: Some("Unique identifier for the User".to_string()),
    ///             required: Some(true),
    ///             canonical_values: None,
    ///             case_exact: Some(false),
    ///             mutability: Some("readWrite".to_string()),
    ///             returned: Some("default".to_string()),
    ///             uniqueness: Some("server".to_string()),
    ///             sub_attributes: None,
    ///             reference_types: None,
    ///         },
    ///     ],
    ///     meta: Meta {
    ///     resource_type: Some("Schema".to_string()),
    ///     created: None,
    ///     last_modified: None,
    ///     version: None,
    ///     location: Some("/v2/Schemas/urn:ietf:params:scim:schemas:core:2.0:User".to_string()),
    /// },
    /// };
    ///
    /// match user.serialize() {
    ///     Ok(json) => println!("Serialized User: {}", json),
    ///     Err(e) => println!("Serialization error: {}", e),
    /// }
    /// ```
    pub fn serialize(&self) -> Result<String, SCIMError> {
        serde_json::to_string(&self).map_err(SCIMError::SerializationError)
    }

    /// Deserializes a JSON string into a `Schema` instance, using the custom SCIMError for error handling.
    ///
    /// # Parameters
    ///
    /// * `json` - A string slice that holds the JSON representation of a `Schema`.
    ///
    /// # Returns
    ///
    /// This method returns a `Result<Schema, SCIMError>`, where `Ok(User)` is the deserialized `Schema` instance,
    /// and `Err(SCIMError)` is the custom error encountered during deserialization.
    ///
    /// # Examples
    ///
    /// ```
    /// use scim_v2::models::scim_schema::Schema;
    ///
    /// let schema_json = r#"{
    ///   "id": "urn:ietf:params:scim:schemas:core:2.0:ResourceType",
    ///   "name": "ResourceType",
    ///   "description": "Specifies the schema that describes a SCIM resource type",
    ///   "attributes": [
    ///     {
    ///       "name": "id",
    ///       "type": "string",
    ///       "multiValued": false,
    ///       "description": "The resource type's server unique id. May be the same as the 'name' attribute.",
    ///       "required": false,
    ///       "caseExact": false,
    ///       "mutability": "readOnly",
    ///       "returned": "default",
    ///       "uniqueness": "none"
    ///     },
    ///     {
    ///       "name": "name",
    ///       "type": "string",
    ///       "multiValued": false,
    ///       "description": "The resource type name.  When applicable, service providers MUST specify the name, e.g., 'User'.",
    ///       "required": true,
    ///       "caseExact": false,
    ///       "mutability": "readOnly",
    ///       "returned": "default",
    ///       "uniqueness": "none"
    ///     },
    ///     {
    ///       "name": "description",
    ///       "type": "string",
    ///       "multiValued": false,
    ///       "description": "The resource type's human-readable description.  When applicable, service providers MUST specify the description.",
    ///       "required": false,
    ///       "caseExact": false,
    ///       "mutability": "readOnly",
    ///       "returned": "default",
    ///       "uniqueness": "none"
    ///     },
    ///     {
    ///       "name": "endpoint",
    ///       "type": "reference",
    ///       "referenceTypes": [
    ///         "uri"
    ///       ],
    ///       "multiValued": false,
    ///       "description": "The resource type's HTTP-addressable endpoint relative to the Base URL, e.g., '/Users'.",
    ///       "required": true,
    ///       "caseExact": false,
    ///       "mutability": "readOnly",
    ///       "returned": "default",
    ///       "uniqueness": "none"
    ///     },
    ///     {
    ///       "name": "schema",
    ///       "type": "reference",
    ///       "referenceTypes": [
    ///         "uri"
    ///       ],
    ///       "multiValued": false,
    ///       "description": "The resource type's primary/base schema URI.",
    ///       "required": true,
    ///       "caseExact": true,
    ///       "mutability": "readOnly",
    ///       "returned": "default",
    ///       "uniqueness": "none"
    ///     },
    ///     {
    ///       "name": "schemaExtensions",
    ///       "type": "complex",
    ///       "multiValued": false,
    ///       "description": "A list of URIs of the resource type's schema extensions.",
    ///       "required": true,
    ///       "mutability": "readOnly",
    ///       "returned": "default",
    ///       "subAttributes": [
    ///         {
    ///           "name": "schema",
    ///           "type": "reference",
    ///           "referenceTypes": [
    ///             "uri"
    ///           ],
    ///           "multiValued": false,
    ///           "description": "The URI of a schema extension.",
    ///           "required": true,
    ///           "caseExact": true,
    ///           "mutability": "readOnly",
    ///           "returned": "default",
    ///           "uniqueness": "none"
    ///         },
    ///         {
    ///           "name": "required",
    ///           "type": "boolean",
    ///           "multiValued": false,
    ///           "description": "A Boolean value that specifies whether or not the schema extension is required for the resource type.  If true, a resource of this type MUST include this schema extension and also include any attributes declared as required in this schema extension. If false, a resource of this type MAY omit this schema extension.",
    ///           "required": true,
    ///           "mutability": "readOnly",
    ///           "returned": "default"
    ///         }
    ///       ]
    ///     }
    ///   ]
    /// }"#;
    /// match Schema::deserialize(schema_json) {
    ///     Ok(schema) => println!("Deserialized User: {:?}", schema),
    ///     Err(e) => println!("Deserialization error: {}", e),
    /// }
    /// ```
    pub fn deserialize(json: &str) -> Result<Self, SCIMError> {
        serde_json::from_str(json).map_err(SCIMError::DeserializationError)
    }
}

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

    #[test]
    fn get_schemas_returns_correct_schemas_for_valid_input() {
        let schemas = get_schemas(vec!["user"]).unwrap();
        assert_eq!(schemas.len(), 1);
        assert_eq!(schemas[0].id, "urn:ietf:params:scim:schemas:core:2.0:User");
        assert_eq!(schemas[0].name, "User");
        assert_eq!(schemas[0].description, "User Account");
        assert_eq!(schemas[0].attributes.len(), 21);
        assert_eq!(
            schemas[0].meta.resource_type.as_ref(),
            Some(&"Schema".to_string())
        );
        assert_eq!(
            schemas[0].meta.location.as_ref(),
            Some(&"/v2/Schemas/urn:ietf:params:scim:schemas:core:2.0:User".to_string())
        );
    }

    #[test]
    fn get_schemas_returns_error_for_invalid_input() {
        let result = get_schemas(vec!["invalid"]);
        assert!(result.is_err());
    }

    #[test]
    fn get_schemas_returns_error_for_missing_file() {
        let result = get_schemas(vec!["missing"]);
        assert!(result.is_err());
    }
}