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
//! Test fixtures for loading and managing test data files.
//!
//! This module provides utilities for loading test schemas and resources
//! from JSON files, with caching for performance.

use serde_json::Value;
use std::collections::HashMap;
use std::sync::OnceLock;

/// Cache for loaded fixtures to avoid repeated file I/O
static FIXTURE_CACHE: OnceLock<HashMap<String, Value>> = OnceLock::new();

/// Load a fixture from the fixtures directory with caching
pub fn load_fixture(path: &str) -> Value {
    let cache = FIXTURE_CACHE.get_or_init(|| HashMap::new());

    // Check cache first
    if let Some(cached) = cache.get(path) {
        return cached.clone();
    }

    // Load from file if not cached
    let fixture_path = format!("tests/fixtures/{}", path);
    let content = std::fs::read_to_string(&fixture_path)
        .unwrap_or_else(|_| panic!("Failed to load fixture: {}", fixture_path));

    serde_json::from_str(&content)
        .unwrap_or_else(|_| panic!("Failed to parse JSON fixture: {}", fixture_path))
}

/// Load a schema fixture
pub fn load_schema(name: &str) -> Value {
    load_fixture(&format!("schemas/{}.json", name))
}

/// Load a valid resource fixture
pub fn load_valid_resource(name: &str) -> Value {
    load_fixture(&format!("resources/valid/{}.json", name))
}

/// Load an invalid resource fixture
pub fn load_invalid_resource(name: &str) -> Value {
    load_fixture(&format!("resources/invalid/{}.json", name))
}

/// RFC 7643 examples as constants for easy reference
pub mod rfc_examples {
    use serde_json::{Value, json};

    /// RFC 7643 Section 8.1 - Minimal User representation
    pub fn user_minimal() -> Value {
        json!({
            "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
            "id": "2819c223-7f76-453a-919d-413861904646",
            "userName": "bjensen@example.com",
            "meta": {
                "resourceType": "User",
                "created": "2010-01-23T04:56:22Z",
                "lastModified": "2011-05-13T04:42:34Z",
                "version": "3694e05e9dff590",
                "location": "https://example.com/v2/Users/2819c223-7f76-453a-919d-413861904646"
            }
        })
    }

    /// RFC 7643 Section 8.2 - Full User representation
    pub fn user_full() -> Value {
        json!({
            "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
            "id": "2819c223-7f76-453a-919d-413861904646",
            "externalId": "701984",
            "userName": "bjensen@example.com",
            "name": {
                "formatted": "Ms. Barbara J Jensen, III",
                "familyName": "Jensen",
                "givenName": "Barbara",
                "middleName": "Jane",
                "honorificPrefix": "Ms.",
                "honorificSuffix": "III"
            },
            "displayName": "Babs Jensen",
            "nickName": "Babs",
            "profileUrl": "https://login.example.com/bjensen",
            "emails": [
                {
                    "value": "bjensen@example.com",
                    "type": "work",
                    "primary": true
                },
                {
                    "value": "babs@jensen.org",
                    "type": "home"
                }
            ],
            "addresses": [
                {
                    "type": "work",
                    "streetAddress": "100 Universal City Plaza",
                    "locality": "Hollywood",
                    "region": "CA",
                    "postalCode": "91608",
                    "country": "USA",
                    "formatted": "100 Universal City Plaza\nHollywood, CA 91608 USA",
                    "primary": true
                },
                {
                    "type": "home",
                    "streetAddress": "456 Hollywood Blvd",
                    "locality": "Hollywood",
                    "region": "CA",
                    "postalCode": "91608",
                    "country": "USA",
                    "formatted": "456 Hollywood Blvd\nHollywood, CA 91608 USA"
                }
            ],
            "phoneNumbers": [
                {
                    "value": "555-555-5555",
                    "type": "work"
                },
                {
                    "value": "555-555-4444",
                    "type": "mobile"
                }
            ],
            "ims": [
                {
                    "value": "someaimhandle",
                    "type": "aim"
                }
            ],
            "photos": [
                {
                    "value": "https://photos.example.com/profilephoto/72930000000Ccne/F",
                    "type": "photo"
                },
                {
                    "value": "https://photos.example.com/profilephoto/72930000000Ccne/T",
                    "type": "thumbnail"
                }
            ],
            "userType": "Employee",
            "title": "Tour Guide",
            "preferredLanguage": "en-US",
            "locale": "en-US",
            "timezone": "America/Los_Angeles",
            "active": true,
            "password": "t1meMa$heen",
            "groups": [
                {
                    "value": "e9e30dba-f08f-4109-8486-d5c6a331660a",
                    "$ref": "https://example.com/v2/Groups/e9e30dba-f08f-4109-8486-d5c6a331660a",
                    "display": "Tour Guides"
                },
                {
                    "value": "fc348aa8-3835-40eb-a20b-c726e15c55b5",
                    "$ref": "https://example.com/v2/Groups/fc348aa8-3835-40eb-a20b-c726e15c55b5",
                    "display": "Employees"
                },
                {
                    "value": "71ddacd2-a8e7-49b8-a5db-ae50d0a5bfd7",
                    "$ref": "https://example.com/v2/Groups/71ddacd2-a8e7-49b8-a5db-ae50d0a5bfd7",
                    "display": "US Employees"
                }
            ],
            "x509Certificates": [
                {
                    "value": "MIIDQzCC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B/A67g42/vzgAtoRUeDov1+GFiBZ+GNF/cAYKcMtGcrs2i97ZkJMo="
                }
            ],
            "meta": {
                "resourceType": "User",
                "created": "2010-01-23T04:56:22Z",
                "lastModified": "2011-05-13T04:42:34Z",
                "version": "a330bc54f0671c9",
                "location": "https://example.com/v2/Users/2819c223-7f76-453a-919d-413861904646"
            }
        })
    }

    /// RFC 7643 Section 8.3 - Enterprise User extension representation
    pub fn user_enterprise() -> Value {
        let mut user = user_full();

        // Add enterprise extension to schemas
        user["schemas"] = json!([
            "urn:ietf:params:scim:schemas:core:2.0:User",
            "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
        ]);

        // Add enterprise extension attributes
        user["urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"] = json!({
            "employeeNumber": "701984",
            "costCenter": "4130",
            "organization": "Universal Studios",
            "division": "Theme Park",
            "department": "Tour Operations",
            "manager": {
                "value": "26118915-6090-4610-87e4-49d8ca9f808d",
                "$ref": "../Users/26118915-6090-4610-87e4-49d8ca9f808d",
                "displayName": "John Smith"
            }
        });

        user
    }

    /// RFC 7643 Section 8.4 - Group representation
    pub fn group_basic() -> Value {
        json!({
            "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
            "id": "e9e30dba-f08f-4109-8486-d5c6a331660a",
            "displayName": "Tour Guides",
            "members": [
                {
                    "value": "2819c223-7f76-453a-919d-413861904646",
                    "$ref": "https://example.com/v2/Users/2819c223-7f76-453a-919d-413861904646",
                    "display": "Babs Jensen"
                },
                {
                    "value": "902c246b-6245-4190-8e05-00816be7344a",
                    "$ref": "https://example.com/v2/Users/902c246b-6245-4190-8e05-00816be7344a",
                    "display": "Mandy Pepperidge"
                }
            ],
            "meta": {
                "resourceType": "Group",
                "created": "2010-01-23T04:56:22Z",
                "lastModified": "2011-05-13T04:42:34Z",
                "version": "3694e05e9dff592",
                "location": "https://example.com/v2/Groups/e9e30dba-f08f-4109-8486-d5c6a331660a"
            }
        })
    }
}

/// Test-specific fixtures for systematic validation testing
pub mod test_fixtures {
    use serde_json::{Value, json};

    /// Basic test extension schema for testing extension validation
    pub fn test_extension_schema() -> Value {
        json!({
            "id": "urn:example:params:scim:schemas:extension:test:2.0:User",
            "name": "TestExtension",
            "description": "Test extension for validation testing",
            "attributes": [
                {
                    "name": "testString",
                    "type": "string",
                    "multiValued": false,
                    "required": false,
                    "caseExact": false,
                    "mutability": "readWrite",
                    "returned": "default",
                    "uniqueness": "none"
                },
                {
                    "name": "testRequired",
                    "type": "string",
                    "multiValued": false,
                    "required": true,
                    "caseExact": false,
                    "mutability": "readWrite",
                    "returned": "default",
                    "uniqueness": "none"
                },
                {
                    "name": "testReadOnly",
                    "type": "string",
                    "multiValued": false,
                    "required": false,
                    "caseExact": false,
                    "mutability": "readOnly",
                    "returned": "default",
                    "uniqueness": "none"
                }
            ]
        })
    }

    /// Intentionally malformed schema for testing schema validation
    pub fn invalid_schema() -> Value {
        json!({
            "id": "not-a-valid-uri",
            "name": 123, // Should be string
            "attributes": "not-an-array" // Should be array
        })
    }

    /// Minimal valid schemas for testing
    pub fn minimal_user_schema() -> Value {
        json!({
            "id": "urn:ietf:params:scim:schemas:core:2.0:User",
            "name": "User",
            "description": "User Account",
            "attributes": [
                {
                    "name": "userName",
                    "type": "string",
                    "multiValued": false,
                    "required": true,
                    "caseExact": false,
                    "mutability": "readWrite",
                    "returned": "default",
                    "uniqueness": "server"
                }
            ]
        })
    }

    pub fn minimal_group_schema() -> Value {
        json!({
            "id": "urn:ietf:params:scim:schemas:core:2.0:Group",
            "name": "Group",
            "description": "Group",
            "attributes": [
                {
                    "name": "displayName",
                    "type": "string",
                    "multiValued": false,
                    "required": true,
                    "caseExact": false,
                    "mutability": "readWrite",
                    "returned": "default",
                    "uniqueness": "none"
                }
            ]
        })
    }
}

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

    #[test]
    fn test_rfc_examples_user_minimal() {
        let user = rfc_examples::user_minimal();
        assert_eq!(
            user["schemas"][0],
            "urn:ietf:params:scim:schemas:core:2.0:User"
        );
        assert_eq!(user["userName"], "bjensen@example.com");
        assert!(!user["id"].as_str().unwrap().is_empty());
    }

    #[test]
    fn test_rfc_examples_user_full() {
        let user = rfc_examples::user_full();
        assert_eq!(
            user["schemas"][0],
            "urn:ietf:params:scim:schemas:core:2.0:User"
        );
        assert_eq!(user["displayName"], "Babs Jensen");
        assert!(user["emails"].is_array());
        assert_eq!(user["emails"][0]["primary"], true);
    }

    #[test]
    fn test_rfc_examples_user_enterprise() {
        let user = rfc_examples::user_enterprise();
        assert_eq!(user["schemas"].as_array().unwrap().len(), 2);
        assert!(
            user["schemas"]
                .as_array()
                .unwrap()
                .contains(&serde_json::json!(
                    "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
                ))
        );

        let enterprise_ext = &user["urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"];
        assert_eq!(enterprise_ext["employeeNumber"], "701984");
        assert_eq!(enterprise_ext["department"], "Tour Operations");
    }

    #[test]
    fn test_rfc_examples_group_basic() {
        let group = rfc_examples::group_basic();
        assert_eq!(
            group["schemas"][0],
            "urn:ietf:params:scim:schemas:core:2.0:Group"
        );
        assert_eq!(group["displayName"], "Tour Guides");
        assert!(group["members"].is_array());
        assert_eq!(group["members"].as_array().unwrap().len(), 2);
    }

    #[test]
    fn test_test_fixtures_schemas() {
        let schema = test_fixtures::minimal_user_schema();
        assert_eq!(schema["id"], "urn:ietf:params:scim:schemas:core:2.0:User");
        assert_eq!(schema["name"], "User");

        let attrs = schema["attributes"].as_array().unwrap();
        assert_eq!(attrs.len(), 1);
        assert_eq!(attrs[0]["name"], "userName");
        assert_eq!(attrs[0]["required"], true);
    }

    #[test]
    fn test_invalid_schema_fixture() {
        let schema = test_fixtures::invalid_schema();
        assert_eq!(schema["id"], "not-a-valid-uri");
        assert_eq!(schema["name"], 123);
        assert_eq!(schema["attributes"], "not-an-array");
    }
}