Moved the check of existence of a master password into master_pass.rs, added buttons for /get_account

This commit is contained in:
StNicolay 2023-05-04 19:03:03 +03:00
parent 17e4f1892c
commit 83aa2c90b1
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
4 changed files with 44 additions and 21 deletions

8
Cargo.lock generated
View File

@ -1324,9 +1324,9 @@ dependencies = [
[[package]]
name = "linux-raw-sys"
version = "0.3.6"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b64f40e5e03e0d54f03845c8197d0291253cdbedfb1cb46b13c2c117554a9f4c"
checksum = "ece97ea872ece730aed82664c424eb4c8291e1ff2480247ccf7409044bc6479f"
[[package]]
name = "lock_api"
@ -2018,9 +2018,9 @@ dependencies = [
[[package]]
name = "rustix"
version = "0.37.18"
version = "0.37.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bbfc1d1c7c40c01715f47d71444744a81669ca84e8b63e25a55e169b1f86433"
checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d"
dependencies = [
"bitflags",
"errno",

View File

@ -2,7 +2,7 @@
use rand::{rngs::OsRng, RngCore};
use scrypt::{scrypt, Params};
use sea_orm::{entity::prelude::*, ActiveValue::Set};
use sea_orm::{entity::prelude::*, ActiveValue::Set, QuerySelect};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "master_pass")]
@ -54,4 +54,16 @@ impl Entity {
let password_hash = hash_password(master_pass.as_ref(), &model.salt)?;
Ok(Some(password_hash == model.password_hash))
}
/// Checks if the master password for the user exists
pub async fn exists(user_id: u64, db: &DatabaseConnection) -> crate::Result<bool> {
let id = Self::find()
.select_only()
.column(Column::UserId)
.filter(Column::UserId.eq(user_id))
.into_tuple::<u64>()
.one(db)
.await?;
Ok(id.is_some())
}
}

View File

@ -1,7 +1,13 @@
use crate::entity::{account, prelude::Account};
use crate::handlers::{utils::package_handler, MainDialogue, State};
use futures::TryStreamExt;
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode};
use teloxide::{
adaptors::Throttle,
prelude::*,
types::ParseMode,
types::{KeyboardButton, KeyboardMarkup},
};
use tokio::task::spawn_blocking;
async fn get_master_pass(
@ -12,6 +18,7 @@ async fn get_master_pass(
name: String,
master_pass: String,
) -> crate::Result<()> {
let _ = bot.delete_message(msg.chat.id, msg.id).await;
dialogue.exit().await?;
let user_id = msg.from().unwrap().id.0;
let account = Account::find()
@ -52,8 +59,18 @@ pub async fn get_account(
bot: Throttle<Bot>,
msg: Message,
dialogue: MainDialogue,
db: DatabaseConnection,
) -> crate::Result<()> {
bot.send_message(msg.chat.id, "Send account name").await?;
let account_names: Vec<Vec<KeyboardButton>> = Account::get_names(msg.from().unwrap().id.0, &db)
.await?
.map_ok(|account| KeyboardButton::new(account))
.try_chunks(3)
.try_collect()
.await?;
let markup = KeyboardMarkup::new(account_names).resize_keyboard(true);
bot.send_message(msg.chat.id, "Send account name")
.reply_markup(markup)
.await?;
dialogue
.update(State::GetAccountName(package_handler(get_account_name)))
.await?;

View File

@ -2,7 +2,7 @@ use crate::{
entity::{master_pass, prelude::*},
handlers::{utils::package_handler, MainDialogue, State},
};
use sea_orm::{prelude::*, QuerySelect};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::task;
@ -15,19 +15,6 @@ async fn get_master_pass(
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = msg.from().unwrap().id.0;
let exists = MasterPass::find()
.select_only()
.column(master_pass::Column::UserId)
.filter(master_pass::Column::UserId.eq(user_id))
.into_tuple::<u64>()
.one(&db)
.await?
.is_some();
if exists {
bot.send_message(msg.chat.id, "Master password already exists")
.await?;
return Ok(());
}
let model = task::spawn_blocking(move || {
master_pass::ActiveModel::from_unencrypted(user_id, &master_password)
})
@ -41,7 +28,14 @@ pub async fn set_master_pass(
bot: Throttle<Bot>,
msg: Message,
dialogue: MainDialogue,
db: DatabaseConnection,
) -> crate::Result<()> {
let user_id = msg.from().unwrap().id.0;
if MasterPass::exists(user_id, &db).await? {
bot.send_message(msg.chat.id, "Master password already exists")
.await?;
return Ok(());
}
bot.send_message(msg.chat.id, "Send new master password")
.await?;
dialogue