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
//! Storage abstraction layer for SCIM resources.
//!
//! This module provides a clean separation between storage concerns and SCIM protocol logic.
//! The `StorageProvider` trait defines pure data storage operations that are protocol-agnostic,
//! allowing for pluggable storage backends while keeping SCIM-specific logic in the provider layer.
//!
//! # Architecture
//!
//! The storage layer is responsible for:
//! - Pure PUT/GET/DELETE operations on JSON data
//! - Tenant isolation and data organization
//! - Basic querying and filtering
//! - Data persistence and retrieval
//!
//! The storage layer is NOT responsible for:
//! - SCIM metadata generation (timestamps, versions, etc.)
//! - SCIM validation rules
//! - Business logic (limits, permissions, etc.)
//! - Protocol-specific transformations
//!
//! # Design Philosophy
//!
//! This interface follows the principle that at the storage level, CREATE and UPDATE are
//! the same operation - you're just putting data at a location. The distinction between
//! "create" vs "update" is business logic that belongs in the SCIM provider layer.
//!
//! # Example Usage
//!
//! ```rust
//! use scim_server::storage::{StorageProvider, StorageKey, InMemoryStorage};
//! use serde_json::json;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let storage = InMemoryStorage::new();
//!
//! // Put a resource (works for both create and update)
//! let key = StorageKey::new("tenant1", "User", "123");
//! let user_data = json!({
//!     "id": "123",
//!     "userName": "john.doe",
//!     "displayName": "John Doe"
//! });
//! let stored_data = storage.put(key.clone(), user_data).await?;
//!
//! // Get the resource
//! let retrieved = storage.get(key.clone()).await?;
//! assert!(retrieved.is_some());
//!
//! // Delete the resource
//! let was_deleted = storage.delete(key).await?;
//! assert!(was_deleted);
//! # Ok(())
//! # }
//! ```

pub mod errors;
pub mod in_memory;
pub mod sqlite;

#[cfg(test)]
pub mod tests;

pub use errors::StorageError;
pub use in_memory::InMemoryStorage;
pub use sqlite::SqliteStorage;

use serde_json::Value;
use std::fmt;
use std::future::Future;

/// Statistics about storage usage.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StorageStats {
    /// Number of tenants with data
    pub tenant_count: usize,
    /// Number of resource types across all tenants
    pub resource_type_count: usize,
    /// Total number of individual resources
    pub total_resources: usize,
}

/// Statistics about provider usage (moved from obsolete in_memory module).
///
/// This provides metrics about resource counts, tenants, and resource types
/// for monitoring and debugging purposes.
#[derive(Debug, Clone)]
pub struct ProviderStats {
    /// Number of active tenants in the provider
    pub tenant_count: usize,
    /// Total number of resources across all tenants
    pub total_resources: usize,
    /// Number of distinct resource types
    pub resource_type_count: usize,
    /// List of resource type names
    pub resource_types: Vec<String>,
}

impl ProviderStats {
    /// Create new empty statistics.
    pub fn new() -> Self {
        Self {
            tenant_count: 0,
            total_resources: 0,
            resource_type_count: 0,
            resource_types: Vec::new(),
        }
    }

    /// Check if the provider is empty (no resources).
    pub fn is_empty(&self) -> bool {
        self.total_resources == 0
    }
}

impl Default for ProviderStats {
    fn default() -> Self {
        Self::new()
    }
}

/// A hierarchical key for identifying resources in storage.
///
/// Resources are organized as: `tenant_id` → `resource_type` → `resource_id`
/// This provides natural tenant isolation and efficient querying.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StorageKey {
    tenant_id: String,
    resource_type: String,
    resource_id: String,
}

impl StorageKey {
    /// Create a new storage key.
    pub fn new(
        tenant_id: impl Into<String>,
        resource_type: impl Into<String>,
        resource_id: impl Into<String>,
    ) -> Self {
        Self {
            tenant_id: tenant_id.into(),
            resource_type: resource_type.into(),
            resource_id: resource_id.into(),
        }
    }

    /// Get the tenant ID.
    pub fn tenant_id(&self) -> &str {
        &self.tenant_id
    }

    /// Get the resource type.
    pub fn resource_type(&self) -> &str {
        &self.resource_type
    }

    /// Get the resource ID.
    pub fn resource_id(&self) -> &str {
        &self.resource_id
    }

    /// Create a prefix key for listing resources of a type within a tenant.
    pub fn prefix(tenant_id: impl Into<String>, resource_type: impl Into<String>) -> StoragePrefix {
        StoragePrefix {
            tenant_id: tenant_id.into(),
            resource_type: resource_type.into(),
        }
    }
}

impl fmt::Display for StorageKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}/{}/{}",
            self.tenant_id, self.resource_type, self.resource_id
        )
    }
}

/// A prefix for querying resources by tenant and type.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StoragePrefix {
    tenant_id: String,
    resource_type: String,
}

impl StoragePrefix {
    /// Get the tenant ID.
    pub fn tenant_id(&self) -> &str {
        &self.tenant_id
    }

    /// Get the resource type.
    pub fn resource_type(&self) -> &str {
        &self.resource_type
    }
}

impl fmt::Display for StoragePrefix {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}/{}", self.tenant_id, self.resource_type)
    }
}

/// Core trait for storage providers that handle pure data persistence operations.
///
/// This trait defines a protocol-agnostic interface for storing and retrieving JSON data
/// with tenant isolation. Implementations should focus solely on data persistence and
/// retrieval without any SCIM-specific logic.
///
/// # Design Principles
///
/// - **PUT/GET/DELETE Model**: Simple, fundamental operations
/// - **PUT Returns Data**: Supports SCIM requirement to return resource state after operations
/// - **DELETE Returns Boolean**: Indicates whether resource existed (for proper HTTP status codes)
/// - **Tenant Isolation**: All operations are scoped to a specific tenant via StorageKey
/// - **Protocol Agnostic**: No awareness of SCIM structures or semantics
/// - **Async First**: All operations return futures for scalability
/// - **Error Transparency**: Storage errors are clearly separated from protocol errors
///
/// # Key Design Decisions
///
/// - **No separate CREATE/UPDATE**: Both are just PUT operations. Business logic determines
///   whether this should be treated as create vs update.
/// - **PUT returns stored data**: This enables SCIM providers to return the complete resource
///   state after modifications without a separate GET call.
/// - **DELETE returns boolean**: Allows proper HTTP status code handling (204 vs 404).
pub trait StorageProvider: Send + Sync {
    /// The error type returned by storage operations.
    type Error: std::error::Error + Send + Sync + 'static;

    /// Store data at the specified key and return the stored data.
    ///
    /// # Arguments
    /// * `key` - The storage key identifying the resource location
    /// * `data` - The JSON data to store
    ///
    /// # Returns
    /// The data that was actually stored (may include storage-level metadata).
    ///
    /// # Behavior
    /// - If a resource with the same key already exists, it is completely replaced
    /// - The storage implementation should ensure atomic operations where possible
    /// - No validation is performed on the data structure
    /// - The returned data should be exactly what would be retrieved by `get()`
    fn put(
        &self,
        key: StorageKey,
        data: Value,
    ) -> impl Future<Output = Result<Value, Self::Error>> + Send;

    /// Retrieve data by key.
    ///
    /// # Arguments
    /// * `key` - The storage key identifying the resource
    ///
    /// # Returns
    /// `Some(data)` if the resource exists, `None` if it doesn't exist.
    fn get(
        &self,
        key: StorageKey,
    ) -> impl Future<Output = Result<Option<Value>, Self::Error>> + Send;

    /// Delete data by key.
    ///
    /// # Arguments
    /// * `key` - The storage key identifying the resource
    ///
    /// # Returns
    /// `true` if the resource was deleted, `false` if it didn't exist.
    ///
    /// # Note
    /// This follows SCIM/HTTP semantics where DELETE operations don't return resource data.
    /// The boolean return value allows proper HTTP status code selection (204 vs 404).
    fn delete(&self, key: StorageKey) -> impl Future<Output = Result<bool, Self::Error>> + Send;

    /// List resources matching a prefix with pagination.
    ///
    /// # Arguments
    /// * `prefix` - The storage prefix (tenant + resource type)
    /// * `offset` - The number of resources to skip (0-based)
    /// * `limit` - The maximum number of resources to return
    ///
    /// # Returns
    /// A vector of (key, data) pairs.
    ///
    /// # Behavior
    /// - Results should be consistently ordered (e.g., by resource ID)
    /// - If `offset` exceeds the total count, an empty vector should be returned
    /// - If `limit` is 0, an empty vector should be returned
    fn list(
        &self,
        prefix: StoragePrefix,
        offset: usize,
        limit: usize,
    ) -> impl Future<Output = Result<Vec<(StorageKey, Value)>, Self::Error>> + Send;

    /// Find resources by a specific attribute value.
    ///
    /// # Arguments
    /// * `prefix` - The storage prefix (tenant + resource type)
    /// * `attribute` - The JSON path of the attribute to search (e.g., "userName", "emails.0.value")
    /// * `value` - The exact value to match
    ///
    /// # Returns
    /// A vector of (key, data) pairs for matching resources.
    ///
    /// # Behavior
    /// - Performs exact string matching on the specified attribute
    /// - Supports nested attributes using dot notation
    /// - Returns all matching resources (no pagination)
    /// - Empty vector if no matches found
    fn find_by_attribute(
        &self,
        prefix: StoragePrefix,
        attribute: &str,
        value: &str,
    ) -> impl Future<Output = Result<Vec<(StorageKey, Value)>, Self::Error>> + Send;

    /// Check if a resource exists.
    ///
    /// # Arguments
    /// * `key` - The storage key identifying the resource
    ///
    /// # Returns
    /// `true` if the resource exists, `false` if it doesn't.
    ///
    /// # Performance Note
    /// This should be more efficient than `get()` as it doesn't need to return data.
    fn exists(&self, key: StorageKey) -> impl Future<Output = Result<bool, Self::Error>> + Send;

    /// Count the total number of resources matching a prefix.
    ///
    /// # Arguments
    /// * `prefix` - The storage prefix (tenant + resource type)
    ///
    /// # Returns
    /// The total count of matching resources.
    fn count(
        &self,
        prefix: StoragePrefix,
    ) -> impl Future<Output = Result<usize, Self::Error>> + Send;

    /// List all tenant IDs that currently have data in storage.
    ///
    /// Returns tenant IDs for all tenants that contain at least one resource of any type.
    /// This method enables dynamic tenant discovery without requiring hardcoded tenant patterns.
    ///
    /// # Returns
    ///
    /// A vector of tenant ID strings. Empty vector if no tenants have data.
    ///
    /// # Errors
    ///
    /// Returns storage-specific errors if the discovery operation fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scim_server::storage::{StorageProvider, InMemoryStorage};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let storage = InMemoryStorage::new();
    /// let tenants = storage.list_tenants().await?;
    /// println!("Found {} tenants", tenants.len());
    /// # Ok(())
    /// # }
    /// ```
    fn list_tenants(&self) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send;

    /// List all resource types for a specific tenant.
    ///
    /// Returns resource type names (e.g., "User", "Group") that exist within the specified
    /// tenant. Only resource types with at least one stored resource are included.
    ///
    /// # Arguments
    ///
    /// * `tenant_id` - The tenant ID to query for resource types
    ///
    /// # Returns
    ///
    /// A vector of resource type strings. Empty vector if tenant doesn't exist or has no resources.
    ///
    /// # Errors
    ///
    /// Returns storage-specific errors if the query operation fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scim_server::storage::{StorageProvider, InMemoryStorage};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let storage = InMemoryStorage::new();
    /// let types = storage.list_resource_types("tenant1").await?;
    /// for resource_type in types {
    ///     println!("Tenant has resource type: {}", resource_type);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    fn list_resource_types(
        &self,
        tenant_id: &str,
    ) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send;

    /// List all resource types across all tenants.
    ///
    /// Returns a deduplicated collection of all resource type names found across all tenants
    /// in storage. This provides a global view of resource types without tenant boundaries.
    ///
    /// # Returns
    ///
    /// A vector of unique resource type strings. Empty vector if no resources exist.
    ///
    /// # Errors
    ///
    /// Returns storage-specific errors if the discovery operation fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scim_server::storage::{StorageProvider, InMemoryStorage};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let storage = InMemoryStorage::new();
    /// let all_types = storage.list_all_resource_types().await?;
    /// println!("System supports {} resource types", all_types.len());
    /// # Ok(())
    /// # }
    /// ```
    fn list_all_resource_types(
        &self,
    ) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send;

    /// Clear all data from storage.
    ///
    /// Removes all resources from all tenants, effectively resetting the storage to an empty state.
    /// This operation is primarily intended for testing scenarios and should be used with caution
    /// in production environments.
    ///
    /// # Returns
    ///
    /// `Ok(())` on successful clearing, or a storage-specific error on failure.
    ///
    /// # Errors
    ///
    /// Returns storage-specific errors if the clear operation fails partially or completely.
    ///
    /// # Behavior
    ///
    /// - Removes all resources from all tenants atomically where possible
    /// - After successful clearing, [`list_tenants`] should return an empty vector
    /// - Primarily intended for testing scenarios
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scim_server::storage::{StorageProvider, InMemoryStorage};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let storage = InMemoryStorage::new();
    /// // ... populate storage with data ...
    /// storage.clear().await?;
    /// let tenants = storage.list_tenants().await?;
    /// assert_eq!(tenants.len(), 0);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [`list_tenants`]: Self::list_tenants
    fn clear(&self) -> impl Future<Output = Result<(), Self::Error>> + Send;

    /// Get storage statistics for debugging and monitoring.
    ///
    /// Returns statistics about storage usage including tenant count, resource type count,
    /// and total number of resources across all tenants.
    ///
    /// # Returns
    ///
    /// A `StorageStats` struct containing usage metrics.
    ///
    /// # Errors
    ///
    /// Returns storage-specific errors if the stats collection operation fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scim_server::storage::{StorageProvider, InMemoryStorage};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let storage = InMemoryStorage::new();
    /// let stats = storage.stats().await?;
    /// println!("Total resources: {}", stats.total_resources);
    /// # Ok(())
    /// # }
    /// ```
    fn stats(&self) -> impl Future<Output = Result<StorageStats, Self::Error>> + Send;
}