scim-server 0.4.0

A comprehensive SCIM 2.0 server library for Rust with multi-tenant support and type-safe operations
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! Name value object for SCIM user name components.
//!
//! This module provides a type-safe wrapper around SCIM name attributes with built-in validation.
//! Name attributes represent the components of a user's real name as defined in RFC 7643 Section 4.1.1.

use crate::error::{ValidationError, ValidationResult};
use crate::resource::value_objects::value_object_trait::{SchemaConstructible, ValueObject};
use crate::schema::types::{AttributeDefinition, AttributeType};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::any::Any;
use std::fmt;

/// A validated SCIM name attribute.
///
/// Name represents the components of a user's real name as defined in RFC 7643.
/// It enforces validation rules at construction time, ensuring that only valid name
/// attributes can exist in the system.
///
/// ## Validation Rules
///
/// - At least one name component must be provided (not all fields can be empty/None)
/// - Individual name components cannot be empty strings
/// - All fields are optional but if provided must contain meaningful content
///
/// ## Examples
///
/// ```rust
/// use scim_server::resource::value_objects::Name;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     // Create with full name components
///     let name = Name::new(
///         Some("Ms. Barbara J Jensen, III".to_string()),
///         Some("Jensen".to_string()),
///         Some("Barbara".to_string()),
///         Some("Jane".to_string()),
///         Some("Ms.".to_string()),
///         Some("III".to_string())
///     )?;
///
///     // Create with minimal components
///     let simple_name = Name::new_simple(
///         "John".to_string(),
///         "Doe".to_string()
///     )?;
///
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Name {
    pub formatted: Option<String>,
    #[serde(rename = "familyName")]
    pub family_name: Option<String>,
    #[serde(rename = "givenName")]
    pub given_name: Option<String>,
    #[serde(rename = "middleName")]
    pub middle_name: Option<String>,
    #[serde(rename = "honorificPrefix")]
    pub honorific_prefix: Option<String>,
    #[serde(rename = "honorificSuffix")]
    pub honorific_suffix: Option<String>,
}

impl Name {
    /// Create a new Name with all components.
    ///
    /// This is the primary constructor that enforces all validation rules.
    /// Use this method when creating Name instances from untrusted input.
    ///
    /// # Arguments
    ///
    /// * `formatted` - The full name, formatted for display
    /// * `family_name` - The family name or last name
    /// * `given_name` - The given name or first name
    /// * `middle_name` - The middle name(s)
    /// * `honorific_prefix` - The honorific prefix or title (e.g., "Ms.", "Dr.")
    /// * `honorific_suffix` - The honorific suffix (e.g., "III", "Jr.")
    ///
    /// # Returns
    ///
    /// * `Ok(Name)` - If at least one field is provided and all provided fields are valid
    /// * `Err(ValidationError)` - If all fields are None/empty or any field violates validation rules
    pub fn new(
        formatted: Option<String>,
        family_name: Option<String>,
        given_name: Option<String>,
        middle_name: Option<String>,
        honorific_prefix: Option<String>,
        honorific_suffix: Option<String>,
    ) -> ValidationResult<Self> {
        // Validate individual components
        if let Some(ref f) = formatted {
            Self::validate_name_component(f, "formatted")?;
        }
        if let Some(ref fn_val) = family_name {
            Self::validate_name_component(fn_val, "familyName")?;
        }
        if let Some(ref gn) = given_name {
            Self::validate_name_component(gn, "givenName")?;
        }
        if let Some(ref mn) = middle_name {
            Self::validate_name_component(mn, "middleName")?;
        }
        if let Some(ref hp) = honorific_prefix {
            Self::validate_name_component(hp, "honorificPrefix")?;
        }
        if let Some(ref hs) = honorific_suffix {
            Self::validate_name_component(hs, "honorificSuffix")?;
        }

        // Ensure at least one component is provided
        if formatted.is_none()
            && family_name.is_none()
            && given_name.is_none()
            && middle_name.is_none()
            && honorific_prefix.is_none()
            && honorific_suffix.is_none()
        {
            return Err(ValidationError::custom(
                "At least one name component must be provided",
            ));
        }

        Ok(Self {
            formatted,
            family_name,
            given_name,
            middle_name,
            honorific_prefix,
            honorific_suffix,
        })
    }

    /// Create a simple Name with just given and family names.
    ///
    /// Convenience constructor for creating basic name structures.
    ///
    /// # Arguments
    ///
    /// * `given_name` - The given name or first name
    /// * `family_name` - The family name or last name
    ///
    /// # Returns
    ///
    /// * `Ok(Name)` - If the names are valid
    /// * `Err(ValidationError)` - If any name violates validation rules
    pub fn new_simple(given_name: String, family_name: String) -> ValidationResult<Self> {
        Self::new(None, Some(family_name), Some(given_name), None, None, None)
    }

    /// Create a Name with a formatted display name only.
    ///
    /// Convenience constructor for cases where only a formatted name is available.
    ///
    /// # Arguments
    ///
    /// * `formatted` - The full formatted name
    ///
    /// # Returns
    ///
    /// * `Ok(Name)` - If the formatted name is valid
    /// * `Err(ValidationError)` - If the name violates validation rules
    pub fn new_formatted(formatted: String) -> ValidationResult<Self> {
        Self::new(Some(formatted), None, None, None, None, None)
    }

    /// Create a Name instance without validation for internal use.

    /// Get the formatted name.
    pub fn formatted(&self) -> Option<&str> {
        self.formatted.as_deref()
    }

    /// Get the family name.
    pub fn family_name(&self) -> Option<&str> {
        self.family_name.as_deref()
    }

    /// Get the given name.
    pub fn given_name(&self) -> Option<&str> {
        self.given_name.as_deref()
    }

    /// Get the middle name.
    pub fn middle_name(&self) -> Option<&str> {
        self.middle_name.as_deref()
    }

    /// Get the honorific prefix.
    pub fn honorific_prefix(&self) -> Option<&str> {
        self.honorific_prefix.as_deref()
    }

    /// Get the honorific suffix.
    pub fn honorific_suffix(&self) -> Option<&str> {
        self.honorific_suffix.as_deref()
    }

    /// Generate a formatted display name from components.
    ///
    /// Creates a formatted name string from the available name components
    /// if no explicit formatted name is provided.
    ///
    /// # Returns
    ///
    /// The formatted name if available, otherwise a constructed name from components,
    /// or None if no components are available.
    pub fn display_name(&self) -> Option<String> {
        if let Some(ref formatted) = self.formatted {
            return Some(formatted.clone());
        }

        let mut parts = Vec::new();

        if let Some(ref prefix) = self.honorific_prefix {
            parts.push(prefix.as_str());
        }
        if let Some(ref given) = self.given_name {
            parts.push(given.as_str());
        }
        if let Some(ref middle) = self.middle_name {
            parts.push(middle.as_str());
        }
        if let Some(ref family) = self.family_name {
            parts.push(family.as_str());
        }
        if let Some(ref suffix) = self.honorific_suffix {
            parts.push(suffix.as_str());
        }

        if parts.is_empty() {
            None
        } else {
            Some(parts.join(" "))
        }
    }

    /// Check if the name has any meaningful content.
    pub fn is_empty(&self) -> bool {
        self.formatted.is_none()
            && self.family_name.is_none()
            && self.given_name.is_none()
            && self.middle_name.is_none()
            && self.honorific_prefix.is_none()
            && self.honorific_suffix.is_none()
    }

    /// Validate a name component.
    fn validate_name_component(value: &str, field_name: &str) -> ValidationResult<()> {
        if value.trim().is_empty() {
            return Err(ValidationError::custom(format!(
                "{}: Name component cannot be empty or contain only whitespace",
                field_name
            )));
        }

        // Check for reasonable length (SCIM doesn't specify but let's be practical)
        if value.len() > 256 {
            return Err(ValidationError::custom(format!(
                "{}: Name component exceeds maximum length of 256 characters",
                field_name
            )));
        }

        // Check for control characters that shouldn't be in names
        if value
            .chars()
            .any(|c| c.is_control() && c != '\n' && c != '\r' && c != '\t')
        {
            return Err(ValidationError::custom(format!(
                "{}: Name component contains invalid control characters",
                field_name
            )));
        }

        Ok(())
    }

    /// Create a Name from a JSON value.
    pub fn from_json(value: &Value) -> ValidationResult<Self> {
        if let Value::Object(obj) = value {
            let formatted = obj
                .get("formatted")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());

            let family_name = obj
                .get("familyName")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());

            let given_name = obj
                .get("givenName")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());

            let middle_name = obj
                .get("middleName")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());

            let honorific_prefix = obj
                .get("honorificPrefix")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());

            let honorific_suffix = obj
                .get("honorificSuffix")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());

            Self::new(
                formatted,
                family_name,
                given_name,
                middle_name,
                honorific_prefix,
                honorific_suffix,
            )
        } else {
            Err(ValidationError::InvalidAttributeType {
                attribute: "name".to_string(),
                expected: "object".to_string(),
                actual: "non-object".to_string(),
            })
        }
    }
}

impl ValueObject for Name {
    fn attribute_type(&self) -> AttributeType {
        AttributeType::Complex
    }

    fn attribute_name(&self) -> &str {
        "name"
    }

    fn to_json(&self) -> ValidationResult<Value> {
        Ok(serde_json::to_value(self)?)
    }

    fn validate_against_schema(&self, definition: &AttributeDefinition) -> ValidationResult<()> {
        if definition.data_type != AttributeType::Complex {
            return Err(ValidationError::InvalidAttributeType {
                attribute: definition.name.clone(),
                expected: "complex".to_string(),
                actual: format!("{:?}", definition.data_type),
            });
        }

        if definition.name != "name" {
            return Err(ValidationError::InvalidAttributeName {
                actual: definition.name.clone(),
                expected: "name".to_string(),
            });
        }

        Ok(())
    }

    fn as_json_value(&self) -> Value {
        serde_json::to_value(self).unwrap_or(Value::Null)
    }

    fn supports_definition(&self, definition: &AttributeDefinition) -> bool {
        definition.data_type == AttributeType::Complex && definition.name == "name"
    }

    fn clone_boxed(&self) -> Box<dyn ValueObject> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl SchemaConstructible for Name {
    fn from_schema_and_value(
        definition: &AttributeDefinition,
        value: &Value,
    ) -> ValidationResult<Self> {
        if definition.name != "name" || definition.data_type != AttributeType::Complex {
            return Err(ValidationError::UnsupportedAttributeType {
                attribute: definition.name.clone(),
                type_name: format!("{:?}", definition.data_type),
            });
        }

        Self::from_json(value)
    }

    fn can_construct_from(definition: &AttributeDefinition) -> bool {
        definition.name == "name" && definition.data_type == AttributeType::Complex
    }

    fn constructor_priority() -> u8 {
        100 // High priority for exact name match
    }
}

impl fmt::Display for Name {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.display_name() {
            Some(name) => write!(f, "{}", name),
            None => write!(f, "[Empty Name]"),
        }
    }
}

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

    #[test]
    fn test_valid_name_full() {
        let name = Name::new(
            Some("Ms. Barbara J Jensen, III".to_string()),
            Some("Jensen".to_string()),
            Some("Barbara".to_string()),
            Some("Jane".to_string()),
            Some("Ms.".to_string()),
            Some("III".to_string()),
        );

        assert!(name.is_ok());
        let name = name.unwrap();
        assert_eq!(name.formatted(), Some("Ms. Barbara J Jensen, III"));
        assert_eq!(name.family_name(), Some("Jensen"));
        assert_eq!(name.given_name(), Some("Barbara"));
        assert_eq!(name.middle_name(), Some("Jane"));
        assert_eq!(name.honorific_prefix(), Some("Ms."));
        assert_eq!(name.honorific_suffix(), Some("III"));
    }

    #[test]
    fn test_valid_name_simple() {
        let name = Name::new_simple("John".to_string(), "Doe".to_string());

        assert!(name.is_ok());
        let name = name.unwrap();
        assert_eq!(name.given_name(), Some("John"));
        assert_eq!(name.family_name(), Some("Doe"));
        assert_eq!(name.formatted(), None);
    }

    #[test]
    fn test_valid_name_formatted_only() {
        let name = Name::new_formatted("John Doe".to_string());

        assert!(name.is_ok());
        let name = name.unwrap();
        assert_eq!(name.formatted(), Some("John Doe"));
        assert_eq!(name.given_name(), None);
        assert_eq!(name.family_name(), None);
    }

    #[test]
    fn test_empty_name_components() {
        let result = Name::new(Some("".to_string()), None, None, None, None, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_whitespace_only_components() {
        let result = Name::new(None, Some("   ".to_string()), None, None, None, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_all_none_components() {
        let result = Name::new(None, None, None, None, None, None);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("At least one name component")
        );
    }

    #[test]
    fn test_too_long_component() {
        let long_name = "a".repeat(300);
        let result = Name::new_formatted(long_name);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("exceeds maximum length")
        );
    }

    #[test]
    fn test_control_characters() {
        let result = Name::new_formatted("John\x00Doe".to_string());
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("invalid control characters")
        );
    }

    #[test]
    fn test_display_name_with_formatted() {
        let name = Name::new_formatted("Dr. John Smith Jr.".to_string()).unwrap();
        assert_eq!(name.display_name(), Some("Dr. John Smith Jr.".to_string()));
    }

    #[test]
    fn test_display_name_from_components() {
        let name = Name::new(
            None,
            Some("Smith".to_string()),
            Some("John".to_string()),
            Some("Michael".to_string()),
            Some("Dr.".to_string()),
            Some("Jr.".to_string()),
        )
        .unwrap();

        assert_eq!(
            name.display_name(),
            Some("Dr. John Michael Smith Jr.".to_string())
        );
    }

    #[test]
    fn test_display_name_partial_components() {
        let name = Name::new(
            None,
            Some("Doe".to_string()),
            Some("Jane".to_string()),
            None,
            None,
            None,
        )
        .unwrap();

        assert_eq!(name.display_name(), Some("Jane Doe".to_string()));
    }

    #[test]
    fn test_display() {
        let name = Name::new_simple("John".to_string(), "Doe".to_string()).unwrap();
        assert_eq!(format!("{}", name), "John Doe");

        // Test display with formatted name
        let formatted_name = Name::new_formatted("Dr. John Smith Doe Jr.".to_string()).unwrap();
        let display_str = format!("{}", formatted_name);
        assert_eq!(display_str, "Dr. John Smith Doe Jr.");
    }

    #[test]
    fn test_serialization() {
        let name = Name::new(
            Some("Ms. Barbara J Jensen, III".to_string()),
            Some("Jensen".to_string()),
            Some("Barbara".to_string()),
            Some("Jane".to_string()),
            Some("Ms.".to_string()),
            Some("III".to_string()),
        )
        .unwrap();

        let json = serde_json::to_string(&name).unwrap();
        assert!(json.contains("\"formatted\":\"Ms. Barbara J Jensen, III\""));
        assert!(json.contains("\"familyName\":\"Jensen\""));
        assert!(json.contains("\"givenName\":\"Barbara\""));
    }

    #[test]
    fn test_deserialization() {
        let json = r#"{
            "formatted": "Ms. Barbara J Jensen, III",
            "familyName": "Jensen",
            "givenName": "Barbara",
            "middleName": "Jane",
            "honorificPrefix": "Ms.",
            "honorificSuffix": "III"
        }"#;

        let name: Name = serde_json::from_str(json).unwrap();
        assert_eq!(name.formatted(), Some("Ms. Barbara J Jensen, III"));
        assert_eq!(name.family_name(), Some("Jensen"));
        assert_eq!(name.given_name(), Some("Barbara"));
    }

    #[test]
    fn test_equality() {
        let name1 = Name::new_simple("John".to_string(), "Doe".to_string()).unwrap();
        let name2 = Name::new_simple("John".to_string(), "Doe".to_string()).unwrap();
        let name3 = Name::new_simple("Jane".to_string(), "Doe".to_string()).unwrap();

        assert_eq!(name1, name2);
        assert_ne!(name1, name3);
    }

    #[test]
    fn test_clone() {
        let original = Name::new(
            Some("Dr. John Smith".to_string()),
            Some("Smith".to_string()),
            Some("John".to_string()),
            None,
            Some("Dr.".to_string()),
            None,
        )
        .unwrap();

        let cloned = original.clone();
        assert_eq!(original, cloned);
        assert_eq!(cloned.formatted(), Some("Dr. John Smith"));
        assert_eq!(cloned.family_name(), Some("Smith"));
    }

    #[test]
    fn test_allows_newlines_in_formatted() {
        let name = Name::new_formatted("John\nDoe".to_string());
        assert!(name.is_ok());
    }

    #[test]
    fn test_allows_tabs_in_formatted() {
        let name = Name::new_formatted("John\tDoe".to_string());
        assert!(name.is_ok());
    }
}