argon encrypted register endpoint
This commit is contained in:
@@ -14,12 +14,21 @@ use serde_json::Value;
|
||||
use chrono::{Utc};
|
||||
use rusqlite::{params, Connection, OptionalExtension};
|
||||
|
||||
|
||||
|
||||
//use crate::utils::db_pool::;
|
||||
use crate::utils::db_pool::{
|
||||
HotelPool,
|
||||
AppState,
|
||||
use crate::utils::db_pool::{HotelPool,AppState};
|
||||
|
||||
|
||||
use rand_core::OsRng;
|
||||
use argon2::{
|
||||
password_hash::{
|
||||
PasswordHash, PasswordHasher, PasswordVerifier, SaltString
|
||||
},
|
||||
Argon2
|
||||
};
|
||||
|
||||
|
||||
pub async fn auth_middleware(
|
||||
mut req: HttpRequest<Body>,
|
||||
next: Next,
|
||||
@@ -60,6 +69,72 @@ pub async fn auth_middleware(
|
||||
Ok(next.run(req).await)
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Hash a new password
|
||||
fn hash_password(password: &str) -> anyhow::Result<String> {
|
||||
let salt = SaltString::generate(&mut OsRng); // unique per password
|
||||
let argon2 = Argon2::default(); // Argon2id with good defaults
|
||||
|
||||
let password_hash = argon2
|
||||
.hash_password(password.as_bytes(), &salt)
|
||||
.map_err(|e| anyhow::anyhow!(e))?
|
||||
.to_string();
|
||||
|
||||
Ok(password_hash)
|
||||
}
|
||||
|
||||
// Verify an incoming password against stored hash
|
||||
fn verify_password(password: &str, stored_hash: &str) -> bool {
|
||||
let parsed_hash = PasswordHash::new(stored_hash).unwrap();
|
||||
Argon2::default()
|
||||
.verify_password(password.as_bytes(), &parsed_hash)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct RegisterValues{
|
||||
username: String,
|
||||
password: String,
|
||||
hotel_id: i32,
|
||||
displayname: String,
|
||||
}
|
||||
|
||||
pub struct RegisterPayload(pub RegisterValues);
|
||||
|
||||
impl<S> FromRequest<S> for RegisterPayload
|
||||
where S: Send + Sync,
|
||||
{
|
||||
type Rejection = (StatusCode, String);
|
||||
|
||||
async fn from_request(req: ExtractRequest, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let Json(payload) = Json::<RegisterValues>::from_request(req, state)
|
||||
.await
|
||||
.map_err(|err| (StatusCode::BAD_REQUEST, format!("Invalid body: {}", err)))?;
|
||||
Ok(RegisterPayload(payload))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn register_user (
|
||||
State(state): State<AppState>,
|
||||
RegisterPayload(payload): RegisterPayload
|
||||
) -> Result<impl IntoResponse, (StatusCode, &'static str)> {
|
||||
|
||||
let hashed_password = hash_password(&payload.password)
|
||||
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Password hashing failed"))?;
|
||||
|
||||
let conn = state.logs_pool.get()
|
||||
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "DB connection error"))?;
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO users (username, password, hotel_id, displayname) VALUES (?1, ?2, ?3, ?4)",
|
||||
params![payload.username, hashed_password, payload.hotel_id, payload.displayname],
|
||||
)
|
||||
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "DB insert error"))?;
|
||||
|
||||
Ok((StatusCode::CREATED, "User registered successfully"))
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct LoginValues {
|
||||
username : String,
|
||||
@@ -214,14 +289,4 @@ pub async fn clean_auth_loging(
|
||||
|
||||
fn internal_error<E: std::fmt::Display>(err: E) -> (StatusCode, String) {
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, format!("Internal error: {}", err))
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
match result {
|
||||
Ok(rows) if rows > 0 => (StatusCode::OK, format!("Logged")),
|
||||
Ok(_) => (StatusCode::NOT_FOUND, "No used".to_string()),
|
||||
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("DB error: {err}")),
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
@@ -12,5 +12,6 @@ pub fn utils_routes() -> Router<AppState> {
|
||||
|
||||
Router::new()
|
||||
.route("/login", put(clean_auth_loging))
|
||||
.route("/register", put(register_user))
|
||||
//.with_state(state)
|
||||
}
|
||||
Reference in New Issue
Block a user