cedros-login-server 0.0.45

Authentication server for cedros-login with email/password, Google OAuth, and Solana wallet sign-in
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
//! Password hashing and validation service
//!
//! # Argon2 Parameters (MAINT-003)
//!
//! This service uses **Argon2id** with the following default parameters (via `Argon2::default()`):
//!
//! | Parameter | Value | Description |
//! |-----------|-------|-------------|
//! | m_cost | 19456 KiB (~19 MiB) | Memory cost |
//! | t_cost | 2 | Time cost (iterations) |
//! | p_cost | 1 | Parallelism |
//!
//! These parameters are the `argon2` crate defaults and meet OWASP recommendations for
//! password hashing. The ~50-100ms hash time on typical hardware provides good security
//! while remaining responsive for interactive authentication.
//!
//! To adjust parameters, replace `Argon2::default()` with `Argon2::new()` and custom `Params`.
//!
//! # Common Password Checking (SEC-28)
//!
//! The service checks passwords against a list of commonly-used passwords to prevent
//! users from choosing easily-guessable passwords. This list includes:
//! - Top 100 most common passwords from various breach datasets
//! - Common keyboard patterns (qwerty, 12345, etc.)
//! - Common words/phrases (password, letmein, etc.)
//!
//! Enable via `PASSWORD_CHECK_COMMON=true` (default: true).

use std::collections::HashSet;
use std::sync::OnceLock;

use argon2::{
    password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
    Argon2,
};
use tokio::task;

use crate::errors::AppError;

/// Common passwords that are rejected during registration/password change.
/// This list is compiled from various breach datasets and security research.
/// All entries are lowercase for case-insensitive matching.
const COMMON_PASSWORDS: &[&str] = &[
    // Top passwords from breach datasets
    "password",
    "123456",
    "12345678",
    "1234567890",
    "qwerty",
    "abc123",
    "monkey",
    "1234567",
    "letmein",
    "trustno1",
    "dragon",
    "baseball",
    "iloveyou",
    "master",
    "sunshine",
    "ashley",
    "bailey",
    "passw0rd",
    "shadow",
    "123123",
    "654321",
    "superman",
    "qazwsx",
    "michael",
    "football",
    "password1",
    "password123",
    "batman",
    "login",
    "welcome",
    "admin",
    "princess",
    "starwars",
    "admin123",
    "hello",
    "charlie",
    "donald",
    "login",
    "loveme",
    "mustang",
    "access",
    "ninja",
    "hunter",
    "zaq1zaq1",
    "qwerty123",
    "letmein1",
    "welcome1",
    "password!",
    "passw0rd!",
    "qwerty!",
    "123456!",
    "hello123",
    "welcome123",
    "secret",
    "secret123",
    "changeme",
    "passpass",
    "freedom",
    "whatever",
    "qwertyuiop",
    "123456789",
    "12345678!",
    // Common patterns
    "aaaaaa",
    "111111",
    "000000",
    "abcdef",
    "abcdefg",
    "abcdefgh",
    "abc12345",
    "123abc",
    "1q2w3e4r",
    "1qaz2wsx",
    "zxcvbnm",
    "asdfghjkl",
    "1234qwer",
    "qwer1234",
    // Year-based
    "2020",
    "2021",
    "2022",
    "2023",
    "2024",
    "2025",
    // Common word + number patterns (base words)
    "summer",
    "winter",
    "spring",
    "autumn",
    "january",
    "february",
    "monday",
    "friday",
    "soccer",
    "hockey",
    "cookie",
    "chocolate",
    "computer",
    "internet",
    "flower",
    "orange",
    "pepper",
    "cheese",
    "winner",
    "loser",
];

/// O(1) lookup set built from the common password list.
static COMMON_PASSWORD_SET: OnceLock<HashSet<&'static str>> = OnceLock::new();

fn common_password_set() -> &'static HashSet<&'static str> {
    COMMON_PASSWORD_SET.get_or_init(|| COMMON_PASSWORDS.iter().copied().collect())
}

/// Check if a password is in the common password list.
/// Performs case-insensitive comparison and also checks if the password
/// (without trailing digits/special chars) matches a common base word.
fn is_common_password(password: &str) -> bool {
    let lower = password.to_lowercase();

    // Direct match — O(1) via HashSet
    if common_password_set().contains(lower.as_str()) {
        return true;
    }

    // Check base word (strip trailing digits and special chars)
    // Catches patterns like "Password123!" matching "password"
    let base: String = lower
        .chars()
        .take_while(|c| c.is_ascii_alphabetic())
        .collect();

    if !base.is_empty() && common_password_set().contains(base.as_str()) {
        return true;
    }

    false
}

/// Password validation rules
#[derive(Clone)]
pub struct PasswordRules {
    pub min_length: usize,
    pub require_uppercase: bool,
    pub require_lowercase: bool,
    pub require_number: bool,
    pub require_special: bool,
    /// Check against list of commonly-breached passwords
    pub check_common_passwords: bool,
}

impl Default for PasswordRules {
    fn default() -> Self {
        Self {
            min_length: 10,
            require_uppercase: true,
            require_lowercase: true,
            require_number: true,
            require_special: true,
            check_common_passwords: true,
        }
    }
}

/// SRV-12: Generate dummy hash at runtime with current Argon2 params.
/// This ensures timing consistency even if params change in the future.
fn dummy_hash() -> &'static str {
    use std::sync::OnceLock;
    static HASH: OnceLock<String> = OnceLock::new();
    HASH.get_or_init(|| {
        let salt = SaltString::generate(&mut OsRng);
        Argon2::default()
            .hash_password(b"timing-attack-mitigation-dummy", &salt)
            .expect("dummy hash generation must succeed")
            .to_string()
    })
}

/// Password service for hashing and validation
#[derive(Clone)]
pub struct PasswordService {
    rules: PasswordRules,
}

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

impl PasswordService {
    /// Create a new password service with custom rules
    pub fn new(rules: PasswordRules) -> Self {
        Self { rules }
    }

    /// Validate a password against the configured rules
    pub fn validate(&self, password: &str) -> Result<(), AppError> {
        let mut errors = Vec::new();

        if password.len() < self.rules.min_length {
            errors.push(format!(
                "Password must be at least {} characters",
                self.rules.min_length
            ));
        }

        if self.rules.require_uppercase && !password.chars().any(|c| c.is_uppercase()) {
            errors.push("Password must contain at least one uppercase letter".to_string());
        }

        if self.rules.require_lowercase && !password.chars().any(|c| c.is_lowercase()) {
            errors.push("Password must contain at least one lowercase letter".to_string());
        }

        if self.rules.require_number && !password.chars().any(|c| c.is_ascii_digit()) {
            errors.push("Password must contain at least one number".to_string());
        }

        if self.rules.require_special {
            // L-04: Accepted special characters. Includes common symbols users expect.
            // Original set: @$!%*?&#^()
            // Added: - (hyphen), . (period), _ (underscore) - commonly used in passwords
            // Note: Expanding the allowlist is always safe (existing passwords with the
            // original chars still work, and new passwords can now use more chars).
            let special_chars = "@$!%*?&#^()-._";
            if !password.chars().any(|c| special_chars.contains(c)) {
                errors.push("Password must contain at least one special character".to_string());
            }
        }

        // SEC-28: Check against common password list
        if self.rules.check_common_passwords && is_common_password(password) {
            errors.push(
                "This password is too common and easily guessable. Please choose a stronger password."
                    .to_string(),
            );
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(AppError::Validation(errors.join("; ")))
        }
    }

    /// Hash a password using argon2id
    ///
    /// P-01: Runs in spawn_blocking to avoid blocking the async runtime.
    /// Argon2 hashing takes 50-100ms and would otherwise saturate the thread pool.
    pub async fn hash(&self, password: String) -> Result<String, AppError> {
        task::spawn_blocking(move || {
            let salt = SaltString::generate(&mut OsRng);
            let argon2 = Argon2::default();

            argon2
                .hash_password(password.as_bytes(), &salt)
                .map(|hash| hash.to_string())
                .map_err(|e| AppError::Internal(anyhow::anyhow!("Password hashing failed: {}", e)))
        })
        .await
        .map_err(|e| AppError::Internal(anyhow::anyhow!("Password hash task failed: {}", e)))?
    }

    /// Verify a password against a hash (timing-safe)
    ///
    /// P-01: Runs in spawn_blocking to avoid blocking the async runtime.
    pub async fn verify(&self, password: String, hash: String) -> Result<bool, AppError> {
        task::spawn_blocking(move || {
            let parsed_hash = PasswordHash::new(&hash)
                .map_err(|e| AppError::Internal(anyhow::anyhow!("Invalid password hash: {}", e)))?;

            // Argon2 verify is timing-safe
            Ok(Argon2::default()
                .verify_password(password.as_bytes(), &parsed_hash)
                .is_ok())
        })
        .await
        .map_err(|e| AppError::Internal(anyhow::anyhow!("Password verify task failed: {}", e)))?
    }

    /// Perform a dummy password verification to normalize timing.
    ///
    /// This method is called when an email is not found in the database.
    /// By running the same argon2 verification as a real login, we prevent
    /// timing attacks that could enumerate valid email addresses.
    ///
    /// The result is always false, but the timing matches real verification.
    ///
    /// P-01: Runs in spawn_blocking to avoid blocking the async runtime.
    pub async fn verify_dummy(&self, password: String) {
        let _ = task::spawn_blocking(move || {
            // Parse the pre-computed dummy hash
            let hash_str = dummy_hash();
            match PasswordHash::new(hash_str) {
                Ok(parsed_hash) => {
                    // Run verification (will always fail, but takes same time)
                    let _ = Argon2::default().verify_password(password.as_bytes(), &parsed_hash);
                }
                Err(e) => {
                    tracing::error!(error = %e, "SECURITY: Failed to parse dummy hash - falling back to sleep");
                    metrics::counter!("security.password.dummy_hash_fallback").increment(1);
                    use rand::Rng;
                    let sleep_ms: u64 = OsRng.gen_range(100..300);
                    std::thread::sleep(std::time::Duration::from_millis(sleep_ms));
                }
            }
        })
        .await;
    }
}

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

    #[test]
    fn test_password_validation_valid() {
        let service = PasswordService::default();
        assert!(service.validate("SecurePass1!").is_ok());
        assert!(service.validate("MyP@ssw0rd123").is_ok());
    }

    #[test]
    fn test_password_validation_too_short() {
        let service = PasswordService::default();
        let result = service.validate("Short1!");
        assert!(result.is_err());
    }

    #[test]
    fn test_password_validation_missing_uppercase() {
        let service = PasswordService::default();
        let result = service.validate("securepass1!");
        assert!(result.is_err());
    }

    #[test]
    fn test_password_validation_missing_special() {
        let service = PasswordService::default();
        let result = service.validate("SecurePass123");
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_password_hash_and_verify() {
        let service = PasswordService::default();
        let password = "SecurePass1!";

        let hash = service.hash(password.to_string()).await.unwrap();
        assert!(service
            .verify(password.to_string(), hash.clone())
            .await
            .unwrap());
        assert!(!service
            .verify("WrongPassword1!".to_string(), hash)
            .await
            .unwrap());
    }

    #[test]
    fn test_password_validation_missing_lowercase() {
        let service = PasswordService::default();
        let result = service.validate("SECUREPASS1!");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("lowercase"));
    }

    #[test]
    fn test_password_validation_missing_digit() {
        let service = PasswordService::default();
        let result = service.validate("SecurePass!!");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("number"));
    }

    #[test]
    fn test_password_validation_all_special_chars() {
        let service = PasswordService::default();
        // Test various special characters work
        assert!(service.validate("SecurePass1@").is_ok());
        assert!(service.validate("SecurePass1$").is_ok());
        assert!(service.validate("SecurePass1!").is_ok());
        assert!(service.validate("SecurePass1%").is_ok());
        assert!(service.validate("SecurePass1*").is_ok());
        assert!(service.validate("SecurePass1?").is_ok());
        assert!(service.validate("SecurePass1&").is_ok());
        assert!(service.validate("SecurePass1#").is_ok());
        assert!(service.validate("SecurePass1^").is_ok());
        assert!(service.validate("SecurePass1(").is_ok());
        assert!(service.validate("SecurePass1)").is_ok());
    }

    #[tokio::test]
    async fn test_password_hash_is_unique() {
        let service = PasswordService::default();
        let password = "SecurePass1!";

        let hash1 = service.hash(password.to_string()).await.unwrap();
        let hash2 = service.hash(password.to_string()).await.unwrap();

        // Same password should produce different hashes (due to random salt)
        assert_ne!(hash1, hash2);

        // But both should verify correctly
        assert!(service.verify(password.to_string(), hash1).await.unwrap());
        assert!(service.verify(password.to_string(), hash2).await.unwrap());
    }

    #[test]
    fn test_password_exact_minimum_length() {
        let service = PasswordService::default();
        // Exactly 10 characters (minimum)
        assert!(service.validate("Secure1!ab").is_ok());
        // 9 characters (too short)
        assert!(service.validate("Secure1!a").is_err());
    }

    #[tokio::test]
    async fn test_verify_dummy_does_not_panic() {
        let service = PasswordService::default();
        // verify_dummy should not panic with any input
        service.verify_dummy("any-password".to_string()).await;
        service.verify_dummy(String::new()).await;
        service.verify_dummy("a".repeat(1000)).await;
    }

    #[test]
    fn test_common_password_rejection() {
        let service = PasswordService::default();
        // Direct common password matches should be rejected
        let result = service.validate("Password123!");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("too common"));

        let result = service.validate("Qwerty1234!");
        assert!(result.is_err());

        let result = service.validate("Letmein123!");
        assert!(result.is_err());
    }

    #[test]
    fn test_common_password_case_insensitive() {
        let service = PasswordService::default();
        // Case variations should also be rejected
        let result = service.validate("PASSWORD123!");
        assert!(result.is_err());

        let result = service.validate("PaSsWoRd123!");
        assert!(result.is_err());
    }

    #[test]
    fn test_common_password_with_suffix() {
        let service = PasswordService::default();
        // Common base word with numbers/special chars should be rejected
        let result = service.validate("Summer2024!");
        assert!(result.is_err());

        let result = service.validate("Dragon123!!");
        assert!(result.is_err());
    }

    #[test]
    fn test_uncommon_password_accepted() {
        let service = PasswordService::default();
        // Truly random passwords should be accepted
        assert!(service.validate("Xk9#mP2$vLqW").is_ok());
        assert!(service.validate("MyUnique!Pass1").is_ok());
        assert!(service.validate("Th1s1sN0tC0mm0n!").is_ok());
    }

    #[test]
    fn test_common_password_check_disabled() {
        let rules = PasswordRules {
            check_common_passwords: false,
            ..PasswordRules::default()
        };
        let service = PasswordService::new(rules);
        // With check disabled, common passwords meeting other rules should pass
        assert!(service.validate("Password123!").is_ok());
    }

    #[test]
    fn test_is_common_password_helper() {
        assert!(is_common_password("password"));
        assert!(is_common_password("PASSWORD"));
        assert!(is_common_password("Password123"));
        assert!(is_common_password("qwerty"));
        assert!(is_common_password("summer2024"));
        assert!(!is_common_password("xK9mP2vLqW"));
        assert!(!is_common_password("notinlist"));
    }
}