diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..7592df9 --- /dev/null +++ b/.vscode/launch.json @@ -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}" + } + ] +} \ No newline at end of file diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 2c605af..ce10edd 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -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> { - 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), + ] } } diff --git a/migration/src/m20230427_142510_change_password_hash_size.rs b/migration/src/m20230427_142510_change_password_hash_size.rs new file mode 100644 index 0000000..ff2058c --- /dev/null +++ b/migration/src/m20230427_142510_change_password_hash_size.rs @@ -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 + } +} diff --git a/src/entity/master_pass.rs b/src/entity/master_pass.rs index 23f0154..28e4b75 100644 --- a/src/entity/master_pass.rs +++ b/src/entity/master_pass.rs @@ -11,7 +11,7 @@ pub struct Model { pub user_id: u64, #[sea_orm(column_type = "Binary(BlobSize::Blob(Some(64)))")] pub salt: Vec, - #[sea_orm(column_type = "Binary(BlobSize::Blob(Some(128)))")] + #[sea_orm(column_type = "Binary(BlobSize::Blob(Some(64)))")] pub password_hash: Vec, } @@ -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, ¶ms, &mut password_hash)?; Ok(Self { user_id: Set(user_id), diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 8eb4eff..ceddc4d 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -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)), diff --git a/src/handlers/set_master_pass.rs b/src/handlers/set_master_pass.rs new file mode 100644 index 0000000..f50471f --- /dev/null +++ b/src/handlers/set_master_pass.rs @@ -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, + 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(()) +}