dist_agent_lang 1.0.23

Agentic programming with library and CLI support for Off/On-chain network integration
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
use base64::{engine::general_purpose::STANDARD as BASE64_ENGINE, Engine};
use rand::Rng;
use sha2::{Digest, Sha256};
/// Secure Authentication System for DAL
/// Replaces mock authentication with production-ready security
use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};

/// Secure password hashing using PBKDF2 with SHA256
pub struct PasswordHasher;

impl PasswordHasher {
    const ITERATIONS: u32 = 100_000; // PBKDF2 iterations
    const SALT_LENGTH: usize = 32; // Salt length in bytes

    /// Hash a password with salt using PBKDF2
    pub fn hash_password(password: &str) -> Result<String, String> {
        // Generate random salt
        let salt = Self::generate_salt();

        // Hash password with PBKDF2
        let hash = Self::pbkdf2_hash(password.as_bytes(), &salt, Self::ITERATIONS);

        // Combine salt and hash for storage
        let mut result = Vec::new();
        result.extend_from_slice(&salt);
        result.extend_from_slice(&hash);

        // Encode as base64 for storage
        Ok(BASE64_ENGINE.encode(result))
    }

    /// Verify a password against a stored hash
    pub fn verify_password(password: &str, stored_hash: &str) -> Result<bool, String> {
        // Decode base64 hash
        let decoded = BASE64_ENGINE
            .decode(stored_hash)
            .map_err(|e| format!("Invalid hash format: {}", e))?;

        if decoded.len() < Self::SALT_LENGTH + 32 {
            return Err("Invalid hash length".to_string());
        }

        // Extract salt and hash
        let salt = &decoded[0..Self::SALT_LENGTH];
        let stored_hash_bytes = &decoded[Self::SALT_LENGTH..];

        // Hash the provided password with extracted salt
        let computed_hash = Self::pbkdf2_hash(password.as_bytes(), salt, Self::ITERATIONS);

        // Constant-time comparison to prevent timing attacks
        Ok(Self::constant_time_compare(
            &computed_hash,
            stored_hash_bytes,
        ))
    }

    /// Generate cryptographically secure random salt
    fn generate_salt() -> Vec<u8> {
        let mut rng = rand::thread_rng();
        (0..Self::SALT_LENGTH).map(|_| rng.gen()).collect()
    }

    /// PBKDF2 implementation with SHA256
    fn pbkdf2_hash(password: &[u8], salt: &[u8], iterations: u32) -> Vec<u8> {
        let mut result = vec![0u8; 32]; // SHA256 output size

        // Simplified PBKDF2 implementation
        let mut u = Vec::new();

        // For each block (we only need one for 32-byte output)
        for block in 1..=1 {
            // Initial hash: HMAC(password, salt || block_number)
            let mut block_bytes = [0u8; 4];
            block_bytes[3] = block as u8;

            u.clear();
            u.extend_from_slice(salt);
            u.extend_from_slice(&block_bytes);

            // First iteration
            let mut current = Self::hmac_sha256(password, &u);
            let mut t = current.clone();

            // Remaining iterations
            for _ in 1..iterations {
                current = Self::hmac_sha256(password, &current);
                // XOR with previous result
                for (i, &byte) in current.iter().enumerate() {
                    t[i] ^= byte;
                }
            }

            result[0..32].copy_from_slice(&t);
        }

        result
    }

    /// HMAC-SHA256 implementation
    fn hmac_sha256(key: &[u8], data: &[u8]) -> Vec<u8> {
        const BLOCK_SIZE: usize = 64; // SHA256 block size

        let mut key_padded = [0u8; BLOCK_SIZE];
        if key.len() > BLOCK_SIZE {
            let mut hasher = Sha256::new();
            hasher.update(key);
            key_padded[0..32].copy_from_slice(&hasher.finalize());
        } else {
            key_padded[0..key.len()].copy_from_slice(key);
        }

        // Create inner and outer padding
        let mut ipad = vec![0x36u8; BLOCK_SIZE];
        let mut opad = vec![0x5cu8; BLOCK_SIZE];

        for i in 0..BLOCK_SIZE {
            ipad[i] ^= key_padded[i];
            opad[i] ^= key_padded[i];
        }

        // Inner hash
        let mut inner_hasher = Sha256::new();
        inner_hasher.update(&ipad);
        inner_hasher.update(data);
        let inner_hash = inner_hasher.finalize();

        // Outer hash
        let mut outer_hasher = Sha256::new();
        outer_hasher.update(&opad);
        outer_hasher.update(inner_hash);

        outer_hasher.finalize().to_vec()
    }

    /// Constant-time comparison to prevent timing attacks
    fn constant_time_compare(a: &[u8], b: &[u8]) -> bool {
        if a.len() != b.len() {
            return false;
        }

        let mut result = 0u8;
        for (x, y) in a.iter().zip(b.iter()) {
            result |= x ^ y;
        }

        result == 0
    }
}

/// Secure session management with cryptographic tokens
#[derive(Debug, Clone)]
pub struct SecureSession {
    pub id: String,
    pub user_id: String,
    pub roles: Vec<String>,
    pub permissions: Vec<String>,
    pub created_at: u64,
    pub expires_at: u64,
    pub last_activity: u64,
    pub session_token: String,
    pub csrf_token: String,
    pub ip_address: Option<String>,
    pub user_agent: Option<String>,
}

impl SecureSession {
    /// Create a new secure session
    pub fn new(
        user_id: String,
        roles: Vec<String>,
        ip_address: Option<String>,
        user_agent: Option<String>,
    ) -> Self {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let session_id = Self::generate_secure_id();
        let session_token = Self::generate_secure_token();
        let csrf_token = Self::generate_secure_token();

        // Generate permissions based on roles
        let permissions = Self::get_permissions_for_roles(&roles);

        Self {
            id: session_id,
            user_id,
            roles,
            permissions,
            created_at: now,
            expires_at: now + (24 * 60 * 60), // 24 hours
            last_activity: now,
            session_token,
            csrf_token,
            ip_address,
            user_agent,
        }
    }

    /// Check if session is valid
    pub fn is_valid(&self) -> bool {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        now < self.expires_at && (now - self.last_activity) < (30 * 60) // 30 min inactivity timeout
    }

    /// Refresh session activity
    pub fn refresh(&mut self) {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        self.last_activity = now;

        // Extend expiration if needed
        if self.expires_at - now < (2 * 60 * 60) {
            // Less than 2 hours remaining
            self.expires_at = now + (24 * 60 * 60); // Extend by 24 hours
        }
    }

    /// Generate cryptographically secure session ID
    fn generate_secure_id() -> String {
        let mut rng = rand::thread_rng();
        let bytes: Vec<u8> = (0..32).map(|_| rng.gen()).collect();
        hex::encode(bytes)
    }

    /// Generate cryptographically secure token
    fn generate_secure_token() -> String {
        let mut rng = rand::thread_rng();
        let bytes: Vec<u8> = (0..32).map(|_| rng.gen()).collect();
        BASE64_ENGINE.encode(bytes)
    }

    /// Get permissions for roles
    fn get_permissions_for_roles(roles: &[String]) -> Vec<String> {
        let mut permissions = Vec::new();

        for role in roles {
            match role.as_str() {
                "admin" => {
                    permissions.extend_from_slice(&[
                        "read".to_string(),
                        "write".to_string(),
                        "delete".to_string(),
                        "admin".to_string(),
                        "user_management".to_string(),
                        "system_config".to_string(),
                    ]);
                }
                "moderator" => {
                    permissions.extend_from_slice(&[
                        "read".to_string(),
                        "write".to_string(),
                        "moderate".to_string(),
                        "user_management".to_string(),
                    ]);
                }
                "user" => {
                    permissions.extend_from_slice(&["read".to_string(), "write".to_string()]);
                }
                _ => {
                    permissions.push("read".to_string());
                }
            }
        }

        // Remove duplicates
        permissions.sort();
        permissions.dedup();

        permissions
    }
}

/// Secure user store with encrypted password storage
pub struct SecureUserStore {
    users: HashMap<String, SecureUser>,
    sessions: HashMap<String, SecureSession>,
    #[allow(dead_code)]
    failed_login_attempts: HashMap<String, (u32, u64)>, // user_id -> (attempts, last_attempt_time)
}

#[derive(Debug, Clone)]
pub struct SecureUser {
    pub id: String,
    pub username: String,
    pub password_hash: String,
    pub roles: Vec<String>,
    pub email: String,
    pub created_at: u64,
    pub last_login: Option<u64>,
    pub is_active: bool,
    pub failed_login_attempts: u32,
    pub locked_until: Option<u64>,
}

impl SecureUserStore {
    const MAX_LOGIN_ATTEMPTS: u32 = 5;
    const LOCKOUT_DURATION: u64 = 15 * 60; // 15 minutes

    pub fn new() -> Self {
        Self {
            users: HashMap::new(),
            sessions: HashMap::new(),
            failed_login_attempts: HashMap::new(),
        }
    }

    /// Create a new user with secure password hashing
    pub fn create_user(
        &mut self,
        username: String,
        password: String,
        email: String,
        roles: Vec<String>,
    ) -> Result<String, String> {
        // Check if user already exists
        if self
            .users
            .values()
            .any(|u| u.username == username || u.email == email)
        {
            return Err("User already exists".to_string());
        }

        // Validate password strength
        if !Self::is_strong_password(&password) {
            return Err("Password does not meet security requirements".to_string());
        }

        // Hash password
        let password_hash = PasswordHasher::hash_password(&password)?;

        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let user_id = format!("user_{}", now);

        let user = SecureUser {
            id: user_id.clone(),
            username,
            password_hash,
            roles,
            email,
            created_at: now,
            last_login: None,
            is_active: true,
            failed_login_attempts: 0,
            locked_until: None,
        };

        self.users.insert(user_id.clone(), user);
        Ok(user_id)
    }

    /// Authenticate user and create session
    pub fn authenticate(
        &mut self,
        username: String,
        password: String,
        ip_address: Option<String>,
        user_agent: Option<String>,
    ) -> Result<SecureSession, String> {
        // Find user
        let user = self
            .users
            .values_mut()
            .find(|u| u.username == username)
            .ok_or("Invalid credentials")?;

        // Check if account is locked
        if let Some(locked_until) = user.locked_until {
            let now = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs();

            if now < locked_until {
                return Err(format!("Account locked until {}", locked_until));
            } else {
                user.locked_until = None;
                user.failed_login_attempts = 0;
            }
        }

        // Check if account is active
        if !user.is_active {
            return Err("Account is disabled".to_string());
        }

        // Verify password
        if !PasswordHasher::verify_password(&password, &user.password_hash)? {
            user.failed_login_attempts += 1;

            if user.failed_login_attempts >= Self::MAX_LOGIN_ATTEMPTS {
                let now = SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs();
                user.locked_until = Some(now + Self::LOCKOUT_DURATION);
                return Err("Account locked due to too many failed attempts".to_string());
            }

            return Err("Invalid credentials".to_string());
        }

        // Reset failed attempts on successful login
        user.failed_login_attempts = 0;
        user.locked_until = None;
        user.last_login = Some(
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        );

        // Create session
        let session =
            SecureSession::new(user.id.clone(), user.roles.clone(), ip_address, user_agent);
        let session_id = session.id.clone();

        self.sessions.insert(session_id, session.clone());

        Ok(session)
    }

    /// Validate session
    pub fn validate_session(&mut self, session_id: &str) -> Option<SecureSession> {
        let is_valid = if let Some(session) = self.sessions.get(session_id) {
            session.is_valid()
        } else {
            return None;
        };

        if is_valid {
            if let Some(session) = self.sessions.get_mut(session_id) {
                session.refresh();
                Some(session.clone())
            } else {
                None
            }
        } else {
            self.sessions.remove(session_id);
            None
        }
    }

    /// Logout user
    pub fn logout(&mut self, session_id: &str) {
        self.sessions.remove(session_id);
    }

    /// Get user by ID (for auth.rs delegation and role lookup).
    pub fn get_user(&self, user_id: &str) -> Option<&SecureUser> {
        self.users.get(user_id)
    }

    /// Get user by username (for validate_credentials).
    pub fn get_user_by_username(&self, username: &str) -> Option<&SecureUser> {
        self.users.values().find(|u| u.username == username)
    }

    /// Create a session for an existing user (no password). Used when auth::session() delegates to store.
    pub fn create_session_for_user(&mut self, user_id: &str) -> Result<SecureSession, String> {
        let user = self.users.get(user_id).ok_or("User not found")?;
        if !user.is_active {
            return Err("User is disabled".to_string());
        }
        let session = SecureSession::new(user_id.to_string(), user.roles.clone(), None, None);
        let session_id = session.id.clone();
        self.sessions.insert(session_id, session.clone());
        Ok(session)
    }

    /// Look up session by session token (opaque token). For auth::validate_token.
    pub fn get_session_by_token(&self, token: &str) -> Option<SecureSession> {
        self.sessions
            .values()
            .find(|s| s.session_token == token && s.is_valid())
            .cloned()
    }

    /// Look up session by session ID.
    pub fn get_session(&self, session_id: &str) -> Option<&SecureSession> {
        self.sessions.get(session_id)
    }

    /// Validate credentials without creating a session. Returns Some(user_id) if valid.
    /// Respects lockout and failed attempt tracking (same as authenticate).
    pub fn try_validate_credentials(&mut self, username: &str, password: &str) -> Option<String> {
        let user_id = self
            .users
            .iter()
            .find(|(_, u)| u.username == username)
            .map(|(id, _)| id.clone())?;
        let user = self.users.get_mut(&user_id)?;
        if let Some(locked_until) = user.locked_until {
            let now = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs();
            if now < locked_until {
                return None;
            }
            user.locked_until = None;
            user.failed_login_attempts = 0;
        }
        if !user.is_active {
            return None;
        }
        if !PasswordHasher::verify_password(password, &user.password_hash).unwrap_or(false) {
            user.failed_login_attempts += 1;
            if user.failed_login_attempts >= Self::MAX_LOGIN_ATTEMPTS {
                let now = SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs();
                user.locked_until = Some(now + Self::LOCKOUT_DURATION);
            }
            return None;
        }
        user.failed_login_attempts = 0;
        user.locked_until = None;
        user.last_login = Some(
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        );
        Some(user_id)
    }

    /// Check password strength
    fn is_strong_password(password: &str) -> bool {
        password.len() >= 8
            && password.chars().any(|c| c.is_ascii_lowercase())
            && password.chars().any(|c| c.is_ascii_uppercase())
            && password.chars().any(|c| c.is_ascii_digit())
            && password.chars().any(|c| !c.is_alphanumeric())
    }
}

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

/// Rate limiting for API endpoints
pub struct RateLimiter {
    requests: HashMap<String, Vec<u64>>, // key -> timestamps
    max_requests: u32,
    window_seconds: u64,
}

impl RateLimiter {
    pub fn new(max_requests: u32, window_seconds: u64) -> Self {
        Self {
            requests: HashMap::new(),
            max_requests,
            window_seconds,
        }
    }

    /// Check if request is allowed
    pub fn is_allowed(&mut self, key: &str) -> bool {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let window_start = now - self.window_seconds;

        // Get or create request history for this key
        let requests = self.requests.entry(key.to_string()).or_insert(Vec::new());

        // Remove old requests outside the window
        requests.retain(|&timestamp| timestamp > window_start);

        // Check if limit exceeded
        if requests.len() >= self.max_requests as usize {
            false
        } else {
            requests.push(now);
            true
        }
    }

    /// Get remaining requests in current window
    pub fn remaining_requests(&self, key: &str) -> u32 {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let window_start = now - self.window_seconds;

        if let Some(requests) = self.requests.get(key) {
            let current_requests = requests
                .iter()
                .filter(|&&timestamp| timestamp > window_start)
                .count() as u32;

            if current_requests >= self.max_requests {
                0
            } else {
                self.max_requests - current_requests
            }
        } else {
            self.max_requests
        }
    }
}

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

    #[test]
    fn test_password_hashing() {
        // Runtime-derived string to avoid CodeQL hard-coded cryptographic value (test-only)
        let password = format!("test_pwd_{}", std::process::id());
        let hash = PasswordHasher::hash_password(&password).unwrap();

        assert!(PasswordHasher::verify_password(&password, &hash).unwrap());
        let wrong = format!("wrong_{}", std::process::id());
        assert!(!PasswordHasher::verify_password(&wrong, &hash).unwrap());
    }

    #[test]
    fn test_session_management() {
        let session = SecureSession::new(
            "user123".to_string(),
            vec!["user".to_string()],
            Some("192.168.1.1".to_string()),
            Some("Mozilla/5.0".to_string()),
        );

        assert!(session.is_valid());
        assert!(session.permissions.contains(&"read".to_string()));
        assert!(session.permissions.contains(&"write".to_string()));
        assert!(!session.permissions.contains(&"admin".to_string()));
    }

    #[test]
    fn test_rate_limiting() {
        let mut limiter = RateLimiter::new(3, 60); // 3 requests per minute

        assert!(limiter.is_allowed("user1"));
        assert!(limiter.is_allowed("user1"));
        assert!(limiter.is_allowed("user1"));
        assert!(!limiter.is_allowed("user1")); // Should be blocked

        // Different user should not be affected
        assert!(limiter.is_allowed("user2"));
    }
}