elif-auth 0.4.0

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
//! Password hashing and cryptographic utilities

use crate::{AuthError, AuthResult, PasswordHasher};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use std::collections::HashMap;

#[cfg(feature = "argon2")]
use argon2::{
    password_hash::{PasswordHash, PasswordHasher as _, PasswordVerifier, SaltString},
    Argon2,
};

#[cfg(feature = "bcrypt")]
use bcrypt::{hash, verify, DEFAULT_COST};

/// Argon2 password hasher implementation
#[cfg(feature = "argon2")]
#[derive(Debug, Clone)]
pub struct Argon2Hasher {
    memory_cost: u32,
    time_cost: u32,
    parallelism: u32,
}

#[cfg(feature = "argon2")]
impl Argon2Hasher {
    /// Create a new Argon2 hasher with custom parameters
    pub fn new(memory_cost: u32, time_cost: u32, parallelism: u32) -> Self {
        Self {
            memory_cost,
            time_cost,
            parallelism,
        }
    }

    /// Create an Argon2 hasher with default parameters
    pub fn default() -> Self {
        Self {
            memory_cost: 65536, // 64 MB
            time_cost: 3,       // 3 iterations
            parallelism: 4,     // 4 threads
        }
    }

    /// Create an Argon2 hasher optimized for production
    pub fn production() -> Self {
        Self {
            memory_cost: 65536, // 64 MB
            time_cost: 4,       // 4 iterations
            parallelism: 4,     // 4 threads
        }
    }

    /// Create an Argon2 hasher optimized for development (faster)
    pub fn development() -> Self {
        Self {
            memory_cost: 4096, // 4 MB
            time_cost: 2,      // 2 iterations
            parallelism: 2,    // 2 threads
        }
    }
}

#[cfg(feature = "argon2")]
impl PasswordHasher for Argon2Hasher {
    fn hash_password(&self, password: &str) -> AuthResult<String> {
        let salt = SaltString::generate(&mut thread_rng());
        let argon2 = Argon2::new(
            argon2::Algorithm::Argon2id,
            argon2::Version::V0x13,
            argon2::Params::new(
                self.memory_cost,
                self.time_cost,
                self.parallelism,
                None,
            ).map_err(|e| AuthError::crypto_error(e.to_string()))?,
        );

        let password_hash = argon2
            .hash_password(password.as_bytes(), &salt)
            .map_err(|e| AuthError::crypto_error(e.to_string()))?;

        Ok(password_hash.to_string())
    }

    fn verify_password(&self, password: &str, hash: &str) -> AuthResult<bool> {
        let parsed_hash = PasswordHash::new(hash)
            .map_err(|e| AuthError::crypto_error(e.to_string()))?;

        let argon2 = Argon2::new(
            argon2::Algorithm::Argon2id,
            argon2::Version::V0x13,
            argon2::Params::new(
                self.memory_cost,
                self.time_cost,
                self.parallelism,
                None,
            ).map_err(|e| AuthError::crypto_error(e.to_string()))?,
        );

        match argon2.verify_password(password.as_bytes(), &parsed_hash) {
            Ok(()) => Ok(true),
            Err(_) => Ok(false),
        }
    }

    fn hasher_name(&self) -> &str {
        "argon2"
    }
}

/// bcrypt password hasher implementation
#[cfg(feature = "bcrypt")]
#[derive(Debug, Clone)]
pub struct BcryptHasher {
    cost: u32,
}

#[cfg(feature = "bcrypt")]
impl BcryptHasher {
    /// Create a new bcrypt hasher with custom cost
    pub fn new(cost: u32) -> Self {
        Self { cost }
    }

    /// Create a bcrypt hasher with default cost
    pub fn default() -> Self {
        Self { cost: DEFAULT_COST }
    }

    /// Create a bcrypt hasher optimized for production
    pub fn production() -> Self {
        Self { cost: 12 }
    }

    /// Create a bcrypt hasher optimized for development (faster)
    pub fn development() -> Self {
        Self { cost: 4 }
    }
}

#[cfg(feature = "bcrypt")]
impl PasswordHasher for BcryptHasher {
    fn hash_password(&self, password: &str) -> AuthResult<String> {
        hash(password, self.cost).map_err(AuthError::from)
    }

    fn verify_password(&self, password: &str, hash: &str) -> AuthResult<bool> {
        verify(password, hash).map_err(AuthError::from)
    }

    fn hasher_name(&self) -> &str {
        "bcrypt"
    }
}

/// Password hasher factory for creating different hashers
pub struct PasswordHasherFactory;

impl PasswordHasherFactory {
    /// Create a password hasher by name
    pub fn create_hasher(
        algorithm: &str,
        config: HashMap<String, serde_json::Value>,
    ) -> AuthResult<Box<dyn PasswordHasher>> {
        match algorithm {
            #[cfg(feature = "argon2")]
            "argon2" => {
                let memory_cost = config
                    .get("memory_cost")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(65536) as u32;
                let time_cost = config
                    .get("time_cost")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(3) as u32;
                let parallelism = config
                    .get("parallelism")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(4) as u32;

                Ok(Box::new(Argon2Hasher::new(memory_cost, time_cost, parallelism)))
            }
            #[cfg(feature = "bcrypt")]
            "bcrypt" => {
                use bcrypt::DEFAULT_COST;
                let cost = config
                    .get("cost")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(DEFAULT_COST as u64) as u32;

                Ok(Box::new(BcryptHasher::new(cost)))
            }
            _ => Err(AuthError::config_error(format!(
                "Unknown password hashing algorithm: {} (or feature not enabled)",
                algorithm
            ))),
        }
    }

    /// Create default hasher (Argon2)
    pub fn default_hasher() -> Box<dyn PasswordHasher> {
        #[cfg(feature = "argon2")]
        return Box::new(Argon2Hasher::default());
        
        #[cfg(all(not(feature = "argon2"), feature = "bcrypt"))]
        return Box::new(BcryptHasher::default());
        
        #[cfg(all(not(feature = "argon2"), not(feature = "bcrypt")))]
        panic!("No password hasher available. Enable either 'argon2' or 'bcrypt' feature");
    }
}

/// Utility functions for generating random values
pub struct CryptoUtils;

impl CryptoUtils {
    /// Generate a random string of specified length using alphanumeric characters
    pub fn generate_random_string(length: usize) -> String {
        thread_rng()
            .sample_iter(&Alphanumeric)
            .take(length)
            .map(char::from)
            .collect()
    }

    /// Generate a secure random token
    pub fn generate_token(length: usize) -> String {
        Self::generate_random_string(length)
    }

    /// Generate backup codes for MFA
    pub fn generate_backup_codes(count: usize, length: usize) -> Vec<String> {
        (0..count)
            .map(|_| Self::generate_random_string(length))
            .collect()
    }

    /// Generate a cryptographically secure session ID
    pub fn generate_session_id() -> String {
        Self::generate_token(32)
    }

    /// Generate a TOTP secret key
    pub fn generate_totp_secret() -> String {
        Self::generate_token(32)
    }

    /// Generate a JWT secret key
    pub fn generate_jwt_secret(length: Option<usize>) -> String {
        Self::generate_token(length.unwrap_or(64))
    }

    /// Validate password strength based on policy
    pub fn validate_password_strength(
        password: &str,
        min_length: usize,
        max_length: usize,
        require_uppercase: bool,
        require_lowercase: bool,
        require_numbers: bool,
        require_special: bool,
    ) -> AuthResult<()> {
        if password.len() < min_length {
            return Err(AuthError::generic_error(format!(
                "Password must be at least {} characters long",
                min_length
            )));
        }

        if password.len() > max_length {
            return Err(AuthError::generic_error(format!(
                "Password must be at most {} characters long",
                max_length
            )));
        }

        if require_uppercase && !password.chars().any(|c| c.is_uppercase()) {
            return Err(AuthError::generic_error(
                "Password must contain at least one uppercase letter".to_string(),
            ));
        }

        if require_lowercase && !password.chars().any(|c| c.is_lowercase()) {
            return Err(AuthError::generic_error(
                "Password must contain at least one lowercase letter".to_string(),
            ));
        }

        if require_numbers && !password.chars().any(|c| c.is_ascii_digit()) {
            return Err(AuthError::generic_error(
                "Password must contain at least one number".to_string(),
            ));
        }

        if require_special && !password.chars().any(|c| "!@#$%^&*()_+-=[]{}|;:,.<>?".contains(c)) {
            return Err(AuthError::generic_error(
                "Password must contain at least one special character".to_string(),
            ));
        }

        Ok(())
    }

    /// Hash a password with default settings
    pub fn hash_password(password: &str) -> AuthResult<String> {
        let hasher = PasswordHasherFactory::default_hasher();
        hasher.hash_password(password)
    }

    /// Verify a password against its hash
    pub fn verify_password(password: &str, hash: &str) -> AuthResult<bool> {
        let hasher = PasswordHasherFactory::default_hasher();
        hasher.verify_password(password, hash)
    }
}

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

    #[test]
    fn test_argon2_hasher() {
        let hasher = Argon2Hasher::default();
        let password = "test_password_123";
        
        let hash = hasher.hash_password(password).unwrap();
        assert!(!hash.is_empty());
        assert_ne!(hash, password);
        
        assert!(hasher.verify_password(password, &hash).unwrap());
        assert!(!hasher.verify_password("wrong_password", &hash).unwrap());
    }

    #[test]
    fn test_bcrypt_hasher() {
        let hasher = BcryptHasher::development(); // Use low cost for tests
        let password = "test_password_123";
        
        let hash = hasher.hash_password(password).unwrap();
        assert!(!hash.is_empty());
        assert_ne!(hash, password);
        
        assert!(hasher.verify_password(password, &hash).unwrap());
        assert!(!hasher.verify_password("wrong_password", &hash).unwrap());
    }

    #[test]
    fn test_password_hasher_factory() {
        let mut config = HashMap::new();
        config.insert("cost".to_string(), serde_json::Value::Number(serde_json::Number::from(4)));
        
        let hasher = PasswordHasherFactory::create_hasher("bcrypt", config).unwrap();
        assert_eq!(hasher.hasher_name(), "bcrypt");
        
        let mut config = HashMap::new();
        config.insert("memory_cost".to_string(), serde_json::Value::Number(serde_json::Number::from(4096)));
        
        let hasher = PasswordHasherFactory::create_hasher("argon2", config).unwrap();
        assert_eq!(hasher.hasher_name(), "argon2");
        
        let result = PasswordHasherFactory::create_hasher("invalid", HashMap::new());
        assert!(result.is_err());
    }

    #[test]
    fn test_crypto_utils_random_generation() {
        let token1 = CryptoUtils::generate_token(16);
        let token2 = CryptoUtils::generate_token(16);
        
        assert_eq!(token1.len(), 16);
        assert_eq!(token2.len(), 16);
        assert_ne!(token1, token2);
        
        let session_id = CryptoUtils::generate_session_id();
        assert_eq!(session_id.len(), 32);
        
        let backup_codes = CryptoUtils::generate_backup_codes(5, 8);
        assert_eq!(backup_codes.len(), 5);
        assert!(backup_codes.iter().all(|code| code.len() == 8));
    }

    #[test]
    fn test_password_strength_validation() {
        // Valid password
        let result = CryptoUtils::validate_password_strength(
            "Test123!",
            8,
            128,
            true,
            true,
            true,
            true,
        );
        assert!(result.is_ok());

        // Too short
        let result = CryptoUtils::validate_password_strength(
            "Test1!",
            8,
            128,
            true,
            true,
            true,
            true,
        );
        assert!(result.is_err());

        // Missing uppercase
        let result = CryptoUtils::validate_password_strength(
            "test123!",
            8,
            128,
            true,
            true,
            true,
            true,
        );
        assert!(result.is_err());

        // Missing special character
        let result = CryptoUtils::validate_password_strength(
            "Test1234",
            8,
            128,
            true,
            true,
            true,
            true,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_default_password_operations() {
        let password = "test_password_123";
        
        let hash = CryptoUtils::hash_password(password).unwrap();
        assert!(!hash.is_empty());
        
        assert!(CryptoUtils::verify_password(password, &hash).unwrap());
        assert!(!CryptoUtils::verify_password("wrong", &hash).unwrap());
    }
}