scim-server 0.5.3

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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
//! Automated Provider Capability Discovery System
//!
//! This module provides automatic discovery of SCIM provider capabilities by introspecting
//! the current server configuration, registered resource types, schemas, and provider
//! implementation. This eliminates manual capability configuration and ensures that
//! the ServiceProviderConfig always accurately reflects the actual server capabilities.
//!
//! # Key Features
//!
//! * **Automatic Discovery**: Capabilities are derived from registered components
//! * **SCIM Compliance**: Generates RFC 7644 compliant ServiceProviderConfig
//! * **Type Safety**: Leverages Rust's type system for capability constraints
//! * **Real-time Updates**: Capabilities reflect current server state
//! * **Mandatory ETag Support**: All providers automatically support conditional operations
//!
//! # Discovery Sources
//!
//! * **Schemas**: From SchemaRegistry - determines supported resource types
//! * **Operations**: From registered resource handlers - determines CRUD capabilities
//! * **Provider Type**: From ResourceProvider implementation - determines advanced features
//! * **Attribute Metadata**: From schema definitions - determines filtering capabilities
//! * **ETag Versioning**: Always enabled - conditional operations are mandatory for all providers

use crate::error::ScimError;
use crate::providers::ResourceProvider;
use crate::resource::ScimOperation;
use crate::schema::{AttributeDefinition, SchemaRegistry};
use crate::schema_discovery::{AuthenticationScheme, ServiceProviderConfig};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

/// Comprehensive provider capability information automatically discovered
/// from the current server configuration and registered components.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderCapabilities {
    /// SCIM operations supported per resource type
    pub supported_operations: HashMap<String, Vec<ScimOperation>>,

    /// All schemas currently registered and available
    pub supported_schemas: Vec<String>,

    /// Resource types that can be managed
    pub supported_resource_types: Vec<String>,

    /// Bulk operation capabilities
    pub bulk_capabilities: BulkCapabilities,

    /// Filtering and query capabilities
    pub filter_capabilities: FilterCapabilities,

    /// Pagination support information
    pub pagination_capabilities: PaginationCapabilities,

    /// Authentication schemes available
    pub authentication_capabilities: AuthenticationCapabilities,

    /// Provider-specific extended capabilities
    pub extended_capabilities: ExtendedCapabilities,
}

/// Bulk operation support information discovered from provider implementation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BulkCapabilities {
    /// Whether bulk operations are supported at all
    pub supported: bool,

    /// Maximum number of operations in a single bulk request
    pub max_operations: Option<usize>,

    /// Maximum payload size for bulk requests in bytes
    pub max_payload_size: Option<usize>,

    /// Whether bulk operations support failOnErrors
    pub fail_on_errors_supported: bool,
}

/// Filtering capabilities discovered from schema attribute definitions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FilterCapabilities {
    /// Whether filtering is supported
    pub supported: bool,

    /// Maximum number of results that can be returned
    pub max_results: Option<usize>,

    /// Attributes that support filtering (derived from schema)
    pub filterable_attributes: HashMap<String, Vec<String>>, // resource_type -> [attribute_names]

    /// Supported filter operators
    pub supported_operators: Vec<FilterOperator>,

    /// Whether complex filters with AND/OR are supported
    pub complex_filters_supported: bool,
}

/// Pagination support capabilities
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaginationCapabilities {
    /// Whether pagination is supported
    pub supported: bool,

    /// Default page size
    pub default_page_size: Option<usize>,

    /// Maximum page size allowed
    pub max_page_size: Option<usize>,

    /// Whether cursor-based pagination is supported
    pub cursor_based_supported: bool,
}

/// Authentication capabilities (typically configured rather than discovered)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthenticationCapabilities {
    /// Supported authentication schemes
    pub schemes: Vec<AuthenticationScheme>,

    /// Whether multi-factor authentication is supported
    pub mfa_supported: bool,

    /// Whether token refresh is supported
    pub token_refresh_supported: bool,
}

/// Extended capabilities specific to the provider implementation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtendedCapabilities {
    /// Whether ETag versioning is supported (always true - conditional operations are mandatory)
    pub etag_supported: bool,

    /// Whether PATCH operations are supported
    pub patch_supported: bool,

    /// Whether password change operations are supported
    pub change_password_supported: bool,

    /// Whether sorting is supported
    pub sort_supported: bool,

    /// Custom provider-specific capabilities
    pub custom_capabilities: HashMap<String, serde_json::Value>,
}

/// SCIM filter operators that can be supported
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum FilterOperator {
    /// Equal comparison
    #[serde(rename = "eq")]
    Equal,

    /// Not equal comparison
    #[serde(rename = "ne")]
    NotEqual,

    /// Contains operation for strings
    #[serde(rename = "co")]
    Contains,

    /// Starts with operation for strings
    #[serde(rename = "sw")]
    StartsWith,

    /// Ends with operation for strings
    #[serde(rename = "ew")]
    EndsWith,

    /// Present (attribute exists)
    #[serde(rename = "pr")]
    Present,

    /// Greater than
    #[serde(rename = "gt")]
    GreaterThan,

    /// Greater than or equal
    #[serde(rename = "ge")]
    GreaterThanOrEqual,

    /// Less than
    #[serde(rename = "lt")]
    LessThan,

    /// Less than or equal
    #[serde(rename = "le")]
    LessThanOrEqual,
}

/// Trait for providers that support capability introspection
pub trait CapabilityIntrospectable {
    /// Get provider-specific capability information that cannot be auto-discovered
    fn get_provider_specific_capabilities(&self) -> ExtendedCapabilities {
        ExtendedCapabilities::default()
    }

    /// Get bulk operation limits from the provider
    fn get_bulk_limits(&self) -> Option<BulkCapabilities> {
        None
    }

    /// Get pagination limits from the provider
    fn get_pagination_limits(&self) -> Option<PaginationCapabilities> {
        None
    }

    /// Get authentication capabilities (usually configured)
    fn get_authentication_capabilities(&self) -> Option<AuthenticationCapabilities> {
        None
    }
}

/// Automatic capability discovery engine that introspects server configuration
pub struct CapabilityDiscovery;

impl CapabilityDiscovery {
    /// Discover all provider capabilities from the current server state
    ///
    /// This method introspects the registered resource types, schemas, and provider
    /// implementation to automatically determine what capabilities are supported.
    pub fn discover_capabilities<P>(
        schema_registry: &SchemaRegistry,
        resource_handlers: &HashMap<String, std::sync::Arc<crate::resource::ResourceHandler>>,
        supported_operations: &HashMap<String, Vec<ScimOperation>>,
        _provider: &P,
    ) -> Result<ProviderCapabilities, ScimError>
    where
        P: ResourceProvider,
    {
        // Discover supported schemas from registry
        let supported_schemas = Self::discover_schemas(schema_registry);

        // Discover resource types from registered handlers
        let supported_resource_types = Self::discover_resource_types(resource_handlers);

        // Copy operation support directly from registration
        let supported_operations_map = supported_operations.clone();

        // Discover filtering capabilities from schema attributes
        let filter_capabilities =
            Self::discover_filter_capabilities(schema_registry, resource_handlers)?;

        // Use default capabilities for basic providers
        let bulk_capabilities = Self::default_bulk_capabilities();
        let pagination_capabilities = Self::default_pagination_capabilities();
        let authentication_capabilities = Self::default_authentication_capabilities();
        let mut extended_capabilities = ExtendedCapabilities::default();

        // Ensure ETag support is always enabled (conditional operations are mandatory)
        extended_capabilities.etag_supported = true;

        // Detect patch support from registered operations
        extended_capabilities.patch_supported = supported_operations
            .values()
            .any(|ops| ops.contains(&ScimOperation::Patch));

        Ok(ProviderCapabilities {
            supported_operations: supported_operations_map,
            supported_schemas,
            supported_resource_types,
            bulk_capabilities,
            filter_capabilities,
            pagination_capabilities,
            authentication_capabilities,
            extended_capabilities,
        })
    }

    /// Discover capabilities with provider introspection
    ///
    /// This version works with providers that implement CapabilityIntrospectable
    /// to get provider-specific capability information.
    pub fn discover_capabilities_with_introspection<P>(
        schema_registry: &SchemaRegistry,
        resource_handlers: &HashMap<String, std::sync::Arc<crate::resource::ResourceHandler>>,
        supported_operations: &HashMap<String, Vec<ScimOperation>>,
        provider: &P,
    ) -> Result<ProviderCapabilities, ScimError>
    where
        P: ResourceProvider + CapabilityIntrospectable,
    {
        // Discover supported schemas from registry
        let supported_schemas = Self::discover_schemas(schema_registry);

        // Discover resource types from registered handlers
        let supported_resource_types = Self::discover_resource_types(resource_handlers);

        // Copy operation support directly from registration
        let supported_operations_map = supported_operations.clone();

        // Discover filtering capabilities from schema attributes
        let filter_capabilities =
            Self::discover_filter_capabilities(schema_registry, resource_handlers)?;

        // Get provider-specific capabilities
        let bulk_capabilities = provider
            .get_bulk_limits()
            .unwrap_or_else(|| Self::default_bulk_capabilities());

        let pagination_capabilities = provider
            .get_pagination_limits()
            .unwrap_or_else(|| Self::default_pagination_capabilities());

        let authentication_capabilities = provider
            .get_authentication_capabilities()
            .unwrap_or_else(|| Self::default_authentication_capabilities());

        let extended_capabilities = provider.get_provider_specific_capabilities();

        Ok(ProviderCapabilities {
            supported_operations: supported_operations_map,
            supported_schemas,
            supported_resource_types,
            bulk_capabilities,
            filter_capabilities,
            pagination_capabilities,
            authentication_capabilities,
            extended_capabilities,
        })
    }

    /// Discover all registered schemas
    fn discover_schemas(schema_registry: &SchemaRegistry) -> Vec<String> {
        schema_registry
            .get_schemas()
            .iter()
            .map(|schema| schema.id.clone())
            .collect()
    }

    /// Discover registered resource types
    fn discover_resource_types(
        resource_handlers: &HashMap<String, std::sync::Arc<crate::resource::ResourceHandler>>,
    ) -> Vec<String> {
        resource_handlers.keys().cloned().collect()
    }

    /// Discover filtering capabilities from schema attribute definitions
    fn discover_filter_capabilities(
        schema_registry: &SchemaRegistry,
        resource_handlers: &HashMap<String, std::sync::Arc<crate::resource::ResourceHandler>>,
    ) -> Result<FilterCapabilities, ScimError> {
        let mut filterable_attributes = HashMap::new();

        // For each resource type, discover which attributes can be filtered
        for (resource_type, handler) in resource_handlers {
            // Get schema for this resource type
            if let Some(schema) = schema_registry.get_schema(&handler.schema.id) {
                // Recursively collect all filterable attributes including sub-attributes
                let attrs = Self::collect_filterable_attributes(&schema.attributes, "");
                filterable_attributes.insert(resource_type.clone(), attrs);
            }
        }

        // Determine supported operators based on attribute types
        let supported_operators = Self::determine_supported_operators(schema_registry);

        Ok(FilterCapabilities {
            supported: !filterable_attributes.is_empty(),
            max_results: Some(200), // Default SCIM recommendation
            filterable_attributes,
            supported_operators,
            complex_filters_supported: true, // Most implementations support AND/OR
        })
    }

    /// Determine if an attribute can be used in filters
    fn is_attribute_filterable(attr: &AttributeDefinition) -> bool {
        // Most simple attributes are filterable
        // Complex attributes and some special cases may not be
        match attr.data_type {
            crate::schema::AttributeType::Complex => false, // Complex attributes typically not directly filterable
            _ => true, // String, boolean, integer, decimal, dateTime, binary, reference are filterable
        }
    }

    /// Recursively collect filterable attributes from a schema
    fn collect_filterable_attributes(
        attributes: &[AttributeDefinition],
        prefix: &str,
    ) -> Vec<String> {
        let mut filterable = Vec::new();

        for attr in attributes {
            let attr_name = if prefix.is_empty() {
                attr.name.clone()
            } else {
                format!("{}.{}", prefix, attr.name)
            };

            if Self::is_attribute_filterable(attr) {
                filterable.push(attr_name.clone());
            }

            // Recursively check sub-attributes
            if !attr.sub_attributes.is_empty() {
                filterable.extend(Self::collect_filterable_attributes(
                    &attr.sub_attributes,
                    &attr_name,
                ));
            }
        }

        filterable
    }

    /// Determine which filter operators are supported based on schema attribute types
    fn determine_supported_operators(schema_registry: &SchemaRegistry) -> Vec<FilterOperator> {
        let mut operators = HashSet::new();

        // Basic operators always supported
        operators.insert(FilterOperator::Equal);
        operators.insert(FilterOperator::NotEqual);
        operators.insert(FilterOperator::Present);

        // Check if we have string attributes (enables string operations)
        if Self::has_string_attributes(schema_registry) {
            operators.insert(FilterOperator::Contains);
            operators.insert(FilterOperator::StartsWith);
            operators.insert(FilterOperator::EndsWith);
        }

        // Check if we have numeric/date attributes (enables comparison operations)
        if Self::has_comparable_attributes(schema_registry) {
            operators.insert(FilterOperator::GreaterThan);
            operators.insert(FilterOperator::GreaterThanOrEqual);
            operators.insert(FilterOperator::LessThan);
            operators.insert(FilterOperator::LessThanOrEqual);
        }

        operators.into_iter().collect()
    }

    /// Check if any registered schemas have string attributes
    fn has_string_attributes(schema_registry: &SchemaRegistry) -> bool {
        fn has_string_in_attributes(attributes: &[AttributeDefinition]) -> bool {
            attributes.iter().any(|attr| {
                matches!(attr.data_type, crate::schema::AttributeType::String)
                    || has_string_in_attributes(&attr.sub_attributes)
            })
        }

        schema_registry
            .get_schemas()
            .iter()
            .any(|schema| has_string_in_attributes(&schema.attributes))
    }

    /// Check if any registered schemas have comparable attributes (numeric, date)
    fn has_comparable_attributes(schema_registry: &SchemaRegistry) -> bool {
        fn has_comparable_in_attributes(attributes: &[AttributeDefinition]) -> bool {
            attributes.iter().any(|attr| {
                matches!(
                    attr.data_type,
                    crate::schema::AttributeType::Integer
                        | crate::schema::AttributeType::Decimal
                        | crate::schema::AttributeType::DateTime
                ) || has_comparable_in_attributes(&attr.sub_attributes)
            })
        }

        schema_registry
            .get_schemas()
            .iter()
            .any(|schema| has_comparable_in_attributes(&schema.attributes))
    }

    /// Default bulk capabilities for providers that don't specify them
    fn default_bulk_capabilities() -> BulkCapabilities {
        BulkCapabilities {
            supported: false, // Conservative default
            max_operations: None,
            max_payload_size: None,
            fail_on_errors_supported: false,
        }
    }

    /// Default pagination capabilities
    fn default_pagination_capabilities() -> PaginationCapabilities {
        PaginationCapabilities {
            supported: true, // Most providers support basic pagination
            default_page_size: Some(20),
            max_page_size: Some(200),
            cursor_based_supported: false, // Conservative default
        }
    }

    /// Default authentication capabilities
    fn default_authentication_capabilities() -> AuthenticationCapabilities {
        AuthenticationCapabilities {
            schemes: vec![], // Must be explicitly configured
            mfa_supported: false,
            token_refresh_supported: false,
        }
    }

    /// Generate RFC 7644 compliant ServiceProviderConfig from discovered capabilities
    pub fn generate_service_provider_config(
        capabilities: &ProviderCapabilities,
    ) -> ServiceProviderConfig {
        ServiceProviderConfig {
            patch_supported: capabilities.extended_capabilities.patch_supported,
            bulk_supported: capabilities.bulk_capabilities.supported,
            filter_supported: capabilities.filter_capabilities.supported,
            change_password_supported: capabilities.extended_capabilities.change_password_supported,
            sort_supported: capabilities.extended_capabilities.sort_supported,
            etag_supported: capabilities.extended_capabilities.etag_supported,
            authentication_schemes: capabilities.authentication_capabilities.schemes.clone(),
            bulk_max_operations: capabilities
                .bulk_capabilities
                .max_operations
                .map(|n| n as u32),
            bulk_max_payload_size: capabilities
                .bulk_capabilities
                .max_payload_size
                .map(|n| n as u64),
            filter_max_results: capabilities
                .filter_capabilities
                .max_results
                .map(|n| n as u32),
        }
    }
}

impl Default for BulkCapabilities {
    fn default() -> Self {
        Self {
            supported: false,
            max_operations: None,
            max_payload_size: None,
            fail_on_errors_supported: false,
        }
    }
}

impl Default for FilterCapabilities {
    fn default() -> Self {
        Self {
            supported: false,
            max_results: Some(200),
            filterable_attributes: HashMap::new(),
            supported_operators: vec![FilterOperator::Equal, FilterOperator::Present],
            complex_filters_supported: false,
        }
    }
}

impl Default for PaginationCapabilities {
    fn default() -> Self {
        Self {
            supported: true,
            default_page_size: Some(20),
            max_page_size: Some(200),
            cursor_based_supported: false,
        }
    }
}

impl Default for AuthenticationCapabilities {
    fn default() -> Self {
        Self {
            schemes: vec![],
            mfa_supported: false,
            token_refresh_supported: false,
        }
    }
}

impl Default for ExtendedCapabilities {
    fn default() -> Self {
        Self {
            etag_supported: true, // Always true - conditional operations are mandatory
            patch_supported: false,
            change_password_supported: false,
            sort_supported: false,
            custom_capabilities: HashMap::new(),
        }
    }
}

// Default implementation can be provided via a blanket impl, but users can override
// by implementing the trait directly on their provider types

#[cfg(test)]
mod tests {
    use super::*;
    use crate::schema::SchemaRegistry;
    use std::collections::HashMap;

    #[test]
    fn test_discover_schemas() {
        let registry = SchemaRegistry::new().expect("Failed to create schema registry");
        let schemas = CapabilityDiscovery::discover_schemas(&registry);

        assert!(!schemas.is_empty());
        assert!(schemas.contains(&"urn:ietf:params:scim:schemas:core:2.0:User".to_string()));
    }

    #[test]
    fn test_has_string_attributes() {
        let registry = SchemaRegistry::new().expect("Failed to create schema registry");
        assert!(CapabilityDiscovery::has_string_attributes(&registry));
    }

    #[test]
    fn test_has_comparable_attributes() {
        let registry = SchemaRegistry::new().expect("Failed to create schema registry");
        assert!(CapabilityDiscovery::has_comparable_attributes(&registry));
    }

    #[test]
    fn test_service_provider_config_generation() {
        let capabilities = ProviderCapabilities {
            supported_operations: HashMap::new(),
            supported_schemas: vec!["urn:ietf:params:scim:schemas:core:2.0:User".to_string()],
            supported_resource_types: vec!["User".to_string()],
            bulk_capabilities: BulkCapabilities {
                supported: true,
                max_operations: Some(100),
                max_payload_size: Some(1024 * 1024),
                fail_on_errors_supported: true,
            },
            filter_capabilities: FilterCapabilities::default(),
            pagination_capabilities: PaginationCapabilities::default(),
            authentication_capabilities: AuthenticationCapabilities::default(),
            extended_capabilities: ExtendedCapabilities {
                patch_supported: true,
                ..Default::default()
            },
        };

        let config = CapabilityDiscovery::generate_service_provider_config(&capabilities);

        assert!(config.bulk_supported);
        assert!(config.patch_supported);
        assert_eq!(config.bulk_max_operations, Some(100));
        assert_eq!(config.bulk_max_payload_size, Some(1024 * 1024));
    }

    #[test]
    fn test_filter_operators() {
        let registry = SchemaRegistry::new().expect("Failed to create schema registry");
        let operators = CapabilityDiscovery::determine_supported_operators(&registry);

        log::debug!("Discovered filter operators: {:?}", operators);

        // Should have basic operators
        assert!(operators.contains(&FilterOperator::Equal));
        assert!(operators.contains(&FilterOperator::Present));

        // Should have string operators since User schema has string attributes
        assert!(operators.contains(&FilterOperator::Contains));
        assert!(operators.contains(&FilterOperator::StartsWith));

        // Should have comparison operators since User schema has dateTime attributes (in sub-attributes)
        assert!(operators.contains(&FilterOperator::GreaterThan));
        assert!(operators.contains(&FilterOperator::LessThan));
    }
}