aethershell 0.3.1

The world's first multi-agent shell with typed functional pipelines and multi-modal AI
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
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
//! Authentication and Role-Based Access Control (RBAC)
//!
//! Comprehensive security system for AetherShell:
//! - Token-based authentication (JWT, API keys)
//! - Role-based access control with permissions
//! - Session management
//! - Audit logging
//! - Multi-factor authentication support

use anyhow::{anyhow, Result};
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::{Arc, RwLock};
use uuid::Uuid;

use crate::value::Value;

// ============================================================================
// Core Types
// ============================================================================

/// User identity
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    pub id: String,
    pub username: String,
    pub email: Option<String>,
    pub display_name: Option<String>,
    pub roles: Vec<String>,
    pub permissions: HashSet<String>,
    pub metadata: BTreeMap<String, String>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub last_login: Option<DateTime<Utc>>,
    pub active: bool,
    pub mfa_enabled: bool,
}

impl User {
    pub fn new(username: impl Into<String>) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4().to_string(),
            username: username.into(),
            email: None,
            display_name: None,
            roles: Vec::new(),
            permissions: HashSet::new(),
            metadata: BTreeMap::new(),
            created_at: now,
            updated_at: now,
            last_login: None,
            active: true,
            mfa_enabled: false,
        }
    }

    pub fn with_email(mut self, email: impl Into<String>) -> Self {
        self.email = Some(email.into());
        self
    }

    pub fn with_role(mut self, role: impl Into<String>) -> Self {
        self.roles.push(role.into());
        self
    }

    pub fn has_permission(&self, permission: &str) -> bool {
        self.permissions.contains(permission)
    }

    pub fn has_role(&self, role: &str) -> bool {
        self.roles.contains(&role.to_string())
    }
}

/// Role definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Role {
    pub id: String,
    pub name: String,
    pub description: Option<String>,
    pub permissions: HashSet<String>,
    pub parent_roles: Vec<String>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

impl Role {
    pub fn new(name: impl Into<String>) -> Self {
        let now = Utc::now();
        let name = name.into();
        Self {
            id: Uuid::new_v4().to_string(),
            name: name.clone(),
            description: None,
            permissions: HashSet::new(),
            parent_roles: Vec::new(),
            created_at: now,
            updated_at: now,
        }
    }

    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(desc.into());
        self
    }

    pub fn with_permission(mut self, perm: impl Into<String>) -> Self {
        self.permissions.insert(perm.into());
        self
    }

    pub fn with_parent(mut self, parent: impl Into<String>) -> Self {
        self.parent_roles.push(parent.into());
        self
    }
}

/// Permission definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Permission {
    pub id: String,
    pub name: String,
    pub resource: String,
    pub action: String,
    pub description: Option<String>,
}

impl Permission {
    pub fn new(resource: impl Into<String>, action: impl Into<String>) -> Self {
        let resource = resource.into();
        let action = action.into();
        let name = format!("{}:{}", resource, action);
        Self {
            id: Uuid::new_v4().to_string(),
            name,
            resource,
            action,
            description: None,
        }
    }
}

// ============================================================================
// Authentication Tokens
// ============================================================================

/// Token type enumeration
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TokenType {
    Bearer,
    ApiKey,
    Refresh,
    Service,
}

/// Authentication token
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthToken {
    pub token: String,
    pub token_type: TokenType,
    pub user_id: String,
    pub issued_at: DateTime<Utc>,
    pub expires_at: DateTime<Utc>,
    pub scopes: Vec<String>,
    pub metadata: BTreeMap<String, String>,
    pub revoked: bool,
}

impl AuthToken {
    pub fn new(user_id: impl Into<String>, token_type: TokenType, ttl_minutes: i64) -> Self {
        let now = Utc::now();
        Self {
            token: generate_token(),
            token_type,
            user_id: user_id.into(),
            issued_at: now,
            expires_at: now + Duration::minutes(ttl_minutes),
            scopes: Vec::new(),
            metadata: BTreeMap::new(),
            revoked: false,
        }
    }

    pub fn with_scope(mut self, scope: impl Into<String>) -> Self {
        self.scopes.push(scope.into());
        self
    }

    pub fn is_valid(&self) -> bool {
        !self.revoked && Utc::now() < self.expires_at
    }

    pub fn has_scope(&self, scope: &str) -> bool {
        self.scopes.contains(&scope.to_string()) || self.scopes.contains(&"*".to_string())
    }
}

fn generate_token() -> String {
    use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
    let bytes: [u8; 32] = rand::random();
    URL_SAFE_NO_PAD.encode(bytes)
}

/// API Key for programmatic access
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiKey {
    pub id: String,
    pub name: String,
    pub key_hash: String,
    pub key_prefix: String,
    pub user_id: String,
    pub scopes: Vec<String>,
    pub rate_limit: Option<u32>,
    pub created_at: DateTime<Utc>,
    pub expires_at: Option<DateTime<Utc>>,
    pub last_used: Option<DateTime<Utc>>,
    pub revoked: bool,
}

impl ApiKey {
    pub fn new(name: impl Into<String>, user_id: impl Into<String>) -> (Self, String) {
        let raw_key = generate_token();
        let key_prefix = raw_key[..8].to_string();
        let key_hash = hash_key(&raw_key);

        let api_key = Self {
            id: Uuid::new_v4().to_string(),
            name: name.into(),
            key_hash,
            key_prefix,
            user_id: user_id.into(),
            scopes: Vec::new(),
            rate_limit: None,
            created_at: Utc::now(),
            expires_at: None,
            last_used: None,
            revoked: false,
        };

        (api_key, raw_key)
    }

    pub fn verify(&self, key: &str) -> bool {
        !self.revoked && hash_key(key) == self.key_hash
    }

    pub fn is_expired(&self) -> bool {
        self.expires_at.map(|exp| Utc::now() > exp).unwrap_or(false)
    }
}

fn hash_key(key: &str) -> String {
    use sha2::{Digest, Sha256};
    let mut hasher = Sha256::new();
    hasher.update(key.as_bytes());
    format!("{:x}", hasher.finalize())
}

// ============================================================================
// Session Management
// ============================================================================

/// User session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Session {
    pub id: String,
    pub user_id: String,
    pub token: String,
    pub refresh_token: Option<String>,
    pub ip_address: Option<String>,
    pub user_agent: Option<String>,
    pub created_at: DateTime<Utc>,
    pub expires_at: DateTime<Utc>,
    pub last_activity: DateTime<Utc>,
    pub active: bool,
}

impl Session {
    pub fn new(user_id: impl Into<String>, ttl_hours: i64) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4().to_string(),
            user_id: user_id.into(),
            token: generate_token(),
            refresh_token: Some(generate_token()),
            ip_address: None,
            user_agent: None,
            created_at: now,
            expires_at: now + Duration::hours(ttl_hours),
            last_activity: now,
            active: true,
        }
    }

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

    pub fn touch(&mut self) {
        self.last_activity = Utc::now();
    }
}

// ============================================================================
// Audit Logging
// ============================================================================

/// Audit event type
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuditEventType {
    Login,
    Logout,
    LoginFailed,
    TokenCreated,
    TokenRevoked,
    PermissionGranted,
    PermissionDenied,
    RoleAssigned,
    RoleRemoved,
    UserCreated,
    UserUpdated,
    UserDeleted,
    ApiKeyCreated,
    ApiKeyRevoked,
    ConfigChanged,
    Custom(String),
}

/// Audit log entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditEntry {
    pub id: String,
    pub timestamp: DateTime<Utc>,
    pub event_type: AuditEventType,
    pub user_id: Option<String>,
    pub resource: Option<String>,
    pub action: String,
    pub result: AuditResult,
    pub ip_address: Option<String>,
    pub details: BTreeMap<String, String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuditResult {
    Success,
    Failure(String),
}

impl AuditEntry {
    pub fn new(event_type: AuditEventType, action: impl Into<String>) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            timestamp: Utc::now(),
            event_type,
            user_id: None,
            resource: None,
            action: action.into(),
            result: AuditResult::Success,
            ip_address: None,
            details: BTreeMap::new(),
        }
    }

    pub fn with_user(mut self, user_id: impl Into<String>) -> Self {
        self.user_id = Some(user_id.into());
        self
    }

    pub fn with_resource(mut self, resource: impl Into<String>) -> Self {
        self.resource = Some(resource.into());
        self
    }

    pub fn with_result(mut self, result: AuditResult) -> Self {
        self.result = result;
        self
    }

    pub fn with_detail(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.details.insert(key.into(), value.into());
        self
    }
}

// ============================================================================
// RBAC Manager
// ============================================================================

/// Role-Based Access Control Manager
#[allow(dead_code)]
pub struct RbacManager {
    users: RwLock<HashMap<String, User>>,
    roles: RwLock<HashMap<String, Role>>,
    permissions: RwLock<HashMap<String, Permission>>,
    // Cache resolved permissions for users
    permission_cache: RwLock<HashMap<String, HashSet<String>>>,
}

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

impl RbacManager {
    pub fn new() -> Self {
        let manager = Self {
            users: RwLock::new(HashMap::new()),
            roles: RwLock::new(HashMap::new()),
            permissions: RwLock::new(HashMap::new()),
            permission_cache: RwLock::new(HashMap::new()),
        };
        manager.setup_default_roles();
        manager
    }

    fn setup_default_roles(&self) {
        // Admin role with all permissions
        let admin = Role::new("admin")
            .with_description("Full system access")
            .with_permission("*:*");
        self.add_role(admin);

        // Operator role
        let operator = Role::new("operator")
            .with_description("Operations access")
            .with_permission("agent:read")
            .with_permission("agent:execute")
            .with_permission("workflow:read")
            .with_permission("workflow:execute")
            .with_permission("metrics:read");
        self.add_role(operator);

        // Viewer role
        let viewer = Role::new("viewer")
            .with_description("Read-only access")
            .with_permission("agent:read")
            .with_permission("workflow:read")
            .with_permission("metrics:read");
        self.add_role(viewer);

        // Agent role for automated agents
        let agent_role = Role::new("agent")
            .with_description("Agent service account")
            .with_permission("agent:execute")
            .with_permission("tool:use")
            .with_permission("api:call");
        self.add_role(agent_role);
    }

    pub fn add_user(&self, user: User) -> Result<()> {
        let mut users = self.users.write().map_err(|_| anyhow!("Lock poisoned"))?;
        users.insert(user.id.clone(), user);
        Ok(())
    }

    pub fn get_user(&self, user_id: &str) -> Option<User> {
        self.users.read().ok()?.get(user_id).cloned()
    }

    pub fn get_user_by_username(&self, username: &str) -> Option<User> {
        self.users
            .read()
            .ok()?
            .values()
            .find(|u| u.username == username)
            .cloned()
    }

    pub fn add_role(&self, role: Role) {
        if let Ok(mut roles) = self.roles.write() {
            roles.insert(role.name.clone(), role);
        }
    }

    pub fn get_role(&self, name: &str) -> Option<Role> {
        self.roles.read().ok()?.get(name).cloned()
    }

    pub fn assign_role(&self, user_id: &str, role_name: &str) -> Result<()> {
        let mut users = self.users.write().map_err(|_| anyhow!("Lock poisoned"))?;
        let user = users
            .get_mut(user_id)
            .ok_or_else(|| anyhow!("User not found"))?;

        if !user.roles.contains(&role_name.to_string()) {
            user.roles.push(role_name.to_string());
            user.updated_at = Utc::now();
        }

        // Invalidate permission cache
        if let Ok(mut cache) = self.permission_cache.write() {
            cache.remove(user_id);
        }

        Ok(())
    }

    pub fn remove_role(&self, user_id: &str, role_name: &str) -> Result<()> {
        let mut users = self.users.write().map_err(|_| anyhow!("Lock poisoned"))?;
        let user = users
            .get_mut(user_id)
            .ok_or_else(|| anyhow!("User not found"))?;

        user.roles.retain(|r| r != role_name);
        user.updated_at = Utc::now();

        // Invalidate permission cache
        if let Ok(mut cache) = self.permission_cache.write() {
            cache.remove(user_id);
        }

        Ok(())
    }

    pub fn resolve_permissions(&self, user_id: &str) -> HashSet<String> {
        // Check cache first
        if let Ok(cache) = self.permission_cache.read() {
            if let Some(perms) = cache.get(user_id) {
                return perms.clone();
            }
        }

        let user = match self.get_user(user_id) {
            Some(u) => u,
            None => return HashSet::new(),
        };

        let mut permissions = user.permissions.clone();

        // Resolve role permissions
        let roles = self.roles.read().ok();
        if let Some(roles) = roles {
            let mut visited = HashSet::new();
            for role_name in &user.roles {
                self.resolve_role_permissions(role_name, &roles, &mut permissions, &mut visited);
            }
        }

        // Update cache
        if let Ok(mut cache) = self.permission_cache.write() {
            cache.insert(user_id.to_string(), permissions.clone());
        }

        permissions
    }

    fn resolve_role_permissions(
        &self,
        role_name: &str,
        roles: &HashMap<String, Role>,
        permissions: &mut HashSet<String>,
        visited: &mut HashSet<String>,
    ) {
        if visited.contains(role_name) {
            return; // Avoid cycles
        }
        visited.insert(role_name.to_string());

        if let Some(role) = roles.get(role_name) {
            permissions.extend(role.permissions.clone());

            // Resolve parent roles recursively
            for parent in &role.parent_roles {
                self.resolve_role_permissions(parent, roles, permissions, visited);
            }
        }
    }

    pub fn check_permission(&self, user_id: &str, permission: &str) -> bool {
        let permissions = self.resolve_permissions(user_id);

        // Check wildcard permissions
        if permissions.contains("*:*") {
            return true;
        }

        // Check resource wildcard
        let parts: Vec<&str> = permission.split(':').collect();
        if parts.len() == 2 {
            let resource_wildcard = format!("{}:*", parts[0]);
            if permissions.contains(&resource_wildcard) {
                return true;
            }
        }

        permissions.contains(permission)
    }

    pub fn list_users(&self) -> Vec<User> {
        self.users
            .read()
            .map(|u| u.values().cloned().collect())
            .unwrap_or_default()
    }

    pub fn list_roles(&self) -> Vec<Role> {
        self.roles
            .read()
            .map(|r| r.values().cloned().collect())
            .unwrap_or_default()
    }
}

// ============================================================================
// Authentication Manager
// ============================================================================

/// Credential type for authentication
#[derive(Debug, Clone)]
pub enum Credential {
    Password { username: String, password: String },
    Token { token: String },
    ApiKey { key: String },
    OAuth { provider: String, code: String },
}

/// Authentication result
#[derive(Debug, Clone)]
pub struct AuthResult {
    pub success: bool,
    pub user: Option<User>,
    pub session: Option<Session>,
    pub token: Option<AuthToken>,
    pub error: Option<String>,
}

impl AuthResult {
    pub fn success(user: User, session: Session, token: AuthToken) -> Self {
        Self {
            success: true,
            user: Some(user),
            session: Some(session),
            token: Some(token),
            error: None,
        }
    }

    pub fn failure(error: impl Into<String>) -> Self {
        Self {
            success: false,
            user: None,
            session: None,
            token: None,
            error: Some(error.into()),
        }
    }
}

/// Authentication Manager
pub struct AuthManager {
    rbac: Arc<RbacManager>,
    sessions: RwLock<HashMap<String, Session>>,
    tokens: RwLock<HashMap<String, AuthToken>>,
    api_keys: RwLock<HashMap<String, ApiKey>>,
    audit_log: RwLock<Vec<AuditEntry>>,
    password_hashes: RwLock<HashMap<String, String>>,
    max_audit_entries: usize,
    session_ttl_hours: i64,
    token_ttl_minutes: i64,
}

impl AuthManager {
    pub fn new(rbac: Arc<RbacManager>) -> Self {
        Self {
            rbac,
            sessions: RwLock::new(HashMap::new()),
            tokens: RwLock::new(HashMap::new()),
            api_keys: RwLock::new(HashMap::new()),
            audit_log: RwLock::new(Vec::new()),
            password_hashes: RwLock::new(HashMap::new()),
            max_audit_entries: 10000,
            session_ttl_hours: 24,
            token_ttl_minutes: 60,
        }
    }

    /// Register a new user with password
    pub fn register_user(&self, username: &str, password: &str) -> Result<User> {
        // Check if username exists
        if self.rbac.get_user_by_username(username).is_some() {
            return Err(anyhow!("Username already exists"));
        }

        let user = User::new(username);
        let password_hash = hash_key(password);

        // Store user
        self.rbac.add_user(user.clone())?;

        // Store password hash
        if let Ok(mut hashes) = self.password_hashes.write() {
            hashes.insert(user.id.clone(), password_hash);
        }

        // Audit
        self.audit(
            AuditEntry::new(AuditEventType::UserCreated, "register")
                .with_user(&user.id)
                .with_detail("username", username),
        );

        Ok(user)
    }

    /// Authenticate with credentials
    pub fn authenticate(&self, credential: Credential) -> AuthResult {
        match credential {
            Credential::Password { username, password } => {
                self.authenticate_password(&username, &password)
            }
            Credential::Token { token } => self.authenticate_token(&token),
            Credential::ApiKey { key } => self.authenticate_api_key(&key),
            Credential::OAuth { provider, code } => self.authenticate_oauth(&provider, &code),
        }
    }

    fn authenticate_password(&self, username: &str, password: &str) -> AuthResult {
        let user = match self.rbac.get_user_by_username(username) {
            Some(u) => u,
            None => {
                self.audit(
                    AuditEntry::new(AuditEventType::LoginFailed, "login")
                        .with_detail("username", username)
                        .with_result(AuditResult::Failure("User not found".to_string())),
                );
                return AuthResult::failure("Invalid credentials");
            }
        };

        if !user.active {
            self.audit(
                AuditEntry::new(AuditEventType::LoginFailed, "login")
                    .with_user(&user.id)
                    .with_result(AuditResult::Failure("User inactive".to_string())),
            );
            return AuthResult::failure("User account is inactive");
        }

        // Verify password
        let password_hash = hash_key(password);
        let stored_hash = self
            .password_hashes
            .read()
            .ok()
            .and_then(|h| h.get(&user.id).cloned());

        if stored_hash != Some(password_hash) {
            self.audit(
                AuditEntry::new(AuditEventType::LoginFailed, "login")
                    .with_user(&user.id)
                    .with_result(AuditResult::Failure("Invalid password".to_string())),
            );
            return AuthResult::failure("Invalid credentials");
        }

        // Create session and token
        let session = Session::new(&user.id, self.session_ttl_hours);
        let token = AuthToken::new(&user.id, TokenType::Bearer, self.token_ttl_minutes);

        // Store session and token
        if let Ok(mut sessions) = self.sessions.write() {
            sessions.insert(session.id.clone(), session.clone());
        }
        if let Ok(mut tokens) = self.tokens.write() {
            tokens.insert(token.token.clone(), token.clone());
        }

        // Update last login
        if let Ok(mut users) = self.rbac.users.write() {
            if let Some(u) = users.get_mut(&user.id) {
                u.last_login = Some(Utc::now());
            }
        }

        self.audit(
            AuditEntry::new(AuditEventType::Login, "login")
                .with_user(&user.id)
                .with_detail("session_id", &session.id),
        );

        AuthResult::success(user, session, token)
    }

    fn authenticate_token(&self, token: &str) -> AuthResult {
        let auth_token = match self.tokens.read().ok().and_then(|t| t.get(token).cloned()) {
            Some(t) => t,
            None => return AuthResult::failure("Invalid token"),
        };

        if !auth_token.is_valid() {
            return AuthResult::failure("Token expired or revoked");
        }

        let user = match self.rbac.get_user(&auth_token.user_id) {
            Some(u) => u,
            None => return AuthResult::failure("User not found"),
        };

        if !user.active {
            return AuthResult::failure("User account is inactive");
        }

        // Create new session for token auth
        let session = Session::new(&user.id, self.session_ttl_hours);
        if let Ok(mut sessions) = self.sessions.write() {
            sessions.insert(session.id.clone(), session.clone());
        }

        AuthResult::success(user, session, auth_token)
    }

    fn authenticate_api_key(&self, key: &str) -> AuthResult {
        let api_key = self
            .api_keys
            .read()
            .ok()
            .and_then(|keys| keys.values().find(|k| k.verify(key)).cloned());

        let api_key = match api_key {
            Some(k) => k,
            None => return AuthResult::failure("Invalid API key"),
        };

        if api_key.revoked || api_key.is_expired() {
            return AuthResult::failure("API key is revoked or expired");
        }

        let user = match self.rbac.get_user(&api_key.user_id) {
            Some(u) => u,
            None => return AuthResult::failure("User not found"),
        };

        // Update last used
        if let Ok(mut keys) = self.api_keys.write() {
            if let Some(k) = keys.get_mut(&api_key.id) {
                k.last_used = Some(Utc::now());
            }
        }

        let session = Session::new(&user.id, self.session_ttl_hours);
        let token = AuthToken::new(&user.id, TokenType::ApiKey, self.token_ttl_minutes);

        AuthResult::success(user, session, token)
    }

    fn authenticate_oauth(&self, _provider: &str, _code: &str) -> AuthResult {
        // OAuth implementation would go here
        AuthResult::failure("OAuth not implemented")
    }

    /// Create a new API key
    pub fn create_api_key(&self, user_id: &str, name: &str) -> Result<(ApiKey, String)> {
        let user = self
            .rbac
            .get_user(user_id)
            .ok_or_else(|| anyhow!("User not found"))?;

        let (api_key, raw_key) = ApiKey::new(name, &user.id);

        if let Ok(mut keys) = self.api_keys.write() {
            keys.insert(api_key.id.clone(), api_key.clone());
        }

        self.audit(
            AuditEntry::new(AuditEventType::ApiKeyCreated, "create_api_key")
                .with_user(user_id)
                .with_detail("key_id", &api_key.id)
                .with_detail("key_name", name),
        );

        Ok((api_key, raw_key))
    }

    /// Revoke an API key
    pub fn revoke_api_key(&self, key_id: &str) -> Result<()> {
        if let Ok(mut keys) = self.api_keys.write() {
            if let Some(key) = keys.get_mut(key_id) {
                key.revoked = true;

                self.audit(
                    AuditEntry::new(AuditEventType::ApiKeyRevoked, "revoke_api_key")
                        .with_user(&key.user_id)
                        .with_detail("key_id", key_id),
                );

                return Ok(());
            }
        }
        Err(anyhow!("API key not found"))
    }

    /// Validate a session
    pub fn validate_session(&self, session_id: &str) -> Option<Session> {
        let session = self.sessions.read().ok()?.get(session_id).cloned()?;

        if session.is_valid() {
            // Touch session
            if let Ok(mut sessions) = self.sessions.write() {
                if let Some(s) = sessions.get_mut(session_id) {
                    s.touch();
                }
            }
            Some(session)
        } else {
            None
        }
    }

    /// Logout / invalidate session
    pub fn logout(&self, session_id: &str) -> Result<()> {
        if let Ok(mut sessions) = self.sessions.write() {
            if let Some(session) = sessions.get_mut(session_id) {
                session.active = false;

                self.audit(
                    AuditEntry::new(AuditEventType::Logout, "logout")
                        .with_user(&session.user_id)
                        .with_detail("session_id", session_id),
                );

                return Ok(());
            }
        }
        Err(anyhow!("Session not found"))
    }

    /// Check if user has permission
    pub fn check_permission(&self, user_id: &str, permission: &str) -> bool {
        let allowed = self.rbac.check_permission(user_id, permission);

        if !allowed {
            self.audit(
                AuditEntry::new(AuditEventType::PermissionDenied, "check_permission")
                    .with_user(user_id)
                    .with_resource(permission)
                    .with_result(AuditResult::Failure("Permission denied".to_string())),
            );
        }

        allowed
    }

    /// Get audit log
    pub fn get_audit_log(&self, limit: usize) -> Vec<AuditEntry> {
        self.audit_log
            .read()
            .map(|l| l.iter().rev().take(limit).cloned().collect())
            .unwrap_or_default()
    }

    /// Get audit log for a specific user
    pub fn get_user_audit_log(&self, user_id: &str, limit: usize) -> Vec<AuditEntry> {
        self.audit_log
            .read()
            .map(|l| {
                l.iter()
                    .filter(|e| e.user_id.as_ref() == Some(&user_id.to_string()))
                    .rev()
                    .take(limit)
                    .cloned()
                    .collect()
            })
            .unwrap_or_default()
    }

    fn audit(&self, entry: AuditEntry) {
        if let Ok(mut log) = self.audit_log.write() {
            if log.len() >= self.max_audit_entries {
                log.remove(0);
            }
            log.push(entry);
        }
    }

    /// Get active sessions for a user
    pub fn get_user_sessions(&self, user_id: &str) -> Vec<Session> {
        self.sessions
            .read()
            .map(|s| {
                s.values()
                    .filter(|session| session.user_id == user_id && session.is_valid())
                    .cloned()
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Export auth summary as Value
    pub fn summary(&self) -> Value {
        let mut result: BTreeMap<String, Value> = BTreeMap::new();

        // Users
        let users: Vec<Value> = self
            .rbac
            .list_users()
            .iter()
            .map(|u| {
                let mut m: BTreeMap<String, Value> = BTreeMap::new();
                m.insert("id".to_string(), Value::Str(u.id.clone()));
                m.insert("username".to_string(), Value::Str(u.username.clone()));
                m.insert(
                    "roles".to_string(),
                    Value::Array(u.roles.iter().map(|r| Value::Str(r.clone())).collect()),
                );
                m.insert("active".to_string(), Value::Bool(u.active));
                Value::Record(m)
            })
            .collect();
        result.insert("users".to_string(), Value::Array(users));

        // Roles
        let roles: Vec<Value> = self
            .rbac
            .list_roles()
            .iter()
            .map(|r| {
                let mut m: BTreeMap<String, Value> = BTreeMap::new();
                m.insert("name".to_string(), Value::Str(r.name.clone()));
                m.insert(
                    "permissions".to_string(),
                    Value::Array(
                        r.permissions
                            .iter()
                            .map(|p| Value::Str(p.clone()))
                            .collect(),
                    ),
                );
                Value::Record(m)
            })
            .collect();
        result.insert("roles".to_string(), Value::Array(roles));

        // Active sessions count
        let active_sessions = self
            .sessions
            .read()
            .map(|s| s.values().filter(|sess| sess.is_valid()).count())
            .unwrap_or(0);
        result.insert(
            "active_sessions".to_string(),
            Value::Int(active_sessions as i64),
        );

        // API keys count
        let api_keys = self
            .api_keys
            .read()
            .map(|k| k.values().filter(|key| !key.revoked).count())
            .unwrap_or(0);
        result.insert("active_api_keys".to_string(), Value::Int(api_keys as i64));

        // Recent audit entries
        let audit: Vec<Value> = self
            .get_audit_log(10)
            .iter()
            .map(|e| {
                let mut m: BTreeMap<String, Value> = BTreeMap::new();
                m.insert(
                    "timestamp".to_string(),
                    Value::Str(e.timestamp.to_rfc3339()),
                );
                m.insert(
                    "event".to_string(),
                    Value::Str(format!("{:?}", e.event_type)),
                );
                m.insert("action".to_string(), Value::Str(e.action.clone()));
                if let Some(ref user) = e.user_id {
                    m.insert("user".to_string(), Value::Str(user.clone()));
                }
                Value::Record(m)
            })
            .collect();
        result.insert("recent_audit".to_string(), Value::Array(audit));

        Value::Record(result)
    }
}

// ============================================================================
// Builtins for Shell Integration
// ============================================================================

/// Create auth builtins for AetherShell
pub fn create_auth_builtins() -> BTreeMap<String, Value> {
    let mut builtins: BTreeMap<String, Value> = BTreeMap::new();

    builtins.insert(
        "auth_login".to_string(),
        Value::Str("Authenticate user".to_string()),
    );
    builtins.insert(
        "auth_logout".to_string(),
        Value::Str("End current session".to_string()),
    );
    builtins.insert(
        "auth_whoami".to_string(),
        Value::Str("Get current user info".to_string()),
    );
    builtins.insert(
        "auth_create_key".to_string(),
        Value::Str("Create API key".to_string()),
    );
    builtins.insert(
        "auth_revoke_key".to_string(),
        Value::Str("Revoke API key".to_string()),
    );
    builtins.insert(
        "auth_check".to_string(),
        Value::Str("Check permission".to_string()),
    );
    builtins.insert(
        "auth_audit".to_string(),
        Value::Str("View audit log".to_string()),
    );
    builtins.insert(
        "rbac_users".to_string(),
        Value::Str("List users".to_string()),
    );
    builtins.insert(
        "rbac_roles".to_string(),
        Value::Str("List roles".to_string()),
    );
    builtins.insert(
        "rbac_assign".to_string(),
        Value::Str("Assign role to user".to_string()),
    );

    builtins
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_user_creation() {
        let user = User::new("testuser")
            .with_email("test@example.com")
            .with_role("viewer");

        assert_eq!(user.username, "testuser");
        assert_eq!(user.email, Some("test@example.com".to_string()));
        assert!(user.has_role("viewer"));
        assert!(user.active);
    }

    #[test]
    fn test_role_creation() {
        let role = Role::new("custom")
            .with_description("Custom role")
            .with_permission("resource:read")
            .with_permission("resource:write");

        assert_eq!(role.name, "custom");
        assert!(role.permissions.contains("resource:read"));
        assert!(role.permissions.contains("resource:write"));
    }

    #[test]
    fn test_permission_format() {
        let perm = Permission::new("agent", "execute");
        assert_eq!(perm.name, "agent:execute");
        assert_eq!(perm.resource, "agent");
        assert_eq!(perm.action, "execute");
    }

    #[test]
    fn test_auth_token() {
        let token = AuthToken::new("user123", TokenType::Bearer, 60)
            .with_scope("read")
            .with_scope("write");

        assert!(token.is_valid());
        assert!(token.has_scope("read"));
        assert!(token.has_scope("write"));
        assert!(!token.has_scope("admin"));
    }

    #[test]
    fn test_api_key() {
        let (api_key, raw_key) = ApiKey::new("test-key", "user123");

        assert!(api_key.verify(&raw_key));
        assert!(!api_key.verify("wrong-key"));
        assert!(!api_key.revoked);
    }

    #[test]
    fn test_session() {
        let session = Session::new("user123", 24);

        assert!(session.is_valid());
        assert_eq!(session.user_id, "user123");
        assert!(session.active);
    }

    #[test]
    fn test_rbac_manager_default_roles() {
        let rbac = RbacManager::new();

        assert!(rbac.get_role("admin").is_some());
        assert!(rbac.get_role("operator").is_some());
        assert!(rbac.get_role("viewer").is_some());
    }

    #[test]
    fn test_rbac_permission_resolution() {
        let rbac = RbacManager::new();

        let user = User::new("testuser");
        rbac.add_user(user.clone()).unwrap();
        rbac.assign_role(&user.id, "viewer").unwrap();

        assert!(rbac.check_permission(&user.id, "agent:read"));
        assert!(!rbac.check_permission(&user.id, "agent:write"));
    }

    #[test]
    fn test_rbac_admin_wildcard() {
        let rbac = RbacManager::new();

        let user = User::new("admin_user");
        rbac.add_user(user.clone()).unwrap();
        rbac.assign_role(&user.id, "admin").unwrap();

        // Admin should have all permissions
        assert!(rbac.check_permission(&user.id, "anything:everything"));
    }

    #[test]
    fn test_auth_manager_registration() {
        let rbac = Arc::new(RbacManager::new());
        let auth = AuthManager::new(rbac);

        let user = auth.register_user("newuser", "password123").unwrap();
        assert_eq!(user.username, "newuser");
    }

    #[test]
    fn test_auth_manager_login() {
        let rbac = Arc::new(RbacManager::new());
        let auth = AuthManager::new(Arc::clone(&rbac));

        auth.register_user("logintest", "secret").unwrap();

        let result = auth.authenticate(Credential::Password {
            username: "logintest".to_string(),
            password: "secret".to_string(),
        });

        assert!(result.success);
        assert!(result.user.is_some());
        assert!(result.session.is_some());
        assert!(result.token.is_some());
    }

    #[test]
    fn test_auth_manager_invalid_login() {
        let rbac = Arc::new(RbacManager::new());
        let auth = AuthManager::new(Arc::clone(&rbac));

        auth.register_user("user1", "correct").unwrap();

        let result = auth.authenticate(Credential::Password {
            username: "user1".to_string(),
            password: "wrong".to_string(),
        });

        assert!(!result.success);
        assert!(result.error.is_some());
    }

    #[test]
    fn test_api_key_authentication() {
        let rbac = Arc::new(RbacManager::new());
        let auth = AuthManager::new(Arc::clone(&rbac));

        let user = auth.register_user("apiuser", "pass").unwrap();
        let (_, raw_key) = auth.create_api_key(&user.id, "test-key").unwrap();

        let result = auth.authenticate(Credential::ApiKey { key: raw_key });

        assert!(result.success);
    }

    #[test]
    fn test_token_authentication() {
        let rbac = Arc::new(RbacManager::new());
        let auth = AuthManager::new(Arc::clone(&rbac));

        auth.register_user("tokenuser", "pass").unwrap();

        let login_result = auth.authenticate(Credential::Password {
            username: "tokenuser".to_string(),
            password: "pass".to_string(),
        });

        let token = login_result.token.unwrap().token;

        let result = auth.authenticate(Credential::Token { token });
        assert!(result.success);
    }

    #[test]
    fn test_session_validation() {
        let rbac = Arc::new(RbacManager::new());
        let auth = AuthManager::new(Arc::clone(&rbac));

        auth.register_user("sessuser", "pass").unwrap();

        let result = auth.authenticate(Credential::Password {
            username: "sessuser".to_string(),
            password: "pass".to_string(),
        });

        let session_id = result.session.unwrap().id;

        assert!(auth.validate_session(&session_id).is_some());
        assert!(auth.validate_session("invalid").is_none());
    }

    #[test]
    fn test_logout() {
        let rbac = Arc::new(RbacManager::new());
        let auth = AuthManager::new(Arc::clone(&rbac));

        auth.register_user("logoutuser", "pass").unwrap();

        let result = auth.authenticate(Credential::Password {
            username: "logoutuser".to_string(),
            password: "pass".to_string(),
        });

        let session_id = result.session.unwrap().id;

        auth.logout(&session_id).unwrap();

        // Session should no longer be valid
        assert!(auth.validate_session(&session_id).is_none());
    }

    #[test]
    fn test_audit_log() {
        let rbac = Arc::new(RbacManager::new());
        let auth = AuthManager::new(Arc::clone(&rbac));

        auth.register_user("audituser", "pass").unwrap();

        auth.authenticate(Credential::Password {
            username: "audituser".to_string(),
            password: "pass".to_string(),
        });

        let audit = auth.get_audit_log(10);
        assert!(!audit.is_empty());

        let has_login = audit.iter().any(|e| e.event_type == AuditEventType::Login);
        assert!(has_login);
    }

    #[test]
    fn test_revoke_api_key() {
        let rbac = Arc::new(RbacManager::new());
        let auth = AuthManager::new(Arc::clone(&rbac));

        let user = auth.register_user("revokeuser", "pass").unwrap();
        let (api_key, raw_key) = auth.create_api_key(&user.id, "revoke-test").unwrap();

        // Should work before revocation
        let result = auth.authenticate(Credential::ApiKey {
            key: raw_key.clone(),
        });
        assert!(result.success);

        // Revoke
        auth.revoke_api_key(&api_key.id).unwrap();

        // Should fail after revocation
        let result = auth.authenticate(Credential::ApiKey { key: raw_key });
        assert!(!result.success);
    }

    #[test]
    fn test_permission_check_with_audit() {
        let rbac = Arc::new(RbacManager::new());
        let auth = AuthManager::new(Arc::clone(&rbac));

        let user = auth.register_user("permuser", "pass").unwrap();
        rbac.assign_role(&user.id, "viewer").unwrap();

        // This should pass
        assert!(auth.check_permission(&user.id, "agent:read"));

        // This should fail and be audited
        assert!(!auth.check_permission(&user.id, "admin:action"));

        let audit = auth.get_audit_log(10);
        let has_denied = audit
            .iter()
            .any(|e| e.event_type == AuditEventType::PermissionDenied);
        assert!(has_denied);
    }

    #[test]
    fn test_auth_summary() {
        let rbac = Arc::new(RbacManager::new());
        let auth = AuthManager::new(Arc::clone(&rbac));

        auth.register_user("summaryuser", "pass").unwrap();

        let summary = auth.summary();
        match summary {
            Value::Record(r) => {
                assert!(r.contains_key("users"));
                assert!(r.contains_key("roles"));
                assert!(r.contains_key("active_sessions"));
            }
            _ => panic!("Expected Record"),
        }
    }

    #[test]
    fn test_audit_entry_builder() {
        let entry = AuditEntry::new(AuditEventType::Login, "test_action")
            .with_user("user123")
            .with_resource("session")
            .with_detail("ip", "127.0.0.1")
            .with_result(AuditResult::Success);

        assert_eq!(entry.event_type, AuditEventType::Login);
        assert_eq!(entry.user_id, Some("user123".to_string()));
        assert_eq!(entry.resource, Some("session".to_string()));
        assert_eq!(entry.details.get("ip"), Some(&"127.0.0.1".to_string()));
    }

    #[test]
    fn test_role_inheritance() {
        let rbac = RbacManager::new();

        // Create a role that inherits from viewer
        let custom = Role::new("custom_viewer")
            .with_parent("viewer")
            .with_permission("custom:action");
        rbac.add_role(custom);

        let user = User::new("inherituser");
        rbac.add_user(user.clone()).unwrap();
        rbac.assign_role(&user.id, "custom_viewer").unwrap();

        // Should have viewer permissions
        assert!(rbac.check_permission(&user.id, "agent:read"));
        // Should have custom permission
        assert!(rbac.check_permission(&user.id, "custom:action"));
    }

    #[test]
    fn test_auth_builtins() {
        let builtins = create_auth_builtins();
        assert!(builtins.contains_key("auth_login"));
        assert!(builtins.contains_key("auth_logout"));
        assert!(builtins.contains_key("rbac_users"));
        assert!(builtins.contains_key("rbac_assign"));
    }

    #[test]
    fn test_duplicate_username() {
        let rbac = Arc::new(RbacManager::new());
        let auth = AuthManager::new(Arc::clone(&rbac));

        auth.register_user("duplicate", "pass1").unwrap();
        let result = auth.register_user("duplicate", "pass2");

        assert!(result.is_err());
    }
}