pass_manager/src/state/get_master_pass.rs

38 lines
1.1 KiB
Rust

use crate::prelude::*;
use cryptography::hashing::HashedBytes;
use log::error;
use tokio::task::spawn_blocking;
/// Returns true if the provided master password is valid
#[inline]
async fn check_master_pass(
msg: &Message,
db: &Pool,
master_pass: &str,
) -> crate::Result<Option<String>> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let Some(model) = MasterPass::get(user_id, db).await? else {
error!("User was put into the GetMasterPass state with no master password set");
return Ok(Some(
"No master password set. Use /cancel and set it by using /set_master_pass".to_owned(),
));
};
let is_valid = {
let hash = HashedBytes::from(model);
let master_pass = master_pass.to_owned();
spawn_blocking(move || hash.verify(master_pass.as_bytes())).await?
};
if !is_valid {
return Ok(Some("Wrong master password. Try again".to_owned()));
}
Ok(None)
}
crate::simple_state_handler!(
get_master_pass,
check_master_pass,
"Couldn't get the text of the message. Send the master password again"
);