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, locale: LocaleRef, master_pass: &str, ) -> crate::Result> { let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let Some(model) = MasterPass::get(user_id, db).await? else { error!( "{:?}", anyhow::anyhow!( "User was put into the GetMasterPass state with no master password set" ) ); return Ok(Some(locale.master_password_is_not_set.to_owned())); }; let is_valid = { let hash = HashedBytes::from(model); let master_pass: Box<[u8]> = master_pass.as_bytes().into(); spawn_blocking(move || hash.verify(&master_pass)).await? }; if !is_valid { return Ok(Some(locale.wrong_master_password.to_owned())); } Ok(None) } crate::simple_state_handler!(get_master_pass, check_master_pass);