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
use serde::{Deserialize, Serialize};

use crate::models::scim_schema::Meta;
use crate::utils::error::SCIMError;

#[derive(Serialize, Deserialize, Debug)]
pub struct ServiceProviderConfig {
    #[serde(rename = "documentationUri", skip_serializing_if = "Option::is_none")]
    pub documentation_uri: Option<String>,
    pub patch: Supported,
    pub bulk: Bulk,
    pub filter: Filter,
    #[serde(rename = "changePassword")]
    pub change_password: Supported,
    pub sort: Supported,
    pub etag: Supported,
    #[serde(rename = "authenticationSchemes")]
    pub authentication_schemes: Vec<AuthenticationScheme>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<Meta>,
}

impl Default for ServiceProviderConfig {
    fn default() -> Self {
        ServiceProviderConfig {
            documentation_uri: None,
            patch: Supported { supported: false },
            bulk: Bulk {
                supported: false,
                max_operations: 0,
                max_payload_size: 0,
            },
            filter: Filter {
                supported: false,
                max_results: 0,
            },
            change_password: Supported { supported: false },
            sort: Supported { supported: false },
            etag: Supported { supported: false },
            authentication_schemes: vec![],
            meta: None,
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct AuthenticationScheme {
    pub name: String,
    pub r#type: String,
    pub description: String,
    #[serde(rename = "specUri")]
    pub spec_uri: String,
    #[serde(rename = "documentationUri", skip_serializing_if = "Option::is_none")]
    pub documentation_uri: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub primary: Option<bool>,
}

impl Default for AuthenticationScheme {
    fn default() -> Self {
        AuthenticationScheme {
            name: "".to_string(),
            r#type: "".to_string(),
            description: "".to_string(),
            spec_uri: "".to_string(),
            documentation_uri: Some("".to_string()),
            primary: None,
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Filter {
    pub supported: bool,
    #[serde(rename = "maxResults")]
    pub max_results: i64,
}

impl Default for Filter {
    fn default() -> Self {
        Filter {
            supported: false,
            max_results: 100,
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Bulk {
    pub supported: bool,
    #[serde(rename = "maxOperations")]
    pub max_operations: i64,
    #[serde(rename = "maxPayloadSize")]
    pub max_payload_size: i64,
}

impl Default for Bulk {
    fn default() -> Self {
        Bulk {
            supported: false,
            max_operations: 1000,
            max_payload_size: 1048576,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Supported {
    pub supported: bool,
}

/// Converts a JSON string into a `ServiceProviderConfig` struct.
///
/// This method attempts to parse a JSON string to construct a `ServiceProviderConfig` 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 `ServiceProviderConfig` object.
///
/// # Examples
///
/// ```rust
/// use scim_v2::models::service_provider_config::ServiceProviderConfig;
///
/// let config_json = r#"{
///             "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"],
///             "documentationUri": "http:///example.com/help/scim.html",
///             "patch": { "supported": true },
///             "bulk": {
///                 "supported": true,
///                 "maxOperations": 1000,
///                 "maxPayloadSize": 1048576
///             },
///             "filter": {
///                 "supported": true,
///                 "maxResults": 200
///             },
///             "changePassword": { "supported": true },
///             "sort": { "supported": true },
///             "etag": { "supported": true },
///             "authenticationSchemes": [
///                 {
///                     "name": "OAuth Bearer Token",
///                     "description": "Authentication scheme using the OAuth Bearer Token Standard",
///                     "specUri": "http:///www.rfc-editor.org/info/rfc6750",
///                     "documentationUri": "http:///example.com/help/oauth.html"
///                 },
///                 {
///                     "name": "HTTP Basic",
///                     "description": "Authentication scheme using the HTTP Basic Standard",
///                     "specUri": "http:///www.rfc-editor.org/info/rfc2617",
///                     "documentationUri": "http:///example.com/help/httpBasic.html"
///                 }
///             ]
///         }"#;
/// match ServiceProviderConfig::try_from(config_json) {
///     Ok(config) => println!("Successfully converted JSON to ServiceProviderConfig: {:?}", config),
///     Err(e) => println!("Error converting from JSON to ServiceProviderConfig: {}", e),
/// }
/// ```
impl TryFrom<&str> for ServiceProviderConfig {
    type Error = SCIMError;

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

impl ServiceProviderConfig {
    /// Validates a service provider config.
    ///
    /// This function checks if the service provider config has `patch`, `bulk`, `filter`, `change_password`, `sort`, and `etag`. If any of these fields are missing, it returns an error.
    ///
    /// # Arguments
    ///
    /// * `config` - A reference to a ServiceProviderConfig instance.
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the service provider config is valid.
    /// * `Err(SCIMError::MissingRequiredField)` - If a required field is missing.
    ///
    /// # Example
    ///
    /// ```
    /// use scim_v2::models::service_provider_config::ServiceProviderConfig;
    ///
    /// let config = ServiceProviderConfig {
    ///     // Initialize config fields here...
    ///     // ...
    ///     ..Default::default()
    /// };
    ///
    /// match config.validate() {
    ///     Ok(_) => println!("ServiceProviderConfig is valid."),
    ///     Err(e) => println!("ServiceProviderConfig is invalid: {}", e),
    /// }
    /// ```
    pub fn validate(&self) -> Result<(), SCIMError> {
        if !self.patch.supported {
            return Err(SCIMError::MissingRequiredField("patch".to_string()));
        }
        if !self.bulk.supported {
            return Err(SCIMError::MissingRequiredField("bulk".to_string()));
        }
        if !self.filter.supported {
            return Err(SCIMError::MissingRequiredField("filter".to_string()));
        }
        if !self.change_password.supported {
            return Err(SCIMError::MissingRequiredField(
                "change_password".to_string(),
            ));
        }
        if !self.sort.supported {
            return Err(SCIMError::MissingRequiredField("sort".to_string()));
        }
        if !self.etag.supported {
            return Err(SCIMError::MissingRequiredField("etag".to_string()));
        }
        Ok(())
    }

    /// Serializes the `ServiceProviderConfig` 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 `ServiceProviderConfig` instance, and `Err(SCIMError)` contains
    /// the custom error encountered during serialization.
    ///
    /// # Examples
    ///
    /// ```
    /// use scim_v2::models::service_provider_config::ServiceProviderConfig;
    ///
    /// let config = ServiceProviderConfig {
    ///     // Initialize config fields here...
    ///     // ...
    ///     ..Default::default()
    /// };
    ///
    /// match config.serialize() {
    ///     Ok(json) => println!("Serialized ServiceProviderConfig: {}", 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 `ServiceProviderConfig` instance, using the custom SCIMError for error handling.
    ///
    /// # Parameters
    ///
    /// * `json` - A string slice that holds the JSON representation of a `ServiceProviderConfig`.
    ///
    /// # Returns
    ///
    /// This method returns a `Result<ServiceProviderConfig, SCIMError>`, where `Ok(ServiceProviderConfig)` is the deserialized `ServiceProviderConfig` instance,
    /// and `Err(SCIMError)` is the custom error encountered during deserialization.
    ///
    /// # Examples
    ///
    /// ```
    /// use scim_v2::models::service_provider_config::ServiceProviderConfig;
    ///
    /// let config_json = r#"{
    ///             "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"],
    ///             "documentationUri": "http:///example.com/help/scim.html",
    ///             "patch": { "supported": true },
    ///             "bulk": {
    ///                 "supported": true,
    ///                 "maxOperations": 1000,
    ///                 "maxPayloadSize": 1048576
    ///             },
    ///             "filter": {
    ///                 "supported": true,
    ///                 "maxResults": 200
    ///             },
    ///             "changePassword": { "supported": true },
    ///             "sort": { "supported": true },
    ///             "etag": { "supported": true },
    ///             "authenticationSchemes": [
    ///                 {
    ///                     "name": "OAuth Bearer Token",
    ///                     "description": "Authentication scheme using the OAuth Bearer Token Standard",
    ///                     "specUri": "http:///www.rfc-editor.org/info/rfc6750",
    ///                     "documentationUri": "http:///example.com/help/oauth.html"
    ///                 },
    ///                 {
    ///                     "name": "HTTP Basic",
    ///                     "description": "Authentication scheme using the HTTP Basic Standard",
    ///                     "specUri": "http:///www.rfc-editor.org/info/rfc2617",
    ///                     "documentationUri": "http:///example.com/help/httpBasic.html"
    ///                 }
    ///             ]
    ///         }"#;
    /// match ServiceProviderConfig::deserialize(config_json) {
    ///     Ok(user) => println!("Deserialized User: {:?}", user),
    ///     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 pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn service_provider_config_deserialization() {
        let json_data = r#"{
            "schemas": ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"],
            "documentationUri": "http://example.com/help/scim.html",
            "patch": { "supported": true },
            "bulk": {
                "supported": true,
                "maxOperations": 1000,
                "maxPayloadSize": 1048576
            },
            "filter": {
                "supported": true,
                "maxResults": 200
            },
            "changePassword": { "supported": true },
            "sort": { "supported": true },
            "etag": { "supported": true },
            "authenticationSchemes": [
                {
                    "name": "OAuth Bearer Token",
                    "description": "Authentication scheme using the OAuth Bearer Token Standard",
                    "specUri": "http://www.rfc-editor.org/info/rfc6750",
                    "documentationUri": "http://example.com/help/oauth.html",
                    "type": "oauthbearertoken",
                    "primary": true
                },
                {
                    "name": "HTTP Basic",
                    "description": "Authentication scheme using the HTTP Basic Standard",
                    "specUri": "http://www.rfc-editor.org/info/rfc2617",
                    "documentationUri": "http://example.com/help/httpBasic.html",
                    "type": "httpbasic"
                }
            ]
        }"#;

        let config: Result<ServiceProviderConfig, serde_json::Error> =
            serde_json::from_str(json_data);

        if let Err(e) = &config {
            eprintln!("Deserialization failed: {:?}", e);
        }
        assert!(config.is_ok());
        let config = config.unwrap();
        assert_eq!(
            config.documentation_uri,
            Some("http://example.com/help/scim.html".to_string())
        );
        assert_eq!(config.patch.supported, true);
        assert_eq!(config.bulk.supported, true);
        assert_eq!(config.bulk.max_operations, 1000);
        assert_eq!(config.bulk.max_payload_size, 1048576);
        assert_eq!(config.filter.supported, true);
        assert_eq!(config.filter.max_results, 200);
        assert_eq!(config.change_password.supported, true);
        assert_eq!(config.sort.supported, true);
        assert_eq!(config.etag.supported, true);
        assert_eq!(config.authentication_schemes.len(), 2);
        let oauth_scheme = &config.authentication_schemes[0];
        assert_eq!(oauth_scheme.name, "OAuth Bearer Token");
        assert_eq!(
            oauth_scheme.description,
            "Authentication scheme using the OAuth Bearer Token Standard"
        );
        assert_eq!(
            oauth_scheme.spec_uri,
            "http://www.rfc-editor.org/info/rfc6750"
        );
        assert_eq!(
            oauth_scheme.documentation_uri,
            Some("http://example.com/help/oauth.html".to_string())
        );
        assert_eq!(oauth_scheme.r#type, "oauthbearertoken");
        assert_eq!(oauth_scheme.primary, Some(true));
        let http_scheme = &config.authentication_schemes[1];
        assert_eq!(http_scheme.name, "HTTP Basic");
        assert_eq!(
            http_scheme.description,
            "Authentication scheme using the HTTP Basic Standard"
        );
        assert_eq!(
            http_scheme.spec_uri,
            "http://www.rfc-editor.org/info/rfc2617"
        );
        assert_eq!(
            http_scheme.documentation_uri,
            Some("http://example.com/help/httpBasic.html".to_string())
        );
        assert_eq!(http_scheme.r#type, "httpbasic");
    }
}