elif-auth 0.4.1

Authentication and authorization system for elif.rs framework - JWT, sessions, RBAC, password hashing, and middleware
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! Multi-factor authentication provider
//!
//! Provides TOTP (Time-based One-Time Password) functionality and backup codes
//! for enhanced security in the authentication flow.

use crate::{AuthError, AuthResult};
use chrono::{DateTime, Utc};
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

#[cfg(feature = "mfa")]
use base32;
#[cfg(feature = "mfa")]
use qrcode::{render::unicode, QrCode};
#[cfg(feature = "mfa")]
use totp_lite::{totp, Sha1};
#[cfg(feature = "mfa")]
use urlencoding;

/// MFA configuration
#[derive(Debug, Clone)]
pub struct MfaConfig {
    /// TOTP time step in seconds (usually 30)
    pub time_step: u64,
    /// Number of time windows to check (for time tolerance)
    pub window_tolerance: u8,
    /// Secret key length in bytes (recommended: 20 for SHA-1)
    pub secret_length: usize,
    /// Issuer name for TOTP URIs
    pub issuer: String,
    /// Number of backup codes to generate
    pub backup_codes_count: usize,
}

impl Default for MfaConfig {
    fn default() -> Self {
        Self {
            time_step: 30,
            window_tolerance: 1,
            secret_length: 20,
            issuer: "elif.rs".to_string(),
            backup_codes_count: 10,
        }
    }
}

/// MFA secret containing TOTP secret and metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MfaSecret {
    /// User ID this secret belongs to
    pub user_id: Uuid,
    /// Base32-encoded TOTP secret
    pub secret: String,
    /// Backup codes (hashed)
    pub backup_codes: Vec<String>,
    /// Used backup codes (to prevent reuse)
    pub used_backup_codes: Vec<String>,
    /// MFA setup completion timestamp
    pub setup_completed_at: Option<DateTime<Utc>>,
    /// Last successful verification timestamp
    pub last_verified_at: Option<DateTime<Utc>>,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
}

/// MFA setup information for user enrollment
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MfaSetup {
    /// QR code as text/ASCII art for terminal display
    pub qr_code: String,
    /// Manual entry key (base32 secret)
    pub manual_key: String,
    /// TOTP URI for QR code
    pub totp_uri: String,
    /// Generated backup codes (plaintext - show only once)
    pub backup_codes: Vec<String>,
}

/// MFA verification result
#[derive(Debug, Clone, PartialEq)]
pub enum MfaVerificationResult {
    /// Verification successful with TOTP
    TotpSuccess,
    /// Verification successful with backup code
    BackupCodeSuccess,
    /// Verification failed
    Failed,
    /// MFA not set up for this user
    NotSetup,
}

/// Multi-factor authentication provider
pub struct MfaProvider {
    config: MfaConfig,
    #[allow(dead_code)]
    secrets: HashMap<Uuid, MfaSecret>,
}

impl MfaProvider {
    /// Create a new MFA provider with default configuration
    pub fn new() -> AuthResult<Self> {
        Self::with_config(MfaConfig::default())
    }

    /// Create a new MFA provider with custom configuration
    pub fn with_config(config: MfaConfig) -> AuthResult<Self> {
        Ok(Self {
            config,
            secrets: HashMap::new(),
        })
    }

    /// Generate a new MFA setup for a user
    pub fn generate_setup(&self, _user_id: Uuid, username: &str) -> AuthResult<MfaSetup> {
        #[cfg(not(feature = "mfa"))]
        {
            let _ = (_user_id, username);
            return Err(AuthError::generic_error(
                "MFA feature not enabled - compile with 'mfa' feature",
            ));
        }

        #[cfg(feature = "mfa")]
        {
            // Generate random secret
            let secret_bytes = self.generate_secret();
            let secret_base32 =
                base32::encode(base32::Alphabet::Rfc4648 { padding: true }, &secret_bytes);

            // Generate backup codes
            let backup_codes = self.generate_backup_codes();

            // Create TOTP URI
            let totp_uri = format!(
                "otpauth://totp/{}:{}?secret={}&issuer={}&digits=6&period={}",
                urlencoding::encode(&self.config.issuer),
                urlencoding::encode(username),
                secret_base32,
                urlencoding::encode(&self.config.issuer),
                self.config.time_step
            );

            // Generate QR code
            let qr_code = self.generate_qr_code(&totp_uri)?;

            Ok(MfaSetup {
                qr_code,
                manual_key: secret_base32,
                totp_uri,
                backup_codes,
            })
        }
    }

    /// Complete MFA setup for a user by verifying the first TOTP code
    pub fn complete_setup(
        &mut self,
        user_id: Uuid,
        setup: &MfaSetup,
        totp_code: &str,
    ) -> AuthResult<()> {
        #[cfg(not(feature = "mfa"))]
        {
            let _ = (user_id, setup, totp_code);
            return Err(AuthError::generic_error(
                "MFA feature not enabled - compile with 'mfa' feature",
            ));
        }

        #[cfg(feature = "mfa")]
        {
            // Verify the TOTP code
            if !self.verify_totp_code(&setup.manual_key, totp_code)? {
                return Err(AuthError::invalid_credentials("Invalid TOTP code"));
            }

            // Hash backup codes for storage
            let hashed_backup_codes = setup
                .backup_codes
                .iter()
                .map(|code| self.hash_backup_code(code))
                .collect::<Result<Vec<_>, _>>()?;

            // Store the secret
            let secret = MfaSecret {
                user_id,
                secret: setup.manual_key.clone(),
                backup_codes: hashed_backup_codes,
                used_backup_codes: Vec::new(),
                setup_completed_at: Some(Utc::now()),
                last_verified_at: Some(Utc::now()),
                created_at: Utc::now(),
            };

            self.secrets.insert(user_id, secret);
            Ok(())
        }
    }

    /// Verify MFA for a user
    pub fn verify_mfa(&mut self, user_id: Uuid, code: &str) -> AuthResult<MfaVerificationResult> {
        #[cfg(not(feature = "mfa"))]
        {
            let _ = (user_id, code);
            return Ok(MfaVerificationResult::NotSetup);
        }

        #[cfg(feature = "mfa")]
        {
            if !self.secrets.contains_key(&user_id) {
                return Ok(MfaVerificationResult::NotSetup);
            }

            // Try TOTP verification first - use a separate scope to avoid borrowing conflicts
            let totp_secret = {
                let secret = self.secrets.get(&user_id).unwrap();
                secret.secret.clone()
            };

            if self.verify_totp_code(&totp_secret, code)? {
                // Update last verified time after verification succeeds
                if let Some(secret) = self.secrets.get_mut(&user_id) {
                    secret.last_verified_at = Some(Utc::now());
                }
                return Ok(MfaVerificationResult::TotpSuccess);
            }

            // Try backup code verification - need to separate the operations to avoid borrowing conflicts
            let backup_code_valid = if let Some(secret) = self.secrets.get_mut(&user_id) {
                // Clone the necessary data to avoid borrowing conflicts
                let backup_codes = secret.backup_codes.clone();
                let used_codes = secret.used_backup_codes.clone();

                let code_hash = self.hash_backup_code(code)?;

                // Check if this backup code exists and hasn't been used
                backup_codes.contains(&code_hash) && !used_codes.contains(&code_hash)
            } else {
                false
            };

            if backup_code_valid {
                // Compute hash before mutable borrow
                let code_hash = self.hash_backup_code(code)?;

                // Update the secret after verification
                if let Some(secret) = self.secrets.get_mut(&user_id) {
                    secret.used_backup_codes.push(code_hash);
                    secret.last_verified_at = Some(Utc::now());
                }
                return Ok(MfaVerificationResult::BackupCodeSuccess);
            }

            Ok(MfaVerificationResult::Failed)
        }
    }

    /// Check if MFA is enabled for a user
    pub fn is_mfa_enabled(&self, user_id: Uuid) -> bool {
        self.secrets
            .get(&user_id)
            .map(|secret| secret.setup_completed_at.is_some())
            .unwrap_or(false)
    }

    /// Disable MFA for a user
    pub fn disable_mfa(&mut self, user_id: Uuid) -> AuthResult<bool> {
        Ok(self.secrets.remove(&user_id).is_some())
    }

    /// Get remaining backup codes count for a user
    pub fn get_remaining_backup_codes_count(&self, user_id: Uuid) -> AuthResult<usize> {
        match self.secrets.get(&user_id) {
            Some(secret) => {
                // Use saturating_sub to avoid potential underflow if used codes exceed total
                let remaining = secret
                    .backup_codes
                    .len()
                    .saturating_sub(secret.used_backup_codes.len());
                Ok(remaining)
            }
            None => Ok(0),
        }
    }

    /// Generate new backup codes (invalidates old ones)
    pub fn regenerate_backup_codes(&mut self, user_id: Uuid) -> AuthResult<Vec<String>> {
        #[cfg(not(feature = "mfa"))]
        {
            let _ = user_id;
            return Err(AuthError::generic_error(
                "MFA feature not enabled - compile with 'mfa' feature",
            ));
        }

        #[cfg(feature = "mfa")]
        {
            if !self.secrets.contains_key(&user_id) {
                return Err(AuthError::not_found("MFA not setup for user"));
            }

            // Generate new backup codes
            let new_backup_codes = self.generate_backup_codes();
            let hashed_codes = new_backup_codes
                .iter()
                .map(|code| self.hash_backup_code(code))
                .collect::<Result<Vec<_>, _>>()?;

            // Replace old backup codes
            if let Some(secret) = self.secrets.get_mut(&user_id) {
                secret.backup_codes = hashed_codes;
                secret.used_backup_codes.clear();
            }

            Ok(new_backup_codes)
        }
    }

    // Private helper methods

    #[cfg(feature = "mfa")]
    fn generate_secret(&self) -> Vec<u8> {
        let mut secret = vec![0u8; self.config.secret_length];
        thread_rng().fill(&mut secret[..]);
        secret
    }

    #[cfg(feature = "mfa")]
    fn generate_backup_codes(&self) -> Vec<String> {
        let mut codes = Vec::with_capacity(self.config.backup_codes_count);
        let mut rng = thread_rng();

        for _ in 0..self.config.backup_codes_count {
            // Generate 8-digit backup code
            let code = format!("{:08}", rng.gen_range(10000000..99999999));
            codes.push(code);
        }

        codes
    }

    #[cfg(feature = "mfa")]
    fn generate_qr_code(&self, totp_uri: &str) -> AuthResult<String> {
        let qr_code = QrCode::new(totp_uri)
            .map_err(|e| AuthError::generic_error(format!("Failed to generate QR code: {}", e)))?;

        let qr_string = qr_code
            .render::<unicode::Dense1x2>()
            .dark_color(unicode::Dense1x2::Light)
            .light_color(unicode::Dense1x2::Dark)
            .build();

        Ok(qr_string)
    }

    #[cfg(feature = "mfa")]
    fn verify_totp_code(&self, secret_base32: &str, code: &str) -> AuthResult<bool> {
        // Decode the base32 secret
        let secret = base32::decode(base32::Alphabet::Rfc4648 { padding: true }, secret_base32)
            .ok_or_else(|| AuthError::generic_error("Invalid secret format"))?;

        // Validate code format (should be 6 digits)
        if code.len() != 6 || !code.chars().all(|c| c.is_ascii_digit()) {
            return Err(AuthError::invalid_credentials("Invalid TOTP code format"));
        }

        let current_time = Utc::now().timestamp() as u64;

        // Check current time window and tolerance windows
        for i in 0..=(self.config.window_tolerance * 2) {
            let time_offset = (i as i64) - (self.config.window_tolerance as i64);
            let time_window =
                ((current_time as i64) + (time_offset * self.config.time_step as i64)) as u64;

            // Generate TOTP code for this time window
            let expected_code = totp::<Sha1>(&secret, time_window);

            // The standard totp function returns 8 digits, but we need 6
            // So we need to take the last 6 digits to match standard authenticator apps
            let expected_code_6digits = if expected_code.len() >= 6 {
                &expected_code[expected_code.len() - 6..]
            } else {
                &expected_code
            };

            if expected_code_6digits == code {
                return Ok(true);
            }
        }

        Ok(false)
    }

    #[cfg(feature = "mfa")]
    pub fn verify_backup_code(&mut self, secret: &mut MfaSecret, code: &str) -> AuthResult<bool> {
        let code_hash = self.hash_backup_code(code)?;

        // Check if this backup code exists and hasn't been used
        if secret.backup_codes.contains(&code_hash)
            && !secret.used_backup_codes.contains(&code_hash)
        {
            secret.used_backup_codes.push(code_hash);
            return Ok(true);
        }

        Ok(false)
    }

    #[cfg(feature = "mfa")]
    pub fn hash_backup_code(&self, code: &str) -> AuthResult<String> {
        // Simple hash for backup codes - in production, use proper hashing
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        code.hash(&mut hasher);
        Ok(format!("{:x}", hasher.finish()))
    }
}

impl Default for MfaProvider {
    fn default() -> Self {
        Self::new().expect("Failed to create default MFA provider")
    }
}

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

    #[tokio::test]
    async fn test_mfa_provider_creation() {
        let provider = MfaProvider::new();
        assert!(provider.is_ok());
    }

    #[tokio::test]
    async fn test_mfa_config_defaults() {
        let config = MfaConfig::default();
        assert_eq!(config.time_step, 30);
        assert_eq!(config.window_tolerance, 1);
        assert_eq!(config.secret_length, 20);
        assert_eq!(config.issuer, "elif.rs");
        assert_eq!(config.backup_codes_count, 10);
    }

    #[tokio::test]
    async fn test_mfa_provider_with_custom_config() {
        let config = MfaConfig {
            time_step: 60,
            window_tolerance: 2,
            secret_length: 32,
            issuer: "test-app".to_string(),
            backup_codes_count: 12,
        };

        let provider = MfaProvider::with_config(config.clone());
        assert!(provider.is_ok());

        let provider = provider.unwrap();
        assert_eq!(provider.config.time_step, 60);
        assert_eq!(provider.config.issuer, "test-app");
    }

    #[cfg(feature = "mfa")]
    #[tokio::test]
    async fn test_mfa_setup_generation() {
        let provider = MfaProvider::new().unwrap();
        let user_id = Uuid::new_v4();
        let username = "testuser";

        let setup = provider.generate_setup(user_id, username);
        assert!(setup.is_ok());

        let setup = setup.unwrap();
        assert!(!setup.qr_code.is_empty());
        assert!(!setup.manual_key.is_empty());
        assert!(setup.totp_uri.contains("otpauth://totp/"));
        assert!(setup.totp_uri.contains(username));
        assert_eq!(setup.backup_codes.len(), 10);
    }

    #[tokio::test]
    async fn test_mfa_not_enabled_by_default() {
        let mut provider = MfaProvider::new().unwrap();
        let user_id = Uuid::new_v4();

        assert!(!provider.is_mfa_enabled(user_id));

        let result = provider.verify_mfa(user_id, "123456");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), MfaVerificationResult::NotSetup);
    }

    #[tokio::test]
    async fn test_mfa_disable() {
        let mut provider = MfaProvider::new().unwrap();
        let user_id = Uuid::new_v4();

        // Initially no MFA setup
        let disabled = provider.disable_mfa(user_id).unwrap();
        assert!(!disabled);

        // TODO: Test with actual MFA setup once setup is working
    }

    #[tokio::test]
    async fn test_backup_codes_count() {
        let provider = MfaProvider::new().unwrap();
        let user_id = Uuid::new_v4();

        let count = provider.get_remaining_backup_codes_count(user_id);
        assert!(count.is_ok());
        assert_eq!(count.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_backup_codes_count_underflow() {
        let mut provider = MfaProvider::new().unwrap();
        let user_id = Uuid::new_v4();

        // Simulate corrupted state where used codes exceed total backup codes
        provider.secrets.insert(
            user_id,
            MfaSecret {
                user_id,
                secret: "dummy".to_string(),
                backup_codes: Vec::new(),
                used_backup_codes: vec!["used".to_string()],
                setup_completed_at: None,
                last_verified_at: None,
                created_at: Utc::now(),
            },
        );

        let count = provider.get_remaining_backup_codes_count(user_id);
        assert!(count.is_ok());
        assert_eq!(count.unwrap(), 0);
    }

    #[cfg(not(feature = "mfa"))]
    #[tokio::test]
    async fn test_mfa_disabled_error_messages() {
        let provider = MfaProvider::new().unwrap();
        let user_id = Uuid::new_v4();

        let setup_result = provider.generate_setup(user_id, "testuser");
        assert!(setup_result.is_err());
        assert!(setup_result
            .unwrap_err()
            .to_string()
            .contains("MFA feature not enabled"));
    }

    #[cfg(feature = "mfa")]
    #[tokio::test]
    async fn test_backup_code_generation() {
        let provider = MfaProvider::new().unwrap();
        let codes = provider.generate_backup_codes();

        assert_eq!(codes.len(), 10);
        for code in &codes {
            assert_eq!(code.len(), 8);
            assert!(code.chars().all(|c| c.is_ascii_digit()));
        }

        // All codes should be unique
        let unique_codes: std::collections::HashSet<_> = codes.iter().collect();
        assert_eq!(unique_codes.len(), codes.len());
    }

    #[cfg(feature = "mfa")]
    #[tokio::test]
    async fn test_secret_generation() {
        let provider = MfaProvider::new().unwrap();
        let secret1 = provider.generate_secret();
        let secret2 = provider.generate_secret();

        assert_eq!(secret1.len(), 20);
        assert_eq!(secret2.len(), 20);
        assert_ne!(secret1, secret2); // Should generate different secrets
    }

    #[cfg(feature = "mfa")]
    #[tokio::test]
    async fn test_totp_uri_format() {
        let provider = MfaProvider::new().unwrap();
        let user_id = Uuid::new_v4();
        let username = "test@example.com";

        let setup = provider.generate_setup(user_id, username).unwrap();

        assert!(setup.totp_uri.starts_with("otpauth://totp/"));
        assert!(setup.totp_uri.contains("elif.rs"));
        assert!(setup.totp_uri.contains("test%40example.com")); // URL encoded
        assert!(setup.totp_uri.contains("digits=6"));
        assert!(setup.totp_uri.contains("period=30"));
    }
}