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
//! Resource provider trait for implementing SCIM data access.
//!
//! This module defines the core trait that users must implement to provide
//! data storage and retrieval for SCIM resources. Supports both single-tenant
//! and multi-tenant operations with automatic ETag concurrency control.
//!
//! # Key Types
//!
//! - [`ResourceProvider`] - Main trait for implementing storage backends
//! - [`ConditionalResult`] - Result type for conditional operations with version control
//!
//! # Examples
//!
//! ```rust
//! use scim_server::resource::ResourceProvider;
//!
//! struct MyProvider;
//! // Implement ResourceProvider for your storage backend
//! ```

use super::conditional_provider::VersionedResource;
use super::core::{ListQuery, RequestContext, Resource};
use super::version::{ConditionalResult, ScimVersion};
use serde_json::Value;
use std::future::Future;

/// Unified resource provider trait supporting both single and multi-tenant operations.
///
/// This trait provides a unified interface for SCIM resource operations that works
/// for both single-tenant and multi-tenant scenarios:
///
/// - **Single-tenant**: Operations use RequestContext with tenant_context = None
/// - **Multi-tenant**: Operations use RequestContext with tenant_context = Some(...)
///
/// The provider implementation can check `context.tenant_id()` to determine
/// the effective tenant for the operation.
pub trait ResourceProvider {
    /// Error type returned by all provider operations
    type Error: std::error::Error + Send + Sync + 'static;

    /// Create a resource for the tenant specified in the request context.
    ///
    /// # Arguments
    /// * `resource_type` - The type of resource to create (e.g., "User", "Group")
    /// * `data` - The resource data as JSON
    /// * `context` - Request context containing tenant information (if multi-tenant)
    ///
    /// # Returns
    /// The created resource with any server-generated fields (id, metadata, etc.)
    ///
    /// # Tenant Handling
    /// - Single-tenant: `context.tenant_id()` returns `None`
    /// - Multi-tenant: `context.tenant_id()` returns `Some(tenant_id)`
    fn create_resource(
        &self,
        resource_type: &str,
        data: Value,
        context: &RequestContext,
    ) -> impl Future<Output = Result<Resource, Self::Error>> + Send;

    /// Get a resource by ID from the tenant specified in the request context.
    ///
    /// # Arguments
    /// * `resource_type` - The type of resource to retrieve
    /// * `id` - The unique identifier of the resource
    /// * `context` - Request context containing tenant information (if multi-tenant)
    ///
    /// # Returns
    /// The resource if found, None if not found within the tenant scope
    fn get_resource(
        &self,
        resource_type: &str,
        id: &str,
        context: &RequestContext,
    ) -> impl Future<Output = Result<Option<Resource>, Self::Error>> + Send;

    /// Update a resource in the tenant specified in the request context.
    ///
    /// # Arguments
    /// * `resource_type` - The type of resource to update
    /// * `id` - The unique identifier of the resource
    /// * `data` - The updated resource data as JSON
    /// * `context` - Request context containing tenant information (if multi-tenant)
    ///
    /// # Returns
    /// The updated resource
    fn update_resource(
        &self,
        resource_type: &str,
        id: &str,
        data: Value,
        context: &RequestContext,
    ) -> impl Future<Output = Result<Resource, Self::Error>> + Send;

    /// Delete a resource from the tenant specified in the request context.
    ///
    /// # Arguments
    /// * `resource_type` - The type of resource to delete
    /// * `id` - The unique identifier of the resource
    /// * `context` - Request context containing tenant information (if multi-tenant)
    fn delete_resource(
        &self,
        resource_type: &str,
        id: &str,
        context: &RequestContext,
    ) -> impl Future<Output = Result<(), Self::Error>> + Send;

    /// List resources from the tenant specified in the request context.
    ///
    /// # Arguments
    /// * `resource_type` - The type of resources to list
    /// * `query` - Optional query parameters for filtering, sorting, pagination
    /// * `context` - Request context containing tenant information (if multi-tenant)
    ///
    /// # Returns
    /// A vector of resources from the specified tenant
    fn list_resources(
        &self,
        resource_type: &str,
        _query: Option<&ListQuery>,
        context: &RequestContext,
    ) -> impl Future<Output = Result<Vec<Resource>, Self::Error>> + Send;

    /// Find a resource by attribute value within the tenant specified in the request context.
    ///
    /// # Arguments
    /// * `resource_type` - The type of resource to search
    /// * `attribute` - The attribute name to search by
    /// * `value` - The attribute value to search for
    /// * `context` - Request context containing tenant information (if multi-tenant)
    ///
    /// # Returns
    /// The first matching resource, if found within the tenant scope
    fn find_resource_by_attribute(
        &self,
        resource_type: &str,
        attribute: &str,
        value: &Value,
        context: &RequestContext,
    ) -> impl Future<Output = Result<Option<Resource>, Self::Error>> + Send;

    /// Check if a resource exists within the tenant specified in the request context.
    ///
    /// # Arguments
    /// * `resource_type` - The type of resource to check
    /// * `id` - The unique identifier of the resource
    /// * `context` - Request context containing tenant information (if multi-tenant)
    ///
    /// # Returns
    /// True if the resource exists within the tenant scope, false otherwise
    fn resource_exists(
        &self,
        resource_type: &str,
        id: &str,
        context: &RequestContext,
    ) -> impl Future<Output = Result<bool, Self::Error>> + Send;

    /// Conditionally update a resource if the version matches.
    ///
    /// This operation will only succeed if the current resource version matches
    /// the expected version, preventing accidental overwriting of modified resources.
    /// This provides optimistic concurrency control for SCIM operations.
    ///
    /// # ETag Concurrency Control
    ///
    /// This method implements the core of ETag-based conditional operations:
    /// - Fetches the current resource and its version
    /// - Compares the current version with the expected version
    /// - Only proceeds with the update if versions match
    /// - Returns version conflict information if they don't match
    ///
    /// # Arguments
    /// * `resource_type` - The type of resource to update
    /// * `id` - The unique identifier of the resource
    /// * `data` - The updated resource data as JSON
    /// * `expected_version` - The version the client expects the resource to have
    /// * `context` - Request context containing tenant information
    ///
    /// # Returns
    /// * `Success(VersionedResource)` - Update succeeded with new version
    /// * `VersionMismatch(VersionConflict)` - Resource was modified by another client
    /// * `NotFound` - Resource does not exist
    ///
    /// # Default Implementation
    /// The default implementation provides automatic conditional update support
    /// by checking the current resource version before performing the update.
    /// Providers can override this for more efficient implementations that
    /// perform version checking at the storage layer.
    ///
    /// # Examples
    /// ```rust,no_run
    /// use scim_server::resource::{
    ///     provider::ResourceProvider,
    ///     version::{ScimVersion, ConditionalResult},
    ///     conditional_provider::VersionedResource,
    ///     RequestContext,
    /// };
    /// use serde_json::json;
    ///
    /// # async fn example<P: ResourceProvider + Sync>(provider: &P) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    /// let context = RequestContext::with_generated_id();
    /// let expected_version = ScimVersion::from_hash("abc123");
    /// let update_data = json!({"userName": "new.name", "active": false});
    ///
    /// match provider.conditional_update("User", "123", update_data, &expected_version, &context).await? {
    ///     ConditionalResult::Success(versioned_resource) => {
    ///         println!("Update successful, new version: {}",
    ///                 versioned_resource.version().to_http_header());
    ///     },
    ///     ConditionalResult::VersionMismatch(conflict) => {
    ///         println!("Version conflict: expected {}, current {}",
    ///                 conflict.expected, conflict.current);
    ///     },
    ///     ConditionalResult::NotFound => {
    ///         println!("Resource not found");
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    fn conditional_update(
        &self,
        resource_type: &str,
        id: &str,
        data: Value,
        expected_version: &ScimVersion,
        context: &RequestContext,
    ) -> impl Future<Output = Result<ConditionalResult<VersionedResource>, Self::Error>> + Send
    where
        Self: Sync,
    {
        async move {
            // Default implementation: get current resource, check version, then update
            match self.get_resource(resource_type, id, context).await? {
                Some(current_resource) => {
                    let current_versioned = VersionedResource::new(current_resource);
                    if current_versioned.version().matches(expected_version) {
                        let updated = self
                            .update_resource(resource_type, id, data, context)
                            .await?;
                        Ok(ConditionalResult::Success(VersionedResource::new(updated)))
                    } else {
                        Ok(ConditionalResult::VersionMismatch(
                            super::version::VersionConflict::standard_message(
                                expected_version.clone(),
                                current_versioned.version().clone(),
                            ),
                        ))
                    }
                }
                None => Ok(ConditionalResult::NotFound),
            }
        }
    }

    /// Conditionally delete a resource if the version matches.
    ///
    /// This operation will only succeed if the current resource version matches
    /// the expected version, preventing accidental deletion of modified resources.
    /// This is critical for maintaining data integrity in concurrent environments.
    ///
    /// # ETag Concurrency Control
    ///
    /// This method prevents accidental deletion of resources that have been
    /// modified by other clients:
    /// - Fetches the current resource and its version
    /// - Compares the current version with the expected version
    /// - Only proceeds with the deletion if versions match
    /// - Ensures the client is deleting the resource they intended to delete
    ///
    /// # Arguments
    /// * `resource_type` - The type of resource to delete
    /// * `id` - The unique identifier of the resource
    /// * `expected_version` - The version the client expects the resource to have
    /// * `context` - Request context containing tenant information
    ///
    /// # Returns
    /// * `Success(())` - Delete succeeded
    /// * `VersionMismatch(VersionConflict)` - Resource was modified by another client
    /// * `NotFound` - Resource does not exist
    ///
    /// # Default Implementation
    /// The default implementation provides automatic conditional delete support
    /// by checking the current resource version before performing the delete.
    /// Providers can override this for more efficient implementations that
    /// perform version checking at the storage layer.
    ///
    /// # Examples
    /// ```rust,no_run
    /// use scim_server::resource::{
    ///     provider::ResourceProvider,
    ///     version::{ScimVersion, ConditionalResult},
    ///     RequestContext,
    /// };
    ///
    /// # async fn example<P: ResourceProvider + Sync>(provider: &P) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    /// let context = RequestContext::with_generated_id();
    /// let expected_version = ScimVersion::from_hash("def456");
    ///
    /// match provider.conditional_delete("User", "123", &expected_version, &context).await? {
    ///     ConditionalResult::Success(()) => {
    ///         println!("User deleted successfully");
    ///     },
    ///     ConditionalResult::VersionMismatch(conflict) => {
    ///         println!("Cannot delete: resource was modified. Expected {}, current {}",
    ///                 conflict.expected, conflict.current);
    ///     },
    ///     ConditionalResult::NotFound => {
    ///         println!("User not found");
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    fn conditional_delete(
        &self,
        resource_type: &str,
        id: &str,
        expected_version: &ScimVersion,
        context: &RequestContext,
    ) -> impl Future<Output = Result<ConditionalResult<()>, Self::Error>> + Send
    where
        Self: Sync,
    {
        async move {
            // Default implementation: get current resource, check version, then delete
            match self.get_resource(resource_type, id, context).await? {
                Some(current_resource) => {
                    let current_versioned = VersionedResource::new(current_resource);
                    if current_versioned.version().matches(expected_version) {
                        self.delete_resource(resource_type, id, context).await?;
                        Ok(ConditionalResult::Success(()))
                    } else {
                        Ok(ConditionalResult::VersionMismatch(
                            super::version::VersionConflict::standard_message(
                                expected_version.clone(),
                                current_versioned.version().clone(),
                            ),
                        ))
                    }
                }
                None => Ok(ConditionalResult::NotFound),
            }
        }
    }

    /// Get a resource with its version information.
    ///
    /// This is a convenience method that returns both the resource and its version
    /// information wrapped in a [`VersionedResource`]. This is useful when you need
    /// both the resource data and its version for subsequent conditional operations.
    ///
    /// The default implementation calls the existing `get_resource` method and
    /// automatically wraps the result in a `VersionedResource` with a computed version.
    ///
    /// # Arguments
    /// * `resource_type` - The type of resource to retrieve
    /// * `id` - The unique identifier of the resource
    /// * `context` - Request context containing tenant information
    ///
    /// # Returns
    /// The versioned resource if found, `None` if not found
    ///
    /// # Examples
    /// ```rust,no_run
    /// use scim_server::resource::{
    ///     provider::ResourceProvider,
    ///     RequestContext,
    /// };
    ///
    /// # async fn example<P: ResourceProvider + Sync>(provider: &P) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    /// let context = RequestContext::with_generated_id();
    ///
    /// if let Some(versioned_resource) = provider.get_versioned_resource("User", "123", &context).await? {
    ///     println!("Resource ID: {}", versioned_resource.resource().get_id().unwrap_or("unknown"));
    ///     println!("Resource version: {}", versioned_resource.version().to_http_header());
    ///
    ///     // Can use the version for subsequent conditional operations
    ///     let current_version = versioned_resource.version().clone();
    ///     // ... use current_version for conditional_update or conditional_delete
    /// }
    /// # Ok(())
    /// # }
    /// ```
    fn get_versioned_resource(
        &self,
        resource_type: &str,
        id: &str,
        context: &RequestContext,
    ) -> impl Future<Output = Result<Option<VersionedResource>, Self::Error>> + Send
    where
        Self: Sync,
    {
        async move {
            match self.get_resource(resource_type, id, context).await? {
                Some(resource) => Ok(Some(VersionedResource::new(resource))),
                None => Ok(None),
            }
        }
    }

    /// Apply PATCH operations to a resource within the tenant specified in the request context.
    ///
    /// # Arguments
    /// * `resource_type` - The type of resource to patch
    /// * `id` - The unique identifier of the resource
    /// * `patch_request` - The PATCH operation request as JSON (RFC 7644 Section 3.5.2)
    /// * `context` - Request context containing tenant information (if multi-tenant)
    ///
    /// # Returns
    /// The updated resource after applying the patch operations
    ///
    /// # PATCH Operations
    /// Supports the three SCIM PATCH operations:
    /// - `add` - Add new attribute values
    /// - `remove` - Remove attribute values
    /// - `replace` - Replace existing attribute values
    ///
    /// # Default Implementation
    /// The default implementation provides basic PATCH operation support by:
    /// 1. Fetching the current resource
    /// 2. Applying each operation in sequence
    /// 3. Updating the resource with the modified data
    fn patch_resource(
        &self,
        resource_type: &str,
        id: &str,
        patch_request: &Value,
        context: &RequestContext,
    ) -> impl Future<Output = Result<Resource, Self::Error>> + Send
    where
        Self: Sync,
    {
        async move {
            // Get the current resource
            let current = self
                .get_resource(resource_type, id, context)
                .await?
                .ok_or_else(|| {
                    // This will need to be converted to the provider's error type
                    // For now, we'll use a placeholder that will be handled by implementers
                    // In practice, providers should define their own NotFound error variant
                    unreachable!("Resource not found - providers must handle this case")
                })?;

            // Extract operations from patch request
            let operations = patch_request
                .get("Operations")
                .and_then(|ops| ops.as_array())
                .ok_or_else(|| {
                    unreachable!("Invalid patch request - providers must handle this case")
                })?;

            // Apply operations to create modified resource data
            let mut modified_data = current.to_json().map_err(|_| {
                unreachable!("Failed to serialize resource - providers must handle this case")
            })?;

            for operation in operations {
                self.apply_patch_operation(&mut modified_data, operation)?;
            }

            // Update the resource with modified data
            self.update_resource(resource_type, id, modified_data, context)
                .await
        }
    }

    /// Apply a single PATCH operation to resource data.
    ///
    /// This is a helper method used by the default patch_resource implementation.
    /// Providers can override this method to customize patch operation behavior.
    ///
    /// # Arguments
    /// * `resource_data` - Mutable reference to the resource JSON data
    /// * `operation` - The patch operation to apply
    ///
    /// # Returns
    /// Result indicating success or failure of the operation
    fn apply_patch_operation(
        &self,
        _resource_data: &mut Value,
        _operation: &Value,
    ) -> Result<(), Self::Error> {
        // This is a simplified implementation that providers should override
        // with proper SCIM PATCH semantics
        // Default implementation is intentionally minimal
        Ok(())
    }
}

/// Extension trait providing convenience methods for common provider operations.
///
/// This trait automatically implements ergonomic helper methods for both single-tenant
/// and multi-tenant scenarios on any type that implements ResourceProvider.
pub trait ResourceProviderExt: ResourceProvider {
    /// Convenience method for single-tenant resource creation.
    ///
    /// Creates a RequestContext with no tenant information and calls create_resource.
    fn create_single_tenant(
        &self,
        resource_type: &str,
        data: Value,
        request_id: Option<String>,
    ) -> impl Future<Output = Result<Resource, Self::Error>> + Send
    where
        Self: Sync,
    {
        async move {
            let context = match request_id {
                Some(id) => RequestContext::new(id),
                None => RequestContext::with_generated_id(),
            };
            self.create_resource(resource_type, data, &context).await
        }
    }

    /// Convenience method for multi-tenant resource creation.
    ///
    /// Creates a RequestContext with the specified tenant and calls create_resource.
    fn create_multi_tenant(
        &self,
        tenant_id: &str,
        resource_type: &str,
        data: Value,
        request_id: Option<String>,
    ) -> impl Future<Output = Result<Resource, Self::Error>> + Send
    where
        Self: Sync,
    {
        async move {
            use super::core::TenantContext;

            let tenant_context = TenantContext {
                tenant_id: tenant_id.to_string(),
                client_id: "default-client".to_string(),
                permissions: Default::default(),
                isolation_level: Default::default(),
            };

            let context = match request_id {
                Some(id) => RequestContext::with_tenant(id, tenant_context),
                None => RequestContext::with_tenant_generated_id(tenant_context),
            };

            self.create_resource(resource_type, data, &context).await
        }
    }

    /// Convenience method for single-tenant resource retrieval.
    fn get_single_tenant(
        &self,
        resource_type: &str,
        id: &str,
        request_id: Option<String>,
    ) -> impl Future<Output = Result<Option<Resource>, Self::Error>> + Send
    where
        Self: Sync,
    {
        async move {
            let context = match request_id {
                Some(req_id) => RequestContext::new(req_id),
                None => RequestContext::with_generated_id(),
            };
            self.get_resource(resource_type, id, &context).await
        }
    }

    /// Convenience method for multi-tenant resource retrieval.
    fn get_multi_tenant(
        &self,
        tenant_id: &str,
        resource_type: &str,
        id: &str,
        request_id: Option<String>,
    ) -> impl Future<Output = Result<Option<Resource>, Self::Error>> + Send
    where
        Self: Sync,
    {
        async move {
            use super::core::TenantContext;

            let tenant_context = TenantContext {
                tenant_id: tenant_id.to_string(),
                client_id: "default-client".to_string(),
                permissions: Default::default(),
                isolation_level: Default::default(),
            };

            let context = match request_id {
                Some(req_id) => RequestContext::with_tenant(req_id, tenant_context),
                None => RequestContext::with_tenant_generated_id(tenant_context),
            };

            self.get_resource(resource_type, id, &context).await
        }
    }
}

/// Blanket implementation of ResourceProviderExt for all types implementing ResourceProvider.
impl<T: ResourceProvider> ResourceProviderExt for T {}