CONFIG ERROR
.fig.toml could not be parsed. Continuing with default settings.
error: could not parse configuration
--> .fig.toml
TOML parse error at line 17, column 1
|
17 | [present]
| ^^^^^^^^^
missing field `files`
help: fix the syntax shown above; default settings apply until it is valid1 10 20 30 40 50 60 70 80 901use actix_identity::Identity;2use argon2::password_hash::SaltString;3use argon2::{Argon2, PasswordHash, PasswordHasher, PasswordVerifier};4use rand::rngs::OsRng;5use uuid::Uuid;67use crate::db::Database;8use crate::models::User;910pub const SESSION_COOKIE_NAME: &str = "pear_session";1112pub fn hash_password(password: &str) -> Result<String, String> {13 let argon2 = Argon2::default();14 let salt = SaltString::generate(&mut OsRng);15 argon216 .hash_password(password.as_bytes(), &salt)17 .map(|hash| hash.to_string())18 .map_err(|e| e.to_string())19}2021pub fn verify_password(password: &str, hash: &str) -> Result<bool, String> {22 let argon2 = Argon2::default();23 let parsed_hash = PasswordHash::new(hash).map_err(|e| e.to_string())?;24 Ok(argon225 .verify_password(password.as_bytes(), &parsed_hash)26 .is_ok())27}2829pub fn create_user(username: &str, email: &str, password: &str) -> Result<User, String> {30 let email = email.trim();31 Ok(User {32 id: Uuid::new_v4().to_string(),33 username: username.trim().to_string(),34 email: (!email.is_empty()).then(|| email.to_string()),35 password_hash: hash_password(password)?,36 created_at: chrono::Utc::now().to_rfc3339(),37 })38}3940/// Resolves the logged-in user from the Actix identity, if present.41pub async fn current_user(identity: Option<Identity>, db: &Database) -> Option<User> {42 let user_id = identity?.id().ok()?;43 db.get_user_by_id(&user_id).await.ok().flatten()44}4546pub fn validate_username(username: &str) -> Result<(), String> {47 let username = username.trim();48 if username.len() < 3 {49 return Err("Username must be at least 3 characters".to_string());50 }51 if username.len() > 32 {52 return Err("Username must be at most 32 characters".to_string());53 }54 if !username55 .chars()56 .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')57 {58 return Err("Username may only contain letters, numbers, '-' and '_'".to_string());59 }60 Ok(())61}6263pub fn validate_email(email: &str) -> Result<(), String> {64 let email = email.trim();65 if email.is_empty() || !email.contains('@') {66 return Err("Please enter a valid email address".to_string());67 }68 Ok(())69}7071pub fn validate_password(password: &str) -> Result<(), String> {72 if password.len() < 8 {73 return Err("Password must be at least 8 characters".to_string());74 }75 Ok(())76}7778#[cfg(test)]79mod tests {80 use super::*;8182 #[test]83 fn test_hash_and_verify_password() {84 let hash = hash_password("correct horse").unwrap();85 assert!(verify_password("correct horse", &hash).unwrap());86 assert!(!verify_password("wrong", &hash).unwrap());87 }8889 #[test]90 fn test_create_user_hashes_password_and_drops_blank_email() {91 let user = create_user("alice", " ", "password123").unwrap();92 assert_eq!(user.username, "alice");93 assert_eq!(user.email, None);94 assert!(verify_password("password123", &user.password_hash).unwrap());9596 let with_email = create_user("bob", "bob@example.com", "password123").unwrap();97 assert_eq!(with_email.email, Some("bob@example.com".to_string()));98 }99100 #[test]101 fn test_validate_username() {102 assert!(validate_username("alice").is_ok());103 assert!(validate_username("a").is_err());104 assert!(validate_username("al ice").is_err());105 }106}