elif-auth 0.4.0

Authentication and authorization system for elif.rs framework - JWT, sessions, RBAC, password hashing, and middleware
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
//! Authentication guards for HTTP requests
//! 
//! Provides authentication middleware that can work with any auth provider

use std::collections::{HashSet, HashMap};

use crate::{UserContext, AuthError, AuthResult};

/// Resource-action pair for RBAC permissions
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResourceAction {
    /// Resource identifier (e.g., "users", "articles")
    pub resource: String,
    
    /// Action identifier (e.g., "create", "read", "update", "delete")
    pub action: String,
}

impl ResourceAction {
    /// Create a new resource-action pair
    pub fn new(resource: impl Into<String>, action: impl Into<String>) -> Self {
        Self {
            resource: resource.into(),
            action: action.into(),
        }
    }
}

/// Authentication guard configuration
#[derive(Debug, Clone)]
pub struct AuthGuardConfig {
    /// Paths that skip authentication
    pub skip_paths: HashSet<String>,
    
    /// Whether authentication is optional
    pub optional: bool,
    
    /// Required roles (any of these roles grants access)
    pub required_roles: Vec<String>,
    
    /// Required permissions (any of these permissions grants access)
    pub required_permissions: Vec<String>,
    
    /// Whether to require all roles (true) or any role (false)
    pub require_all_roles: bool,
    
    /// Whether to require all permissions (true) or any permission (false)
    pub require_all_permissions: bool,
    
    /// Required resource-action permissions for RBAC
    pub required_resource_actions: Vec<ResourceAction>,
    
    /// Authorization context for conditional permissions
    pub auth_context: HashMap<String, serde_json::Value>,
}

impl Default for AuthGuardConfig {
    fn default() -> Self {
        Self {
            skip_paths: ["/health", "/metrics"]
                .iter()
                .map(|s| s.to_string())
                .collect(),
            optional: false,
            required_roles: Vec::new(),
            required_permissions: Vec::new(),
            require_all_roles: false,
            require_all_permissions: false,
            required_resource_actions: Vec::new(),
            auth_context: HashMap::new(),
        }
    }
}

/// Base authentication guard trait
pub trait AuthGuard {
    /// Check if the path should skip authentication
    fn should_skip_path(&self, path: &str) -> bool;
    
    /// Check if authentication is optional
    fn is_optional(&self) -> bool;
    
    /// Validate user context against guard requirements
    fn validate_user(&self, user: &UserContext) -> AuthResult<()>;
    
    /// Validate user context with RBAC resource-action permissions
    fn validate_user_with_rbac(&self, user: &UserContext) -> AuthResult<()> {
        // First run standard validation
        self.validate_user(user)?;
        
        // Then validate resource-action permissions
        if !self.config().required_resource_actions.is_empty() {
            self.validate_resource_actions(user)?;
        }
        
        Ok(())
    }
    
    /// Validate resource-action permissions
    fn validate_resource_actions(&self, user: &UserContext) -> AuthResult<()> {
        for resource_action in &self.config().required_resource_actions {
            let permission_key = format!("{}.{}", resource_action.resource, resource_action.action);
            
            if !user.permissions.contains(&permission_key) {
                return Err(AuthError::insufficient_permissions(&format!(
                    "User lacks permission for {}.{}",
                    resource_action.resource,
                    resource_action.action
                )));
            }
        }
        
        Ok(())
    }
    
    /// Get guard configuration
    fn config(&self) -> &AuthGuardConfig;
}

/// Required authentication guard - authentication must succeed
#[derive(Debug, Clone)]
pub struct RequireAuth {
    config: AuthGuardConfig,
}

impl RequireAuth {
    /// Create a new required authentication guard
    pub fn new() -> Self {
        Self {
            config: AuthGuardConfig::default(),
        }
    }
    
    /// Create with custom configuration
    pub fn with_config(config: AuthGuardConfig) -> Self {
        Self { config }
    }
    
    /// Add a path to skip authentication
    pub fn skip_path<S: Into<String>>(mut self, path: S) -> Self {
        self.config.skip_paths.insert(path.into());
        self
    }
    
    /// Add multiple paths to skip authentication
    pub fn skip_paths<I, S>(mut self, paths: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        for path in paths {
            self.config.skip_paths.insert(path.into());
        }
        self
    }
    
    /// Require specific role
    pub fn require_role<S: Into<String>>(mut self, role: S) -> Self {
        self.config.required_roles.push(role.into());
        self
    }
    
    /// Require specific roles
    pub fn require_roles<I, S>(mut self, roles: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.config.required_roles.extend(roles.into_iter().map(|r| r.into()));
        self
    }
    
    /// Require specific permission
    pub fn require_permission<S: Into<String>>(mut self, permission: S) -> Self {
        self.config.required_permissions.push(permission.into());
        self
    }
    
    /// Require specific permissions
    pub fn require_permissions<I, S>(mut self, permissions: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.config.required_permissions.extend(permissions.into_iter().map(|p| p.into()));
        self
    }
    
    /// Require ALL specified roles instead of ANY
    pub fn require_all_roles(mut self) -> Self {
        self.config.require_all_roles = true;
        self
    }
    
    /// Require ALL specified permissions instead of ANY
    pub fn require_all_permissions(mut self) -> Self {
        self.config.require_all_permissions = true;
        self
    }
    
    /// Require specific resource-action permission
    pub fn require_resource_action(mut self, resource: impl Into<String>, action: impl Into<String>) -> Self {
        self.config.required_resource_actions.push(ResourceAction::new(resource, action));
        self
    }
    
    /// Require multiple resource-action permissions
    pub fn require_resource_actions<I>(mut self, resource_actions: I) -> Self
    where
        I: IntoIterator<Item = (String, String)>,
    {
        for (resource, action) in resource_actions {
            self.config.required_resource_actions.push(ResourceAction::new(resource, action));
        }
        self
    }
    
    /// Add context for conditional permissions
    pub fn with_context<K, V>(mut self, key: K, value: V) -> Self
    where
        K: Into<String>,
        V: Into<serde_json::Value>,
    {
        self.config.auth_context.insert(key.into(), value.into());
        self
    }
    
    /// Add multiple context values
    pub fn with_contexts<I, K, V>(mut self, contexts: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<serde_json::Value>,
    {
        for (key, value) in contexts {
            self.config.auth_context.insert(key.into(), value.into());
        }
        self
    }
}

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

impl AuthGuard for RequireAuth {
    fn should_skip_path(&self, path: &str) -> bool {
        self.config.skip_paths.contains(path)
    }
    
    fn is_optional(&self) -> bool {
        false // Required authentication is never optional
    }
    
    fn validate_user(&self, user: &UserContext) -> AuthResult<()> {
        // Check role requirements
        if !self.config.required_roles.is_empty() {
            let has_required_roles = if self.config.require_all_roles {
                // Check if user has ALL required roles
                self.config.required_roles.iter().all(|role| user.roles.contains(role))
            } else {
                // Check if user has ANY required role
                self.config.required_roles.iter().any(|role| user.roles.contains(role))
            };
            
            if !has_required_roles {
                return Err(AuthError::insufficient_permissions(&format!(
                    "User lacks required roles: {:?}", 
                    self.config.required_roles
                )));
            }
        }
        
        // Check permission requirements
        if !self.config.required_permissions.is_empty() {
            let has_required_permissions = if self.config.require_all_permissions {
                // Check if user has ALL required permissions
                self.config.required_permissions.iter().all(|perm| user.permissions.contains(perm))
            } else {
                // Check if user has ANY required permission
                self.config.required_permissions.iter().any(|perm| user.permissions.contains(perm))
            };
            
            if !has_required_permissions {
                return Err(AuthError::insufficient_permissions(&format!(
                    "User lacks required permissions: {:?}", 
                    self.config.required_permissions
                )));
            }
        }
        
        Ok(())
    }
    
    fn config(&self) -> &AuthGuardConfig {
        &self.config
    }
}

/// Optional authentication guard - authentication failure is allowed
#[derive(Debug, Clone)]
pub struct OptionalAuth {
    config: AuthGuardConfig,
}

impl OptionalAuth {
    /// Create a new optional authentication guard
    pub fn new() -> Self {
        let mut config = AuthGuardConfig::default();
        config.optional = true;
        
        Self { config }
    }
    
    /// Create with custom configuration
    pub fn with_config(mut config: AuthGuardConfig) -> Self {
        config.optional = true;
        Self { config }
    }
    
    /// Add a path to skip authentication
    pub fn skip_path<S: Into<String>>(mut self, path: S) -> Self {
        self.config.skip_paths.insert(path.into());
        self
    }
    
    /// Add multiple paths to skip authentication
    pub fn skip_paths<I, S>(mut self, paths: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        for path in paths {
            self.config.skip_paths.insert(path.into());
        }
        self
    }
}

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

impl AuthGuard for OptionalAuth {
    fn should_skip_path(&self, path: &str) -> bool {
        self.config.skip_paths.contains(path)
    }
    
    fn is_optional(&self) -> bool {
        true // Optional authentication allows failures
    }
    
    fn validate_user(&self, _user: &UserContext) -> AuthResult<()> {
        // Optional auth doesn't validate - any authenticated user is allowed
        Ok(())
    }
    
    fn config(&self) -> &AuthGuardConfig {
        &self.config
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use chrono::Utc;

    fn create_test_user() -> UserContext {
        UserContext {
            user_id: "123".to_string(),
            username: "test@example.com".to_string(),
            roles: vec!["user".to_string(), "moderator".to_string()],
            permissions: vec!["read".to_string(), "write".to_string()],
            auth_provider: "test".to_string(),
            authenticated_at: Utc::now(),
            expires_at: None,
            additional_data: HashMap::new(),
        }
    }

    #[test]
    fn test_require_auth_creation() {
        let guard = RequireAuth::new();
        assert!(!guard.is_optional());
        assert!(guard.should_skip_path("/health"));
        assert!(guard.should_skip_path("/metrics"));
        assert!(!guard.should_skip_path("/protected"));
    }

    #[test]
    fn test_require_auth_custom_skip_paths() {
        let guard = RequireAuth::new()
            .skip_path("/public")
            .skip_paths(["/docs", "/swagger"]);
        
        assert!(guard.should_skip_path("/public"));
        assert!(guard.should_skip_path("/docs"));
        assert!(guard.should_skip_path("/swagger"));
        assert!(!guard.should_skip_path("/private"));
    }

    #[test]
    fn test_require_auth_role_validation_any() {
        let user = create_test_user();
        
        // Should pass - user has 'user' role
        let guard = RequireAuth::new().require_role("user");
        assert!(guard.validate_user(&user).is_ok());
        
        // Should pass - user has 'moderator' role
        let guard = RequireAuth::new().require_role("moderator");
        assert!(guard.validate_user(&user).is_ok());
        
        // Should fail - user doesn't have 'admin' role
        let guard = RequireAuth::new().require_role("admin");
        assert!(guard.validate_user(&user).is_err());
        
        // Should pass - user has one of the required roles
        let guard = RequireAuth::new().require_roles(["admin", "moderator"]);
        assert!(guard.validate_user(&user).is_ok());
    }

    #[test]
    fn test_require_auth_role_validation_all() {
        let user = create_test_user();
        
        // Should pass - user has both required roles
        let guard = RequireAuth::new()
            .require_roles(["user", "moderator"])
            .require_all_roles();
        assert!(guard.validate_user(&user).is_ok());
        
        // Should fail - user doesn't have 'admin' role
        let guard = RequireAuth::new()
            .require_roles(["user", "admin"])
            .require_all_roles();
        assert!(guard.validate_user(&user).is_err());
    }

    #[test]
    fn test_require_auth_permission_validation_any() {
        let user = create_test_user();
        
        // Should pass - user has 'read' permission
        let guard = RequireAuth::new().require_permission("read");
        assert!(guard.validate_user(&user).is_ok());
        
        // Should fail - user doesn't have 'delete' permission
        let guard = RequireAuth::new().require_permission("delete");
        assert!(guard.validate_user(&user).is_err());
        
        // Should pass - user has one of the required permissions
        let guard = RequireAuth::new().require_permissions(["delete", "write"]);
        assert!(guard.validate_user(&user).is_ok());
    }

    #[test]
    fn test_require_auth_permission_validation_all() {
        let user = create_test_user();
        
        // Should pass - user has both required permissions
        let guard = RequireAuth::new()
            .require_permissions(["read", "write"])
            .require_all_permissions();
        assert!(guard.validate_user(&user).is_ok());
        
        // Should fail - user doesn't have 'delete' permission
        let guard = RequireAuth::new()
            .require_permissions(["read", "delete"])
            .require_all_permissions();
        assert!(guard.validate_user(&user).is_err());
    }

    #[test]
    fn test_optional_auth_creation() {
        let guard = OptionalAuth::new();
        assert!(guard.is_optional());
        assert!(guard.should_skip_path("/health"));
        assert!(guard.should_skip_path("/metrics"));
    }

    #[test]
    fn test_optional_auth_validation() {
        let user = create_test_user();
        let guard = OptionalAuth::new();
        
        // Optional auth always passes validation
        assert!(guard.validate_user(&user).is_ok());
    }

    #[test]
    fn test_optional_auth_custom_skip_paths() {
        let guard = OptionalAuth::new()
            .skip_path("/api")
            .skip_paths(["/v1", "/v2"]);
        
        assert!(guard.should_skip_path("/api"));
        assert!(guard.should_skip_path("/v1"));
        assert!(guard.should_skip_path("/v2"));
        assert!(!guard.should_skip_path("/protected"));
    }

    #[test]
    fn test_resource_action_creation() {
        let resource_action = ResourceAction::new("users", "create");
        assert_eq!(resource_action.resource, "users");
        assert_eq!(resource_action.action, "create");
    }

    #[test]
    fn test_require_auth_rbac_resource_actions() {
        let mut user = create_test_user();
        user.permissions = vec![
            "users.create".to_string(),
            "articles.read".to_string(),
            "articles.edit".to_string(),
        ];
        
        // Should pass - user has users.create permission
        let guard = RequireAuth::new()
            .require_resource_action("users", "create");
        assert!(guard.validate_user_with_rbac(&user).is_ok());
        
        // Should fail - user doesn't have users.delete permission
        let guard = RequireAuth::new()
            .require_resource_action("users", "delete");
        assert!(guard.validate_user_with_rbac(&user).is_err());
        
        // Should pass - user has both required permissions
        let guard = RequireAuth::new()
            .require_resource_actions([
                ("users".to_string(), "create".to_string()),
                ("articles".to_string(), "read".to_string()),
            ]);
        assert!(guard.validate_user_with_rbac(&user).is_ok());
        
        // Should fail - user doesn't have users.delete permission
        let guard = RequireAuth::new()
            .require_resource_actions([
                ("users".to_string(), "create".to_string()),
                ("users".to_string(), "delete".to_string()),
            ]);
        assert!(guard.validate_user_with_rbac(&user).is_err());
    }

    #[test]
    fn test_require_auth_with_context() {
        let guard = RequireAuth::new()
            .with_context("owner", "user123")
            .with_contexts([
                ("department".to_string(), serde_json::json!("engineering")),
                ("role_level".to_string(), serde_json::json!(5)),
            ]);
        
        assert_eq!(guard.config().auth_context.get("owner").unwrap(), "user123");
        assert_eq!(guard.config().auth_context.get("department").unwrap(), "engineering");
        assert_eq!(guard.config().auth_context.get("role_level").unwrap(), &serde_json::json!(5));
    }

    #[test]
    fn test_validate_resource_actions() {
        let mut user = create_test_user();
        user.permissions = vec!["users.create".to_string(), "articles.read".to_string()];
        
        let guard = RequireAuth::new()
            .require_resource_action("users", "create")
            .require_resource_action("articles", "read");
        
        // Should pass validation
        assert!(guard.validate_resource_actions(&user).is_ok());
        
        // Add a permission the user doesn't have
        let guard = RequireAuth::new()
            .require_resource_action("users", "create")
            .require_resource_action("users", "delete");
        
        // Should fail validation
        assert!(guard.validate_resource_actions(&user).is_err());
    }
}