Added command to add master password

This commit is contained in:
2023-04-27 17:40:49 +03:00
parent e8fc43f9ad
commit 3f8adb96f9
6 changed files with 192 additions and 4 deletions

View File

@@ -11,7 +11,7 @@ pub struct Model {
pub user_id: u64,
#[sea_orm(column_type = "Binary(BlobSize::Blob(Some(64)))")]
pub salt: Vec<u8>,
#[sea_orm(column_type = "Binary(BlobSize::Blob(Some(128)))")]
#[sea_orm(column_type = "Binary(BlobSize::Blob(Some(64)))")]
pub password_hash: Vec<u8>,
}
@@ -28,9 +28,9 @@ impl ActiveModel {
Params::RECOMMENDED_LOG_N,
Params::RECOMMENDED_R,
Params::RECOMMENDED_P,
128,
64,
)?;
let mut password_hash = vec![0; 128];
let mut password_hash = vec![0; 64];
scrypt(password.as_ref(), &salt, &params, &mut password_hash)?;
Ok(Self {
user_id: Set(user_id),

View File

@@ -11,6 +11,7 @@ mod default;
mod get_account;
mod get_accounts;
mod help;
mod set_master_pass;
#[derive(BotCommands, Clone)]
#[command(rename_rule = "snake_case")]
@@ -23,6 +24,8 @@ enum Command {
GetAccount(String, String),
#[command()]
GetAccounts,
#[command()]
SetMasterPass(String),
}
pub fn get_dispatcher(
@@ -46,6 +49,10 @@ pub fn get_dispatcher(
)
.branch(
dptree::case![Command::GetAccounts].endpoint(get_accounts::get_accounts),
)
.branch(
dptree::case![Command::SetMasterPass(param)]
.endpoint(set_master_pass::set_master_pass),
),
)
.branch(dptree::endpoint(default::default)),

View File

@@ -0,0 +1,32 @@
use crate::entity::{master_pass, prelude::*};
use sea_orm::{prelude::*, QuerySelect};
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::task;
pub async fn set_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
master_pass: String,
) -> crate::Result<()> {
let user_id = msg.from().unwrap().id.0;
println!("User id: {user_id}");
let exists = MasterPass::find()
.filter(master_pass::Column::UserId.eq(user_id))
.limit(1)
.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_pass)
})
.await??;
model.insert(&db).await?;
bot.send_message(msg.chat.id, "Success").await?;
Ok(())
}