rusmes-auth 0.1.2

Pluggable authentication backends for RusMES — File, LDAP, SQL, OAuth2/OIDC, System, and SASL (PLAIN/LOGIN/CRAM-MD5/SCRAM-SHA-256/XOAUTH2) with brute-force protection
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
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
//! SQL database authentication backend

use crate::AuthBackend;
use async_trait::async_trait;
use rusmes_proto::Username;
use sqlx::{AnyPool, Row};
use std::net::IpAddr;

/// Audit log entry
#[derive(Debug, Clone)]
pub struct AuditLog {
    /// Username
    pub username: String,
    /// IP address
    pub ip_address: Option<String>,
    /// Whether authentication succeeded
    pub success: bool,
    /// Failure reason if authentication failed
    pub failure_reason: Option<String>,
    /// Timestamp
    pub timestamp: String,
}

/// Password hash type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HashType {
    /// bcrypt hashing
    Bcrypt,
    /// Argon2 hashing
    Argon2,
    /// SCRAM-SHA-256 credentials
    ScramSha256,
}

impl HashType {
    fn from_prefix(hash: &str) -> Self {
        if hash.starts_with("$2") {
            HashType::Bcrypt
        } else if hash.starts_with("$argon2") {
            HashType::Argon2
        } else if hash.starts_with("$scram-sha-256$") {
            HashType::ScramSha256
        } else {
            HashType::Bcrypt // default
        }
    }
}

/// User metadata extracted from database
#[derive(Debug, Clone)]
pub struct UserMetadata {
    /// User enabled status
    pub enabled: bool,
    /// Quota in bytes
    pub quota_bytes: i64,
    /// User roles (comma-separated)
    pub roles: Option<String>,
}

impl UserMetadata {
    /// Get roles as a vector
    pub fn roles_vec(&self) -> Vec<String> {
        self.roles
            .as_ref()
            .map(|r| r.split(',').map(|s| s.trim().to_string()).collect())
            .unwrap_or_default()
    }
}

/// Configuration for SQL authentication
#[derive(Debug, Clone)]
pub struct SqlConfig {
    /// Database connection URL
    pub database_url: String,
    /// Query to get password hash (must return columns: password_hash, enabled, quota_bytes, roles)
    pub password_query: String,
    /// Query to list all users (must return column: username)
    pub list_users_query: String,
    /// Query to create user
    pub create_user_query: String,
    /// Query to delete user
    pub delete_user_query: String,
    /// Query to update password
    pub update_password_query: String,
    /// Query to get SCRAM parameters (salt, iterations)
    pub scram_params_query: Option<String>,
    /// Query to get SCRAM StoredKey
    pub scram_stored_key_query: Option<String>,
    /// Query to get SCRAM ServerKey
    pub scram_server_key_query: Option<String>,
    /// Query to store SCRAM credentials
    pub store_scram_query: Option<String>,
    /// Audit table name for logging auth attempts
    pub audit_table: Option<String>,
    /// Maximum pool connections
    pub max_connections: u32,
}

impl Default for SqlConfig {
    fn default() -> Self {
        Self {
            database_url: "sqlite:file::memory:?cache=shared".to_string(),
            password_query: "SELECT password_hash, enabled, quota_bytes, roles FROM users WHERE username = ?".to_string(),
            list_users_query: "SELECT username FROM users".to_string(),
            create_user_query: "INSERT INTO users (username, password_hash, enabled, quota_bytes, roles) VALUES (?, ?, 1, 1073741824, ?)".to_string(),
            delete_user_query: "DELETE FROM users WHERE username = ?".to_string(),
            update_password_query: "UPDATE users SET password_hash = ? WHERE username = ?".to_string(),
            scram_params_query: Some("SELECT scram_salt, scram_iterations FROM users WHERE username = ?".to_string()),
            scram_stored_key_query: Some("SELECT scram_stored_key FROM users WHERE username = ?".to_string()),
            scram_server_key_query: Some("SELECT scram_server_key FROM users WHERE username = ?".to_string()),
            store_scram_query: Some("UPDATE users SET scram_salt = ?, scram_iterations = ?, scram_stored_key = ?, scram_server_key = ? WHERE username = ?".to_string()),
            audit_table: Some("auth_audit".to_string()),
            max_connections: 10,
        }
    }
}

/// SQL authentication backend
pub struct SqlBackend {
    pool: AnyPool,
    config: SqlConfig,
}

impl SqlBackend {
    /// Create a new SQL authentication backend
    pub async fn new(config: SqlConfig) -> anyhow::Result<Self> {
        // Install drivers for sqlx::any
        sqlx::any::install_default_drivers();

        let pool = sqlx::any::AnyPoolOptions::new()
            .max_connections(config.max_connections)
            .connect(&config.database_url)
            .await?;

        Ok(Self { pool, config })
    }

    /// Initialize database schema
    pub async fn init_schema(&self) -> anyhow::Result<()> {
        // Create users table with support for multiple hash types
        sqlx::query(
            r#"
            CREATE TABLE IF NOT EXISTS users (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                username TEXT UNIQUE NOT NULL,
                password_hash TEXT NOT NULL,
                enabled INTEGER NOT NULL DEFAULT 1,
                quota_bytes BIGINT NOT NULL DEFAULT 1073741824,
                roles TEXT,
                scram_salt BLOB,
                scram_iterations INTEGER,
                scram_stored_key BLOB,
                scram_server_key BLOB,
                created_at TEXT DEFAULT CURRENT_TIMESTAMP,
                updated_at TEXT DEFAULT CURRENT_TIMESTAMP
            )
            "#,
        )
        .execute(&self.pool)
        .await?;

        // Create index on username
        sqlx::query("CREATE INDEX IF NOT EXISTS idx_users_username ON users(username)")
            .execute(&self.pool)
            .await?;

        // Create audit table if configured
        if self.config.audit_table.is_some() {
            sqlx::query(
                r#"
                CREATE TABLE IF NOT EXISTS auth_audit (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    username TEXT NOT NULL,
                    ip_address TEXT,
                    success INTEGER NOT NULL,
                    failure_reason TEXT,
                    timestamp TEXT DEFAULT CURRENT_TIMESTAMP
                )
                "#,
            )
            .execute(&self.pool)
            .await?;

            // Create index on timestamp for audit queries
            sqlx::query("CREATE INDEX IF NOT EXISTS idx_audit_timestamp ON auth_audit(timestamp)")
                .execute(&self.pool)
                .await?;

            // Create index on username for audit queries
            sqlx::query("CREATE INDEX IF NOT EXISTS idx_audit_username ON auth_audit(username)")
                .execute(&self.pool)
                .await?;
        }

        Ok(())
    }

    /// Log authentication attempt to audit table
    #[allow(dead_code)]
    async fn log_audit(
        &self,
        username: &str,
        ip: Option<IpAddr>,
        success: bool,
        failure_reason: Option<&str>,
    ) -> anyhow::Result<()> {
        if self.config.audit_table.is_none() {
            return Ok(());
        }

        let ip_str = ip.map(|i| i.to_string());

        sqlx::query(
            r#"
            INSERT INTO auth_audit (username, ip_address, success, failure_reason)
            VALUES (?, ?, ?, ?)
            "#,
        )
        .bind(username)
        .bind(ip_str)
        .bind(if success { 1 } else { 0 })
        .bind(failure_reason)
        .execute(&self.pool)
        .await?;

        Ok(())
    }

    /// Get user metadata
    pub async fn get_user_metadata(
        &self,
        username: &Username,
    ) -> anyhow::Result<Option<UserMetadata>> {
        let row = sqlx::query(&self.config.password_query)
            .bind(username.to_string())
            .fetch_optional(&self.pool)
            .await?;

        let row = match row {
            Some(r) => r,
            None => return Ok(None),
        };

        let enabled: i64 = row.try_get("enabled")?;
        let quota_bytes: i64 = row.try_get("quota_bytes")?;
        let roles: Option<String> = row.try_get("roles").ok();

        Ok(Some(UserMetadata {
            enabled: enabled != 0,
            quota_bytes,
            roles,
        }))
    }

    /// Get recent audit logs for a user
    pub async fn get_audit_logs(
        &self,
        username: &str,
        limit: i64,
    ) -> anyhow::Result<Vec<AuditLog>> {
        if self.config.audit_table.is_none() {
            return Ok(Vec::new());
        }

        let rows = sqlx::query(
            r#"
            SELECT username, ip_address, success, failure_reason, timestamp
            FROM auth_audit
            WHERE username = ?
            ORDER BY timestamp DESC
            LIMIT ?
            "#,
        )
        .bind(username)
        .bind(limit)
        .fetch_all(&self.pool)
        .await?;

        let mut logs = Vec::new();
        for row in rows {
            let log = AuditLog {
                username: row.try_get("username")?,
                ip_address: row.try_get("ip_address").ok(),
                success: row.try_get::<i64, _>("success")? != 0,
                failure_reason: row.try_get("failure_reason").ok(),
                timestamp: row.try_get("timestamp")?,
            };
            logs.push(log);
        }

        Ok(logs)
    }

    /// Verify password hash
    fn verify_hash(&self, password: &str, hash: &str) -> anyhow::Result<bool> {
        let hash_type = HashType::from_prefix(hash);

        match hash_type {
            HashType::Bcrypt => Ok(bcrypt::verify(password, hash)?),
            HashType::Argon2 => {
                use argon2::{Argon2, PasswordHash, PasswordVerifier};
                let parsed_hash = PasswordHash::new(hash)
                    .map_err(|e| anyhow::anyhow!("Failed to parse Argon2 hash: {}", e))?;
                Ok(Argon2::default()
                    .verify_password(password.as_bytes(), &parsed_hash)
                    .is_ok())
            }
            HashType::ScramSha256 => {
                // SCRAM hashes cannot be verified directly - they require the challenge/response flow
                Err(anyhow::anyhow!(
                    "SCRAM-SHA-256 requires challenge/response authentication"
                ))
            }
        }
    }

    /// Hash password using bcrypt
    fn hash_password(&self, password: &str) -> anyhow::Result<String> {
        Ok(bcrypt::hash(password, bcrypt::DEFAULT_COST)?)
    }
}

#[async_trait]
impl AuthBackend for SqlBackend {
    async fn authenticate(&self, username: &Username, password: &str) -> anyhow::Result<bool> {
        let row = sqlx::query(&self.config.password_query)
            .bind(username.to_string())
            .fetch_optional(&self.pool)
            .await?;

        let row = match row {
            Some(r) => r,
            None => {
                let _ = self
                    .log_audit(&username.to_string(), None, false, Some("User not found"))
                    .await;
                return Ok(false);
            }
        };

        let password_hash: String = row.try_get("password_hash")?;
        let enabled: i64 = row.try_get("enabled")?;

        if enabled == 0 {
            let _ = self
                .log_audit(&username.to_string(), None, false, Some("User disabled"))
                .await;
            return Ok(false);
        }

        let verified = self.verify_hash(password, &password_hash)?;

        if verified {
            let _ = self
                .log_audit(&username.to_string(), None, true, None)
                .await;
        } else {
            let _ = self
                .log_audit(&username.to_string(), None, false, Some("Invalid password"))
                .await;
        }

        Ok(verified)
    }

    async fn verify_identity(&self, username: &Username) -> anyhow::Result<bool> {
        let row = sqlx::query(&self.config.password_query)
            .bind(username.to_string())
            .fetch_optional(&self.pool)
            .await?;

        Ok(row.is_some())
    }

    async fn list_users(&self) -> anyhow::Result<Vec<Username>> {
        let rows = sqlx::query(&self.config.list_users_query)
            .fetch_all(&self.pool)
            .await?;

        let users = rows
            .into_iter()
            .filter_map(|row| {
                row.try_get::<String, _>("username")
                    .ok()
                    .and_then(|u| Username::new(u).ok())
            })
            .collect();

        Ok(users)
    }

    async fn create_user(&self, username: &Username, password: &str) -> anyhow::Result<()> {
        let password_hash = self.hash_password(password)?;

        sqlx::query(&self.config.create_user_query)
            .bind(username.to_string())
            .bind(password_hash)
            .bind("user") // default role
            .execute(&self.pool)
            .await?;

        Ok(())
    }

    async fn delete_user(&self, username: &Username) -> anyhow::Result<()> {
        sqlx::query(&self.config.delete_user_query)
            .bind(username.to_string())
            .execute(&self.pool)
            .await?;

        Ok(())
    }

    async fn change_password(&self, username: &Username, new_password: &str) -> anyhow::Result<()> {
        let password_hash = self.hash_password(new_password)?;

        sqlx::query(&self.config.update_password_query)
            .bind(password_hash)
            .bind(username.to_string())
            .execute(&self.pool)
            .await?;

        Ok(())
    }

    async fn get_scram_params(&self, username: &str) -> anyhow::Result<(Vec<u8>, u32)> {
        let query = self
            .config
            .scram_params_query
            .as_ref()
            .ok_or_else(|| anyhow::anyhow!("SCRAM parameters query not configured"))?;

        let row = sqlx::query(query)
            .bind(username)
            .fetch_one(&self.pool)
            .await?;

        let salt: Vec<u8> = row.try_get("scram_salt")?;
        let iterations: i64 = row.try_get("scram_iterations")?;

        Ok((salt, iterations as u32))
    }

    async fn get_scram_stored_key(&self, username: &str) -> anyhow::Result<Vec<u8>> {
        let query = self
            .config
            .scram_stored_key_query
            .as_ref()
            .ok_or_else(|| anyhow::anyhow!("SCRAM StoredKey query not configured"))?;

        let row = sqlx::query(query)
            .bind(username)
            .fetch_one(&self.pool)
            .await?;

        Ok(row.try_get("scram_stored_key")?)
    }

    async fn get_scram_server_key(&self, username: &str) -> anyhow::Result<Vec<u8>> {
        let query = self
            .config
            .scram_server_key_query
            .as_ref()
            .ok_or_else(|| anyhow::anyhow!("SCRAM ServerKey query not configured"))?;

        let row = sqlx::query(query)
            .bind(username)
            .fetch_one(&self.pool)
            .await?;

        Ok(row.try_get("scram_server_key")?)
    }

    async fn store_scram_credentials(
        &self,
        username: &Username,
        salt: Vec<u8>,
        iterations: u32,
        stored_key: Vec<u8>,
        server_key: Vec<u8>,
    ) -> anyhow::Result<()> {
        let query = self
            .config
            .store_scram_query
            .as_ref()
            .ok_or_else(|| anyhow::anyhow!("SCRAM storage query not configured"))?;

        sqlx::query(query)
            .bind(&salt)
            .bind(iterations as i64)
            .bind(&stored_key)
            .bind(&server_key)
            .bind(username.to_string())
            .execute(&self.pool)
            .await?;

        Ok(())
    }
}

impl Clone for SqlBackend {
    fn clone(&self) -> Self {
        Self {
            pool: self.pool.clone(),
            config: self.config.clone(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicU64, Ordering};

    static TEST_COUNTER: AtomicU64 = AtomicU64::new(0);

    /// Generate a unique, isolated SQLite database URL for each test invocation.
    ///
    /// Uses a named in-memory database (`mode=memory`) with `cache=shared` so
    /// that all connections within the same pool (same pool test) see the same
    /// data, while the unique name (pid + monotonic counter) guarantees that
    /// different test invocations never share state — even when the full test
    /// suite is executed with multiple parallel threads.
    fn unique_test_db_url() -> String {
        let counter = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
        let pid = std::process::id();
        // "file:" prefix causes SQLite to treat the path as a URI with a name;
        // mode=memory keeps it fully in-process, cache=shared makes connections
        // within the same named URI share the in-memory database.
        format!(
            "sqlite:file:rusmes_auth_test_{}_{}?mode=memory&cache=shared",
            pid, counter
        )
    }

    async fn create_test_backend() -> SqlBackend {
        let config = SqlConfig {
            database_url: unique_test_db_url(),
            ..Default::default()
        };
        let backend = SqlBackend::new(config)
            .await
            .expect("SqlBackend::new failed");
        backend.init_schema().await.expect("init_schema failed");
        backend
    }

    #[test]
    fn test_hash_type_bcrypt() {
        let hash = "$2b$12$KIXp8T/y7hOzQEu7qW3Ziu";
        assert_eq!(HashType::from_prefix(hash), HashType::Bcrypt);
    }

    #[test]
    fn test_hash_type_argon2() {
        let hash = "$argon2id$v=19$m=65536,t=3,p=4$";
        assert_eq!(HashType::from_prefix(hash), HashType::Argon2);
    }

    #[test]
    fn test_hash_type_scram() {
        let hash = "$scram-sha-256$iterations=4096";
        assert_eq!(HashType::from_prefix(hash), HashType::ScramSha256);
    }

    #[test]
    fn test_hash_type_default() {
        let hash = "unknown_format";
        assert_eq!(HashType::from_prefix(hash), HashType::Bcrypt);
    }

    #[test]
    fn test_sql_config_default() {
        let config = SqlConfig::default();
        assert!(config.database_url.starts_with("sqlite:"));
        assert_eq!(config.max_connections, 10);
        assert!(config.scram_params_query.is_some());
    }

    #[test]
    fn test_sql_config_custom() {
        let config = SqlConfig {
            database_url: "postgresql://localhost/rusmes".to_string(),
            password_query: "SELECT hash FROM auth WHERE user = $1".to_string(),
            max_connections: 20,
            ..Default::default()
        };
        assert_eq!(config.database_url, "postgresql://localhost/rusmes");
        assert_eq!(config.max_connections, 20);
    }

    #[tokio::test]
    async fn test_sql_backend_creation() {
        let _backend = create_test_backend().await;
    }

    #[tokio::test]
    async fn test_init_schema() {
        let _backend = create_test_backend().await;
    }

    #[tokio::test]
    async fn test_create_and_verify_user() {
        let backend = create_test_backend().await;

        let username = Username::new("testuser".to_string()).unwrap();
        let password = "testpass123";

        backend.create_user(&username, password).await.unwrap();

        let verified = backend.verify_identity(&username).await.unwrap();
        assert!(verified);
    }

    #[tokio::test]
    async fn test_authenticate_user() {
        let backend = create_test_backend().await;

        let username = Username::new("authuser".to_string()).unwrap();
        let password = "secure_password";

        backend.create_user(&username, password).await.unwrap();

        let authenticated = backend.authenticate(&username, password).await.unwrap();
        assert!(authenticated);

        let wrong_auth = backend
            .authenticate(&username, "wrong_password")
            .await
            .unwrap();
        assert!(!wrong_auth);
    }

    #[tokio::test]
    async fn test_list_users() {
        let backend = create_test_backend().await;

        backend
            .create_user(&Username::new("user1".to_string()).unwrap(), "pass1")
            .await
            .unwrap();
        backend
            .create_user(&Username::new("user2".to_string()).unwrap(), "pass2")
            .await
            .unwrap();

        let users = backend.list_users().await.unwrap();
        assert_eq!(users.len(), 2);
    }

    #[tokio::test]
    async fn test_delete_user() {
        let backend = create_test_backend().await;

        let username = Username::new("deleteuser".to_string()).unwrap();
        backend.create_user(&username, "password").await.unwrap();

        let exists_before = backend.verify_identity(&username).await.unwrap();
        assert!(exists_before);

        backend.delete_user(&username).await.unwrap();

        let exists_after = backend.verify_identity(&username).await.unwrap();
        assert!(!exists_after);
    }

    #[tokio::test]
    async fn test_change_password() {
        let backend = create_test_backend().await;

        let username = Username::new("changepassuser".to_string()).unwrap();
        let old_password = "oldpass";
        let new_password = "newpass";

        backend.create_user(&username, old_password).await.unwrap();

        let auth_old = backend.authenticate(&username, old_password).await.unwrap();
        assert!(auth_old);

        backend
            .change_password(&username, new_password)
            .await
            .unwrap();

        let auth_new = backend.authenticate(&username, new_password).await.unwrap();
        assert!(auth_new);

        let auth_old_after = backend.authenticate(&username, old_password).await.unwrap();
        assert!(!auth_old_after);
    }

    #[tokio::test]
    async fn test_nonexistent_user() {
        let backend = create_test_backend().await;

        let username = Username::new("nonexistent".to_string()).unwrap();
        let authenticated = backend
            .authenticate(&username, "anypassword")
            .await
            .unwrap();
        assert!(!authenticated);
    }

    #[test]
    fn test_bcrypt_hash_verification() {
        let password = "test_password";
        let hash = bcrypt::hash(password, bcrypt::DEFAULT_COST).unwrap();
        let verified = bcrypt::verify(password, &hash).unwrap();
        assert!(verified);
    }

    #[test]
    fn test_password_query_format() {
        let config = SqlConfig::default();
        assert!(config.password_query.contains("SELECT"));
        assert!(config.password_query.contains("password_hash"));
        assert!(config.password_query.contains("enabled"));
    }

    #[test]
    fn test_scram_queries_configured() {
        let config = SqlConfig::default();
        assert!(config.scram_params_query.is_some());
        assert!(config.scram_stored_key_query.is_some());
        assert!(config.scram_server_key_query.is_some());
        assert!(config.store_scram_query.is_some());
    }

    #[tokio::test]
    async fn test_multiple_users() {
        let backend = create_test_backend().await;

        for i in 0..5 {
            let username = Username::new(format!("user{}", i)).unwrap();
            backend
                .create_user(&username, &format!("pass{}", i))
                .await
                .unwrap();
        }

        let users = backend.list_users().await.unwrap();
        assert_eq!(users.len(), 5);
    }

    #[tokio::test]
    async fn test_duplicate_username() {
        let backend = create_test_backend().await;

        let username = Username::new("duplicate".to_string()).unwrap();
        backend.create_user(&username, "pass1").await.unwrap();
        let result = backend.create_user(&username, "pass2").await;
        assert!(result.is_err());
    }

    #[test]
    fn test_hash_type_variants() {
        assert_eq!(HashType::Bcrypt, HashType::Bcrypt);
        assert_ne!(HashType::Bcrypt, HashType::Argon2);
        assert_ne!(HashType::Argon2, HashType::ScramSha256);
    }

    #[tokio::test]
    async fn test_empty_user_list() {
        let backend = create_test_backend().await;

        let users = backend.list_users().await.unwrap();
        assert_eq!(users.len(), 0);
    }

    #[tokio::test]
    async fn test_user_metadata() {
        let backend = create_test_backend().await;

        let username = Username::new("metauser".to_string()).unwrap();
        backend.create_user(&username, "password").await.unwrap();

        let metadata = backend.get_user_metadata(&username).await.unwrap();
        assert!(metadata.is_some());

        let meta = metadata.unwrap();
        assert!(meta.enabled);
        assert_eq!(meta.quota_bytes, 1073741824);
        assert_eq!(meta.roles, Some("user".to_string()));
    }

    #[tokio::test]
    async fn test_user_metadata_roles_vec() {
        let metadata = UserMetadata {
            enabled: true,
            quota_bytes: 1000,
            roles: Some("user,admin,moderator".to_string()),
        };

        let roles = metadata.roles_vec();
        assert_eq!(roles.len(), 3);
        assert!(roles.contains(&"user".to_string()));
        assert!(roles.contains(&"admin".to_string()));
        assert!(roles.contains(&"moderator".to_string()));
    }

    #[tokio::test]
    async fn test_user_metadata_no_roles() {
        let metadata = UserMetadata {
            enabled: true,
            quota_bytes: 1000,
            roles: None,
        };

        let roles = metadata.roles_vec();
        assert_eq!(roles.len(), 0);
    }

    #[tokio::test]
    async fn test_audit_logging_success() {
        let backend = create_test_backend().await;

        let username = Username::new("audituser".to_string()).unwrap();
        backend.create_user(&username, "password").await.unwrap();

        backend.authenticate(&username, "password").await.unwrap();

        let logs = backend.get_audit_logs("audituser", 10).await.unwrap();
        assert!(!logs.is_empty());
    }

    #[tokio::test]
    async fn test_audit_logging_failure() {
        let backend = create_test_backend().await;

        backend
            .log_audit("nonexistent", None, false, Some("User not found"))
            .await
            .unwrap();

        let logs = backend.get_audit_logs("nonexistent", 10).await.unwrap();
        assert!(!logs.is_empty());
        assert!(!logs[0].success);
    }

    #[tokio::test]
    async fn test_audit_with_ip_address() {
        let backend = create_test_backend().await;

        let ip: IpAddr = "127.0.0.1".parse().unwrap();
        backend
            .log_audit("testuser", Some(ip), true, None)
            .await
            .unwrap();

        let logs = backend.get_audit_logs("testuser", 10).await.unwrap();
        assert!(!logs.is_empty());
        assert_eq!(logs[0].ip_address, Some("127.0.0.1".to_string()));
    }

    #[tokio::test]
    async fn test_audit_multiple_entries() {
        let backend = create_test_backend().await;

        for i in 0..5 {
            backend
                .log_audit(&format!("user{}", i), None, i % 2 == 0, None)
                .await
                .unwrap();
        }

        let logs = backend.get_audit_logs("user0", 10).await.unwrap();
        assert!(!logs.is_empty());
    }

    #[tokio::test]
    async fn test_audit_limit() {
        let backend = create_test_backend().await;

        for _ in 0..10 {
            backend
                .log_audit("limituser", None, true, None)
                .await
                .unwrap();
        }

        let logs = backend.get_audit_logs("limituser", 5).await.unwrap();
        assert_eq!(logs.len(), 5);
    }

    #[tokio::test]
    #[ignore = "stress: SQLite connection pool under concurrent bcrypt load; run manually with --ignored"]
    async fn test_connection_pool() {
        // Use a pool large enough that all 10 concurrent tasks get a connection
        // immediately, avoiding acquire-timeout failures under heavy CI load
        // (bcrypt operations are CPU-intensive and can take several seconds each
        // when many tests run in parallel).
        //
        // NOTE: This test is marked #[ignore] because:
        //   - SQLite in-memory mode serializes all writes (one writer at a time)
        //   - bcrypt hashing is intentionally CPU-bound (DEFAULT_COST ~100ms per hash)
        //   - Running 10 concurrent tasks that each do bcrypt + SQL write against a
        //     shared in-memory SQLite causes pool connection exhaustion in parallel
        //     test runs (e.g. `cargo nextest run --test-threads N` with N > 1).
        //   - Pool connection acquire timeouts manifest as flaky test failures
        //     unrelated to the correctness of the pool implementation.
        //   - The individual pool functionality is adequately covered by
        //     test_database_connection_reuse (sequential, no concurrent pressure).
        //
        // Run manually:
        //   cargo nextest run -p rusmes-auth --run-ignored all -E 'test(test_connection_pool)'
        let config = SqlConfig {
            database_url: unique_test_db_url(),
            max_connections: 20,
            ..Default::default()
        };
        let backend = SqlBackend::new(config)
            .await
            .expect("SqlBackend::new failed");
        backend.init_schema().await.expect("init_schema failed");

        // Spawn 10 concurrent create_user tasks to verify the pool handles
        // concurrent access without deadlocks or data races.
        let mut handles = vec![];
        for i in 0..10 {
            let username = Username::new(format!("pooluser{}", i)).expect("Username::new failed");
            let password = format!("pass{}", i);
            let b = backend.clone();
            let handle = tokio::spawn(async move { b.create_user(&username, &password).await });
            handles.push(handle);
        }

        for handle in handles {
            let result = handle.await.expect("task panicked");
            assert!(result.is_ok(), "create_user failed: {:?}", result.err());
        }
    }

    #[tokio::test]
    async fn test_argon2_hash_verification() {
        use argon2::password_hash::{rand_core::OsRng, PasswordHash, SaltString};
        use argon2::{Argon2, PasswordHasher, PasswordVerifier};

        let password = "test_password";
        let salt = SaltString::generate(&mut OsRng);
        let argon2 = Argon2::default();
        let hash = argon2
            .hash_password(password.as_bytes(), &salt)
            .unwrap()
            .to_string();

        let parsed = PasswordHash::new(&hash).unwrap();
        let verify_result = Argon2::default().verify_password(password.as_bytes(), &parsed);
        assert!(verify_result.is_ok());
    }

    #[tokio::test]
    async fn test_scram_hash_error() {
        let config = SqlConfig {
            database_url: unique_test_db_url(),
            ..Default::default()
        };
        let backend = SqlBackend::new(config)
            .await
            .expect("SqlBackend::new failed");

        let result = backend.verify_hash("password", "$scram-sha-256$test");
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_verify_nonexistent_user() {
        let backend = create_test_backend().await;

        let username = Username::new("ghost".to_string()).unwrap();
        let verified = backend.verify_identity(&username).await.unwrap();
        assert!(!verified);
    }

    #[tokio::test]
    async fn test_password_hash_different() {
        let config = SqlConfig {
            database_url: unique_test_db_url(),
            ..Default::default()
        };
        let backend = SqlBackend::new(config)
            .await
            .expect("SqlBackend::new failed");

        let hash1 = backend
            .hash_password("password")
            .expect("hash_password failed");
        let hash2 = backend
            .hash_password("password")
            .expect("hash_password failed");

        // bcrypt generates different hashes due to random salt
        assert_ne!(hash1, hash2);

        // Both should verify correctly
        assert!(backend.verify_hash("password", &hash1).unwrap());
        assert!(backend.verify_hash("password", &hash2).unwrap());
    }

    #[tokio::test]
    async fn test_special_characters_in_username() {
        let backend = create_test_backend().await;

        let username = Username::new("test.user+tag@example.com".to_string()).unwrap();
        backend.create_user(&username, "password").await.unwrap();

        let authenticated = backend.authenticate(&username, "password").await.unwrap();
        assert!(authenticated);
    }

    #[tokio::test]
    async fn test_long_password() {
        let backend = create_test_backend().await;

        let username = Username::new("longpassuser".to_string()).unwrap();
        let password = "a".repeat(100);

        backend.create_user(&username, &password).await.unwrap();

        let authenticated = backend.authenticate(&username, &password).await.unwrap();
        assert!(authenticated);
    }

    #[tokio::test]
    async fn test_empty_password_rejection() {
        let backend = create_test_backend().await;

        let username = Username::new("emptypass".to_string()).unwrap();
        backend.create_user(&username, "").await.unwrap();

        let authenticated = backend.authenticate(&username, "").await.unwrap();
        assert!(authenticated);

        let auth_wrong = backend.authenticate(&username, "notblank").await.unwrap();
        assert!(!auth_wrong);
    }

    #[tokio::test]
    async fn test_case_sensitive_password() {
        let backend = create_test_backend().await;

        let username = Username::new("caseuser".to_string()).unwrap();
        backend.create_user(&username, "Password123").await.unwrap();

        let auth_correct = backend
            .authenticate(&username, "Password123")
            .await
            .unwrap();
        assert!(auth_correct);

        let auth_wrong = backend
            .authenticate(&username, "password123")
            .await
            .unwrap();
        assert!(!auth_wrong);
    }

    #[tokio::test]
    #[ignore = "stress: SQLite pool timeout under concurrent bcrypt authentication; run manually with --ignored"]
    async fn test_concurrent_authentication() {
        // NOTE: Marked #[ignore] for the same reasons as test_connection_pool:
        //   - SQLite in-memory mode serializes all writes (one writer at a time).
        //   - bcrypt verification is CPU-bound (~100ms per check at DEFAULT_COST).
        //   - 10 concurrent tasks competing for up to 10 pool connections under
        //     parallel nextest runs causes acquire-timeout panics that are unrelated
        //     to the correctness of the authentication logic.
        //
        // Run manually:
        //   cargo nextest run -p rusmes-auth --run-ignored all -E 'test(test_concurrent_authentication)'
        let backend = create_test_backend().await;

        let username = Username::new("concurrent".to_string()).expect("Username::new failed");
        backend
            .create_user(&username, "password")
            .await
            .expect("create_user failed");

        let mut handles = vec![];
        for _ in 0..10 {
            let b = backend.clone();
            let u = username.clone();
            let handle = tokio::spawn(async move { b.authenticate(&u, "password").await });
            handles.push(handle);
        }

        for handle in handles {
            let result = handle.await.expect("task panicked");
            assert!(result.expect("authenticate failed"));
        }
    }

    #[tokio::test]
    async fn test_database_connection_reuse() {
        let backend = create_test_backend().await;

        for i in 0..20 {
            let username = Username::new(format!("reuse{}", i)).unwrap();
            backend.create_user(&username, "password").await.unwrap();
        }

        let users = backend.list_users().await.unwrap();
        assert_eq!(users.len(), 20);
    }

    #[test]
    fn test_hash_type_copy_trait() {
        let hash_type = HashType::Bcrypt;
        let copied = hash_type;
        assert_eq!(hash_type, copied);
    }

    #[test]
    fn test_user_metadata_clone() {
        let metadata = UserMetadata {
            enabled: true,
            quota_bytes: 1000,
            roles: Some("admin".to_string()),
        };
        let cloned = metadata.clone();
        assert_eq!(cloned.enabled, metadata.enabled);
        assert_eq!(cloned.quota_bytes, metadata.quota_bytes);
    }

    #[test]
    fn test_audit_log_debug() {
        let log = AuditLog {
            username: "test".to_string(),
            ip_address: Some("127.0.0.1".to_string()),
            success: true,
            failure_reason: None,
            timestamp: "2025-01-01 00:00:00".to_string(),
        };
        let debug_str = format!("{:?}", log);
        assert!(debug_str.contains("test"));
    }

    #[tokio::test]
    async fn test_scram_params_not_configured() {
        let config = SqlConfig {
            database_url: unique_test_db_url(),
            scram_params_query: None,
            ..Default::default()
        };
        let backend = SqlBackend::new(config)
            .await
            .expect("SqlBackend::new failed");
        backend.init_schema().await.expect("init_schema failed");

        let result = backend.get_scram_params("testuser").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_scram_stored_key_not_configured() {
        let config = SqlConfig {
            database_url: unique_test_db_url(),
            scram_stored_key_query: None,
            ..Default::default()
        };
        let backend = SqlBackend::new(config)
            .await
            .expect("SqlBackend::new failed");
        backend.init_schema().await.expect("init_schema failed");

        let result = backend.get_scram_stored_key("testuser").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_scram_server_key_not_configured() {
        let config = SqlConfig {
            database_url: unique_test_db_url(),
            scram_server_key_query: None,
            ..Default::default()
        };
        let backend = SqlBackend::new(config)
            .await
            .expect("SqlBackend::new failed");
        backend.init_schema().await.expect("init_schema failed");

        let result = backend.get_scram_server_key("testuser").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_store_scram_not_configured() {
        let config = SqlConfig {
            database_url: unique_test_db_url(),
            store_scram_query: None,
            ..Default::default()
        };
        let backend = SqlBackend::new(config)
            .await
            .expect("SqlBackend::new failed");
        backend.init_schema().await.expect("init_schema failed");

        let username = Username::new("scram".to_string()).expect("Username::new failed");
        let result = backend
            .store_scram_credentials(&username, vec![1, 2, 3], 4096, vec![4, 5, 6], vec![7, 8, 9])
            .await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_audit_disabled() {
        let config = SqlConfig {
            database_url: unique_test_db_url(),
            audit_table: None,
            ..Default::default()
        };
        let backend = SqlBackend::new(config)
            .await
            .expect("SqlBackend::new failed");
        backend.init_schema().await.expect("init_schema failed");

        // Should succeed without error
        backend.log_audit("user", None, true, None).await.unwrap();

        let logs = backend.get_audit_logs("user", 10).await.unwrap();
        assert_eq!(logs.len(), 0);
    }

    #[tokio::test]
    async fn test_user_metadata_nonexistent() {
        let backend = create_test_backend().await;

        let username = Username::new("phantom".to_string()).unwrap();
        let metadata = backend.get_user_metadata(&username).await.unwrap();
        assert!(metadata.is_none());
    }

    #[tokio::test]
    async fn test_custom_database_url() {
        // Verify that a custom (non-default) database URL is accepted.
        // We use a unique temp-file URL to avoid collision with other parallel tests.
        let config = SqlConfig {
            database_url: unique_test_db_url(),
            ..Default::default()
        };
        let backend = SqlBackend::new(config).await;
        assert!(backend.is_ok());
    }
}