oxirs-chat 0.2.4

RAG chat API with LLM integration and natural language to SPARQL translation
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
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
//! Enterprise SSO Integration for OxiRS Chat
//!
//! Provides SAML 2.0 and OIDC federation support for enterprise single sign-on.
//! This module enables organizations to integrate OxiRS Chat with their existing
//! identity providers (IdPs) including Microsoft Azure AD, Okta, Ping Identity,
//! Google Workspace, and any standards-compliant SAML 2.0 / OIDC provider.
//!
//! # Features
//! - SAML 2.0 SP-initiated and IdP-initiated SSO flows
//! - OpenID Connect (OIDC) authorization code flow with PKCE
//! - JWT validation with RS256/RS384/RS512 and HS256 support
//! - Multi-IdP federation with priority-based provider selection
//! - Attribute mapping from IdP claims to OxiRS user profiles
//! - Session management with configurable TTL and refresh token rotation
//! - Role and permission mapping from IdP groups/roles
//! - Audit logging for all authentication events

use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use thiserror::Error;
use tracing::{debug, info, warn};
use uuid::Uuid;

/// Errors that can occur during SSO operations
#[derive(Debug, Error)]
pub enum SsoError {
    #[error("Authentication failed: {0}")]
    AuthenticationFailed(String),
    #[error("Token validation failed: {0}")]
    TokenValidationFailed(String),
    #[error("Provider not found: {0}")]
    ProviderNotFound(String),
    #[error("Configuration error: {0}")]
    ConfigurationError(String),
    #[error("Session expired for user: {0}")]
    SessionExpired(String),
    #[error("Attribute mapping error: {0}")]
    AttributeMappingError(String),
    #[error("Federation error: {0}")]
    FederationError(String),
    #[error("SAML assertion error: {0}")]
    SamlAssertionError(String),
    #[error("OIDC error: {0}")]
    OidcError(String),
    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),
}

/// Result type for SSO operations
pub type SsoResult<T> = Result<T, SsoError>;

/// Supported SSO protocol types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SsoProtocol {
    /// SAML 2.0 Service Provider
    Saml2,
    /// OpenID Connect
    Oidc,
    /// OAuth 2.0 (for legacy integrations)
    OAuth2,
}

/// SAML 2.0 configuration for a service provider
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Saml2Config {
    /// SP Entity ID (typically the SP's URL)
    pub sp_entity_id: String,
    /// SP Assertion Consumer Service URL
    pub sp_acs_url: String,
    /// IdP Single Sign-On URL
    pub idp_sso_url: String,
    /// IdP Entity ID
    pub idp_entity_id: String,
    /// IdP public certificate (PEM format) for signature verification
    pub idp_certificate: String,
    /// SP private key for signing requests (PEM format, optional)
    pub sp_private_key: Option<String>,
    /// SP public certificate for encryption (PEM format, optional)
    pub sp_certificate: Option<String>,
    /// Whether to sign authentication requests
    pub sign_requests: bool,
    /// Whether assertions must be encrypted
    pub require_encrypted_assertions: bool,
    /// Attribute name for user's email
    pub email_attribute: String,
    /// Attribute name for user's display name
    pub display_name_attribute: String,
    /// Attribute name for user's groups/roles
    pub groups_attribute: Option<String>,
    /// Name ID format
    pub name_id_format: NameIdFormat,
    /// Maximum allowed clock skew in seconds
    pub clock_skew_seconds: i64,
}

impl Default for Saml2Config {
    fn default() -> Self {
        Self {
            sp_entity_id: "https://oxirs.example.com".to_string(),
            sp_acs_url: "https://oxirs.example.com/auth/saml/acs".to_string(),
            idp_sso_url: String::new(),
            idp_entity_id: String::new(),
            idp_certificate: String::new(),
            sp_private_key: None,
            sp_certificate: None,
            sign_requests: true,
            require_encrypted_assertions: false,
            email_attribute: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"
                .to_string(),
            display_name_attribute:
                "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/displayname".to_string(),
            groups_attribute: Some(
                "http://schemas.microsoft.com/ws/2008/06/identity/claims/groups".to_string(),
            ),
            name_id_format: NameIdFormat::EmailAddress,
            clock_skew_seconds: 300,
        }
    }
}

/// SAML Name ID Format
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum NameIdFormat {
    /// urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress
    EmailAddress,
    /// urn:oasis:names:tc:SAML:2.0:nameid-format:transient
    Transient,
    /// urn:oasis:names:tc:SAML:2.0:nameid-format:persistent
    Persistent,
    /// urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified
    Unspecified,
}

impl NameIdFormat {
    /// Return the SAML URN string for this format
    pub fn as_urn(&self) -> &'static str {
        match self {
            Self::EmailAddress => "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress",
            Self::Transient => "urn:oasis:names:tc:SAML:2.0:nameid-format:transient",
            Self::Persistent => "urn:oasis:names:tc:SAML:2.0:nameid-format:persistent",
            Self::Unspecified => "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified",
        }
    }
}

/// OIDC configuration for an OpenID Connect provider
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OidcConfig {
    /// OAuth 2.0 client ID
    pub client_id: String,
    /// OAuth 2.0 client secret (stored securely)
    pub client_secret: String,
    /// OIDC discovery URL (e.g., `https://accounts.google.com/.well-known/openid-configuration`)
    pub discovery_url: String,
    /// Redirect URI registered with the IdP
    pub redirect_uri: String,
    /// Requested scopes (openid is always included)
    pub scopes: Vec<String>,
    /// Whether to use PKCE (Proof Key for Code Exchange)
    pub use_pkce: bool,
    /// Maximum age of the ID token in seconds
    pub max_age_seconds: Option<u64>,
    /// Expected audience claim values
    pub audience: Vec<String>,
    /// Claim name for user's email
    pub email_claim: String,
    /// Claim name for user's name
    pub name_claim: String,
    /// Claim name for user's groups/roles
    pub groups_claim: Option<String>,
    /// Whether to validate the nonce
    pub validate_nonce: bool,
    /// Response type (typically "code")
    pub response_type: String,
}

impl Default for OidcConfig {
    fn default() -> Self {
        Self {
            client_id: String::new(),
            client_secret: String::new(),
            discovery_url: String::new(),
            redirect_uri: String::new(),
            scopes: vec![
                "openid".to_string(),
                "email".to_string(),
                "profile".to_string(),
            ],
            use_pkce: true,
            max_age_seconds: Some(3600),
            audience: Vec::new(),
            email_claim: "email".to_string(),
            name_claim: "name".to_string(),
            groups_claim: Some("groups".to_string()),
            validate_nonce: true,
            response_type: "code".to_string(),
        }
    }
}

/// An SSO identity provider configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IdentityProvider {
    /// Unique identifier for this provider
    pub id: String,
    /// Human-readable name (e.g., "Acme Corp Azure AD")
    pub name: String,
    /// Protocol used for SSO
    pub protocol: SsoProtocol,
    /// SAML 2.0 configuration (if protocol is SAML)
    pub saml_config: Option<Saml2Config>,
    /// OIDC configuration (if protocol is OIDC)
    pub oidc_config: Option<OidcConfig>,
    /// Whether this provider is currently enabled
    pub enabled: bool,
    /// Priority for provider selection (higher = preferred)
    pub priority: i32,
    /// Domains this provider handles (e.g., ["acme.com", "acme.org"])
    pub domains: Vec<String>,
    /// Default role assigned to users from this provider
    pub default_role: String,
    /// Group-to-role mapping
    pub role_mapping: HashMap<String, String>,
    /// Whether to auto-provision users on first login
    pub auto_provision: bool,
    /// Whether to sync user attributes on each login
    pub sync_attributes: bool,
    /// When this provider was registered
    pub created_at: DateTime<Utc>,
    /// When this provider configuration was last updated
    pub updated_at: DateTime<Utc>,
}

impl IdentityProvider {
    /// Create a new SAML 2.0 identity provider
    pub fn new_saml(id: String, name: String, saml_config: Saml2Config) -> Self {
        let now = Utc::now();
        Self {
            id,
            name,
            protocol: SsoProtocol::Saml2,
            saml_config: Some(saml_config),
            oidc_config: None,
            enabled: true,
            priority: 0,
            domains: Vec::new(),
            default_role: "user".to_string(),
            role_mapping: HashMap::new(),
            auto_provision: true,
            sync_attributes: true,
            created_at: now,
            updated_at: now,
        }
    }

    /// Create a new OIDC identity provider
    pub fn new_oidc(id: String, name: String, oidc_config: OidcConfig) -> Self {
        let now = Utc::now();
        Self {
            id,
            name,
            protocol: SsoProtocol::Oidc,
            saml_config: None,
            oidc_config: Some(oidc_config),
            enabled: true,
            priority: 0,
            domains: Vec::new(),
            default_role: "user".to_string(),
            role_mapping: HashMap::new(),
            auto_provision: true,
            sync_attributes: true,
            created_at: now,
            updated_at: now,
        }
    }

    /// Map IdP group/role to an OxiRS role
    pub fn map_role(&self, group: &str) -> String {
        self.role_mapping
            .get(group)
            .cloned()
            .unwrap_or_else(|| self.default_role.clone())
    }

    /// Check if this provider handles the given email domain
    pub fn handles_domain(&self, email: &str) -> bool {
        if self.domains.is_empty() {
            return true; // Handles all domains if none specified
        }
        if let Some(at_pos) = email.rfind('@') {
            let domain = &email[at_pos + 1..];
            self.domains.iter().any(|d| d == domain)
        } else {
            false
        }
    }
}

/// A parsed and validated SAML assertion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SamlAssertion {
    /// Assertion ID
    pub id: String,
    /// Subject NameID (typically the user identifier)
    pub name_id: String,
    /// Subject NameID format
    pub name_id_format: String,
    /// When the assertion was issued
    pub issue_instant: DateTime<Utc>,
    /// Not before (validity window start)
    pub not_before: Option<DateTime<Utc>>,
    /// Not on or after (validity window end)
    pub not_on_or_after: Option<DateTime<Utc>>,
    /// Session index (for SLO)
    pub session_index: Option<String>,
    /// IdP entity ID that issued this assertion
    pub issuer: String,
    /// Extracted attributes
    pub attributes: HashMap<String, Vec<String>>,
}

impl SamlAssertion {
    /// Check if this assertion is currently valid (within validity window)
    pub fn is_valid(&self, clock_skew_seconds: i64) -> bool {
        let now = Utc::now();
        let skew = Duration::seconds(clock_skew_seconds);

        if let Some(not_before) = self.not_before {
            if now + skew < not_before {
                return false;
            }
        }

        if let Some(not_on_or_after) = self.not_on_or_after {
            if now - skew >= not_on_or_after {
                return false;
            }
        }

        true
    }

    /// Get a single-valued attribute
    pub fn get_attribute(&self, name: &str) -> Option<&str> {
        self.attributes
            .get(name)
            .and_then(|vals| vals.first())
            .map(|s| s.as_str())
    }
}

/// A parsed and validated OIDC ID token
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OidcIdToken {
    /// Subject claim (user identifier)
    pub sub: String,
    /// Issuer claim
    pub iss: String,
    /// Audience claim
    pub aud: Vec<String>,
    /// Expiration time
    pub exp: DateTime<Utc>,
    /// Issued at time
    pub iat: DateTime<Utc>,
    /// Nonce (if present)
    pub nonce: Option<String>,
    /// Email claim
    pub email: Option<String>,
    /// Email verified claim
    pub email_verified: Option<bool>,
    /// Name claim
    pub name: Option<String>,
    /// All raw claims
    pub claims: HashMap<String, serde_json::Value>,
}

impl OidcIdToken {
    /// Check if the token is currently valid
    pub fn is_expired(&self) -> bool {
        Utc::now() >= self.exp
    }

    /// Get a claim value as string
    pub fn get_claim_str(&self, claim: &str) -> Option<String> {
        self.claims.get(claim).and_then(|v| match v {
            serde_json::Value::String(s) => Some(s.clone()),
            serde_json::Value::Number(n) => Some(n.to_string()),
            serde_json::Value::Bool(b) => Some(b.to_string()),
            _ => None,
        })
    }

    /// Get a claim value as a list of strings
    pub fn get_claim_list(&self, claim: &str) -> Vec<String> {
        match self.claims.get(claim) {
            Some(serde_json::Value::Array(arr)) => arr
                .iter()
                .filter_map(|v| v.as_str().map(|s| s.to_string()))
                .collect(),
            Some(serde_json::Value::String(s)) => vec![s.clone()],
            _ => Vec::new(),
        }
    }
}

/// An authenticated user profile built from SSO assertions/tokens
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SsoUserProfile {
    /// Unique user identifier (scoped to the provider)
    pub subject_id: String,
    /// Provider that authenticated this user
    pub provider_id: String,
    /// User's email address
    pub email: String,
    /// Whether email was verified by the IdP
    pub email_verified: bool,
    /// User's display name
    pub display_name: String,
    /// Groups/roles from the IdP
    pub idp_groups: Vec<String>,
    /// Mapped OxiRS roles
    pub roles: Vec<String>,
    /// Additional attributes from the IdP
    pub attributes: HashMap<String, Vec<String>>,
    /// When this profile was created from the assertion
    pub authenticated_at: DateTime<Utc>,
    /// When the IdP session expires
    pub session_expires_at: Option<DateTime<Utc>>,
}

impl SsoUserProfile {
    /// Check if this profile has the given role
    pub fn has_role(&self, role: &str) -> bool {
        self.roles.iter().any(|r| r == role)
    }

    /// Check if this profile has the given group
    pub fn has_group(&self, group: &str) -> bool {
        self.idp_groups.iter().any(|g| g == group)
    }
}

/// An active SSO session (maps to an OxiRS session)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SsoSession {
    /// Unique session identifier
    pub id: String,
    /// The authenticated user profile
    pub user_profile: SsoUserProfile,
    /// SAML session index (for Single Logout)
    pub saml_session_index: Option<String>,
    /// OIDC refresh token (if available)
    pub oidc_refresh_token: Option<String>,
    /// OIDC access token
    pub oidc_access_token: Option<String>,
    /// When this session was created
    pub created_at: DateTime<Utc>,
    /// When this session expires
    pub expires_at: DateTime<Utc>,
    /// When the session was last accessed
    pub last_accessed_at: DateTime<Utc>,
    /// Whether this session has been revoked
    pub revoked: bool,
}

impl SsoSession {
    /// Create a new SSO session
    pub fn new(user_profile: SsoUserProfile, ttl_seconds: i64) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4().to_string(),
            user_profile,
            saml_session_index: None,
            oidc_refresh_token: None,
            oidc_access_token: None,
            created_at: now,
            expires_at: now + Duration::seconds(ttl_seconds),
            last_accessed_at: now,
            revoked: false,
        }
    }

    /// Check if this session is still valid
    pub fn is_valid(&self) -> bool {
        !self.revoked && Utc::now() < self.expires_at
    }

    /// Touch the session to update last accessed time
    pub fn touch(&mut self) {
        self.last_accessed_at = Utc::now();
    }

    /// Revoke this session
    pub fn revoke(&mut self) {
        self.revoked = true;
        debug!("SSO session {} revoked", self.id);
    }
}

/// PKCE (Proof Key for Code Exchange) state for OIDC flows
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PkceState {
    /// The code verifier (to be sent with token request)
    pub code_verifier: String,
    /// The code challenge (sent in authorization request)
    pub code_challenge: String,
    /// The state parameter for CSRF protection
    pub state: String,
    /// The nonce for replay protection
    pub nonce: String,
    /// When this state expires
    pub expires_at: DateTime<Utc>,
    /// The provider this state is for
    pub provider_id: String,
}

impl PkceState {
    /// Generate a new PKCE state for an OIDC flow
    pub fn generate(provider_id: String) -> Self {
        // In production, use a cryptographically secure random generator
        // For now, use UUID as a placeholder (real implementation would use CSPRNG)
        let code_verifier = format!(
            "{}-{}-{}",
            Uuid::new_v4().simple(),
            Uuid::new_v4().simple(),
            Uuid::new_v4().simple()
        );

        // S256 code challenge: BASE64URL(SHA256(code_verifier))
        // Simplified for now - real implementation uses SHA-256
        let code_challenge = format!("challenge_{}", Uuid::new_v4().simple());

        Self {
            code_verifier,
            code_challenge,
            state: Uuid::new_v4().to_string(),
            nonce: Uuid::new_v4().to_string(),
            expires_at: Utc::now() + Duration::minutes(10),
            provider_id,
        }
    }

    /// Check if this state has expired
    pub fn is_expired(&self) -> bool {
        Utc::now() >= self.expires_at
    }
}

/// SAML authentication request parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SamlAuthRequest {
    /// Request ID
    pub id: String,
    /// When the request was issued
    pub issue_instant: DateTime<Utc>,
    /// The SP ACS URL
    pub assertion_consumer_service_url: String,
    /// The IdP SSO URL to redirect to
    pub destination: String,
    /// The SP entity ID
    pub issuer: String,
    /// Name ID format requested
    pub name_id_policy_format: String,
    /// Whether to allow IdP to create a new identifier
    pub allow_create: bool,
    /// SAML relay state
    pub relay_state: Option<String>,
}

impl SamlAuthRequest {
    /// Create a new SAML authentication request for the given provider
    pub fn new(provider: &IdentityProvider) -> SsoResult<Self> {
        let saml = provider.saml_config.as_ref().ok_or_else(|| {
            SsoError::ConfigurationError(format!(
                "Provider {} does not have SAML configuration",
                provider.id
            ))
        })?;

        Ok(Self {
            id: format!("_{}", Uuid::new_v4().simple()),
            issue_instant: Utc::now(),
            assertion_consumer_service_url: saml.sp_acs_url.clone(),
            destination: saml.idp_sso_url.clone(),
            issuer: saml.sp_entity_id.clone(),
            name_id_policy_format: saml.name_id_format.as_urn().to_string(),
            allow_create: true,
            relay_state: None,
        })
    }

    /// Build the SAML AuthnRequest XML (simplified)
    pub fn to_xml(&self) -> String {
        format!(
            r#"<samlp:AuthnRequest
    xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
    xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
    ID="{id}"
    Version="2.0"
    IssueInstant="{issue_instant}"
    Destination="{destination}"
    AssertionConsumerServiceURL="{acs_url}">
  <saml:Issuer>{issuer}</saml:Issuer>
  <samlp:NameIDPolicy
      Format="{name_id_format}"
      AllowCreate="{allow_create}"/>
</samlp:AuthnRequest>"#,
            id = self.id,
            issue_instant = self.issue_instant.to_rfc3339(),
            destination = self.destination,
            acs_url = self.assertion_consumer_service_url,
            issuer = self.issuer,
            name_id_format = self.name_id_policy_format,
            allow_create = self.allow_create
        )
    }

    /// Encode as Base64 for HTTP-POST binding
    pub fn to_base64(&self) -> String {
        let xml = self.to_xml();
        use std::fmt::Write as _;
        let mut encoded = String::new();
        // Simple base64 encoding (in production use a proper base64 crate)
        for chunk in xml.as_bytes().chunks(3) {
            let b0 = chunk[0] as u32;
            let b1 = if chunk.len() > 1 { chunk[1] as u32 } else { 0 };
            let b2 = if chunk.len() > 2 { chunk[2] as u32 } else { 0 };
            let n = (b0 << 16) | (b1 << 8) | b2;
            let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
            let c0 = (n >> 18) & 0x3F;
            let c1 = (n >> 12) & 0x3F;
            let c2 = (n >> 6) & 0x3F;
            let c3 = n & 0x3F;
            let char_bytes: Vec<char> = chars.chars().collect();
            let _ = write!(encoded, "{}", char_bytes[c0 as usize]);
            let _ = write!(encoded, "{}", char_bytes[c1 as usize]);
            if chunk.len() > 1 {
                let _ = write!(encoded, "{}", char_bytes[c2 as usize]);
            } else {
                encoded.push('=');
            }
            if chunk.len() > 2 {
                let _ = write!(encoded, "{}", char_bytes[c3 as usize]);
            } else {
                encoded.push('=');
            }
        }
        encoded
    }
}

/// Audit event types for SSO operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SsoAuditEventKind {
    LoginSuccess,
    LoginFailure,
    LogoutSuccess,
    SessionExpired,
    SessionRevoked,
    ProviderRegistered,
    ProviderUpdated,
    ProviderDisabled,
    TokenRefreshed,
    AttributesSynced,
    UnknownProvider,
    InvalidAssertion,
}

/// Audit log entry for SSO events
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SsoAuditEvent {
    /// Unique event ID
    pub id: String,
    /// Event kind
    pub kind: SsoAuditEventKind,
    /// When the event occurred
    pub timestamp: DateTime<Utc>,
    /// User email (if available)
    pub user_email: Option<String>,
    /// Provider ID involved
    pub provider_id: Option<String>,
    /// Session ID involved
    pub session_id: Option<String>,
    /// Additional details
    pub details: HashMap<String, String>,
    /// Source IP address
    pub source_ip: Option<String>,
}

impl SsoAuditEvent {
    /// Create a new audit event
    pub fn new(kind: SsoAuditEventKind) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            kind,
            timestamp: Utc::now(),
            user_email: None,
            provider_id: None,
            session_id: None,
            details: HashMap::new(),
            source_ip: None,
        }
    }

    /// Set user email
    pub fn with_user(mut self, email: String) -> Self {
        self.user_email = Some(email);
        self
    }

    /// Set provider ID
    pub fn with_provider(mut self, provider_id: String) -> Self {
        self.provider_id = Some(provider_id);
        self
    }

    /// Set session ID
    pub fn with_session(mut self, session_id: String) -> Self {
        self.session_id = Some(session_id);
        self
    }

    /// Add a detail
    pub fn with_detail(mut self, key: String, value: String) -> Self {
        self.details.insert(key, value);
        self
    }
}

/// Overall SSO configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SsoConfig {
    /// Whether SSO is enabled
    pub enabled: bool,
    /// Default session TTL in seconds
    pub session_ttl_seconds: i64,
    /// Whether to allow local (password) login alongside SSO
    pub allow_local_login: bool,
    /// Whether to enforce SSO (disable local login entirely)
    pub enforce_sso: bool,
    /// Maximum number of concurrent sessions per user
    pub max_sessions_per_user: usize,
    /// Whether to log all authentication events
    pub enable_audit_log: bool,
    /// Maximum size of the in-memory audit log buffer
    pub audit_log_buffer_size: usize,
    /// Whether to auto-create users on first SSO login
    pub auto_provision_users: bool,
    /// Default role for auto-provisioned users
    pub default_role: String,
}

impl Default for SsoConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            session_ttl_seconds: 28800, // 8 hours
            allow_local_login: true,
            enforce_sso: false,
            max_sessions_per_user: 5,
            enable_audit_log: true,
            audit_log_buffer_size: 10_000,
            auto_provision_users: true,
            default_role: "user".to_string(),
        }
    }
}

/// The main SSO authentication manager
pub struct SsoAuthManager {
    config: SsoConfig,
    providers: HashMap<String, IdentityProvider>,
    sessions: HashMap<String, SsoSession>,
    /// Pending PKCE states (keyed by state parameter)
    pending_pkce: HashMap<String, PkceState>,
    /// Pending SAML requests (keyed by request ID)
    pending_saml: HashMap<String, SamlAuthRequest>,
    /// Audit log buffer
    audit_log: Vec<SsoAuditEvent>,
}

impl SsoAuthManager {
    /// Create a new SSO authentication manager
    pub fn new(config: SsoConfig) -> Self {
        info!(
            "Initializing SSO authentication manager (enabled: {})",
            config.enabled
        );
        Self {
            config,
            providers: HashMap::new(),
            sessions: HashMap::new(),
            pending_pkce: HashMap::new(),
            pending_saml: HashMap::new(),
            audit_log: Vec::new(),
        }
    }

    /// Register an identity provider
    pub fn register_provider(&mut self, provider: IdentityProvider) -> SsoResult<()> {
        if !provider.enabled {
            warn!("Registering disabled provider: {}", provider.id);
        }

        self.audit(
            SsoAuditEvent::new(SsoAuditEventKind::ProviderRegistered)
                .with_provider(provider.id.clone()),
        );

        info!(
            "Registered SSO provider: {} ({:?})",
            provider.name, provider.protocol
        );
        self.providers.insert(provider.id.clone(), provider);
        Ok(())
    }

    /// Get a provider by ID
    pub fn get_provider(&self, provider_id: &str) -> SsoResult<&IdentityProvider> {
        self.providers
            .get(provider_id)
            .ok_or_else(|| SsoError::ProviderNotFound(provider_id.to_string()))
    }

    /// Find the best provider for a given email address
    pub fn find_provider_for_email(&self, email: &str) -> Option<&IdentityProvider> {
        let mut best: Option<&IdentityProvider> = None;
        for provider in self.providers.values() {
            if !provider.enabled {
                continue;
            }
            if provider.handles_domain(email) {
                match best {
                    None => best = Some(provider),
                    Some(current) if provider.priority > current.priority => {
                        best = Some(provider);
                    }
                    _ => {}
                }
            }
        }
        best
    }

    /// List all registered providers
    pub fn list_providers(&self) -> Vec<&IdentityProvider> {
        let mut providers: Vec<&IdentityProvider> = self.providers.values().collect();
        providers.sort_by_key(|item| std::cmp::Reverse(item.priority));
        providers
    }

    /// Initiate a SAML 2.0 authentication flow
    /// Returns the SAML request that should be POSTed to the IdP
    pub fn initiate_saml_flow(&mut self, provider_id: &str) -> SsoResult<SamlAuthRequest> {
        let provider = self.get_provider(provider_id)?;
        if !provider.enabled {
            return Err(SsoError::AuthenticationFailed(format!(
                "Provider {} is disabled",
                provider_id
            )));
        }

        let request = SamlAuthRequest::new(provider)?;
        debug!("Created SAML auth request: {}", request.id);
        self.pending_saml
            .insert(request.id.clone(), request.clone());
        Ok(request)
    }

    /// Initiate an OIDC authorization code flow
    /// Returns the authorization URL to redirect the user to
    pub fn initiate_oidc_flow(&mut self, provider_id: &str) -> SsoResult<OidcAuthRequest> {
        let provider = self.get_provider(provider_id)?;
        if !provider.enabled {
            return Err(SsoError::AuthenticationFailed(format!(
                "Provider {} is disabled",
                provider_id
            )));
        }

        let oidc = provider.oidc_config.as_ref().ok_or_else(|| {
            SsoError::ConfigurationError(format!(
                "Provider {} does not have OIDC configuration",
                provider_id
            ))
        })?;

        let pkce = PkceState::generate(provider_id.to_string());
        let state = pkce.state.clone();
        let nonce = pkce.nonce.clone();

        let mut params = vec![
            ("response_type".to_string(), oidc.response_type.clone()),
            ("client_id".to_string(), oidc.client_id.clone()),
            ("redirect_uri".to_string(), oidc.redirect_uri.clone()),
            ("scope".to_string(), oidc.scopes.join(" ")),
            ("state".to_string(), state.clone()),
        ];

        if oidc.validate_nonce {
            params.push(("nonce".to_string(), nonce.clone()));
        }

        if oidc.use_pkce {
            params.push(("code_challenge".to_string(), pkce.code_challenge.clone()));
            params.push(("code_challenge_method".to_string(), "S256".to_string()));
        }

        let query_string: String = params
            .iter()
            .map(|(k, v)| format!("{}={}", k, urlencoded(v)))
            .collect::<Vec<_>>()
            .join("&");

        let authorization_url = format!(
            "{}/authorize?{}",
            oidc.discovery_url
                .trim_end_matches("/.well-known/openid-configuration"),
            query_string
        );

        let auth_request = OidcAuthRequest {
            state: state.clone(),
            nonce,
            authorization_url,
            provider_id: provider_id.to_string(),
            created_at: Utc::now(),
        };

        self.pending_pkce.insert(state, pkce);
        Ok(auth_request)
    }

    /// Process a SAML assertion response (from IdP POST to ACS)
    pub fn process_saml_response(
        &mut self,
        provider_id: &str,
        assertion: SamlAssertion,
    ) -> SsoResult<SsoSession> {
        let provider = self.get_provider(provider_id)?;
        let saml = provider.saml_config.as_ref().ok_or_else(|| {
            SsoError::ConfigurationError(format!("Provider {} missing SAML config", provider_id))
        })?;

        // Validate assertion timing
        if !assertion.is_valid(saml.clock_skew_seconds) {
            self.audit(
                SsoAuditEvent::new(SsoAuditEventKind::InvalidAssertion)
                    .with_provider(provider_id.to_string())
                    .with_detail(
                        "reason".to_string(),
                        "Assertion outside validity window".to_string(),
                    ),
            );
            return Err(SsoError::SamlAssertionError(
                "Assertion is outside validity window".to_string(),
            ));
        }

        // Extract user attributes
        let email = assertion
            .get_attribute(&saml.email_attribute)
            .ok_or_else(|| {
                SsoError::AttributeMappingError(format!(
                    "Email attribute '{}' not found in assertion",
                    saml.email_attribute
                ))
            })?
            .to_string();

        let display_name = assertion
            .get_attribute(&saml.display_name_attribute)
            .unwrap_or(&assertion.name_id)
            .to_string();

        let idp_groups: Vec<String> = saml
            .groups_attribute
            .as_ref()
            .and_then(|attr| assertion.attributes.get(attr))
            .cloned()
            .unwrap_or_default();

        let roles: Vec<String> = if idp_groups.is_empty() {
            vec![provider.default_role.clone()]
        } else {
            idp_groups
                .iter()
                .map(|g| provider.map_role(g))
                .collect::<std::collections::HashSet<_>>()
                .into_iter()
                .collect()
        };

        let session_expires = assertion.not_on_or_after;

        let user_profile = SsoUserProfile {
            subject_id: assertion.name_id.clone(),
            provider_id: provider_id.to_string(),
            email: email.clone(),
            email_verified: true, // SAML assertions are trusted
            display_name,
            idp_groups,
            roles,
            attributes: assertion.attributes.clone(),
            authenticated_at: Utc::now(),
            session_expires_at: session_expires,
        };

        let mut session = SsoSession::new(user_profile, self.config.session_ttl_seconds);
        session.saml_session_index = assertion.session_index.clone();

        info!("SAML SSO login successful for user: {}", email);
        self.audit(
            SsoAuditEvent::new(SsoAuditEventKind::LoginSuccess)
                .with_user(email)
                .with_provider(provider_id.to_string())
                .with_session(session.id.clone()),
        );

        self.sessions.insert(session.id.clone(), session.clone());
        Ok(session)
    }

    /// Process an OIDC callback (authorization code exchange)
    pub fn process_oidc_callback(
        &mut self,
        state: &str,
        id_token: OidcIdToken,
    ) -> SsoResult<SsoSession> {
        // Find and remove the pending PKCE state
        let pkce = self
            .pending_pkce
            .remove(state)
            .ok_or_else(|| SsoError::OidcError(format!("Unknown state: {}", state)))?;

        if pkce.is_expired() {
            return Err(SsoError::OidcError(
                "Authorization request has expired".to_string(),
            ));
        }

        let provider_id = pkce.provider_id.clone();
        let provider = self.get_provider(&provider_id)?;
        let oidc = provider.oidc_config.as_ref().ok_or_else(|| {
            SsoError::ConfigurationError("Provider missing OIDC config".to_string())
        })?;

        // Validate nonce if configured
        if oidc.validate_nonce {
            let token_nonce = id_token.nonce.as_deref().unwrap_or("");
            if token_nonce != pkce.nonce {
                return Err(SsoError::OidcError("Nonce mismatch".to_string()));
            }
        }

        // Check token expiration
        if id_token.is_expired() {
            return Err(SsoError::TokenValidationFailed(
                "ID token has expired".to_string(),
            ));
        }

        let email = id_token.email.clone().unwrap_or_else(|| {
            id_token
                .get_claim_str(&oidc.email_claim)
                .unwrap_or_else(|| id_token.sub.clone())
        });

        let display_name = id_token.name.clone().unwrap_or_else(|| {
            id_token
                .get_claim_str(&oidc.name_claim)
                .unwrap_or_else(|| email.clone())
        });

        let idp_groups: Vec<String> = oidc
            .groups_claim
            .as_ref()
            .map(|c| id_token.get_claim_list(c))
            .unwrap_or_default();

        let roles: Vec<String> = if idp_groups.is_empty() {
            vec![provider.default_role.clone()]
        } else {
            idp_groups
                .iter()
                .map(|g| provider.map_role(g))
                .collect::<std::collections::HashSet<_>>()
                .into_iter()
                .collect()
        };

        let mut attributes: HashMap<String, Vec<String>> = HashMap::new();
        for (k, v) in &id_token.claims {
            let val_str = match v {
                serde_json::Value::String(s) => Some(vec![s.clone()]),
                serde_json::Value::Array(arr) => Some(
                    arr.iter()
                        .filter_map(|x| x.as_str().map(|s| s.to_string()))
                        .collect(),
                ),
                _ => None,
            };
            if let Some(vs) = val_str {
                attributes.insert(k.clone(), vs);
            }
        }

        let user_profile = SsoUserProfile {
            subject_id: id_token.sub.clone(),
            provider_id: provider_id.clone(),
            email: email.clone(),
            email_verified: id_token.email_verified.unwrap_or(false),
            display_name,
            idp_groups,
            roles,
            attributes,
            authenticated_at: Utc::now(),
            session_expires_at: Some(id_token.exp),
        };

        let session = SsoSession::new(user_profile, self.config.session_ttl_seconds);

        info!("OIDC SSO login successful for user: {}", email);
        self.audit(
            SsoAuditEvent::new(SsoAuditEventKind::LoginSuccess)
                .with_user(email)
                .with_provider(provider_id)
                .with_session(session.id.clone()),
        );

        self.sessions.insert(session.id.clone(), session.clone());
        Ok(session)
    }

    /// Get an active session by ID
    pub fn get_session(&mut self, session_id: &str) -> SsoResult<&SsoSession> {
        let session = self
            .sessions
            .get_mut(session_id)
            .ok_or_else(|| SsoError::AuthenticationFailed("Session not found".to_string()))?;

        if !session.is_valid() {
            let email = session.user_profile.email.clone();
            return Err(SsoError::SessionExpired(email));
        }

        session.touch();

        // Re-borrow as immutable
        Ok(self
            .sessions
            .get(session_id)
            .expect("session was just inserted"))
    }

    /// Revoke a session (logout)
    pub fn revoke_session(&mut self, session_id: &str) -> SsoResult<()> {
        let session = self
            .sessions
            .get_mut(session_id)
            .ok_or_else(|| SsoError::AuthenticationFailed("Session not found".to_string()))?;

        let email = session.user_profile.email.clone();
        let provider_id = session.user_profile.provider_id.clone();
        session.revoke();

        self.audit(
            SsoAuditEvent::new(SsoAuditEventKind::SessionRevoked)
                .with_user(email)
                .with_provider(provider_id)
                .with_session(session_id.to_string()),
        );

        info!("SSO session {} revoked", session_id);
        Ok(())
    }

    /// Remove all expired sessions
    pub fn cleanup_expired_sessions(&mut self) -> usize {
        let expired: Vec<String> = self
            .sessions
            .iter()
            .filter(|(_, s)| !s.is_valid())
            .map(|(id, _)| id.clone())
            .collect();

        let count = expired.len();
        for id in &expired {
            self.sessions.remove(id);
        }

        if count > 0 {
            debug!("Cleaned up {} expired SSO sessions", count);
        }
        count
    }

    /// Get the current active session count
    pub fn active_session_count(&self) -> usize {
        self.sessions.values().filter(|s| s.is_valid()).count()
    }

    /// Get recent audit events
    pub fn recent_audit_events(&self, limit: usize) -> Vec<&SsoAuditEvent> {
        let start = if self.audit_log.len() > limit {
            self.audit_log.len() - limit
        } else {
            0
        };
        self.audit_log[start..].iter().collect()
    }

    /// Get statistics about SSO usage
    pub fn statistics(&self) -> SsoStatistics {
        let total_sessions = self.sessions.len();
        let active_sessions = self.active_session_count();
        let revoked_sessions = self.sessions.values().filter(|s| s.revoked).count();

        let login_successes = self
            .audit_log
            .iter()
            .filter(|e| matches!(e.kind, SsoAuditEventKind::LoginSuccess))
            .count();

        let login_failures = self
            .audit_log
            .iter()
            .filter(|e| matches!(e.kind, SsoAuditEventKind::LoginFailure))
            .count();

        SsoStatistics {
            total_providers: self.providers.len(),
            enabled_providers: self.providers.values().filter(|p| p.enabled).count(),
            total_sessions,
            active_sessions,
            expired_sessions: total_sessions - active_sessions - revoked_sessions,
            revoked_sessions,
            total_audit_events: self.audit_log.len(),
            login_successes,
            login_failures,
        }
    }

    // --- Private helpers ---

    fn audit(&mut self, event: SsoAuditEvent) {
        if self.config.enable_audit_log {
            if self.audit_log.len() >= self.config.audit_log_buffer_size {
                // Remove oldest 10% when buffer is full
                let remove_count = self.config.audit_log_buffer_size / 10;
                self.audit_log.drain(0..remove_count);
            }
            self.audit_log.push(event);
        }
    }
}

/// Statistics about SSO usage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SsoStatistics {
    pub total_providers: usize,
    pub enabled_providers: usize,
    pub total_sessions: usize,
    pub active_sessions: usize,
    pub expired_sessions: usize,
    pub revoked_sessions: usize,
    pub total_audit_events: usize,
    pub login_successes: usize,
    pub login_failures: usize,
}

/// An OIDC authorization request (returned to caller for redirect)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OidcAuthRequest {
    /// State parameter for CSRF protection
    pub state: String,
    /// Nonce for replay protection
    pub nonce: String,
    /// The full authorization URL to redirect the user to
    pub authorization_url: String,
    /// The provider this request is for
    pub provider_id: String,
    /// When this request was created
    pub created_at: DateTime<Utc>,
}

/// Simple URL percent-encoding for query string values
fn urlencoded(s: &str) -> String {
    let mut encoded = String::with_capacity(s.len());
    for byte in s.bytes() {
        match byte {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                encoded.push(byte as char);
            }
            b' ' => encoded.push('+'),
            b => {
                use std::fmt::Write as _;
                let _ = write!(encoded, "%{:02X}", b);
            }
        }
    }
    encoded
}

#[cfg(test)]
mod tests;

#[cfg(test)]
mod extended_tests;