Made verify_master_pass compute the hash in a blocking thread

This commit is contained in:
StNicolay 2023-05-04 19:39:36 +03:00
parent 83aa2c90b1
commit e129934aac
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
2 changed files with 8 additions and 4 deletions

View File

@ -3,6 +3,7 @@
use rand::{rngs::OsRng, RngCore};
use scrypt::{scrypt, Params};
use sea_orm::{entity::prelude::*, ActiveValue::Set, QuerySelect};
use tokio::task::spawn_blocking;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "master_pass")]
@ -43,7 +44,7 @@ impl ActiveModel {
impl Entity {
pub async fn verify_master_pass(
user_id: u64,
master_pass: &str,
master_pass: String,
db: &DatabaseConnection,
) -> crate::Result<Option<bool>> {
let model = match Self::find_by_id(user_id).one(db).await {
@ -51,8 +52,10 @@ impl Entity {
Ok(None) => return Ok(None),
Err(err) => return Err(err.into()),
};
let password_hash = hash_password(master_pass.as_ref(), &model.salt)?;
Ok(Some(password_hash == model.password_hash))
let (hash, salt) = (model.password_hash, model.salt);
let password_hash =
spawn_blocking(move || hash_password(master_pass.as_bytes(), &salt)).await??;
Ok(Some(password_hash == hash))
}
/// Checks if the master password for the user exists

View File

@ -12,7 +12,8 @@ pub async fn check_master_pass<'a>(
db: &'a DatabaseConnection,
master_pass: &'a str,
) -> crate::Result<bool> {
let result = MasterPass::verify_master_pass(msg.from().unwrap().id.0, master_pass, db).await;
let result =
MasterPass::verify_master_pass(msg.from().unwrap().id.0, master_pass.to_owned(), db).await;
match result {
Ok(Some(true)) => Ok(true),
Ok(Some(false)) => {