Moved the check of existence of a master password into master_pass.rs, added buttons for /get_account
This commit is contained in:
parent
17e4f1892c
commit
83aa2c90b1
8
Cargo.lock
generated
8
Cargo.lock
generated
@ -1324,9 +1324,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "linux-raw-sys"
|
name = "linux-raw-sys"
|
||||||
version = "0.3.6"
|
version = "0.3.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b64f40e5e03e0d54f03845c8197d0291253cdbedfb1cb46b13c2c117554a9f4c"
|
checksum = "ece97ea872ece730aed82664c424eb4c8291e1ff2480247ccf7409044bc6479f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lock_api"
|
name = "lock_api"
|
||||||
@ -2018,9 +2018,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustix"
|
name = "rustix"
|
||||||
version = "0.37.18"
|
version = "0.37.19"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8bbfc1d1c7c40c01715f47d71444744a81669ca84e8b63e25a55e169b1f86433"
|
checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"errno",
|
"errno",
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use rand::{rngs::OsRng, RngCore};
|
use rand::{rngs::OsRng, RngCore};
|
||||||
use scrypt::{scrypt, Params};
|
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)]
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||||
#[sea_orm(table_name = "master_pass")]
|
#[sea_orm(table_name = "master_pass")]
|
||||||
@ -54,4 +54,16 @@ impl Entity {
|
|||||||
let password_hash = hash_password(master_pass.as_ref(), &model.salt)?;
|
let password_hash = hash_password(master_pass.as_ref(), &model.salt)?;
|
||||||
Ok(Some(password_hash == model.password_hash))
|
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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,13 @@
|
|||||||
use crate::entity::{account, prelude::Account};
|
use crate::entity::{account, prelude::Account};
|
||||||
use crate::handlers::{utils::package_handler, MainDialogue, State};
|
use crate::handlers::{utils::package_handler, MainDialogue, State};
|
||||||
|
use futures::TryStreamExt;
|
||||||
use sea_orm::prelude::*;
|
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;
|
use tokio::task::spawn_blocking;
|
||||||
|
|
||||||
async fn get_master_pass(
|
async fn get_master_pass(
|
||||||
@ -12,6 +18,7 @@ async fn get_master_pass(
|
|||||||
name: String,
|
name: String,
|
||||||
master_pass: String,
|
master_pass: String,
|
||||||
) -> crate::Result<()> {
|
) -> crate::Result<()> {
|
||||||
|
let _ = bot.delete_message(msg.chat.id, msg.id).await;
|
||||||
dialogue.exit().await?;
|
dialogue.exit().await?;
|
||||||
let user_id = msg.from().unwrap().id.0;
|
let user_id = msg.from().unwrap().id.0;
|
||||||
let account = Account::find()
|
let account = Account::find()
|
||||||
@ -52,8 +59,18 @@ pub async fn get_account(
|
|||||||
bot: Throttle<Bot>,
|
bot: Throttle<Bot>,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MainDialogue,
|
dialogue: MainDialogue,
|
||||||
|
db: DatabaseConnection,
|
||||||
) -> crate::Result<()> {
|
) -> 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
|
dialogue
|
||||||
.update(State::GetAccountName(package_handler(get_account_name)))
|
.update(State::GetAccountName(package_handler(get_account_name)))
|
||||||
.await?;
|
.await?;
|
||||||
|
@ -2,7 +2,7 @@ use crate::{
|
|||||||
entity::{master_pass, prelude::*},
|
entity::{master_pass, prelude::*},
|
||||||
handlers::{utils::package_handler, MainDialogue, State},
|
handlers::{utils::package_handler, MainDialogue, State},
|
||||||
};
|
};
|
||||||
use sea_orm::{prelude::*, QuerySelect};
|
use sea_orm::prelude::*;
|
||||||
use teloxide::{adaptors::Throttle, prelude::*};
|
use teloxide::{adaptors::Throttle, prelude::*};
|
||||||
use tokio::task;
|
use tokio::task;
|
||||||
|
|
||||||
@ -15,19 +15,6 @@ async fn get_master_pass(
|
|||||||
) -> crate::Result<()> {
|
) -> crate::Result<()> {
|
||||||
dialogue.exit().await?;
|
dialogue.exit().await?;
|
||||||
let user_id = msg.from().unwrap().id.0;
|
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 || {
|
let model = task::spawn_blocking(move || {
|
||||||
master_pass::ActiveModel::from_unencrypted(user_id, &master_password)
|
master_pass::ActiveModel::from_unencrypted(user_id, &master_password)
|
||||||
})
|
})
|
||||||
@ -41,7 +28,14 @@ pub async fn set_master_pass(
|
|||||||
bot: Throttle<Bot>,
|
bot: Throttle<Bot>,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MainDialogue,
|
dialogue: MainDialogue,
|
||||||
|
db: DatabaseConnection,
|
||||||
) -> crate::Result<()> {
|
) -> 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")
|
bot.send_message(msg.chat.id, "Send new master password")
|
||||||
.await?;
|
.await?;
|
||||||
dialogue
|
dialogue
|
||||||
|
Loading…
x
Reference in New Issue
Block a user