leptos_sync_core/security/authentication/
validation.rs1use super::config::AuthConfig;
4use crate::SyncError;
5
6pub fn validate_password(password: &str, config: &AuthConfig) -> Result<(), SyncError> {
8 if password.len() < config.password_min_length {
9 return Err(SyncError::AuthenticationError(format!(
10 "Password must be at least {} characters long",
11 config.password_min_length
12 )));
13 }
14
15 if config.require_uppercase && !password.chars().any(|c| c.is_uppercase()) {
16 return Err(SyncError::AuthenticationError(
17 "Password must contain at least one uppercase letter".to_string(),
18 ));
19 }
20
21 if config.require_lowercase && !password.chars().any(|c| c.is_lowercase()) {
22 return Err(SyncError::AuthenticationError(
23 "Password must contain at least one lowercase letter".to_string(),
24 ));
25 }
26
27 if config.require_numbers && !password.chars().any(|c| c.is_numeric()) {
28 return Err(SyncError::AuthenticationError(
29 "Password must contain at least one number".to_string(),
30 ));
31 }
32
33 if config.require_special_chars
34 && !password
35 .chars()
36 .any(|c| "!@#$%^&*()_+-=[]{}|;:,.<>?".contains(c))
37 {
38 return Err(SyncError::AuthenticationError(
39 "Password must contain at least one special character".to_string(),
40 ));
41 }
42
43 Ok(())
44}