ipfrs 0.2.0

Next-generation distributed file system with content-addressing, semantic search, and logic programming
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
//! Authentication and Authorization for IPFRS
//!
//! This module provides:
//! - API key authentication
//! - JWT token authentication
//! - OAuth2 integration
//! - Role-based access control (RBAC)
//! - Resource-level permissions

use anyhow::{anyhow, Context, Result};
use chrono::{Duration, Utc};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

/// User roles for role-based access control
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Role {
    /// Administrator with full access
    Admin,
    /// Regular user with read/write access
    User,
    /// Read-only access
    ReadOnly,
    /// Service account for automated operations
    Service,
}

impl Role {
    /// Check if this role has administrative privileges
    pub fn is_admin(&self) -> bool {
        matches!(self, Role::Admin)
    }

    /// Get default permissions for this role
    pub fn default_permissions(&self) -> HashSet<Permission> {
        match self {
            Role::Admin => {
                // Admins get all permissions
                vec![
                    Permission::BlockRead,
                    Permission::BlockWrite,
                    Permission::BlockDelete,
                    Permission::DagRead,
                    Permission::DagWrite,
                    Permission::SemanticRead,
                    Permission::SemanticWrite,
                    Permission::LogicRead,
                    Permission::LogicWrite,
                    Permission::NetworkRead,
                    Permission::NetworkWrite,
                    Permission::AdminManage,
                ]
                .into_iter()
                .collect()
            }
            Role::User => vec![
                Permission::BlockRead,
                Permission::BlockWrite,
                Permission::DagRead,
                Permission::DagWrite,
                Permission::SemanticRead,
                Permission::SemanticWrite,
                Permission::LogicRead,
                Permission::LogicWrite,
                Permission::NetworkRead,
            ]
            .into_iter()
            .collect(),
            Role::ReadOnly => vec![
                Permission::BlockRead,
                Permission::DagRead,
                Permission::SemanticRead,
                Permission::LogicRead,
                Permission::NetworkRead,
            ]
            .into_iter()
            .collect(),
            Role::Service => {
                // Service accounts get automated operation permissions
                vec![
                    Permission::BlockRead,
                    Permission::BlockWrite,
                    Permission::DagRead,
                    Permission::DagWrite,
                    Permission::SemanticWrite,
                    Permission::LogicWrite,
                ]
                .into_iter()
                .collect()
            }
        }
    }
}

/// Fine-grained permissions for resources
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Permission {
    /// Read blocks from storage
    BlockRead,
    /// Write blocks to storage
    BlockWrite,
    /// Delete blocks from storage
    BlockDelete,
    /// Read DAG structures
    DagRead,
    /// Write DAG structures
    DagWrite,
    /// Read semantic indexes
    SemanticRead,
    /// Write semantic indexes
    SemanticWrite,
    /// Read logic knowledge base
    LogicRead,
    /// Write logic knowledge base
    LogicWrite,
    /// Read network information
    NetworkRead,
    /// Modify network connections
    NetworkWrite,
    /// Manage users and permissions
    AdminManage,
}

/// Authentication token (API key or JWT)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthToken {
    /// Token identifier
    pub id: String,
    /// User ID associated with this token
    pub user_id: String,
    /// Token type (api_key or jwt)
    pub token_type: TokenType,
    /// Token secret/value
    pub secret: String,
    /// Expiration time (None for API keys)
    pub expires_at: Option<chrono::DateTime<Utc>>,
    /// User roles
    pub roles: HashSet<Role>,
    /// Custom permissions (overrides role defaults if set)
    pub permissions: Option<HashSet<Permission>>,
    /// Metadata
    pub metadata: HashMap<String, String>,
}

impl AuthToken {
    /// Check if token is expired
    pub fn is_expired(&self) -> bool {
        if let Some(exp) = self.expires_at {
            Utc::now() > exp
        } else {
            false
        }
    }

    /// Get effective permissions for this token
    pub fn effective_permissions(&self) -> HashSet<Permission> {
        if let Some(ref perms) = self.permissions {
            perms.clone()
        } else {
            // Merge permissions from all roles
            self.roles
                .iter()
                .flat_map(|role| role.default_permissions())
                .collect()
        }
    }

    /// Check if token has a specific permission
    pub fn has_permission(&self, permission: Permission) -> bool {
        self.effective_permissions().contains(&permission)
    }

    /// Check if token has admin role
    pub fn is_admin(&self) -> bool {
        self.roles.iter().any(|r| r.is_admin())
    }
}

/// Token type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TokenType {
    /// API key (long-lived, no expiration by default)
    ApiKey,
    /// JWT token (short-lived, expires)
    Jwt,
}

/// User account
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    /// User ID
    pub id: String,
    /// Username
    pub username: String,
    /// Email address
    pub email: Option<String>,
    /// Password hash (bcrypt)
    pub password_hash: Option<String>,
    /// User roles
    pub roles: HashSet<Role>,
    /// Custom permissions (overrides role defaults if set)
    pub permissions: Option<HashSet<Permission>>,
    /// Account enabled
    pub enabled: bool,
    /// Account creation time
    pub created_at: chrono::DateTime<Utc>,
    /// Last login time
    pub last_login: Option<chrono::DateTime<Utc>>,
}

impl User {
    /// Create a new user
    pub fn new(id: String, username: String) -> Self {
        Self {
            id,
            username,
            email: None,
            password_hash: None,
            roles: HashSet::new(),
            permissions: None,
            enabled: true,
            created_at: Utc::now(),
            last_login: None,
        }
    }

    /// Add a role to this user
    pub fn add_role(&mut self, role: Role) {
        self.roles.insert(role);
    }

    /// Get effective permissions for this user
    pub fn effective_permissions(&self) -> HashSet<Permission> {
        if let Some(ref perms) = self.permissions {
            perms.clone()
        } else {
            // Merge permissions from all roles
            self.roles
                .iter()
                .flat_map(|role| role.default_permissions())
                .collect()
        }
    }

    /// Check if user has a specific permission
    pub fn has_permission(&self, permission: Permission) -> bool {
        self.effective_permissions().contains(&permission)
    }
}

/// OAuth2 configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OAuth2Config {
    /// OAuth2 provider name
    pub provider: String,
    /// Client ID
    pub client_id: String,
    /// Client secret
    pub client_secret: String,
    /// Authorization endpoint
    pub auth_url: String,
    /// Token endpoint
    pub token_url: String,
    /// Redirect URI
    pub redirect_uri: String,
    /// Scopes
    pub scopes: Vec<String>,
}

/// Authentication manager
pub struct AuthManager {
    /// Token storage (token_id -> token)
    tokens: Arc<RwLock<HashMap<String, AuthToken>>>,
    /// User storage (user_id -> user)
    users: Arc<RwLock<HashMap<String, User>>>,
    /// Username lookup (username -> user_id)
    username_lookup: Arc<RwLock<HashMap<String, String>>>,
    /// OAuth2 configurations
    oauth2_configs: Arc<RwLock<HashMap<String, OAuth2Config>>>,
    /// JWT secret for signing tokens
    jwt_secret: String,
    /// Default token expiration (for JWT)
    default_token_expiration: Duration,
}

impl AuthManager {
    /// Create a new authentication manager
    pub fn new(jwt_secret: String) -> Self {
        Self {
            tokens: Arc::new(RwLock::new(HashMap::new())),
            users: Arc::new(RwLock::new(HashMap::new())),
            username_lookup: Arc::new(RwLock::new(HashMap::new())),
            oauth2_configs: Arc::new(RwLock::new(HashMap::new())),
            jwt_secret,
            default_token_expiration: Duration::hours(24),
        }
    }

    /// Set default token expiration
    pub fn with_token_expiration(mut self, duration: Duration) -> Self {
        self.default_token_expiration = duration;
        self
    }

    /// Create a new user
    pub fn create_user(
        &self,
        username: String,
        email: Option<String>,
        roles: HashSet<Role>,
    ) -> Result<User> {
        let user_id = uuid::Uuid::new_v4().to_string();

        // Check if username already exists
        {
            let lookup = self.username_lookup.read();
            if lookup.contains_key(&username) {
                return Err(anyhow!("Username already exists"));
            }
        }

        let mut user = User::new(user_id.clone(), username.clone());
        user.email = email;
        user.roles = roles;

        // Store user
        {
            let mut users = self.users.write();
            users.insert(user_id.clone(), user.clone());
        }

        // Update username lookup
        {
            let mut lookup = self.username_lookup.write();
            lookup.insert(username, user_id);
        }

        Ok(user)
    }

    /// Get user by ID
    pub fn get_user(&self, user_id: &str) -> Option<User> {
        self.users.read().get(user_id).cloned()
    }

    /// Get user by username
    pub fn get_user_by_username(&self, username: &str) -> Option<User> {
        let user_id = self.username_lookup.read().get(username).cloned()?;
        self.get_user(&user_id)
    }

    /// Update user
    pub fn update_user(&self, user: User) -> Result<()> {
        let mut users = self.users.write();
        users.insert(user.id.clone(), user);
        Ok(())
    }

    /// Delete user
    pub fn delete_user(&self, user_id: &str) -> Result<()> {
        let mut users = self.users.write();
        if let Some(user) = users.remove(user_id) {
            let mut lookup = self.username_lookup.write();
            lookup.remove(&user.username);
        }
        Ok(())
    }

    /// Create an API key for a user
    pub fn create_api_key(&self, user_id: &str, name: Option<String>) -> Result<AuthToken> {
        let user = self.get_user(user_id).context("User not found")?;

        let token_id = uuid::Uuid::new_v4().to_string();
        let secret = format!(
            "ipfrs_{}",
            uuid::Uuid::new_v4().to_string().replace('-', "")
        );

        let mut metadata = HashMap::new();
        if let Some(n) = name {
            metadata.insert("name".to_string(), n);
        }
        metadata.insert("created_at".to_string(), Utc::now().to_rfc3339());

        let token = AuthToken {
            id: token_id.clone(),
            user_id: user_id.to_string(),
            token_type: TokenType::ApiKey,
            secret: secret.clone(),
            expires_at: None,
            roles: user.roles.clone(),
            permissions: user.permissions.clone(),
            metadata,
        };

        // Store token
        {
            let mut tokens = self.tokens.write();
            tokens.insert(token_id, token.clone());
        }

        Ok(token)
    }

    /// Create a JWT token for a user
    pub fn create_jwt_token(&self, user_id: &str, duration: Option<Duration>) -> Result<AuthToken> {
        let user = self.get_user(user_id).context("User not found")?;

        let token_id = uuid::Uuid::new_v4().to_string();
        let expires_at = Utc::now() + duration.unwrap_or(self.default_token_expiration);

        // In a real implementation, this would use a proper JWT library
        // For now, we'll create a simple token structure
        let secret = self.encode_jwt(&token_id, user_id, &user.roles, expires_at)?;

        let token = AuthToken {
            id: token_id.clone(),
            user_id: user_id.to_string(),
            token_type: TokenType::Jwt,
            secret,
            expires_at: Some(expires_at),
            roles: user.roles.clone(),
            permissions: user.permissions.clone(),
            metadata: HashMap::new(),
        };

        // Store token
        {
            let mut tokens = self.tokens.write();
            tokens.insert(token_id, token.clone());
        }

        Ok(token)
    }

    /// Verify and decode a JWT token
    #[allow(dead_code)]
    fn encode_jwt(
        &self,
        token_id: &str,
        user_id: &str,
        _roles: &HashSet<Role>,
        expires_at: chrono::DateTime<Utc>,
    ) -> Result<String> {
        // Simplified JWT encoding - in production, use jsonwebtoken crate
        let payload = format!("{}:{}:{}", token_id, user_id, expires_at.timestamp());
        let signature = format!(
            "{:x}",
            md5::compute(format!("{}{}", payload, self.jwt_secret))
        );
        Ok(format!("{}.{}", payload, signature))
    }

    /// Verify a token (API key or JWT)
    pub fn verify_token(&self, secret: &str) -> Result<AuthToken> {
        // Check if it's an API key
        if secret.starts_with("ipfrs_") {
            let tokens = self.tokens.read();
            for token in tokens.values() {
                if token.secret == secret {
                    if token.is_expired() {
                        return Err(anyhow!("Token expired"));
                    }
                    return Ok(token.clone());
                }
            }
            return Err(anyhow!("Invalid API key"));
        }

        // Try to decode as JWT
        self.decode_jwt(secret)
    }

    /// Decode and verify JWT
    #[allow(dead_code)]
    fn decode_jwt(&self, jwt: &str) -> Result<AuthToken> {
        let parts: Vec<&str> = jwt.split('.').collect();
        if parts.len() != 2 {
            return Err(anyhow!("Invalid JWT format"));
        }

        let payload = parts[0];
        let signature = parts[1];

        // Verify signature
        let expected_sig = format!(
            "{:x}",
            md5::compute(format!("{}{}", payload, self.jwt_secret))
        );
        if signature != expected_sig {
            return Err(anyhow!("Invalid JWT signature"));
        }

        // Parse payload
        let payload_parts: Vec<&str> = payload.split(':').collect();
        if payload_parts.len() != 3 {
            return Err(anyhow!("Invalid JWT payload"));
        }

        let token_id = payload_parts[0];

        // Look up token
        let tokens = self.tokens.read();
        let token = tokens.get(token_id).context("Token not found")?;

        if token.is_expired() {
            return Err(anyhow!("Token expired"));
        }

        Ok(token.clone())
    }

    /// Revoke a token
    pub fn revoke_token(&self, token_id: &str) -> Result<()> {
        let mut tokens = self.tokens.write();
        tokens.remove(token_id);
        Ok(())
    }

    /// Check if a token has a specific permission
    pub fn check_permission(&self, token: &AuthToken, permission: Permission) -> Result<()> {
        if !token.has_permission(permission) {
            return Err(anyhow!(
                "Insufficient permissions: {:?} required",
                permission
            ));
        }
        Ok(())
    }

    /// Add OAuth2 provider configuration
    pub fn add_oauth2_provider(&self, config: OAuth2Config) -> Result<()> {
        let mut configs = self.oauth2_configs.write();
        configs.insert(config.provider.clone(), config);
        Ok(())
    }

    /// Get OAuth2 authorization URL
    pub fn get_oauth2_auth_url(&self, provider: &str, state: &str) -> Result<String> {
        let configs = self.oauth2_configs.read();
        let config = configs
            .get(provider)
            .context("OAuth2 provider not configured")?;

        let scopes = config.scopes.join(" ");
        Ok(format!(
            "{}?client_id={}&redirect_uri={}&response_type=code&scope={}&state={}",
            config.auth_url,
            config.client_id,
            urlencoding::encode(&config.redirect_uri),
            urlencoding::encode(&scopes),
            state
        ))
    }

    /// List all users (admin only)
    pub fn list_users(&self) -> Vec<User> {
        self.users.read().values().cloned().collect()
    }

    /// List all tokens for a user
    pub fn list_user_tokens(&self, user_id: &str) -> Vec<AuthToken> {
        self.tokens
            .read()
            .values()
            .filter(|t| t.user_id == user_id)
            .cloned()
            .collect()
    }

    /// Clean up expired tokens
    pub fn cleanup_expired_tokens(&self) -> usize {
        let mut tokens = self.tokens.write();
        let before_count = tokens.len();
        tokens.retain(|_, token| !token.is_expired());
        before_count - tokens.len()
    }
}

impl Default for AuthManager {
    fn default() -> Self {
        Self::new("default_secret_change_in_production".to_string())
    }
}

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

    #[test]
    fn test_role_permissions() {
        let admin_perms = Role::Admin.default_permissions();
        assert!(admin_perms.contains(&Permission::AdminManage));
        assert!(admin_perms.contains(&Permission::BlockWrite));

        let readonly_perms = Role::ReadOnly.default_permissions();
        assert!(readonly_perms.contains(&Permission::BlockRead));
        assert!(!readonly_perms.contains(&Permission::BlockWrite));
    }

    #[test]
    fn test_user_creation() {
        let manager = AuthManager::new("test_secret".to_string());

        let result = manager.create_user(
            "alice".to_string(),
            Some("alice@example.com".to_string()),
            vec![Role::User].into_iter().collect(),
        );
        assert!(result.is_ok());

        let user = result.expect("test: user creation should succeed");
        assert_eq!(user.username, "alice");
        assert!(user.has_permission(Permission::BlockRead));
    }

    #[test]
    fn test_api_key_creation() {
        let manager = AuthManager::new("test_secret".to_string());

        let user = manager
            .create_user(
                "bob".to_string(),
                None,
                vec![Role::Admin].into_iter().collect(),
            )
            .expect("test: user creation should succeed");

        let token = manager
            .create_api_key(&user.id, Some("test_key".to_string()))
            .expect("test: API key creation should succeed");
        assert_eq!(token.token_type, TokenType::ApiKey);
        assert!(token.secret.starts_with("ipfrs_"));
        assert!(token.is_admin());
    }

    #[test]
    fn test_jwt_creation() {
        let manager = AuthManager::new("test_secret".to_string());

        let user = manager
            .create_user(
                "charlie".to_string(),
                None,
                vec![Role::User].into_iter().collect(),
            )
            .expect("test: user creation should succeed");

        let token = manager
            .create_jwt_token(&user.id, None)
            .expect("test: JWT creation should succeed");
        assert_eq!(token.token_type, TokenType::Jwt);
        assert!(!token.is_expired());
    }

    #[test]
    fn test_token_verification() {
        let manager = AuthManager::new("test_secret".to_string());

        let user = manager
            .create_user(
                "dave".to_string(),
                None,
                vec![Role::User].into_iter().collect(),
            )
            .expect("test: user creation should succeed");

        let token = manager
            .create_api_key(&user.id, None)
            .expect("test: API key creation should succeed");
        let verified = manager.verify_token(&token.secret);
        assert!(verified.is_ok());

        let invalid = manager.verify_token("ipfrs_invalid");
        assert!(invalid.is_err());
    }

    #[test]
    fn test_permission_check() {
        let manager = AuthManager::new("test_secret".to_string());

        let user = manager
            .create_user(
                "eve".to_string(),
                None,
                vec![Role::ReadOnly].into_iter().collect(),
            )
            .expect("test: user creation should succeed");

        let token = manager
            .create_api_key(&user.id, None)
            .expect("test: API key creation should succeed");

        assert!(manager
            .check_permission(&token, Permission::BlockRead)
            .is_ok());
        assert!(manager
            .check_permission(&token, Permission::BlockWrite)
            .is_err());
    }

    #[test]
    fn test_token_revocation() {
        let manager = AuthManager::new("test_secret".to_string());

        let user = manager
            .create_user(
                "frank".to_string(),
                None,
                vec![Role::User].into_iter().collect(),
            )
            .expect("test: user creation should succeed");

        let token = manager
            .create_api_key(&user.id, None)
            .expect("test: API key creation should succeed");
        assert!(manager.verify_token(&token.secret).is_ok());

        manager
            .revoke_token(&token.id)
            .expect("test: token revocation should succeed");
        assert!(manager.verify_token(&token.secret).is_err());
    }
}