change hash_cost to u32

This commit is contained in:
Rohan Sircar 2021-05-12 17:58:58 +05:30
parent 5c8720d40c
commit 1d74b75ca8
4 changed files with 18 additions and 10 deletions

View File

@ -43,6 +43,7 @@ pub fn get_all(
pub fn insert_new_user(
nu: models::NewUser,
conn: &impl diesel::Connection<Backend = diesel::sqlite::Sqlite>,
hash_cost: Option<u32>,
) -> Result<models::UserDto, errors::DomainError> {
// It is common when using Diesel with Actix web to import schema-related
// modules inside a function's scope (rather than the normal module's scope)
@ -50,7 +51,7 @@ pub fn insert_new_user(
use crate::schema::users::dsl::*;
let nu = {
let mut nu2 = nu;
nu2.password = hash(&nu2.password, DEFAULT_COST)?;
nu2.password = hash(&nu2.password, hash_cost.unwrap_or(DEFAULT_COST))?;
nu2
};

View File

@ -39,13 +39,13 @@ pub struct EnvConfig {
pub database_url: String,
pub http_host: String,
#[serde(default = "default_hash_cost")]
pub hash_cost: u8,
pub hash_cost: u32,
pub logger_format: LoggerFormat,
}
#[derive(Deserialize, Debug, Clone)]
pub struct AppConfig {
pub hash_cost: u8,
pub hash_cost: u32,
}
#[derive(Clone)]
@ -54,7 +54,7 @@ pub struct AppData {
pub pool: DbPool,
}
pub fn default_hash_cost() -> u8 {
pub fn default_hash_cost() -> u32 {
8
}
@ -72,7 +72,8 @@ pub fn configure_app(app_data: AppData) -> Box<dyn Fn(&mut ServiceConfig)> {
.route(
"/{user_id}",
web::get().to(routes::users::get_user),
),
)
.route("", web::post().to(routes::users::add_user)),
)
.route(
"/build-info",
@ -84,7 +85,7 @@ pub fn configure_app(app_data: AppData) -> Box<dyn Fn(&mut ServiceConfig)> {
.service(routes::auth::login)
.service(routes::auth::logout)
.service(routes::auth::index)
.service(routes::users::add_user)
// .service(routes::users::add_user)
.service(fs::Files::new("/", "./static"));
})
}

View File

@ -58,6 +58,8 @@ pub async fn get_user2(
}
}
///List all users
#[tracing::instrument(level = "debug", skip(app_data))]
pub async fn get_all_users(
app_data: web::Data<AppData>,
) -> Result<HttpResponse, DomainError> {
@ -70,7 +72,7 @@ pub async fn get_all_users(
.await
.map_err(|err| DomainError::new_thread_pool_error(err.to_string()))?;
tracing::debug!("{:?}", users);
tracing::trace!("{:?}", users);
if !users.is_empty() {
Ok(HttpResponse::Ok().json(users))
@ -82,7 +84,7 @@ pub async fn get_all_users(
}
//TODO: Add refinement here
/// Inserts new user with name defined in form.
#[post("/do_registration")]
#[tracing::instrument(level = "debug", skip(app_data))]
pub async fn add_user(
app_data: web::Data<AppData>,
form: web::Json<models::NewUser>,
@ -92,7 +94,11 @@ pub async fn add_user(
Ok(_) => web::block(move || {
let pool = &app_data.pool;
let conn = pool.get()?;
actions::insert_new_user(form.0, &conn)
actions::insert_new_user(
form.0,
&conn,
Some(app_data.config.hash_cost),
)
})
.await
.map(|user| {

View File

@ -58,7 +58,7 @@ impl UserService for UserServiceImpl {
nu: models::NewUser,
) -> Result<models::UserDto, errors::DomainError> {
let conn = self.pool.get()?;
actions::insert_new_user(nu, &conn)
actions::insert_new_user(nu, &conn, Some(8))
}
fn verify_password(