|
|
@@ -4,17 +4,10 @@ use serde_json::json;
|
|
|
use sqlx::PgPool;
|
|
|
use email_address::EmailAddress;
|
|
|
use chrono::{DateTime, Utc, Duration};
|
|
|
-use argon2::{
|
|
|
- Argon2,
|
|
|
- password_hash::{
|
|
|
- SaltString,
|
|
|
- PasswordHasher,
|
|
|
- rand_core::OsRng
|
|
|
- }
|
|
|
-};
|
|
|
use crate::{
|
|
|
app_error::AppError,
|
|
|
- models::user::User
|
|
|
+ models::user::User,
|
|
|
+ logic::{valid_password, hash_password}
|
|
|
};
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
@@ -66,18 +59,6 @@ fn is_bot(url: &Option<String>, time: &Option<String>) -> bool {
|
|
|
age > Duration::minutes(10)
|
|
|
}
|
|
|
|
|
|
-fn valid_password(pass: &String, confirm_pass: &String) -> Result<(), AppError> {
|
|
|
- if *pass != *confirm_pass {
|
|
|
- return Err(AppError::invalid_input("Passwords do not match"));
|
|
|
- }
|
|
|
-
|
|
|
- if pass.chars().count() < 12 {
|
|
|
- return Err(AppError::invalid_input("Password must contain at least 12 characters"));
|
|
|
- }
|
|
|
-
|
|
|
- Ok(())
|
|
|
-}
|
|
|
-
|
|
|
fn validate_email(email: &String) -> Result<String, AppError> {
|
|
|
if !EmailAddress::is_valid(email) {
|
|
|
return Err(AppError::invalid_input("Invalid email address"));
|
|
|
@@ -85,12 +66,3 @@ fn validate_email(email: &String) -> Result<String, AppError> {
|
|
|
|
|
|
Ok(email.to_lowercase())
|
|
|
}
|
|
|
-
|
|
|
-fn hash_password(pass: &String) -> Result<String, AppError> {
|
|
|
- let salt = SaltString::generate(&mut OsRng);
|
|
|
- let argon2 = Argon2::default();
|
|
|
- match argon2.hash_password(pass.as_bytes(), &salt) {
|
|
|
- Ok(h) => Ok(h.to_string()),
|
|
|
- Err(e) => Err(AppError::InternalError(e.to_string()))
|
|
|
- }
|
|
|
-}
|