auth-framework 0.5.0-rc18

A comprehensive, production-ready authentication and authorization framework for Rust applications
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
use auth_framework::auth::AuthFramework;
use auth_framework::authentication::credentials::Credential;
use auth_framework::config::AuthConfig;
use auth_framework::testing::test_infrastructure::TestEnvironmentGuard;
use auth_framework::tokens::AuthToken;
use std::sync::Arc;
use std::time::Duration;

/// Comprehensive error path testing to ensure robust error handling
/// These tests verify that the framework fails gracefully in all error scenarios

#[tokio::test]
async fn test_uninitialized_framework_operations() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    let config = AuthConfig::default();
    let framework = AuthFramework::new(config);
    // Deliberately NOT calling framework.initialize()

    // Test authenticate on uninitialized framework
    let credential = Credential::password("user", "pass");
    match framework.authenticate("password", credential).await {
        Ok(_) => panic!("Uninitialized framework should not allow authentication"),
        Err(e) => assert!(
            e.to_string().contains("not initialized") || e.to_string().contains("Internal error")
        ),
    }

    // Test session creation on uninitialized framework
    match framework
        .create_session("user", Duration::from_secs(3600), None, None)
        .await
    {
        Ok(_) => panic!("Uninitialized framework should not allow session creation"),
        Err(e) => assert!(
            e.to_string().contains("not initialized") || e.to_string().contains("Internal error")
        ),
    }

    // Test token validation on uninitialized framework
    let token = AuthToken::new(
        "test_user",
        "test_token",
        Duration::from_secs(3600),
        "test_method",
    );
    match framework.validate_token(&token).await {
        Ok(_) => panic!("Uninitialized framework should not allow token validation"),
        Err(e) => assert!(
            e.to_string().contains("not initialized") || e.to_string().contains("Internal error")
        ),
    }
}

#[tokio::test]
async fn test_malformed_input_handling() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    let config = AuthConfig::default();
    let mut framework = AuthFramework::new(config);
    framework.initialize().await.unwrap();

    // Test extreme input sizes
    let long_string = "a".repeat(10000);
    let long_string2 = "b".repeat(10000);

    let extreme_inputs = vec![
        ("", ""),                                                 // Empty strings
        (long_string.as_str(), long_string2.as_str()),            // Very long strings
        ("user\0with\0nulls", "pass\0with\0nulls"),               // Null bytes
        ("👤🚀💻", "🔐🌟⚡"),                                     // Unicode emoji
        ("\x01\x02\x03", "\x04\x05\x06"),                         // Control characters
        ("user\r\nwith\r\nnewlines", "pass\r\nwith\r\nnewlines"), // Newlines
        ("user\twith\ttabs", "pass\twith\ttabs"),                 // Tabs
    ];

    for (username, password) in extreme_inputs {
        let credential = Credential::password(username, password);

        // Should handle gracefully without panicking
        let result = framework.authenticate("password", credential).await;
        match result {
            Ok(_) => (), // Might be valid depending on implementation
            Err(e) => {
                // Error should be descriptive, not a panic
                assert!(!e.to_string().is_empty());
            }
        }
    }
}

#[tokio::test]
async fn test_invalid_jwt_token_handling() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    let config = AuthConfig::default();
    let mut framework = AuthFramework::new(config);
    framework.initialize().await.unwrap();

    // Test various malformed JWT tokens as token values
    let malformed_tokens = vec![
        "",                                                       // Empty token
        "invalid",                                                // Not a JWT
        "header.payload",                                         // Missing signature
        "header.payload.signature.extra",                         // Too many parts
        "invalid.header.here",                                    // Invalid format
        "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.invalid.signature", // Invalid payload
        "header.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.invalid", // Invalid signature
    ];

    for malformed_token in malformed_tokens {
        // Create a token object with malformed access token
        let token = AuthToken::new(
            "test_user",
            malformed_token,
            Duration::from_secs(3600),
            "jwt",
        );
        match framework.validate_token(&token).await {
            Ok(false) => (), // Correctly rejected
            Ok(true) => {
                if !malformed_token.is_empty() {
                    panic!("Malformed token '{}' should not validate", malformed_token);
                }
            }
            Err(_) => (), // Error is acceptable for malformed input
        }
    }
}

#[tokio::test]
async fn test_concurrent_operation_safety() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    let config = AuthConfig::default();
    let mut framework = AuthFramework::new(config);
    framework.initialize().await.unwrap();

    let framework = Arc::new(framework);
    let mut handles = vec![];

    // Test many concurrent operations to detect race conditions
    for i in 0..50 {
        let framework = framework.clone();
        let handle = tokio::spawn(async move {
            let user_id = format!("user_{}", i);

            // Create session
            let session_result = framework
                .create_session(&user_id, Duration::from_secs(3600), None, None)
                .await;

            if let Ok(session_id) = session_result {
                // Get session
                let get_result = framework.get_session(&session_id).await;

                // Delete session
                let delete_result = framework.delete_session(&session_id).await;

                // Verify operations completed
                (get_result.is_ok(), delete_result.is_ok())
            } else {
                (false, false)
            }
        });
        handles.push(handle);
    }

    // Wait for all operations with timeout to detect deadlocks
    let mut success_count = 0;
    for handle in handles {
        match tokio::time::timeout(Duration::from_secs(10), handle).await {
            Ok(Ok((get_ok, delete_ok))) => {
                if get_ok && delete_ok {
                    success_count += 1;
                }
            }
            Ok(Err(_)) => panic!("Task panicked during concurrent operations"),
            Err(_) => panic!("Deadlock detected in concurrent operations"),
        }
    }

    // Most operations should succeed (allowing for some contention)
    assert!(
        success_count > 40,
        "Too many concurrent operations failed: {}/50",
        success_count
    );
}

#[tokio::test]
async fn test_session_storage_error_recovery() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    let config = AuthConfig::default();
    let mut framework = AuthFramework::new(config);
    framework.initialize().await.unwrap();

    // Test session operations with invalid session IDs
    let long_id = "a".repeat(1000);
    let newline_id = format!("sess_{}", "\r\n".repeat(100));
    let long_sess_id = format!("sess_{}", long_id);

    let invalid_session_ids = vec![
        "",                // Empty
        "invalid-format",  // Wrong format
        &long_sess_id,     // Too long
        "sess_\0\x01\x02", // Control characters
        "sess_👤🚀💻",     // Unicode
        &newline_id,       // Newlines
    ];

    for invalid_id in invalid_session_ids {
        // Get session should handle gracefully
        match framework.get_session(invalid_id).await {
            Ok(None) => (), // Correctly returns None for invalid ID
            Ok(Some(_)) => panic!("Invalid session ID '{}' should not return data", invalid_id),
            Err(_) => (), // Error is acceptable for malformed input
        }

        // Delete session should handle gracefully
        let _ = framework.delete_session(invalid_id).await;
        // Might succeed (idempotent delete) or error is acceptable for malformed input
    }
}

#[tokio::test]
async fn test_authentication_method_error_paths() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    let config = AuthConfig::default();
    let mut framework = AuthFramework::new(config);
    framework.initialize().await.unwrap();

    // Test non-existent authentication methods
    let invalid_methods = vec![
        "",                   // Empty method name
        "nonexistent",        // Non-existent method
        "password\0",         // Method with null byte
        "method with spaces", // Method with spaces
        "🔐method",           // Unicode method
    ];

    for method in invalid_methods {
        let credential = Credential::password("user", "pass");
        match framework.authenticate(method, credential).await {
            Ok(_) => panic!("Invalid method '{}' should not succeed", method),
            Err(e) => {
                // Error should be descriptive
                assert!(!e.to_string().is_empty());
                // Should mention method not found or similar
                let error_msg = e.to_string().to_lowercase();
                assert!(
                    error_msg.contains("not found")
                        || error_msg.contains("invalid")
                        || error_msg.contains("unknown")
                        || error_msg.contains("method")
                );
            }
        }
    }
}

#[tokio::test]
async fn test_token_expiration_edge_cases() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    let config = AuthConfig::default();
    let mut framework = AuthFramework::new(config);
    framework.initialize().await.unwrap();

    // Test token with zero duration (immediate expiration)
    let expired_token = AuthToken::new("test_user", "test_token", Duration::from_secs(0), "test");

    // Wait a tiny bit to ensure expiration
    tokio::time::sleep(Duration::from_millis(1)).await;

    match framework.validate_token(&expired_token).await {
        Ok(false) => (), // Correctly identified as invalid/expired
        Ok(true) => panic!("Expired token should not validate"),
        Err(_) => (), // Error is acceptable for expired token
    }

    // Test token with very long duration (potential overflow)
    let long_token = AuthToken::new(
        "test_user",
        "test_token",
        Duration::from_secs(u64::MAX / 2),
        "test",
    );

    let _ = framework.validate_token(&long_token).await;
    // Any result is acceptable - error is acceptable for extreme values
}

#[tokio::test]
async fn test_cleanup_operations_error_handling() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    let config = AuthConfig::default();
    let mut framework = AuthFramework::new(config);
    framework.initialize().await.unwrap();

    // Test cleanup operations multiple times to ensure idempotency
    for _ in 0..5 {
        // Cleanup should not fail even when there's nothing to clean
        match framework.cleanup_expired_data().await {
            Ok(_) => (), // Success is expected
            Err(e) => panic!("Cleanup should not fail: {}", e),
        }
    }

    // Create some data and immediately clean it up
    let session_result = framework
        .create_session("user", Duration::from_secs(1), None, None)
        .await;
    if let Ok(session_id) = session_result {
        // Wait for expiration
        tokio::time::sleep(Duration::from_secs(2)).await;

        // Cleanup should handle expired data gracefully
        match framework.cleanup_expired_data().await {
            Ok(_) => (), // Success expected
            Err(e) => panic!("Cleanup of expired data failed: {}", e),
        }

        // Session should now be gone
        match framework.get_session(&session_id).await {
            Ok(None) => (),    // Correctly cleaned up
            Ok(Some(_)) => (), // Might still exist depending on cleanup timing
            Err(_) => (),      // Error is acceptable
        }
    }
}

#[tokio::test]
async fn test_credential_validation_edge_cases() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    let config = AuthConfig::default();
    let mut framework = AuthFramework::new(config);
    framework.initialize().await.unwrap();

    // Test various credential edge cases
    let edge_case_credentials = vec![
        Credential::password("", ""),                 // Empty username/password
        Credential::password("user", ""),             // Empty password
        Credential::password("", "password"),         // Empty username
        Credential::password("👤", "🔐"),             // Unicode
        Credential::password("user\0", "pass\0"),     // Null bytes
        Credential::password("user\r\n", "pass\r\n"), // Newlines
    ];

    for credential in edge_case_credentials {
        // Should handle gracefully without panicking
        match framework.authenticate("password", credential).await {
            Ok(_) => (), // Might be valid
            Err(e) => {
                // Error should be descriptive
                assert!(!e.to_string().is_empty());
            }
        }
    }
}

#[tokio::test]
async fn test_memory_pressure_scenarios() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    let config = AuthConfig::default();
    let mut framework = AuthFramework::new(config);
    framework.initialize().await.unwrap();

    let framework = Arc::new(framework);

    // Create many sessions to test memory handling
    let mut session_ids = Vec::new();

    for i in 0..1000 {
        let user_id = format!("user_{}", i);
        match framework
            .create_session(&user_id, Duration::from_secs(3600), None, None)
            .await
        {
            Ok(session_id) => session_ids.push(session_id),
            Err(_) => break, // Stop if we hit limits
        }
    }

    // Framework should handle many sessions without crashing
    assert!(
        session_ids.len() > 100,
        "Should be able to create at least 100 sessions"
    );

    // Cleanup
    for session_id in session_ids {
        let _ = framework.delete_session(&session_id).await;
    }
}

#[tokio::test]
async fn test_boundary_conditions() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    let config = AuthConfig::default();
    let mut framework = AuthFramework::new(config);
    framework.initialize().await.unwrap();

    // Test session duration boundaries
    let boundary_durations = vec![
        Duration::from_secs(0),               // Zero duration
        Duration::from_secs(1),               // Minimal duration
        Duration::from_secs(u32::MAX as u64), // Large duration
    ];

    for duration in boundary_durations {
        if let Ok(session_id) = framework
            .create_session("test_user", duration, None, None)
            .await
        {
            // Verify session exists
            match framework.get_session(&session_id).await {
                Ok(Some(_)) => (),
                Ok(None) => (), // Might have expired immediately
                Err(_) => (),
            }
            // Cleanup
            let _ = framework.delete_session(&session_id).await;
        }
        // Error is acceptable for boundary values
    }
}

#[tokio::test]
async fn test_double_initialization_error() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    let config = AuthConfig::default();
    let mut framework = AuthFramework::new(config);

    // First initialization should succeed
    framework.initialize().await.unwrap();

    // Second initialization should either succeed (idempotent) or fail gracefully
    match framework.initialize().await {
        Ok(_) => (), // Idempotent initialization is acceptable
        Err(e) => {
            // Error should be descriptive and not a panic
            assert!(!e.to_string().is_empty());
            let error_msg = e.to_string().to_lowercase();
            assert!(
                error_msg.contains("already initialized")
                    || error_msg.contains("initialized")
                    || error_msg.contains("duplicate")
            );
        }
    }

    // Framework should still be functional after double init attempt
    let session_result = framework
        .create_session("test_user", Duration::from_secs(3600), None, None)
        .await;
    assert!(session_result.is_ok(), "Framework should remain functional");
}

#[tokio::test]
async fn test_invalid_config_handling() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    // Test framework creation with potentially problematic configs
    let config = AuthConfig::default();
    let mut framework = AuthFramework::new(config);

    // Initialization should handle any config issues gracefully
    match framework.initialize().await {
        Ok(_) => (), // Success is expected for default config
        Err(e) => {
            // Any error should be descriptive
            assert!(!e.to_string().is_empty());
        }
    }
}

#[tokio::test]
async fn test_resource_exhaustion_recovery() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    let config = AuthConfig::default();
    let mut framework = AuthFramework::new(config);
    framework.initialize().await.unwrap();

    let framework = Arc::new(framework);

    // Try to exhaust session storage
    let mut session_ids = Vec::new();
    let mut creation_failed = false;

    for i in 0..5000 {
        match framework
            .create_session(
                &format!("stress_user_{}", i),
                Duration::from_secs(3600),
                None,
                None,
            )
            .await
        {
            Ok(session_id) => session_ids.push(session_id),
            Err(_) => {
                creation_failed = true;
                break;
            }
        }
    }

    // Framework should either handle all sessions or fail gracefully
    if creation_failed {
        println!(
            "Session creation failed at {} sessions (acceptable)",
            session_ids.len()
        );
    } else {
        println!("Created {} sessions successfully", session_ids.len());
    }

    // Framework should still be responsive after stress
    let test_session = framework
        .create_session("recovery_test", Duration::from_secs(3600), None, None)
        .await;

    // Should either succeed or fail gracefully (not hang/crash)
    if let Ok(session_id) = test_session {
        // Verify we can still operate normally
        assert!(framework.get_session(&session_id).await.is_ok());
        let _ = framework.delete_session(&session_id).await;
    }
    // Acceptable if resource constrained

    // Cleanup what we can
    for session_id in session_ids.into_iter().take(100) {
        let _ = framework.delete_session(&session_id).await;
    }
}

#[tokio::test]
async fn test_error_message_consistency() {
    let _env = TestEnvironmentGuard::new().with_jwt_secret("test-secret");

    let config = AuthConfig::default();
    let mut framework = AuthFramework::new(config);
    framework.initialize().await.unwrap();

    // Test that similar error conditions produce consistent error messages
    let test_cases = vec![
        ("", "empty_input"),
        ("nonexistent", "nonexistent_user"),
        ("user\0", "null_byte_input"),
        ("👤", "unicode_input"),
    ];

    for (input, description) in test_cases {
        let credential = Credential::password(input, "test_password");

        // Test the same error condition multiple times
        let mut error_messages = Vec::new();
        for _ in 0..3 {
            match framework.authenticate("password", credential.clone()).await {
                Ok(_) => error_messages.push("SUCCESS".to_string()),
                Err(e) => error_messages.push(e.to_string()),
            }
        }

        // All error messages should be identical for the same input
        let first_message = &error_messages[0];
        let all_same = error_messages.iter().all(|msg| msg == first_message);

        assert!(
            all_same,
            "Inconsistent error messages for {}: {:?}",
            description, error_messages
        );
    }
}