Added command to add master password

This commit is contained in:
StNicolay 2023-04-27 17:40:49 +03:00
parent e8fc43f9ad
commit 3f8adb96f9
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
6 changed files with 192 additions and 4 deletions

101
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,101 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'pass_manager'",
"cargo": {
"args": [
"build",
"--bin=pass_manager",
"--package=pass_manager"
],
"filter": {
"name": "pass_manager",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'pass_manager'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=pass_manager",
"--package=pass_manager"
],
"filter": {
"name": "pass_manager",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in library 'migration'",
"cargo": {
"args": [
"test",
"--no-run",
"--lib",
"--package=migration"
],
"filter": {
"name": "migration",
"kind": "lib"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'migration'",
"cargo": {
"args": [
"build",
"--bin=migration",
"--package=migration"
],
"filter": {
"name": "migration",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'migration'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=migration",
"--package=migration"
],
"filter": {
"name": "migration",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

View File

@ -1,12 +1,16 @@
pub use sea_orm_migration::prelude::*;
mod m20220101_000001_create_table;
mod m20230427_142510_change_password_hash_size;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20220101_000001_create_table::Migration)]
vec![
Box::new(m20220101_000001_create_table::Migration),
Box::new(m20230427_142510_change_password_hash_size::Migration),
]
}
}

View File

@ -0,0 +1,44 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[derive(Iden)]
enum MasterPass {
Table,
#[iden = "password_hash"]
PasswordHash,
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
sea_query::Table::alter()
.table(MasterPass::Table)
.modify_column(
ColumnDef::new(MasterPass::PasswordHash)
.binary_len(64)
.not_null(),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
sea_query::Table::alter()
.table(MasterPass::Table)
.modify_column(
ColumnDef::new(MasterPass::PasswordHash)
.binary_len(128)
.not_null(),
)
.to_owned(),
)
.await
}
}

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(())
}