auth-framework 0.5.0-rc18

A comprehensive, production-ready authentication and authorization framework for Rust applications
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
//! SCIM 2.0 (RFC 7643 / RFC 7644) — System for Cross-domain Identity Management
//!
//! Provides types and a client for provisioning and managing user/group
//! identities across domains using the SCIM 2.0 protocol.
//!
//! # Supported Operations
//!
//! - **Users**: Create, Read, Replace, Patch, Delete, List (with filtering)
//! - **Groups**: Create, Read, Replace, Patch, Delete, List
//! - **Bulk**: Batch operations for efficient provisioning
//! - **Service Provider Config**: Capability discovery
//!
//! # Security
//!
//! - All requests use Bearer token authentication
//! - TLS is required (enforced by the client)
//! - Attribute filtering prevents over-exposure of PII

use crate::errors::{AuthError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// ─── SCIM Core Schema Types (RFC 7643) ───────────────────────────────────────

/// SCIM 2.0 schema URN constants.
pub mod schema {
    pub const USER: &str = "urn:ietf:params:scim:schemas:core:2.0:User";
    pub const GROUP: &str = "urn:ietf:params:scim:schemas:core:2.0:Group";
    pub const ENTERPRISE_USER: &str = "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User";
    pub const LIST_RESPONSE: &str = "urn:ietf:params:scim:api:messages:2.0:ListResponse";
    pub const PATCH_OP: &str = "urn:ietf:params:scim:api:messages:2.0:PatchOp";
    pub const BULK_REQUEST: &str = "urn:ietf:params:scim:api:messages:2.0:BulkRequest";
    pub const BULK_RESPONSE: &str = "urn:ietf:params:scim:api:messages:2.0:BulkResponse";
    pub const ERROR: &str = "urn:ietf:params:scim:api:messages:2.0:Error";
    pub const SEARCH_REQUEST: &str = "urn:ietf:params:scim:api:messages:2.0:SearchRequest";
    pub const SERVICE_PROVIDER_CONFIG: &str =
        "urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig";
}

/// Common SCIM metadata present on every resource.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Meta {
    pub resource_type: String,
    pub created: Option<String>,
    pub last_modified: Option<String>,
    pub location: Option<String>,
    pub version: Option<String>,
}

/// SCIM multi-valued attribute with canonical type/primary flags.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiValuedAttr {
    pub value: String,
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub attr_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub primary: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display: Option<String>,
}

/// SCIM Name component (RFC 7643 §4.1.1).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Name {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub formatted: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub family_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub given_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub middle_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub honorific_prefix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub honorific_suffix: Option<String>,
}

/// SCIM User resource (RFC 7643 §4.1).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScimUser {
    /// Schema URNs.
    pub schemas: Vec<String>,

    /// Unique server-assigned identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    /// Unique identifier for the user (typically login name).
    pub user_name: String,

    /// User's name components.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<Name>,

    /// Display name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,

    /// Email addresses.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub emails: Option<Vec<MultiValuedAttr>>,

    /// Phone numbers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub phone_numbers: Option<Vec<MultiValuedAttr>>,

    /// Whether the user account is active.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active: Option<bool>,

    /// Resource metadata.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<Meta>,

    /// Groups the user belongs to (read-only).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub groups: Option<Vec<MultiValuedAttr>>,

    /// Additional extension attributes.
    #[serde(flatten)]
    pub extensions: HashMap<String, serde_json::Value>,
}

impl ScimUser {
    /// Create a minimal SCIM User with only the required `userName`.
    pub fn new(user_name: impl Into<String>) -> Self {
        Self {
            schemas: vec![schema::USER.to_string()],
            id: None,
            user_name: user_name.into(),
            name: None,
            display_name: None,
            emails: None,
            phone_numbers: None,
            active: Some(true),
            meta: None,
            groups: None,
            extensions: HashMap::new(),
        }
    }
}

/// SCIM Group resource (RFC 7643 §4.2).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScimGroup {
    pub schemas: Vec<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    pub display_name: String,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub members: Option<Vec<MultiValuedAttr>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<Meta>,
}

impl ScimGroup {
    pub fn new(display_name: impl Into<String>) -> Self {
        Self {
            schemas: vec![schema::GROUP.to_string()],
            id: None,
            display_name: display_name.into(),
            members: None,
            meta: None,
        }
    }
}

// ─── SCIM Protocol Messages (RFC 7644) ───────────────────────────────────────

/// SCIM ListResponse (RFC 7644 §3.4.2).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ListResponse<T> {
    pub schemas: Vec<String>,
    pub total_results: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_index: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub items_per_page: Option<u64>,
    #[serde(rename = "Resources")]
    pub resources: Vec<T>,
}

/// SCIM Patch operation (RFC 7644 §3.5.2).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatchOp {
    pub schemas: Vec<String>,
    #[serde(rename = "Operations")]
    pub operations: Vec<PatchOperation>,
}

/// A single patch operation entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatchOperation {
    pub op: PatchOpType,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<serde_json::Value>,
}

/// Patch operation type.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PatchOpType {
    Add,
    Remove,
    Replace,
}

/// SCIM Bulk request (RFC 7644 §3.7).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BulkRequest {
    pub schemas: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fail_on_errors: Option<u32>,
    #[serde(rename = "Operations")]
    pub operations: Vec<BulkOperation>,
}

/// SCIM Bulk response.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BulkResponse {
    pub schemas: Vec<String>,
    #[serde(rename = "Operations")]
    pub operations: Vec<BulkOperationResponse>,
}

/// A single operation inside a bulk request.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BulkOperation {
    pub method: BulkMethod,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bulk_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<serde_json::Value>,
}

/// A single operation response inside a bulk response.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BulkOperationResponse {
    pub method: BulkMethod,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bulk_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub location: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum BulkMethod {
    Post,
    Put,
    Patch,
    Delete,
}

/// SCIM Error response (RFC 7644 §3.12).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScimError {
    pub schemas: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scim_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
}

/// SCIM Search request (RFC 7644 §3.4.3).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SearchRequest {
    pub schemas: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sort_by: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sort_order: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_index: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub count: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attributes: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub excluded_attributes: Option<Vec<String>>,
}

/// Service provider configuration (RFC 7643 §5).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ServiceProviderConfig {
    pub schemas: Vec<String>,
    pub patch: Supported,
    pub bulk: BulkSupported,
    pub filter: FilterSupported,
    pub change_password: Supported,
    pub sort: Supported,
    pub etag: Supported,
    pub authentication_schemes: Vec<AuthenticationScheme>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Supported {
    pub supported: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BulkSupported {
    pub supported: bool,
    pub max_operations: u32,
    pub max_payload_size: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FilterSupported {
    pub supported: bool,
    pub max_results: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthenticationScheme {
    pub name: String,
    pub description: String,
    #[serde(rename = "type")]
    pub scheme_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub spec_uri: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub documentation_uri: Option<String>,
    pub primary: bool,
}

// ─── SCIM Client ─────────────────────────────────────────────────────────────

/// Configuration for the SCIM 2.0 client.
#[derive(Debug, Clone)]
pub struct ScimClientConfig {
    /// Base URL of the SCIM service (e.g. `https://idp.example.com/scim/v2`).
    pub base_url: String,

    /// Bearer token for authentication.
    pub bearer_token: String,

    /// Request timeout in seconds.
    pub timeout_secs: u64,
}

/// SCIM 2.0 client for provisioning users and groups.
pub struct ScimClient {
    config: ScimClientConfig,
    http: reqwest::Client,
}

impl ScimClient {
    /// Create a new SCIM client.
    pub fn new(config: ScimClientConfig) -> Result<Self> {
        if !config.base_url.starts_with("https://") {
            return Err(AuthError::config(
                "SCIM base URL must use HTTPS for security",
            ));
        }

        let http = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(config.timeout_secs))
            .build()
            .map_err(|e| AuthError::internal(format!("Failed to build HTTP client: {e}")))?;

        Ok(Self { config, http })
    }

    // ── Users ────────────────────────────────────────────────────────────

    /// Create a user (POST /Users).
    pub async fn create_user(&self, user: &ScimUser) -> Result<ScimUser> {
        let url = format!("{}/Users", self.config.base_url);
        let resp = self
            .http
            .post(&url)
            .bearer_auth(&self.config.bearer_token)
            .json(user)
            .send()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM create user request failed: {e}")))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(AuthError::internal(format!(
                "SCIM create user failed (HTTP {status}): {body}"
            )));
        }

        resp.json::<ScimUser>()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM create user parse error: {e}")))
    }

    /// Get a user by ID (GET /Users/{id}).
    pub async fn get_user(&self, id: &str) -> Result<ScimUser> {
        let url = format!("{}/Users/{}", self.config.base_url, id);
        let resp = self
            .http
            .get(&url)
            .bearer_auth(&self.config.bearer_token)
            .send()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM get user request failed: {e}")))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(AuthError::internal(format!(
                "SCIM get user failed (HTTP {status}): {body}"
            )));
        }

        resp.json::<ScimUser>()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM get user parse error: {e}")))
    }

    /// Replace a user (PUT /Users/{id}).
    pub async fn replace_user(&self, id: &str, user: &ScimUser) -> Result<ScimUser> {
        let url = format!("{}/Users/{}", self.config.base_url, id);
        let resp = self
            .http
            .put(&url)
            .bearer_auth(&self.config.bearer_token)
            .json(user)
            .send()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM replace user request failed: {e}")))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(AuthError::internal(format!(
                "SCIM replace user failed (HTTP {status}): {body}"
            )));
        }

        resp.json::<ScimUser>()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM replace user parse error: {e}")))
    }

    /// Patch a user (PATCH /Users/{id}).
    pub async fn patch_user(&self, id: &str, patch: &PatchOp) -> Result<ScimUser> {
        let url = format!("{}/Users/{}", self.config.base_url, id);
        let resp = self
            .http
            .patch(&url)
            .bearer_auth(&self.config.bearer_token)
            .json(patch)
            .send()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM patch user request failed: {e}")))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(AuthError::internal(format!(
                "SCIM patch user failed (HTTP {status}): {body}"
            )));
        }

        resp.json::<ScimUser>()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM patch user parse error: {e}")))
    }

    /// Delete a user (DELETE /Users/{id}).
    pub async fn delete_user(&self, id: &str) -> Result<()> {
        let url = format!("{}/Users/{}", self.config.base_url, id);
        let resp = self
            .http
            .delete(&url)
            .bearer_auth(&self.config.bearer_token)
            .send()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM delete user request failed: {e}")))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(AuthError::internal(format!(
                "SCIM delete user failed (HTTP {status}): {body}"
            )));
        }

        Ok(())
    }

    /// List users with optional filter (GET /Users?filter=...).
    pub async fn list_users(
        &self,
        filter: Option<&str>,
        start_index: Option<u64>,
        count: Option<u64>,
    ) -> Result<ListResponse<ScimUser>> {
        let mut url = format!("{}/Users", self.config.base_url);
        let mut params = Vec::new();
        if let Some(f) = filter {
            params.push(format!("filter={}", urlencoding::encode(f)));
        }
        if let Some(si) = start_index {
            params.push(format!("startIndex={si}"));
        }
        if let Some(c) = count {
            params.push(format!("count={c}"));
        }
        if !params.is_empty() {
            url = format!("{}?{}", url, params.join("&"));
        }

        let resp = self
            .http
            .get(&url)
            .bearer_auth(&self.config.bearer_token)
            .send()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM list users request failed: {e}")))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(AuthError::internal(format!(
                "SCIM list users failed (HTTP {status}): {body}"
            )));
        }

        resp.json::<ListResponse<ScimUser>>()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM list users parse error: {e}")))
    }

    // ── Groups ───────────────────────────────────────────────────────────

    /// Create a group (POST /Groups).
    pub async fn create_group(&self, group: &ScimGroup) -> Result<ScimGroup> {
        let url = format!("{}/Groups", self.config.base_url);
        let resp = self
            .http
            .post(&url)
            .bearer_auth(&self.config.bearer_token)
            .json(group)
            .send()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM create group request failed: {e}")))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(AuthError::internal(format!(
                "SCIM create group failed (HTTP {status}): {body}"
            )));
        }

        resp.json::<ScimGroup>()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM create group parse error: {e}")))
    }

    /// Get a group by ID (GET /Groups/{id}).
    pub async fn get_group(&self, id: &str) -> Result<ScimGroup> {
        let url = format!("{}/Groups/{}", self.config.base_url, id);
        let resp = self
            .http
            .get(&url)
            .bearer_auth(&self.config.bearer_token)
            .send()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM get group request failed: {e}")))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(AuthError::internal(format!(
                "SCIM get group failed (HTTP {status}): {body}"
            )));
        }

        resp.json::<ScimGroup>()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM get group parse error: {e}")))
    }

    /// Delete a group (DELETE /Groups/{id}).
    pub async fn delete_group(&self, id: &str) -> Result<()> {
        let url = format!("{}/Groups/{}", self.config.base_url, id);
        let resp = self
            .http
            .delete(&url)
            .bearer_auth(&self.config.bearer_token)
            .send()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM delete group request failed: {e}")))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(AuthError::internal(format!(
                "SCIM delete group failed (HTTP {status}): {body}"
            )));
        }

        Ok(())
    }

    /// List groups with optional filter (GET /Groups?filter=...).
    pub async fn list_groups(
        &self,
        filter: Option<&str>,
        start_index: Option<u64>,
        count: Option<u64>,
    ) -> Result<ListResponse<ScimGroup>> {
        let mut url = format!("{}/Groups", self.config.base_url);
        let mut params = Vec::new();
        if let Some(f) = filter {
            params.push(format!("filter={}", urlencoding::encode(f)));
        }
        if let Some(si) = start_index {
            params.push(format!("startIndex={si}"));
        }
        if let Some(c) = count {
            params.push(format!("count={c}"));
        }
        if !params.is_empty() {
            url = format!("{}?{}", url, params.join("&"));
        }

        let resp = self
            .http
            .get(&url)
            .bearer_auth(&self.config.bearer_token)
            .send()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM list groups request failed: {e}")))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(AuthError::internal(format!(
                "SCIM list groups failed (HTTP {status}): {body}"
            )));
        }

        resp.json::<ListResponse<ScimGroup>>()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM list groups parse error: {e}")))
    }

    // ── Bulk ─────────────────────────────────────────────────────────────

    /// Execute a bulk request (POST /Bulk).
    pub async fn bulk(&self, request: &BulkRequest) -> Result<BulkResponse> {
        let url = format!("{}/Bulk", self.config.base_url);
        let resp = self
            .http
            .post(&url)
            .bearer_auth(&self.config.bearer_token)
            .json(request)
            .send()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM bulk request failed: {e}")))?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(AuthError::internal(format!(
                "SCIM bulk request failed (HTTP {status}): {body}"
            )));
        }

        resp.json::<BulkResponse>()
            .await
            .map_err(|e| AuthError::internal(format!("SCIM bulk response parse error: {e}")))
    }

    // ── Service Provider Config ──────────────────────────────────────────

    /// Retrieve the service provider configuration.
    pub async fn get_service_provider_config(&self) -> Result<ServiceProviderConfig> {
        let url = format!("{}/ServiceProviderConfig", self.config.base_url);
        let resp = self
            .http
            .get(&url)
            .bearer_auth(&self.config.bearer_token)
            .send()
            .await
            .map_err(|e| {
                AuthError::internal(format!("SCIM service provider config request failed: {e}"))
            })?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(AuthError::internal(format!(
                "SCIM service provider config failed (HTTP {status}): {body}"
            )));
        }

        resp.json::<ServiceProviderConfig>().await.map_err(|e| {
            AuthError::internal(format!("SCIM service provider config parse error: {e}"))
        })
    }
}

// ─── Helper constructors ─────────────────────────────────────────────────────

impl PatchOp {
    /// Create a new PatchOp with a list of operations.
    pub fn new(operations: Vec<PatchOperation>) -> Self {
        Self {
            schemas: vec![schema::PATCH_OP.to_string()],
            operations,
        }
    }
}

impl SearchRequest {
    /// Create a search request with a filter.
    pub fn with_filter(filter: impl Into<String>) -> Self {
        Self {
            schemas: vec![schema::SEARCH_REQUEST.to_string()],
            filter: Some(filter.into()),
            sort_by: None,
            sort_order: None,
            start_index: None,
            count: None,
            attributes: None,
            excluded_attributes: None,
        }
    }
}

impl BulkRequest {
    /// Create a new bulk request.
    pub fn new(operations: Vec<BulkOperation>, fail_on_errors: Option<u32>) -> Self {
        Self {
            schemas: vec![schema::BULK_REQUEST.to_string()],
            fail_on_errors,
            operations,
        }
    }
}

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

    #[test]
    fn test_scim_user_serialization() {
        let mut user = ScimUser::new("jdoe");
        user.name = Some(Name {
            given_name: Some("John".into()),
            family_name: Some("Doe".into()),
            ..Default::default()
        });
        user.emails = Some(vec![MultiValuedAttr {
            value: "jdoe@example.com".into(),
            attr_type: Some("work".into()),
            primary: Some(true),
            display: None,
        }]);

        let json = serde_json::to_string(&user).expect("serialize");
        assert!(json.contains("\"userName\":\"jdoe\""));
        assert!(json.contains(schema::USER));
    }

    #[test]
    fn test_scim_group_serialization() {
        let group = ScimGroup::new("Engineering");
        let json = serde_json::to_string(&group).expect("serialize");
        assert!(json.contains("\"displayName\":\"Engineering\""));
        assert!(json.contains(schema::GROUP));
    }

    #[test]
    fn test_patch_op_construction() {
        let patch = PatchOp::new(vec![PatchOperation {
            op: PatchOpType::Replace,
            path: Some("active".into()),
            value: Some(serde_json::Value::Bool(false)),
        }]);
        assert_eq!(patch.schemas[0], schema::PATCH_OP);
        assert_eq!(patch.operations.len(), 1);
    }

    #[test]
    fn test_scim_user_roundtrip() {
        let user = ScimUser::new("alice");
        let json = serde_json::to_value(&user).expect("to value");
        let parsed: ScimUser = serde_json::from_value(json).expect("from value");
        assert_eq!(parsed.user_name, "alice");
        assert_eq!(parsed.active, Some(true));
    }

    #[test]
    fn test_bulk_request_construction() {
        let bulk = BulkRequest::new(
            vec![BulkOperation {
                method: BulkMethod::Post,
                bulk_id: Some("op1".into()),
                path: Some("/Users".into()),
                data: Some(serde_json::to_value(ScimUser::new("bulk_user")).expect("val")),
            }],
            Some(1),
        );
        assert_eq!(bulk.schemas[0], schema::BULK_REQUEST);
        assert_eq!(bulk.fail_on_errors, Some(1));
    }

    #[test]
    fn test_scim_user_empty_username() {
        let user = ScimUser::new("");
        assert_eq!(user.user_name, "");
        // Roundtrip should preserve the empty string
        let json = serde_json::to_value(&user).unwrap();
        let parsed: ScimUser = serde_json::from_value(json).unwrap();
        assert_eq!(parsed.user_name, "");
    }

    #[test]
    fn test_scim_user_all_optional_fields() {
        let mut user = ScimUser::new("fulluser");
        user.id = Some("u-123".into());
        user.display_name = Some("Full User".into());
        user.name = Some(Name {
            formatted: Some("Dr. Full A. User Jr.".into()),
            family_name: Some("User".into()),
            given_name: Some("Full".into()),
            middle_name: Some("A".into()),
            honorific_prefix: Some("Dr.".into()),
            honorific_suffix: Some("Jr.".into()),
        });
        user.emails = Some(vec![
            MultiValuedAttr {
                value: "work@example.com".into(),
                attr_type: Some("work".into()),
                primary: Some(true),
                display: Some("Work Email".into()),
            },
            MultiValuedAttr {
                value: "home@example.com".into(),
                attr_type: Some("home".into()),
                primary: Some(false),
                display: None,
            },
        ]);
        user.phone_numbers = Some(vec![MultiValuedAttr {
            value: "+1-555-0100".into(),
            attr_type: Some("mobile".into()),
            primary: Some(true),
            display: None,
        }]);
        user.active = Some(false);
        user.groups = Some(vec![MultiValuedAttr {
            value: "g-eng".into(),
            attr_type: None,
            primary: None,
            display: Some("Engineering".into()),
        }]);

        let json = serde_json::to_string(&user).unwrap();
        let parsed: ScimUser =
            serde_json::from_value(serde_json::from_str(&json).unwrap()).unwrap();
        assert_eq!(parsed.id.as_deref(), Some("u-123"));
        assert_eq!(parsed.display_name.as_deref(), Some("Full User"));
        assert_eq!(parsed.active, Some(false));
        assert_eq!(parsed.emails.as_ref().unwrap().len(), 2);
        assert_eq!(parsed.phone_numbers.as_ref().unwrap().len(), 1);
        assert_eq!(parsed.groups.as_ref().unwrap().len(), 1);
        let name = parsed.name.as_ref().unwrap();
        assert_eq!(name.honorific_prefix.as_deref(), Some("Dr."));
        assert_eq!(name.honorific_suffix.as_deref(), Some("Jr."));
    }

    #[test]
    fn test_scim_user_deserialization_from_json() {
        let json_str = r#"{
            "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
            "id": "server-1",
            "userName": "jdoe",
            "displayName": "Jane Doe",
            "active": true,
            "meta": {
                "resourceType": "User",
                "created": "2024-01-01T00:00:00Z",
                "lastModified": "2024-06-01T00:00:00Z",
                "location": "https://scim.example.com/Users/server-1",
                "version": "W/\"1\""
            }
        }"#;

        let user: ScimUser = serde_json::from_str(json_str).unwrap();
        assert_eq!(user.user_name, "jdoe");
        assert_eq!(user.id.as_deref(), Some("server-1"));
        assert_eq!(user.display_name.as_deref(), Some("Jane Doe"));
        let meta = user.meta.as_ref().unwrap();
        assert_eq!(meta.resource_type, "User");
        assert_eq!(meta.version.as_deref(), Some("W/\"1\""));
    }

    #[test]
    fn test_scim_group_with_members() {
        let mut group = ScimGroup::new("DevOps");
        group.members = Some(vec![
            MultiValuedAttr {
                value: "u-1".into(),
                attr_type: Some("User".into()),
                primary: None,
                display: Some("Alice".into()),
            },
            MultiValuedAttr {
                value: "u-2".into(),
                attr_type: Some("User".into()),
                primary: None,
                display: Some("Bob".into()),
            },
        ]);

        let json = serde_json::to_string(&group).unwrap();
        let parsed: ScimGroup = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.display_name, "DevOps");
        let members = parsed.members.unwrap();
        assert_eq!(members.len(), 2);
        assert_eq!(members[0].display.as_deref(), Some("Alice"));
    }

    #[test]
    fn test_patch_op_add_and_remove() {
        let patch = PatchOp::new(vec![
            PatchOperation {
                op: PatchOpType::Add,
                path: Some("emails".into()),
                value: Some(serde_json::json!([{"value": "new@example.com", "type": "work"}])),
            },
            PatchOperation {
                op: PatchOpType::Remove,
                path: Some("phoneNumbers".into()),
                value: None,
            },
        ]);

        assert_eq!(patch.operations.len(), 2);

        // Verify JSON serialization of patch ops
        let json = serde_json::to_string(&patch).unwrap();
        assert!(json.contains("\"add\""));
        assert!(json.contains("\"remove\""));
        assert!(json.contains("new@example.com"));
    }

    #[test]
    fn test_bulk_request_multiple_operations() {
        let bulk = BulkRequest::new(
            vec![
                BulkOperation {
                    method: BulkMethod::Post,
                    bulk_id: Some("create-1".into()),
                    path: Some("/Users".into()),
                    data: Some(serde_json::to_value(ScimUser::new("user1")).unwrap()),
                },
                BulkOperation {
                    method: BulkMethod::Put,
                    bulk_id: Some("update-1".into()),
                    path: Some("/Users/existing-id".into()),
                    data: Some(serde_json::to_value(ScimUser::new("user1_updated")).unwrap()),
                },
                BulkOperation {
                    method: BulkMethod::Delete,
                    bulk_id: Some("delete-1".into()),
                    path: Some("/Users/old-id".into()),
                    data: None,
                },
            ],
            Some(2),
        );

        assert_eq!(bulk.operations.len(), 3);
        assert_eq!(bulk.fail_on_errors, Some(2));

        // Roundtrip
        let json = serde_json::to_string(&bulk).unwrap();
        let parsed: BulkRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.operations.len(), 3);
    }

    #[test]
    fn test_search_request_with_filter() {
        let search = SearchRequest::with_filter("userName eq \"jdoe\"");
        assert_eq!(search.filter.as_deref(), Some("userName eq \"jdoe\""));
        assert_eq!(search.schemas[0], schema::SEARCH_REQUEST);
        assert!(search.sort_by.is_none());
        assert!(search.count.is_none());
    }

    #[test]
    fn test_scim_error_serialization() {
        let error = ScimError {
            schemas: vec![schema::ERROR.to_string()],
            status: Some("400".into()),
            scim_type: Some("invalidFilter".into()),
            detail: Some("The filter syntax is invalid".into()),
        };

        let json = serde_json::to_string(&error).unwrap();
        let parsed: ScimError = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.status.as_deref(), Some("400"));
        assert_eq!(parsed.scim_type.as_deref(), Some("invalidFilter"));
    }

    #[test]
    fn test_scim_user_active_defaults_to_true() {
        let user = ScimUser::new("defaultuser");
        assert_eq!(user.active, Some(true));
    }

    #[test]
    fn test_scim_user_extensions_roundtrip() {
        let mut user = ScimUser::new("extuser");
        user.extensions.insert(
            "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User".to_string(),
            serde_json::json!({
                "employeeNumber": "12345",
                "department": "Engineering"
            }),
        );

        let json = serde_json::to_string(&user).unwrap();
        let parsed: ScimUser = serde_json::from_str(&json).unwrap();
        let ext = &parsed.extensions["urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"];
        assert_eq!(ext["employeeNumber"], "12345");
        assert_eq!(ext["department"], "Engineering");
    }

    #[test]
    fn test_scim_client_rejects_http_url() {
        let config = ScimClientConfig {
            base_url: "http://insecure.example.com/scim/v2".to_string(),
            bearer_token: "tok".to_string(),
            timeout_secs: 10,
        };
        match ScimClient::new(config) {
            Err(e) => {
                let msg = format!("{e}");
                assert!(msg.contains("HTTPS"), "got: {msg}");
            }
            Ok(_) => panic!("expected error for HTTP URL"),
        }
    }

    #[test]
    fn test_list_response_deserialization() {
        let json_str = r#"{
            "schemas": ["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
            "totalResults": 2,
            "startIndex": 1,
            "itemsPerPage": 10,
            "Resources": [
                {
                    "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
                    "userName": "alice"
                },
                {
                    "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
                    "userName": "bob"
                }
            ]
        }"#;

        let list: ListResponse<ScimUser> = serde_json::from_str(json_str).unwrap();
        assert_eq!(list.total_results, 2);
        assert_eq!(list.resources.len(), 2);
        assert_eq!(list.resources[0].user_name, "alice");
        assert_eq!(list.resources[1].user_name, "bob");
    }
}