p2p-foundation 0.1.6

Complete P2P networking foundation with sparkly interactive help system, DHT inboxes with infinite TTL, embedded Flutter PWA, native app support, three-word addresses, and built-in AI capabilities
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
//! MCP Security Module
//!
//! This module provides comprehensive security features for the MCP server including:
//! - JWT-based authentication
//! - Peer identity verification
//! - Access control and permissions
//! - Rate limiting and abuse prevention
//! - Message integrity and encryption

use crate::{PeerId, Result, P2PError};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use sha2::{Digest, Sha256};
use tokio::sync::RwLock;
use std::sync::Arc;
use base64::prelude::*;

/// JWT-like token structure for MCP authentication
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MCPToken {
    /// Token header
    pub header: TokenHeader,
    /// Token payload
    pub payload: TokenPayload,
    /// Token signature
    pub signature: String,
}

/// Token header information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenHeader {
    /// Algorithm used for signing
    pub alg: String,
    /// Token type
    pub typ: String,
    /// Key ID
    pub kid: Option<String>,
}

/// Token payload with claims
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenPayload {
    /// Issuer (peer ID)
    pub iss: PeerId,
    /// Subject (target peer ID or tool)
    pub sub: String,
    /// Audience (intended recipient)
    pub aud: String,
    /// Expiration time (Unix timestamp)
    pub exp: u64,
    /// Not before time (Unix timestamp)
    pub nbf: u64,
    /// Issued at time (Unix timestamp)
    pub iat: u64,
    /// JWT ID
    pub jti: String,
    /// Custom claims
    pub claims: HashMap<String, serde_json::Value>,
}

/// Security level for MCP operations
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum SecurityLevel {
    /// Public access - no authentication required
    Public,
    /// Basic authentication required
    Basic,
    /// Strong authentication required
    Strong,
    /// Administrative access required
    Admin,
}

/// Permission for MCP operations
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MCPPermission {
    /// Read access to tools
    ReadTools,
    /// Execute tools
    ExecuteTools,
    /// Register new tools
    RegisterTools,
    /// Modify existing tools
    ModifyTools,
    /// Delete tools
    DeleteTools,
    /// Access prompts
    AccessPrompts,
    /// Access resources
    AccessResources,
    /// Administrative access
    Admin,
    /// Custom permission
    Custom(String),
}

impl MCPPermission {
    /// Get permission string representation
    pub fn as_str(&self) -> &str {
        match self {
            MCPPermission::ReadTools => "read:tools",
            MCPPermission::ExecuteTools => "execute:tools",
            MCPPermission::RegisterTools => "register:tools",
            MCPPermission::ModifyTools => "modify:tools",
            MCPPermission::DeleteTools => "delete:tools",
            MCPPermission::AccessPrompts => "access:prompts",
            MCPPermission::AccessResources => "access:resources",
            MCPPermission::Admin => "admin",
            MCPPermission::Custom(s) => s,
        }
    }
    
    /// Parse permission from string
    pub fn from_str(s: &str) -> Option<Self> {
        match s {
            "read:tools" => Some(MCPPermission::ReadTools),
            "execute:tools" => Some(MCPPermission::ExecuteTools),
            "register:tools" => Some(MCPPermission::RegisterTools),
            "modify:tools" => Some(MCPPermission::ModifyTools),
            "delete:tools" => Some(MCPPermission::DeleteTools),
            "access:prompts" => Some(MCPPermission::AccessPrompts),
            "access:resources" => Some(MCPPermission::AccessResources),
            "admin" => Some(MCPPermission::Admin),
            _ => Some(MCPPermission::Custom(s.to_string())),
        }
    }
}

/// Access control list for a peer
#[derive(Debug, Clone)]
pub struct PeerACL {
    /// Peer ID
    pub peer_id: PeerId,
    /// Granted permissions
    pub permissions: Vec<MCPPermission>,
    /// Security level
    pub security_level: SecurityLevel,
    /// Reputation score (0.0 to 1.0)
    pub reputation: f64,
    /// Last access time
    pub last_access: SystemTime,
    /// Access count
    pub access_count: u64,
    /// Rate limit violations
    pub rate_violations: u32,
    /// Banned until (if applicable)
    pub banned_until: Option<SystemTime>,
}

impl PeerACL {
    /// Create new peer ACL with default permissions
    pub fn new(peer_id: PeerId) -> Self {
        Self {
            peer_id,
            permissions: vec![MCPPermission::ReadTools, MCPPermission::ExecuteTools],
            security_level: SecurityLevel::Basic,
            reputation: 0.5, // Start with neutral reputation
            last_access: SystemTime::now(),
            access_count: 0,
            rate_violations: 0,
            banned_until: None,
        }
    }
    
    /// Check if peer has specific permission
    pub fn has_permission(&self, permission: &MCPPermission) -> bool {
        if self.is_banned() {
            return false;
        }
        
        // Admin permission grants all access
        if self.permissions.contains(&MCPPermission::Admin) {
            return true;
        }
        
        self.permissions.contains(permission)
    }
    
    /// Check if peer is currently banned
    pub fn is_banned(&self) -> bool {
        if let Some(banned_until) = self.banned_until {
            SystemTime::now() < banned_until
        } else {
            false
        }
    }
    
    /// Update access statistics
    pub fn record_access(&mut self) {
        self.last_access = SystemTime::now();
        self.access_count += 1;
    }
    
    /// Record rate limit violation
    pub fn record_rate_violation(&mut self) {
        self.rate_violations += 1;
        
        // Auto-ban after too many violations
        if self.rate_violations >= 10 {
            self.banned_until = Some(SystemTime::now() + Duration::from_secs(3600)); // 1 hour
        }
    }
    
    /// Grant permission to peer
    pub fn grant_permission(&mut self, permission: MCPPermission) {
        if !self.permissions.contains(&permission) {
            self.permissions.push(permission);
        }
    }
    
    /// Revoke permission from peer
    pub fn revoke_permission(&mut self, permission: &MCPPermission) {
        self.permissions.retain(|p| p != permission);
    }
}

/// Rate limiter for controlling request frequency
#[derive(Debug, Clone)]
pub struct RateLimiter {
    /// Requests per minute limit
    pub rpm_limit: u32,
    /// Request timestamps for each peer
    requests: Arc<RwLock<HashMap<PeerId, Vec<SystemTime>>>>,
}

impl RateLimiter {
    /// Create new rate limiter
    pub fn new(rpm_limit: u32) -> Self {
        Self {
            rpm_limit,
            requests: Arc::new(RwLock::new(HashMap::new())),
        }
    }
    
    /// Check if request is allowed for peer
    pub async fn is_allowed(&self, peer_id: &PeerId) -> bool {
        let mut requests = self.requests.write().await;
        let now = SystemTime::now();
        let minute_ago = now - Duration::from_secs(60);
        
        // Get or create request history for peer
        let peer_requests = requests.entry(peer_id.clone()).or_insert_with(Vec::new);
        
        // Remove old requests (older than 1 minute)
        peer_requests.retain(|&req_time| req_time > minute_ago);
        
        // Check if under limit
        if peer_requests.len() < self.rpm_limit as usize {
            peer_requests.push(now);
            true
        } else {
            false
        }
    }
    
    /// Reset rate limit for peer (admin function)
    pub async fn reset_peer(&self, peer_id: &PeerId) {
        let mut requests = self.requests.write().await;
        requests.remove(peer_id);
    }
    
    /// Clean up old entries periodically
    pub async fn cleanup(&self) {
        let mut requests = self.requests.write().await;
        let minute_ago = SystemTime::now() - Duration::from_secs(60);
        
        for peer_requests in requests.values_mut() {
            peer_requests.retain(|&req_time| req_time > minute_ago);
        }
        
        // Remove empty entries
        requests.retain(|_, reqs| !reqs.is_empty());
    }
}

/// MCP Security Manager
pub struct MCPSecurityManager {
    /// Access control lists
    acls: Arc<RwLock<HashMap<PeerId, PeerACL>>>,
    /// Rate limiter
    rate_limiter: RateLimiter,
    /// Shared secret for token signing
    secret_key: Vec<u8>,
    /// Tool security policies
    tool_policies: Arc<RwLock<HashMap<String, SecurityLevel>>>,
    /// Trusted peer list
    trusted_peers: Arc<RwLock<Vec<PeerId>>>,
}

impl MCPSecurityManager {
    /// Create new security manager
    pub fn new(secret_key: Vec<u8>, rpm_limit: u32) -> Self {
        Self {
            acls: Arc::new(RwLock::new(HashMap::new())),
            rate_limiter: RateLimiter::new(rpm_limit),
            secret_key,
            tool_policies: Arc::new(RwLock::new(HashMap::new())),
            trusted_peers: Arc::new(RwLock::new(Vec::new())),
        }
    }
    
    /// Generate authentication token for peer
    pub async fn generate_token(&self, peer_id: &PeerId, permissions: Vec<MCPPermission>, ttl: Duration) -> Result<String> {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|e| P2PError::MCP(format!("Time error: {}", e)))?;
        
        let payload = TokenPayload {
            iss: peer_id.clone(),
            sub: peer_id.clone(),
            aud: "mcp-server".to_string(),
            exp: (now + ttl).as_secs(),
            nbf: now.as_secs(),
            iat: now.as_secs(),
            jti: uuid::Uuid::new_v4().to_string(),
            claims: {
                let mut claims = HashMap::new();
                claims.insert("permissions".to_string(), 
                    serde_json::to_value(permissions.iter().map(|p| p.as_str()).collect::<Vec<_>>()).unwrap());
                claims
            },
        };
        
        let header = TokenHeader {
            alg: "HS256".to_string(),
            typ: "JWT".to_string(),
            kid: None,
        };
        
        // Create token without signature first
        let header_b64 = base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(serde_json::to_vec(&header)
            .map_err(|e| P2PError::Serialization(e))?);
        let payload_b64 = base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(serde_json::to_vec(&payload)
            .map_err(|e| P2PError::Serialization(e))?);
        
        // Sign the token
        let signing_input = format!("{}.{}", header_b64, payload_b64);
        let signature = self.sign_data(signing_input.as_bytes());
        let signature_b64 = base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(signature);
        
        Ok(format!("{}.{}.{}", header_b64, payload_b64, signature_b64))
    }
    
    /// Verify authentication token
    pub async fn verify_token(&self, token: &str) -> Result<TokenPayload> {
        let parts: Vec<&str> = token.split('.').collect();
        if parts.len() != 3 {
            return Err(P2PError::MCP("Invalid token format".to_string()));
        }
        
        let _header_data = base64::prelude::BASE64_URL_SAFE_NO_PAD.decode(parts[0])
            .map_err(|e| P2PError::MCP(format!("Invalid header encoding: {}", e)))?;
        let payload_data = base64::prelude::BASE64_URL_SAFE_NO_PAD.decode(parts[1])
            .map_err(|e| P2PError::MCP(format!("Invalid payload encoding: {}", e)))?;
        let signature = base64::prelude::BASE64_URL_SAFE_NO_PAD.decode(parts[2])
            .map_err(|e| P2PError::MCP(format!("Invalid signature encoding: {}", e)))?;
        
        // Verify signature
        let signing_input = format!("{}.{}", parts[0], parts[1]);
        let expected_signature = self.sign_data(signing_input.as_bytes());
        
        if signature != expected_signature {
            return Err(P2PError::MCP("Invalid token signature".to_string()));
        }
        
        // Parse payload
        let payload: TokenPayload = serde_json::from_slice(&payload_data)
            .map_err(|e| P2PError::MCP(format!("Invalid payload: {}", e)))?;
        
        // Check expiration
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|e| P2PError::MCP(format!("Time error: {}", e)))?
            .as_secs();
        
        if payload.exp < now {
            return Err(P2PError::MCP("Token expired".to_string()));
        }
        
        if payload.nbf > now {
            return Err(P2PError::MCP("Token not yet valid".to_string()));
        }
        
        Ok(payload)
    }
    
    /// Check if peer has permission for operation
    pub async fn check_permission(&self, peer_id: &PeerId, permission: &MCPPermission) -> Result<bool> {
        let acls = self.acls.read().await;
        
        if let Some(acl) = acls.get(peer_id) {
            Ok(acl.has_permission(permission))
        } else {
            // Create default ACL for new peer
            drop(acls);
            let mut acls = self.acls.write().await;
            acls.insert(peer_id.clone(), PeerACL::new(peer_id.clone()));
            Ok(false) // New peers start with no permissions by default
        }
    }
    
    /// Check rate limit for peer
    pub async fn check_rate_limit(&self, peer_id: &PeerId) -> Result<bool> {
        if self.rate_limiter.is_allowed(peer_id).await {
            Ok(true)
        } else {
            // Record violation
            let mut acls = self.acls.write().await;
            if let Some(acl) = acls.get_mut(peer_id) {
                acl.record_rate_violation();
            }
            Ok(false)
        }
    }
    
    /// Grant permission to peer
    pub async fn grant_permission(&self, peer_id: &PeerId, permission: MCPPermission) -> Result<()> {
        let mut acls = self.acls.write().await;
        let acl = acls.entry(peer_id.clone()).or_insert_with(|| PeerACL::new(peer_id.clone()));
        acl.grant_permission(permission);
        Ok(())
    }
    
    /// Revoke permission from peer
    pub async fn revoke_permission(&self, peer_id: &PeerId, permission: &MCPPermission) -> Result<()> {
        let mut acls = self.acls.write().await;
        if let Some(acl) = acls.get_mut(peer_id) {
            acl.revoke_permission(permission);
        }
        Ok(())
    }
    
    /// Add trusted peer
    pub async fn add_trusted_peer(&self, peer_id: PeerId) -> Result<()> {
        let mut trusted = self.trusted_peers.write().await;
        if !trusted.contains(&peer_id) {
            trusted.push(peer_id);
        }
        Ok(())
    }
    
    /// Check if peer is trusted
    pub async fn is_trusted_peer(&self, peer_id: &PeerId) -> bool {
        let trusted = self.trusted_peers.read().await;
        trusted.contains(peer_id)
    }
    
    /// Set security policy for tool
    pub async fn set_tool_policy(&self, tool_name: String, level: SecurityLevel) -> Result<()> {
        let mut policies = self.tool_policies.write().await;
        policies.insert(tool_name, level);
        Ok(())
    }
    
    /// Get security policy for tool
    pub async fn get_tool_policy(&self, tool_name: &str) -> SecurityLevel {
        let policies = self.tool_policies.read().await;
        policies.get(tool_name).cloned().unwrap_or(SecurityLevel::Basic)
    }
    
    /// Sign data with secret key
    fn sign_data(&self, data: &[u8]) -> Vec<u8> {
        let mut hasher = Sha256::new();
        hasher.update(&self.secret_key);
        hasher.update(data);
        hasher.finalize().to_vec()
    }
    
    /// Update peer reputation based on behavior
    pub async fn update_reputation(&self, peer_id: &PeerId, delta: f64) -> Result<()> {
        let mut acls = self.acls.write().await;
        if let Some(acl) = acls.get_mut(peer_id) {
            acl.reputation = (acl.reputation + delta).max(0.0).min(1.0);
        }
        Ok(())
    }
    
    /// Get peer statistics
    pub async fn get_peer_stats(&self, peer_id: &PeerId) -> Option<PeerACL> {
        let acls = self.acls.read().await;
        acls.get(peer_id).cloned()
    }
    
    /// Clean up expired data
    pub async fn cleanup(&self) -> Result<()> {
        self.rate_limiter.cleanup().await;
        
        // Clean up old ACLs (remove entries not accessed in 24 hours)
        let mut acls = self.acls.write().await;
        let day_ago = SystemTime::now() - Duration::from_secs(24 * 3600);
        acls.retain(|_, acl| acl.last_access > day_ago);
        
        Ok(())
    }
}

/// Security audit log entry
#[derive(Debug, Clone)]
pub struct SecurityAuditEntry {
    /// Timestamp
    pub timestamp: SystemTime,
    /// Event type
    pub event_type: String,
    /// Peer ID involved
    pub peer_id: PeerId,
    /// Event details
    pub details: HashMap<String, String>,
    /// Severity level
    pub severity: AuditSeverity,
}

/// Audit severity levels
#[derive(Debug, Clone, PartialEq)]
pub enum AuditSeverity {
    /// Informational
    Info,
    /// Warning
    Warning,
    /// Error
    Error,
    /// Critical security event
    Critical,
}

/// Security audit logger
pub struct SecurityAuditLogger {
    /// Audit entries
    entries: Arc<RwLock<Vec<SecurityAuditEntry>>>,
    /// Maximum entries to keep
    max_entries: usize,
}

impl SecurityAuditLogger {
    /// Create new audit logger
    pub fn new(max_entries: usize) -> Self {
        Self {
            entries: Arc::new(RwLock::new(Vec::new())),
            max_entries,
        }
    }
    
    /// Log security event
    pub async fn log_event(&self, event_type: String, peer_id: PeerId, details: HashMap<String, String>, severity: AuditSeverity) {
        let entry = SecurityAuditEntry {
            timestamp: SystemTime::now(),
            event_type,
            peer_id,
            details,
            severity,
        };
        
        let mut entries = self.entries.write().await;
        entries.push(entry);
        
        // Keep only recent entries
        if entries.len() > self.max_entries {
            let excess = entries.len() - self.max_entries;
            entries.drain(0..excess);
        }
    }
    
    /// Get recent audit entries
    pub async fn get_recent_entries(&self, limit: Option<usize>) -> Vec<SecurityAuditEntry> {
        let entries = self.entries.read().await;
        let limit = limit.unwrap_or(entries.len());
        entries.iter().rev().take(limit).cloned().collect()
    }
    
    /// Get entries by severity
    pub async fn get_entries_by_severity(&self, severity: AuditSeverity) -> Vec<SecurityAuditEntry> {
        let entries = self.entries.read().await;
        entries.iter().filter(|e| e.severity == severity).cloned().collect()
    }
}

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

    /// Helper function to create a test PeerId
    fn create_test_peer() -> PeerId {
        format!("test_peer_{}", rand::random::<u32>())
    }

    /// Helper function to create a test security manager
    fn create_test_security_manager() -> MCPSecurityManager {
        let secret_key = b"test_secret_key_1234567890123456".to_vec();
        MCPSecurityManager::new(secret_key, 60) // 60 RPM limit
    }

    #[test]
    fn test_mcp_permission_string_conversion() {
        let permissions = vec![
            (MCPPermission::ReadTools, "read:tools"),
            (MCPPermission::ExecuteTools, "execute:tools"),
            (MCPPermission::RegisterTools, "register:tools"),
            (MCPPermission::ModifyTools, "modify:tools"),
            (MCPPermission::DeleteTools, "delete:tools"),
            (MCPPermission::AccessPrompts, "access:prompts"),
            (MCPPermission::AccessResources, "access:resources"),
            (MCPPermission::Admin, "admin"),
        ];

        for (permission, expected_str) in permissions {
            assert_eq!(permission.as_str(), expected_str);
            assert_eq!(MCPPermission::from_str(expected_str), Some(permission));
        }

        // Test custom permission
        let custom = MCPPermission::Custom("custom:action".to_string());
        assert_eq!(custom.as_str(), "custom:action");
        assert_eq!(MCPPermission::from_str("custom:action"), Some(custom));

        // Test unknown permission defaults to custom
        let unknown = MCPPermission::from_str("unknown:permission");
        match unknown {
            Some(MCPPermission::Custom(s)) => assert_eq!(s, "unknown:permission"),
            _ => panic!("Expected custom permission"),
        }
    }

    #[test]
    fn test_security_level_ordering() {
        // Test security level ordering
        assert!(SecurityLevel::Public < SecurityLevel::Basic);
        assert!(SecurityLevel::Basic < SecurityLevel::Strong);
        assert!(SecurityLevel::Strong < SecurityLevel::Admin);

        // Test equality
        assert_eq!(SecurityLevel::Public, SecurityLevel::Public);
        assert_eq!(SecurityLevel::Basic, SecurityLevel::Basic);
        assert_eq!(SecurityLevel::Strong, SecurityLevel::Strong);
        assert_eq!(SecurityLevel::Admin, SecurityLevel::Admin);
    }

    #[test]
    fn test_peer_acl_creation() {
        let peer_id = create_test_peer();
        let acl = PeerACL::new(peer_id.clone());

        assert_eq!(acl.peer_id, peer_id);
        assert_eq!(acl.permissions.len(), 2); // Default: ReadTools, ExecuteTools
        assert!(acl.permissions.contains(&MCPPermission::ReadTools));
        assert!(acl.permissions.contains(&MCPPermission::ExecuteTools));
        assert_eq!(acl.security_level, SecurityLevel::Basic);
        assert_eq!(acl.reputation, 0.5);
        assert_eq!(acl.access_count, 0);
        assert_eq!(acl.rate_violations, 0);
        assert!(acl.banned_until.is_none());
        assert!(!acl.is_banned());
    }

    #[test]
    fn test_peer_acl_permissions() {
        let peer_id = create_test_peer();
        let mut acl = PeerACL::new(peer_id);

        // Test default permissions
        assert!(acl.has_permission(&MCPPermission::ReadTools));
        assert!(acl.has_permission(&MCPPermission::ExecuteTools));
        assert!(!acl.has_permission(&MCPPermission::RegisterTools));
        assert!(!acl.has_permission(&MCPPermission::Admin));

        // Grant admin permission
        acl.grant_permission(MCPPermission::Admin);
        // Admin permission grants all access
        assert!(acl.has_permission(&MCPPermission::ReadTools));
        assert!(acl.has_permission(&MCPPermission::ExecuteTools));
        assert!(acl.has_permission(&MCPPermission::RegisterTools));
        assert!(acl.has_permission(&MCPPermission::DeleteTools));
        assert!(acl.has_permission(&MCPPermission::Admin));

        // Revoke admin permission
        acl.revoke_permission(&MCPPermission::Admin);
        assert!(!acl.has_permission(&MCPPermission::RegisterTools));
        assert!(!acl.has_permission(&MCPPermission::Admin));

        // Grant specific permission
        acl.grant_permission(MCPPermission::RegisterTools);
        assert!(acl.has_permission(&MCPPermission::RegisterTools));

        // Revoke specific permission
        acl.revoke_permission(&MCPPermission::RegisterTools);
        assert!(!acl.has_permission(&MCPPermission::RegisterTools));
    }

    #[test]
    fn test_peer_acl_ban_functionality() {
        let peer_id = create_test_peer();
        let mut acl = PeerACL::new(peer_id);

        // Initially not banned
        assert!(!acl.is_banned());
        assert!(acl.has_permission(&MCPPermission::ReadTools));

        // Record violations (but not enough to trigger auto-ban)
        for _ in 0..5 {
            acl.record_rate_violation();
        }
        assert_eq!(acl.rate_violations, 5);
        assert!(!acl.is_banned());

        // Record enough violations to trigger auto-ban
        for _ in 0..5 {
            acl.record_rate_violation();
        }
        assert_eq!(acl.rate_violations, 10);
        assert!(acl.is_banned());

        // Banned peers have no permissions
        assert!(!acl.has_permission(&MCPPermission::ReadTools));
        assert!(!acl.has_permission(&MCPPermission::ExecuteTools));
    }

    #[test]
    fn test_peer_acl_access_tracking() {
        let peer_id = create_test_peer();
        let mut acl = PeerACL::new(peer_id);

        let initial_time = acl.last_access;
        assert_eq!(acl.access_count, 0);

        // Record access
        std::thread::sleep(std::time::Duration::from_millis(10));
        acl.record_access();

        assert_eq!(acl.access_count, 1);
        assert!(acl.last_access > initial_time);

        // Record more access
        acl.record_access();
        assert_eq!(acl.access_count, 2);
    }

    #[tokio::test]
    async fn test_rate_limiter_creation() {
        let limiter = RateLimiter::new(60);
        assert_eq!(limiter.rpm_limit, 60);
    }

    #[tokio::test]
    async fn test_rate_limiter_basic_functionality() {
        let limiter = RateLimiter::new(2); // 2 requests per minute
        let peer_id = create_test_peer();

        // First request should be allowed
        assert!(limiter.is_allowed(&peer_id).await);

        // Second request should be allowed
        assert!(limiter.is_allowed(&peer_id).await);

        // Third request should be denied (over limit)
        assert!(!limiter.is_allowed(&peer_id).await);
    }

    #[tokio::test]
    async fn test_rate_limiter_different_peers() {
        let limiter = RateLimiter::new(1); // 1 request per minute
        let peer1 = create_test_peer();
        let peer2 = create_test_peer();

        // Each peer should have their own limit
        assert!(limiter.is_allowed(&peer1).await);
        assert!(limiter.is_allowed(&peer2).await);

        // Both should be over their individual limits now
        assert!(!limiter.is_allowed(&peer1).await);
        assert!(!limiter.is_allowed(&peer2).await);
    }

    #[tokio::test]
    async fn test_rate_limiter_reset() {
        let limiter = RateLimiter::new(1);
        let peer_id = create_test_peer();

        // Use up the limit
        assert!(limiter.is_allowed(&peer_id).await);
        assert!(!limiter.is_allowed(&peer_id).await);

        // Reset the peer
        limiter.reset_peer(&peer_id).await;

        // Should be allowed again
        assert!(limiter.is_allowed(&peer_id).await);
    }

    #[tokio::test]
    async fn test_rate_limiter_cleanup() {
        let limiter = RateLimiter::new(10);
        let peer_id = create_test_peer();

        // Make some requests
        limiter.is_allowed(&peer_id).await;
        limiter.is_allowed(&peer_id).await;

        // Cleanup shouldn't affect recent requests
        limiter.cleanup().await;

        // Should still have request history
        let requests = limiter.requests.read().await;
        assert!(requests.contains_key(&peer_id));
        let peer_requests = requests.get(&peer_id).unwrap();
        assert_eq!(peer_requests.len(), 2);
    }

    #[tokio::test]
    async fn test_security_manager_creation() {
        let secret_key = b"test_secret_key".to_vec();
        let manager = MCPSecurityManager::new(secret_key.clone(), 60);

        // Verify configuration
        assert_eq!(manager.secret_key, secret_key);
        assert_eq!(manager.rate_limiter.rpm_limit, 60);
    }

    #[tokio::test]
    async fn test_token_generation_and_verification() -> Result<()> {
        let manager = create_test_security_manager();
        let peer_id = create_test_peer();
        let permissions = vec![MCPPermission::ReadTools, MCPPermission::ExecuteTools];
        let ttl = Duration::from_secs(3600); // 1 hour

        // Generate token
        let token = manager.generate_token(&peer_id, permissions.clone(), ttl).await?;
        assert!(!token.is_empty());

        // Verify token
        let payload = manager.verify_token(&token).await?;
        assert_eq!(payload.iss, peer_id);
        assert_eq!(payload.sub, peer_id);
        assert_eq!(payload.aud, "mcp-server");

        // Check permissions in claims
        let permissions_claim = payload.claims.get("permissions").unwrap();
        let permission_strings: Vec<String> = serde_json::from_value(permissions_claim.clone()).unwrap();
        assert_eq!(permission_strings.len(), 2);
        assert!(permission_strings.contains(&"read:tools".to_string()));
        assert!(permission_strings.contains(&"execute:tools".to_string()));

        Ok(())
    }

    #[tokio::test]
    async fn test_token_verification_invalid() {
        let manager = create_test_security_manager();

        // Test invalid token format
        let result = manager.verify_token("invalid.token").await;
        assert!(result.is_err());

        // Test malformed token
        let result = manager.verify_token("invalid.token.format.extra").await;
        assert!(result.is_err());

        // Test empty token
        let result = manager.verify_token("").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_token_signature_verification() -> Result<()> {
        let manager1 = create_test_security_manager();
        let manager2 = MCPSecurityManager::new(b"different_secret".to_vec(), 60);

        let peer_id = create_test_peer();
        let permissions = vec![MCPPermission::ReadTools];
        let ttl = Duration::from_secs(3600);

        // Generate token with manager1
        let token = manager1.generate_token(&peer_id, permissions, ttl).await?;

        // Verify with manager1 should succeed
        assert!(manager1.verify_token(&token).await.is_ok());

        // Verify with manager2 should fail (different secret)
        assert!(manager2.verify_token(&token).await.is_err());

        Ok(())
    }

    #[tokio::test]
    async fn test_permission_management() -> Result<()> {
        let manager = create_test_security_manager();
        let peer_id = create_test_peer();

        // Initially should have no permissions (new peer starts with false)
        assert!(!manager.check_permission(&peer_id, &MCPPermission::ExecuteTools).await?);

        // Grant permission
        manager.grant_permission(&peer_id, MCPPermission::ExecuteTools).await?;
        assert!(manager.check_permission(&peer_id, &MCPPermission::ExecuteTools).await?);

        // Revoke permission
        manager.revoke_permission(&peer_id, &MCPPermission::ExecuteTools).await?;
        assert!(!manager.check_permission(&peer_id, &MCPPermission::ExecuteTools).await?);

        Ok(())
    }

    #[tokio::test]
    async fn test_rate_limit_checking() -> Result<()> {
        let manager = MCPSecurityManager::new(b"test_key".to_vec(), 2); // 2 RPM limit
        let peer_id = create_test_peer();

        // Grant permission first to create ACL entry
        manager.grant_permission(&peer_id, MCPPermission::ReadTools).await?;

        // First two requests should pass
        assert!(manager.check_rate_limit(&peer_id).await?);
        assert!(manager.check_rate_limit(&peer_id).await?);

        // Third request should fail
        assert!(!manager.check_rate_limit(&peer_id).await?);

        // Check that violation was recorded
        let stats = manager.get_peer_stats(&peer_id).await;
        assert!(stats.is_some());
        let acl = stats.unwrap();
        assert_eq!(acl.rate_violations, 1);

        Ok(())
    }

    #[tokio::test]
    async fn test_trusted_peer_management() -> Result<()> {
        let manager = create_test_security_manager();
        let peer_id = create_test_peer();

        // Initially not trusted
        assert!(!manager.is_trusted_peer(&peer_id).await);

        // Add as trusted
        manager.add_trusted_peer(peer_id.clone()).await?;
        assert!(manager.is_trusted_peer(&peer_id).await);

        // Adding same peer again should be idempotent
        manager.add_trusted_peer(peer_id.clone()).await?;
        assert!(manager.is_trusted_peer(&peer_id).await);

        Ok(())
    }

    #[tokio::test]
    async fn test_tool_security_policies() -> Result<()> {
        let manager = create_test_security_manager();

        // Default policy should be Basic
        let policy = manager.get_tool_policy("test_tool").await;
        assert_eq!(policy, SecurityLevel::Basic);

        // Set custom policy
        manager.set_tool_policy("test_tool".to_string(), SecurityLevel::Strong).await?;
        let policy = manager.get_tool_policy("test_tool").await;
        assert_eq!(policy, SecurityLevel::Strong);

        // Set admin policy
        manager.set_tool_policy("admin_tool".to_string(), SecurityLevel::Admin).await?;
        let policy = manager.get_tool_policy("admin_tool").await;
        assert_eq!(policy, SecurityLevel::Admin);

        Ok(())
    }

    #[tokio::test]
    async fn test_reputation_management() -> Result<()> {
        let manager = create_test_security_manager();
        let peer_id = create_test_peer();

        // Grant permission to create ACL entry
        manager.grant_permission(&peer_id, MCPPermission::ReadTools).await?;

        let stats = manager.get_peer_stats(&peer_id).await.unwrap();
        assert_eq!(stats.reputation, 0.5); // Default reputation

        // Increase reputation
        manager.update_reputation(&peer_id, 0.2).await?;
        let stats = manager.get_peer_stats(&peer_id).await.unwrap();
        assert_eq!(stats.reputation, 0.7);

        // Decrease reputation
        manager.update_reputation(&peer_id, -0.3).await?;
        let stats = manager.get_peer_stats(&peer_id).await.unwrap();
        assert!((stats.reputation - 0.4).abs() < 0.001); // Use epsilon for float comparison

        // Test bounds (should clamp to 0.0-1.0)
        manager.update_reputation(&peer_id, -1.0).await?;
        let stats = manager.get_peer_stats(&peer_id).await.unwrap();
        assert_eq!(stats.reputation, 0.0);

        manager.update_reputation(&peer_id, 2.0).await?;
        let stats = manager.get_peer_stats(&peer_id).await.unwrap();
        assert_eq!(stats.reputation, 1.0);

        Ok(())
    }

    #[tokio::test]
    async fn test_security_manager_cleanup() -> Result<()> {
        let manager = create_test_security_manager();
        let peer_id = create_test_peer();

        // Create some data
        manager.grant_permission(&peer_id, MCPPermission::ReadTools).await?;
        manager.check_rate_limit(&peer_id).await?;

        // Cleanup should work without errors
        manager.cleanup().await?;

        Ok(())
    }

    #[tokio::test]
    async fn test_audit_logger_creation() {
        let logger = SecurityAuditLogger::new(100);
        assert_eq!(logger.max_entries, 100);

        let entries = logger.get_recent_entries(None).await;
        assert!(entries.is_empty());
    }

    #[tokio::test]
    async fn test_audit_logger_logging() {
        let logger = SecurityAuditLogger::new(10);
        let peer_id = create_test_peer();

        let mut details = HashMap::new();
        details.insert("action".to_string(), "test_action".to_string());
        details.insert("result".to_string(), "success".to_string());

        // Log an event
        logger.log_event(
            "test_event".to_string(),
            peer_id.clone(),
            details.clone(),
            AuditSeverity::Info,
        ).await;

        let entries = logger.get_recent_entries(None).await;
        assert_eq!(entries.len(), 1);

        let entry = &entries[0];
        assert_eq!(entry.event_type, "test_event");
        assert_eq!(entry.peer_id, peer_id);
        assert_eq!(entry.severity, AuditSeverity::Info);
        assert_eq!(entry.details.get("action"), Some(&"test_action".to_string()));
    }

    #[tokio::test]
    async fn test_audit_logger_severity_filtering() {
        let logger = SecurityAuditLogger::new(10);
        let peer_id = create_test_peer();

        // Log events with different severities
        logger.log_event("info_event".to_string(), peer_id.clone(), HashMap::new(), AuditSeverity::Info).await;
        logger.log_event("warning_event".to_string(), peer_id.clone(), HashMap::new(), AuditSeverity::Warning).await;
        logger.log_event("error_event".to_string(), peer_id.clone(), HashMap::new(), AuditSeverity::Error).await;
        logger.log_event("critical_event".to_string(), peer_id.clone(), HashMap::new(), AuditSeverity::Critical).await;

        // Test filtering by severity
        let info_entries = logger.get_entries_by_severity(AuditSeverity::Info).await;
        assert_eq!(info_entries.len(), 1);
        assert_eq!(info_entries[0].event_type, "info_event");

        let warning_entries = logger.get_entries_by_severity(AuditSeverity::Warning).await;
        assert_eq!(warning_entries.len(), 1);
        assert_eq!(warning_entries[0].event_type, "warning_event");

        let error_entries = logger.get_entries_by_severity(AuditSeverity::Error).await;
        assert_eq!(error_entries.len(), 1);

        let critical_entries = logger.get_entries_by_severity(AuditSeverity::Critical).await;
        assert_eq!(critical_entries.len(), 1);
    }

    #[tokio::test]
    async fn test_audit_logger_max_entries() {
        let logger = SecurityAuditLogger::new(3); // Limit to 3 entries
        let peer_id = create_test_peer();

        // Log 5 events
        for i in 0..5 {
            logger.log_event(
                format!("event_{}", i),
                peer_id.clone(),
                HashMap::new(),
                AuditSeverity::Info,
            ).await;
        }

        let entries = logger.get_recent_entries(None).await;
        assert_eq!(entries.len(), 3); // Should only keep 3 most recent

        // Check that we have the most recent events (2, 3, 4)
        assert_eq!(entries[0].event_type, "event_4"); // Most recent first
        assert_eq!(entries[1].event_type, "event_3");
        assert_eq!(entries[2].event_type, "event_2");
    }

    #[tokio::test]
    async fn test_audit_logger_recent_entries_limit() {
        let logger = SecurityAuditLogger::new(10);
        let peer_id = create_test_peer();

        // Log 5 events
        for i in 0..5 {
            logger.log_event(
                format!("event_{}", i),
                peer_id.clone(),
                HashMap::new(),
                AuditSeverity::Info,
            ).await;
        }

        // Get limited number of recent entries
        let entries = logger.get_recent_entries(Some(3)).await;
        assert_eq!(entries.len(), 3);

        // Should be most recent first
        assert_eq!(entries[0].event_type, "event_4");
        assert_eq!(entries[1].event_type, "event_3");
        assert_eq!(entries[2].event_type, "event_2");
    }

    #[test]
    fn test_audit_severity_equality() {
        assert_eq!(AuditSeverity::Info, AuditSeverity::Info);
        assert_eq!(AuditSeverity::Warning, AuditSeverity::Warning);
        assert_eq!(AuditSeverity::Error, AuditSeverity::Error);
        assert_eq!(AuditSeverity::Critical, AuditSeverity::Critical);

        assert_ne!(AuditSeverity::Info, AuditSeverity::Warning);
        assert_ne!(AuditSeverity::Warning, AuditSeverity::Error);
        assert_ne!(AuditSeverity::Error, AuditSeverity::Critical);
    }

    #[test]
    fn test_token_header_structure() {
        let header = TokenHeader {
            alg: "HS256".to_string(),
            typ: "JWT".to_string(),
            kid: Some("key123".to_string()),
        };

        assert_eq!(header.alg, "HS256");
        assert_eq!(header.typ, "JWT");
        assert_eq!(header.kid, Some("key123".to_string()));
    }

    #[test]
    fn test_token_payload_structure() {
        let peer_id = create_test_peer();
        let now = SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs();

        let mut claims = HashMap::new();
        claims.insert("custom".to_string(), serde_json::json!("value"));

        let payload = TokenPayload {
            iss: peer_id.clone(),
            sub: peer_id.to_string(),
            aud: "test-audience".to_string(),
            exp: now + 3600,
            nbf: now,
            iat: now,
            jti: "unique-id".to_string(),
            claims,
        };

        assert_eq!(payload.iss, peer_id);
        assert_eq!(payload.aud, "test-audience");
        assert_eq!(payload.jti, "unique-id");
        assert!(payload.exp > payload.iat);
        assert_eq!(payload.claims.get("custom"), Some(&serde_json::json!("value")));
    }

    #[test]
    fn test_mcp_token_structure() {
        let peer_id = create_test_peer();

        let header = TokenHeader {
            alg: "HS256".to_string(),
            typ: "JWT".to_string(),
            kid: None,
        };

        let payload = TokenPayload {
            iss: peer_id.clone(),
            sub: peer_id.to_string(),
            aud: "test".to_string(),
            exp: 1234567890,
            nbf: 1234567800,
            iat: 1234567800,
            jti: "test-id".to_string(),
            claims: HashMap::new(),
        };

        let token = MCPToken {
            header: header.clone(),
            payload: payload.clone(),
            signature: "test-signature".to_string(),
        };

        assert_eq!(token.header.alg, header.alg);
        assert_eq!(token.payload.iss, payload.iss);
        assert_eq!(token.signature, "test-signature");
    }
}