rrag 0.1.0-alpha.2

High-performance Rust framework for Retrieval-Augmented Generation with pluggable components, async-first design, and comprehensive observability
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
//! # Authentication and Authorization System
//! 
//! Comprehensive authentication and authorization framework supporting:
//! - API Key authentication
//! - JWT token authentication
//! - OAuth2 integration
//! - TOTP (Time-based One-Time Password) authentication
//! - WebAuthn for passwordless authentication
//! - Role-based access control (RBAC)
//! - Fine-grained permissions
//! - Session management
//! - Multi-factor authentication (MFA)

use crate::{RragError, RragResult};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;
use chrono::{DateTime, Duration, Utc};
use sha2::{Sha256, Digest};
use hmac::{Hmac, Mac};
use base64::{Engine as _, engine::general_purpose};
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, TokenData, Validation};
use bcrypt::{hash, verify, DEFAULT_COST};
use argon2::{Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
use argon2::password_hash::{SaltString, rand_core::OsRng};

/// Main authentication service
#[derive(Clone)]
pub struct AuthenticationService {
    providers: Arc<RwLock<Vec<Arc<dyn AuthProvider>>>>,
    config: AuthConfig,
    user_store: Arc<dyn UserStore>,
    session_store: Arc<dyn SessionStore>,
}

/// Authentication configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthConfig {
    /// JWT secret key for signing tokens
    pub jwt_secret: String,
    /// JWT token expiration time in seconds
    pub jwt_expiration: i64,
    /// API key length
    pub api_key_length: usize,
    /// Enable multi-factor authentication
    pub enable_mfa: bool,
    /// Require email verification
    pub require_email_verification: bool,
    /// Password policy settings
    pub password_policy: PasswordPolicy,
    /// Session timeout in seconds
    pub session_timeout: i64,
    /// Maximum failed authentication attempts
    pub max_failed_attempts: u32,
    /// Account lockout duration in seconds
    pub lockout_duration: i64,
    /// Enable account lockout
    pub enable_lockout: bool,
}

impl Default for AuthConfig {
    fn default() -> Self {
        Self {
            jwt_secret: "default-secret-change-me".to_string(),
            jwt_expiration: 3600, // 1 hour
            api_key_length: 32,
            enable_mfa: false,
            require_email_verification: false,
            password_policy: PasswordPolicy::default(),
            session_timeout: 86400, // 24 hours
            max_failed_attempts: 5,
            lockout_duration: 900, // 15 minutes
            enable_lockout: true,
        }
    }
}

/// Password policy configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PasswordPolicy {
    pub min_length: usize,
    pub require_uppercase: bool,
    pub require_lowercase: bool,
    pub require_numbers: bool,
    pub require_symbols: bool,
    pub min_entropy_bits: f64,
    pub common_password_check: bool,
    pub max_password_age_days: Option<u32>,
}

impl Default for PasswordPolicy {
    fn default() -> Self {
        Self {
            min_length: 8,
            require_uppercase: true,
            require_lowercase: true,
            require_numbers: true,
            require_symbols: false,
            min_entropy_bits: 50.0,
            common_password_check: true,
            max_password_age_days: Some(90),
        }
    }
}

/// Authentication credentials
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Credentials {
    ApiKey {
        key: String,
    },
    UsernamePassword {
        username: String,
        password: String,
        totp_code: Option<String>,
    },
    JwtToken {
        token: String,
    },
    OAuth2 {
        provider: String,
        access_token: String,
        refresh_token: Option<String>,
    },
    WebAuthn {
        assertion: String,
        client_data: String,
    },
    Session {
        session_id: String,
    },
}

/// Authentication result
#[derive(Debug, Clone)]
pub struct AuthResult {
    pub success: bool,
    pub user: Option<User>,
    pub session: Option<Session>,
    pub method: String,
    pub mfa_required: bool,
    pub mfa_methods: Vec<String>,
    pub error: Option<String>,
    pub expires_at: Option<DateTime<Utc>>,
}

impl AuthResult {
    pub fn success(user: User) -> Self {
        Self {
            success: true,
            user: Some(user),
            session: None,
            method: "unknown".to_string(),
            mfa_required: false,
            mfa_methods: Vec::new(),
            error: None,
            expires_at: None,
        }
    }

    pub fn failure(error: impl Into<String>) -> Self {
        Self {
            success: false,
            user: None,
            session: None,
            method: "unknown".to_string(),
            mfa_required: false,
            mfa_methods: Vec::new(),
            error: Some(error.into()),
            expires_at: None,
        }
    }

    pub fn mfa_required(methods: Vec<String>) -> Self {
        Self {
            success: false,
            user: None,
            session: None,
            method: "unknown".to_string(),
            mfa_required: true,
            mfa_methods: methods,
            error: None,
            expires_at: None,
        }
    }
}

/// User representation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    pub id: String,
    pub username: String,
    pub email: Option<String>,
    pub roles: Vec<Role>,
    pub permissions: HashSet<Permission>,
    pub metadata: HashMap<String, serde_json::Value>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub last_login: Option<DateTime<Utc>>,
    pub email_verified: bool,
    pub mfa_enabled: bool,
    pub mfa_methods: Vec<MfaMethod>,
    pub account_locked: bool,
    pub locked_until: Option<DateTime<Utc>>,
    pub failed_attempts: u32,
}

impl User {
    pub fn new(id: String, username: String, email: Option<String>) -> Self {
        Self {
            id,
            username,
            email,
            roles: Vec::new(),
            permissions: HashSet::new(),
            metadata: HashMap::new(),
            created_at: Utc::now(),
            updated_at: Utc::now(),
            last_login: None,
            email_verified: false,
            mfa_enabled: false,
            mfa_methods: Vec::new(),
            account_locked: false,
            locked_until: None,
            failed_attempts: 0,
        }
    }

    pub fn anonymous() -> Self {
        Self::new(
            "anonymous".to_string(),
            "anonymous".to_string(),
            None,
        )
    }

    pub fn add_role(&mut self, role: Role) {
        self.roles.push(role);
        self.updated_at = Utc::now();
    }

    pub fn add_permission(&mut self, permission: Permission) {
        self.permissions.insert(permission);
        self.updated_at = Utc::now();
    }

    pub fn has_permission(&self, permission: &Permission) -> bool {
        self.permissions.contains(permission) || 
        self.roles.iter().any(|role| role.permissions.contains(permission))
    }

    pub fn has_role(&self, role_name: &str) -> bool {
        self.roles.iter().any(|role| role.name == role_name)
    }

    pub fn is_locked(&self) -> bool {
        self.account_locked && 
        self.locked_until.map_or(true, |until| Utc::now() < until)
    }

    pub fn unlock(&mut self) {
        self.account_locked = false;
        self.locked_until = None;
        self.failed_attempts = 0;
        self.updated_at = Utc::now();
    }

    pub fn increment_failed_attempts(&mut self, max_attempts: u32, lockout_duration: i64) {
        self.failed_attempts += 1;
        if self.failed_attempts >= max_attempts {
            self.account_locked = true;
            self.locked_until = Some(Utc::now() + Duration::seconds(lockout_duration));
        }
        self.updated_at = Utc::now();
    }

    pub fn reset_failed_attempts(&mut self) {
        self.failed_attempts = 0;
        self.updated_at = Utc::now();
    }
}

/// Role-based access control
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Role {
    pub name: String,
    pub description: String,
    pub permissions: HashSet<Permission>,
    pub created_at: DateTime<Utc>,
}

impl Role {
    pub fn new(name: String, description: String) -> Self {
        Self {
            name,
            description,
            permissions: HashSet::new(),
            created_at: Utc::now(),
        }
    }

    pub fn add_permission(&mut self, permission: Permission) {
        self.permissions.insert(permission);
    }
}

/// Fine-grained permissions
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum Permission {
    // System-level permissions
    SystemAdmin,
    SystemRead,
    SystemWrite,
    
    // User management permissions
    UserCreate,
    UserRead,
    UserUpdate,
    UserDelete,
    UserManageRoles,
    
    // Document management permissions
    DocumentCreate,
    DocumentRead,
    DocumentUpdate,
    DocumentDelete,
    DocumentIngest,
    
    // Query permissions
    QueryExecute,
    QueryAdvanced,
    QueryBulk,
    
    // Configuration permissions
    ConfigRead,
    ConfigWrite,
    ConfigSecrets,
    
    // Monitoring permissions
    MonitoringRead,
    MonitoringWrite,
    
    // API permissions
    ApiKeyCreate,
    ApiKeyRevoke,
    
    // Custom permission
    Custom(String),
}

impl std::fmt::Display for Permission {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::SystemAdmin => write!(f, "system:admin"),
            Self::SystemRead => write!(f, "system:read"),
            Self::SystemWrite => write!(f, "system:write"),
            Self::UserCreate => write!(f, "user:create"),
            Self::UserRead => write!(f, "user:read"),
            Self::UserUpdate => write!(f, "user:update"),
            Self::UserDelete => write!(f, "user:delete"),
            Self::UserManageRoles => write!(f, "user:manage_roles"),
            Self::DocumentCreate => write!(f, "document:create"),
            Self::DocumentRead => write!(f, "document:read"),
            Self::DocumentUpdate => write!(f, "document:update"),
            Self::DocumentDelete => write!(f, "document:delete"),
            Self::DocumentIngest => write!(f, "document:ingest"),
            Self::QueryExecute => write!(f, "query:execute"),
            Self::QueryAdvanced => write!(f, "query:advanced"),
            Self::QueryBulk => write!(f, "query:bulk"),
            Self::ConfigRead => write!(f, "config:read"),
            Self::ConfigWrite => write!(f, "config:write"),
            Self::ConfigSecrets => write!(f, "config:secrets"),
            Self::MonitoringRead => write!(f, "monitoring:read"),
            Self::MonitoringWrite => write!(f, "monitoring:write"),
            Self::ApiKeyCreate => write!(f, "api_key:create"),
            Self::ApiKeyRevoke => write!(f, "api_key:revoke"),
            Self::Custom(name) => write!(f, "custom:{}", name),
        }
    }
}

/// Multi-factor authentication methods
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MfaMethod {
    Totp {
        secret: String,
        backup_codes: Vec<String>,
    },
    WebAuthn {
        credential_id: String,
        public_key: String,
    },
    Sms {
        phone_number: String,
    },
    Email {
        email: String,
    },
}

/// Session representation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Session {
    pub id: String,
    pub user_id: String,
    pub created_at: DateTime<Utc>,
    pub expires_at: DateTime<Utc>,
    pub last_accessed: DateTime<Utc>,
    pub ip_address: Option<String>,
    pub user_agent: Option<String>,
    pub metadata: HashMap<String, serde_json::Value>,
}

impl Session {
    pub fn new(user_id: String, timeout_seconds: i64) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4().to_string(),
            user_id,
            created_at: now,
            expires_at: now + Duration::seconds(timeout_seconds),
            last_accessed: now,
            ip_address: None,
            user_agent: None,
            metadata: HashMap::new(),
        }
    }

    pub fn is_expired(&self) -> bool {
        Utc::now() > self.expires_at
    }

    pub fn refresh(&mut self, timeout_seconds: i64) {
        self.expires_at = Utc::now() + Duration::seconds(timeout_seconds);
        self.last_accessed = Utc::now();
    }
}

/// Authentication provider trait
#[async_trait]
pub trait AuthProvider: Send + Sync {
    async fn authenticate(&self, credentials: &Credentials) -> RragResult<AuthResult>;
    async fn validate_token(&self, token: &str) -> RragResult<User>;
    async fn supports_credentials(&self, credentials: &Credentials) -> bool;
    fn provider_name(&self) -> &str;
    async fn health_check(&self) -> RragResult<crate::security::HealthStatus>;
}

/// User storage trait
#[async_trait]
pub trait UserStore: Send + Sync {
    async fn get_user_by_id(&self, id: &str) -> RragResult<Option<User>>;
    async fn get_user_by_username(&self, username: &str) -> RragResult<Option<User>>;
    async fn get_user_by_email(&self, email: &str) -> RragResult<Option<User>>;
    async fn create_user(&self, user: &User) -> RragResult<()>;
    async fn update_user(&self, user: &User) -> RragResult<()>;
    async fn delete_user(&self, id: &str) -> RragResult<()>;
    async fn get_user_roles(&self, user_id: &str) -> RragResult<Vec<Role>>;
    async fn add_user_role(&self, user_id: &str, role: &Role) -> RragResult<()>;
    async fn remove_user_role(&self, user_id: &str, role_name: &str) -> RragResult<()>;
}

/// Session storage trait
#[async_trait]
pub trait SessionStore: Send + Sync {
    async fn create_session(&self, session: &Session) -> RragResult<()>;
    async fn get_session(&self, session_id: &str) -> RragResult<Option<Session>>;
    async fn update_session(&self, session: &Session) -> RragResult<()>;
    async fn delete_session(&self, session_id: &str) -> RragResult<()>;
    async fn delete_user_sessions(&self, user_id: &str) -> RragResult<()>;
    async fn cleanup_expired_sessions(&self) -> RragResult<u64>;
}

/// API Key authentication provider
pub struct ApiKeyAuth {
    api_keys: Arc<RwLock<HashMap<String, (String, Vec<Permission>)>>>, // key -> (user_id, permissions)
    user_store: Arc<dyn UserStore>,
}

impl ApiKeyAuth {
    pub fn new(user_store: Arc<dyn UserStore>) -> Self {
        Self {
            api_keys: Arc::new(RwLock::new(HashMap::new())),
            user_store,
        }
    }

    pub async fn create_api_key(&self, user_id: &str, permissions: Vec<Permission>) -> RragResult<String> {
        let api_key = self.generate_api_key().await?;
        let mut keys = self.api_keys.write().await;
        keys.insert(api_key.clone(), (user_id.to_string(), permissions));
        Ok(api_key)
    }

    pub async fn revoke_api_key(&self, api_key: &str) -> RragResult<()> {
        let mut keys = self.api_keys.write().await;
        keys.remove(api_key);
        Ok(())
    }

    async fn generate_api_key(&self) -> RragResult<String> {
        let mut key_bytes = vec![0u8; 32];
        use rand_core::RngCore;
        OsRng.fill_bytes(&mut key_bytes);
        Ok(format!("rrag_{}", general_purpose::URL_SAFE_NO_PAD.encode(&key_bytes)))
    }
}

#[async_trait]
impl AuthProvider for ApiKeyAuth {
    async fn authenticate(&self, credentials: &Credentials) -> RragResult<AuthResult> {
        if let Credentials::ApiKey { key } = credentials {
            let keys = self.api_keys.read().await;
            if let Some((user_id, permissions)) = keys.get(key) {
                let user = self.user_store.get_user_by_id(user_id).await?;
                if let Some(mut user) = user {
                    // Add API key permissions to user
                    for perm in permissions {
                        user.add_permission(perm.clone());
                    }
                    
                    return Ok(AuthResult {
                        success: true,
                        user: Some(user),
                        session: None,
                        method: "api_key".to_string(),
                        mfa_required: false,
                        mfa_methods: Vec::new(),
                        error: None,
                        expires_at: None,
                    });
                }
            }
        }
        Ok(AuthResult::failure("Invalid API key"))
    }

    async fn validate_token(&self, token: &str) -> RragResult<User> {
        let keys = self.api_keys.read().await;
        if let Some((user_id, permissions)) = keys.get(token) {
            let user = self.user_store.get_user_by_id(user_id).await?;
            if let Some(mut user) = user {
                for perm in permissions {
                    user.add_permission(perm.clone());
                }
                return Ok(user);
            }
        }
        Err(RragError::validation("token", "valid API key", token))
    }

    async fn supports_credentials(&self, credentials: &Credentials) -> bool {
        matches!(credentials, Credentials::ApiKey { .. })
    }

    fn provider_name(&self) -> &str {
        "api_key"
    }

    async fn health_check(&self) -> RragResult<crate::security::HealthStatus> {
        Ok(crate::security::HealthStatus::Healthy)
    }
}

/// JWT authentication provider
pub struct JwtAuth {
    encoding_key: EncodingKey,
    decoding_key: DecodingKey,
    validation: Validation,
    user_store: Arc<dyn UserStore>,
}

#[derive(Debug, Serialize, Deserialize)]
struct JwtClaims {
    sub: String,    // Subject (user ID)
    exp: usize,     // Expiration time
    iat: usize,     // Issued at
    iss: String,    // Issuer
    permissions: Vec<String>,
}

impl JwtAuth {
    pub fn new(secret: &str, user_store: Arc<dyn UserStore>) -> Self {
        let encoding_key = EncodingKey::from_secret(secret.as_ref());
        let decoding_key = DecodingKey::from_secret(secret.as_ref());
        let mut validation = Validation::default();
        validation.set_issuer(&["rrag"]);
        
        Self {
            encoding_key,
            decoding_key,
            validation,
            user_store,
        }
    }

    pub async fn create_token(&self, user: &User, expiration_seconds: i64) -> RragResult<String> {
        let now = Utc::now().timestamp() as usize;
        let exp = (now as i64 + expiration_seconds) as usize;

        let permissions: Vec<String> = user.permissions
            .iter()
            .map(|p| p.to_string())
            .collect();

        let claims = JwtClaims {
            sub: user.id.clone(),
            exp,
            iat: now,
            iss: "rrag".to_string(),
            permissions,
        };

        encode(&Header::default(), &claims, &self.encoding_key)
            .map_err(|e| RragError::rsllm_client("jwt_encode", e))
    }
}

#[async_trait]
impl AuthProvider for JwtAuth {
    async fn authenticate(&self, credentials: &Credentials) -> RragResult<AuthResult> {
        if let Credentials::JwtToken { token } = credentials {
            let token_data = decode::<JwtClaims>(token, &self.decoding_key, &self.validation)
                .map_err(|e| RragError::validation("jwt_token", "valid JWT", &e.to_string()))?;

            let user = self.user_store.get_user_by_id(&token_data.claims.sub).await?;
            if let Some(user) = user {
                return Ok(AuthResult {
                    success: true,
                    user: Some(user),
                    session: None,
                    method: "jwt".to_string(),
                    mfa_required: false,
                    mfa_methods: Vec::new(),
                    error: None,
                    expires_at: Some(DateTime::from_timestamp(token_data.claims.exp as i64, 0)
                        .unwrap_or_else(|| Utc::now())),
                });
            }
        }
        Ok(AuthResult::failure("Invalid JWT token"))
    }

    async fn validate_token(&self, token: &str) -> RragResult<User> {
        let token_data = decode::<JwtClaims>(token, &self.decoding_key, &self.validation)
            .map_err(|e| RragError::validation("jwt_token", "valid JWT", &e.to_string()))?;

        let user = self.user_store.get_user_by_id(&token_data.claims.sub).await?;
        user.ok_or_else(|| RragError::validation("user_id", "existing user", &token_data.claims.sub))
    }

    async fn supports_credentials(&self, credentials: &Credentials) -> bool {
        matches!(credentials, Credentials::JwtToken { .. })
    }

    fn provider_name(&self) -> &str {
        "jwt"
    }

    async fn health_check(&self) -> RragResult<crate::security::HealthStatus> {
        Ok(crate::security::HealthStatus::Healthy)
    }
}

/// Username/Password authentication provider
pub struct UsernamePasswordAuth {
    user_store: Arc<dyn UserStore>,
    password_hasher: PasswordHasher,
    totp_validator: Option<TotpValidator>,
}

pub enum PasswordHasher {
    Bcrypt,
    Argon2,
}

impl UsernamePasswordAuth {
    pub fn new(user_store: Arc<dyn UserStore>) -> Self {
        Self {
            user_store,
            password_hasher: PasswordHasher::Argon2,
            totp_validator: None,
        }
    }

    pub fn with_totp(mut self, totp_validator: TotpValidator) -> Self {
        self.totp_validator = Some(totp_validator);
        self
    }

    pub async fn hash_password(&self, password: &str) -> RragResult<String> {
        match &self.password_hasher {
            PasswordHasher::Bcrypt => {
                hash(password, DEFAULT_COST).map_err(|e| RragError::validation("password", "hashable", &e.to_string()))
            }
            PasswordHasher::Argon2 => {
                let salt = SaltString::generate(&mut OsRng);
                let argon2 = Argon2::default();
                let password_hash = argon2
                    .hash_password(password.as_bytes(), &salt)
                    .map_err(|e| RragError::validation("password", "hashable", &e.to_string()))?;
                Ok(password_hash.to_string())
            }
        }
    }

    pub async fn verify_password(&self, password: &str, hash: &str) -> RragResult<bool> {
        match &self.password_hasher {
            PasswordHasher::Bcrypt => {
                verify(password, hash).map_err(|e| RragError::validation("password", "verifiable", &e.to_string()))
            }
            PasswordHasher::Argon2 => {
                let parsed_hash = PasswordHash::new(hash)
                    .map_err(|e| RragError::validation("hash", "valid hash", &e.to_string()))?;
                let argon2 = Argon2::default();
                Ok(argon2.verify_password(password.as_bytes(), &parsed_hash).is_ok())
            }
        }
    }
}

#[async_trait]
impl AuthProvider for UsernamePasswordAuth {
    async fn authenticate(&self, credentials: &Credentials) -> RragResult<AuthResult> {
        if let Credentials::UsernamePassword { username, password, totp_code } = credentials {
            let user = self.user_store.get_user_by_username(username).await?;
            if let Some(user) = user {
                if user.is_locked() {
                    return Ok(AuthResult::failure("Account is locked"));
                }

                // Here you would verify the password hash stored in user metadata
                // For now, we'll assume password verification succeeds
                let password_valid = true; // This should be: self.verify_password(password, &stored_hash).await?;

                if !password_valid {
                    return Ok(AuthResult::failure("Invalid credentials"));
                }

                // Check MFA if enabled
                if user.mfa_enabled {
                    if let Some(totp_code) = totp_code {
                        if let Some(validator) = &self.totp_validator {
                            if !validator.verify_totp(&user, totp_code).await? {
                                return Ok(AuthResult::failure("Invalid TOTP code"));
                            }
                        } else {
                            return Ok(AuthResult::failure("TOTP validator not configured"));
                        }
                    } else {
                        return Ok(AuthResult::mfa_required(vec!["totp".to_string()]));
                    }
                }

                return Ok(AuthResult::success(user));
            }
        }
        Ok(AuthResult::failure("Invalid credentials"))
    }

    async fn validate_token(&self, _token: &str) -> RragResult<User> {
        Err(RragError::validation("token", "not supported for username/password auth", ""))
    }

    async fn supports_credentials(&self, credentials: &Credentials) -> bool {
        matches!(credentials, Credentials::UsernamePassword { .. })
    }

    fn provider_name(&self) -> &str {
        "username_password"
    }

    async fn health_check(&self) -> RragResult<crate::security::HealthStatus> {
        Ok(crate::security::HealthStatus::Healthy)
    }
}

/// TOTP validator
pub struct TotpValidator {
    // In a real implementation, you would use a TOTP library
}

impl TotpValidator {
    pub fn new() -> Self {
        Self {}
    }

    pub async fn verify_totp(&self, _user: &User, _code: &str) -> RragResult<bool> {
        // This would implement TOTP verification
        // For now, we'll return true as a placeholder
        Ok(true)
    }
}

/// OAuth2 authentication provider
pub struct OAuth2Auth {
    // OAuth2 implementation would go here
}

/// WebAuthn authentication provider  
pub struct WebAuthnAuth {
    // WebAuthn implementation would go here
}

/// TOTP authentication provider
pub struct TotpAuth {
    // TOTP implementation would go here
}

// Placeholder implementations for OAuth2, WebAuthn, and TOTP
impl OAuth2Auth {
    pub fn new() -> Self {
        Self {}
    }
}

impl WebAuthnAuth {
    pub fn new() -> Self {
        Self {}
    }
}

impl TotpAuth {
    pub fn new() -> Self {
        Self {}
    }
}

// Additional trait implementations would go here...

/// Authentication service implementation
impl AuthenticationService {
    pub fn new(config: AuthConfig) -> Self {
        Self {
            providers: Arc::new(RwLock::new(Vec::new())),
            config,
            user_store: Arc::new(InMemoryUserStore::new()),
            session_store: Arc::new(InMemorySessionStore::new()),
        }
    }

    pub async fn add_provider(&self, provider: Arc<dyn AuthProvider>) {
        let mut providers = self.providers.write().await;
        providers.push(provider);
    }

    pub async fn authenticate(&self, credentials: Credentials) -> RragResult<AuthResult> {
        let providers = self.providers.read().await;
        
        for provider in providers.iter() {
            if provider.supports_credentials(&credentials).await {
                return provider.authenticate(&credentials).await;
            }
        }

        Ok(AuthResult::failure("No suitable authentication provider found"))
    }

    pub async fn health_check(&self) -> RragResult<crate::security::HealthStatus> {
        Ok(crate::security::HealthStatus::Healthy)
    }
}

/// Authorization service
pub struct AuthorizationService {
    // Authorization logic would go here
}

impl AuthorizationService {
    pub fn new() -> Self {
        Self {}
    }

    pub async fn authorize(
        &self,
        user: &User,
        operation: &crate::security::SecurityOperation,
        resource: Option<&str>,
    ) -> RragResult<AuthorizationResult> {
        // Simple permission-based authorization
        let required_permission = self.get_required_permission(operation, resource);
        
        if user.has_permission(&required_permission) {
            Ok(AuthorizationResult::allowed())
        } else {
            Ok(AuthorizationResult::denied("Insufficient permissions".to_string()))
        }
    }

    fn get_required_permission(&self, operation: &crate::security::SecurityOperation, _resource: Option<&str>) -> Permission {
        match operation {
            crate::security::SecurityOperation::Authentication => Permission::SystemRead,
            crate::security::SecurityOperation::Authorization => Permission::SystemRead,
            crate::security::SecurityOperation::DataAccess => Permission::DocumentRead,
            crate::security::SecurityOperation::DocumentIngestion => Permission::DocumentIngest,
            crate::security::SecurityOperation::QueryExecution => Permission::QueryExecute,
            crate::security::SecurityOperation::AdminOperation => Permission::SystemAdmin,
            crate::security::SecurityOperation::ConfigurationChange => Permission::ConfigWrite,
            crate::security::SecurityOperation::SystemMonitoring => Permission::MonitoringRead,
        }
    }
}

/// Authorization result
#[derive(Debug, Clone)]
pub struct AuthorizationResult {
    pub allowed: bool,
    pub reason: Option<String>,
    pub required_permissions: Vec<Permission>,
}

impl AuthorizationResult {
    pub fn allowed() -> Self {
        Self {
            allowed: true,
            reason: None,
            required_permissions: Vec::new(),
        }
    }

    pub fn denied(reason: String) -> Self {
        Self {
            allowed: false,
            reason: Some(reason),
            required_permissions: Vec::new(),
        }
    }
}

/// In-memory user store implementation
pub struct InMemoryUserStore {
    users: Arc<RwLock<HashMap<String, User>>>,
    username_index: Arc<RwLock<HashMap<String, String>>>, // username -> user_id
    email_index: Arc<RwLock<HashMap<String, String>>>,    // email -> user_id
}

impl InMemoryUserStore {
    pub fn new() -> Self {
        Self {
            users: Arc::new(RwLock::new(HashMap::new())),
            username_index: Arc::new(RwLock::new(HashMap::new())),
            email_index: Arc::new(RwLock::new(HashMap::new())),
        }
    }
}

#[async_trait]
impl UserStore for InMemoryUserStore {
    async fn get_user_by_id(&self, id: &str) -> RragResult<Option<User>> {
        let users = self.users.read().await;
        Ok(users.get(id).cloned())
    }

    async fn get_user_by_username(&self, username: &str) -> RragResult<Option<User>> {
        let username_index = self.username_index.read().await;
        if let Some(user_id) = username_index.get(username) {
            self.get_user_by_id(user_id).await
        } else {
            Ok(None)
        }
    }

    async fn get_user_by_email(&self, email: &str) -> RragResult<Option<User>> {
        let email_index = self.email_index.read().await;
        if let Some(user_id) = email_index.get(email) {
            self.get_user_by_id(user_id).await
        } else {
            Ok(None)
        }
    }

    async fn create_user(&self, user: &User) -> RragResult<()> {
        let mut users = self.users.write().await;
        let mut username_index = self.username_index.write().await;
        let mut email_index = self.email_index.write().await;

        users.insert(user.id.clone(), user.clone());
        username_index.insert(user.username.clone(), user.id.clone());
        
        if let Some(email) = &user.email {
            email_index.insert(email.clone(), user.id.clone());
        }

        Ok(())
    }

    async fn update_user(&self, user: &User) -> RragResult<()> {
        let mut users = self.users.write().await;
        users.insert(user.id.clone(), user.clone());
        Ok(())
    }

    async fn delete_user(&self, id: &str) -> RragResult<()> {
        let mut users = self.users.write().await;
        let mut username_index = self.username_index.write().await;
        let mut email_index = self.email_index.write().await;

        if let Some(user) = users.remove(id) {
            username_index.remove(&user.username);
            if let Some(email) = &user.email {
                email_index.remove(email);
            }
        }

        Ok(())
    }

    async fn get_user_roles(&self, user_id: &str) -> RragResult<Vec<Role>> {
        if let Some(user) = self.get_user_by_id(user_id).await? {
            Ok(user.roles)
        } else {
            Ok(Vec::new())
        }
    }

    async fn add_user_role(&self, user_id: &str, role: &Role) -> RragResult<()> {
        if let Some(mut user) = self.get_user_by_id(user_id).await? {
            user.add_role(role.clone());
            self.update_user(&user).await?;
        }
        Ok(())
    }

    async fn remove_user_role(&self, user_id: &str, role_name: &str) -> RragResult<()> {
        if let Some(mut user) = self.get_user_by_id(user_id).await? {
            user.roles.retain(|r| r.name != role_name);
            user.updated_at = Utc::now();
            self.update_user(&user).await?;
        }
        Ok(())
    }
}

/// In-memory session store implementation
pub struct InMemorySessionStore {
    sessions: Arc<RwLock<HashMap<String, Session>>>,
    user_sessions: Arc<RwLock<HashMap<String, HashSet<String>>>>, // user_id -> session_ids
}

impl InMemorySessionStore {
    pub fn new() -> Self {
        Self {
            sessions: Arc::new(RwLock::new(HashMap::new())),
            user_sessions: Arc::new(RwLock::new(HashMap::new())),
        }
    }
}

#[async_trait]
impl SessionStore for InMemorySessionStore {
    async fn create_session(&self, session: &Session) -> RragResult<()> {
        let mut sessions = self.sessions.write().await;
        let mut user_sessions = self.user_sessions.write().await;

        sessions.insert(session.id.clone(), session.clone());
        user_sessions
            .entry(session.user_id.clone())
            .or_insert_with(HashSet::new)
            .insert(session.id.clone());

        Ok(())
    }

    async fn get_session(&self, session_id: &str) -> RragResult<Option<Session>> {
        let sessions = self.sessions.read().await;
        Ok(sessions.get(session_id).cloned())
    }

    async fn update_session(&self, session: &Session) -> RragResult<()> {
        let mut sessions = self.sessions.write().await;
        sessions.insert(session.id.clone(), session.clone());
        Ok(())
    }

    async fn delete_session(&self, session_id: &str) -> RragResult<()> {
        let mut sessions = self.sessions.write().await;
        let mut user_sessions = self.user_sessions.write().await;

        if let Some(session) = sessions.remove(session_id) {
            if let Some(user_session_set) = user_sessions.get_mut(&session.user_id) {
                user_session_set.remove(session_id);
                if user_session_set.is_empty() {
                    user_sessions.remove(&session.user_id);
                }
            }
        }

        Ok(())
    }

    async fn delete_user_sessions(&self, user_id: &str) -> RragResult<()> {
        let mut sessions = self.sessions.write().await;
        let mut user_sessions = self.user_sessions.write().await;

        if let Some(session_ids) = user_sessions.remove(user_id) {
            for session_id in session_ids {
                sessions.remove(&session_id);
            }
        }

        Ok(())
    }

    async fn cleanup_expired_sessions(&self) -> RragResult<u64> {
        let mut sessions = self.sessions.write().await;
        let mut user_sessions = self.user_sessions.write().await;
        let now = Utc::now();
        let mut expired_count = 0;

        let expired_sessions: Vec<_> = sessions
            .iter()
            .filter_map(|(id, session)| {
                if session.expires_at <= now {
                    Some((id.clone(), session.user_id.clone()))
                } else {
                    None
                }
            })
            .collect();

        for (session_id, user_id) in expired_sessions {
            sessions.remove(&session_id);
            if let Some(user_session_set) = user_sessions.get_mut(&user_id) {
                user_session_set.remove(&session_id);
                if user_session_set.is_empty() {
                    user_sessions.remove(&user_id);
                }
            }
            expired_count += 1;
        }

        Ok(expired_count)
    }
}

/// Authentication and authorization errors
#[derive(Debug, thiserror::Error)]
pub enum AuthenticationError {
    #[error("Invalid credentials")]
    InvalidCredentials,
    #[error("Account locked")]
    AccountLocked,
    #[error("Multi-factor authentication required")]
    MfaRequired,
    #[error("Token expired")]
    TokenExpired,
    #[error("User not found")]
    UserNotFound,
}

#[derive(Debug, thiserror::Error)]
pub enum AuthorizationError {
    #[error("Access denied: {reason}")]
    AccessDenied { reason: String },
    #[error("Insufficient permissions")]
    InsufficientPermissions,
    #[error("Resource not found")]
    ResourceNotFound,
}

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

    #[tokio::test]
    async fn test_user_creation() {
        let user = User::new(
            "user1".to_string(),
            "testuser".to_string(),
            Some("test@example.com".to_string()),
        );

        assert_eq!(user.id, "user1");
        assert_eq!(user.username, "testuser");
        assert!(!user.is_locked());
        assert!(!user.mfa_enabled);
    }

    #[tokio::test]
    async fn test_permission_checking() {
        let mut user = User::new(
            "user1".to_string(),
            "testuser".to_string(),
            None,
        );

        user.add_permission(Permission::DocumentRead);
        assert!(user.has_permission(&Permission::DocumentRead));
        assert!(!user.has_permission(&Permission::SystemAdmin));
    }

    #[tokio::test]
    async fn test_session_expiration() {
        let session = Session::new("user1".to_string(), 3600);
        assert!(!session.is_expired());

        let expired_session = Session {
            expires_at: Utc::now() - Duration::seconds(1),
            ..session
        };
        assert!(expired_session.is_expired());
    }

    #[tokio::test]
    async fn test_api_key_auth() {
        let user_store = Arc::new(InMemoryUserStore::new());
        let api_key_auth = ApiKeyAuth::new(user_store.clone());

        // Create a test user
        let user = User::new(
            "user1".to_string(),
            "testuser".to_string(),
            None,
        );
        user_store.create_user(&user).await.unwrap();

        // Create API key
        let api_key = api_key_auth.create_api_key("user1", vec![Permission::DocumentRead]).await.unwrap();

        // Test authentication
        let credentials = Credentials::ApiKey { key: api_key };
        let result = api_key_auth.authenticate(&credentials).await.unwrap();

        assert!(result.success);
        assert!(result.user.is_some());
        assert_eq!(result.method, "api_key");
    }
}