Added GetNewMasterPass state and added restrictions on the master password

This commit is contained in:
2023-06-04 20:41:10 +03:00
parent 4d4cec1353
commit 9d0fa17a0e
7 changed files with 79 additions and 3 deletions

View File

@ -0,0 +1,44 @@
use crate::MainDialogue;
use cryptography::passwords::check_master_pass;
use sea_orm::DatabaseConnection;
use teloxide::{adaptors::Throttle, prelude::*};
const INVALID_MASTER_PASS_MESSAGE: &str = "Master password is invalid. It must be at least 8 characters long. \
It also has to contain at least one lowercase, one uppercase, one number and one punctuation character";
/// Checks that the account with that name exists
#[inline]
async fn check_new_master_pass(
bot: &Throttle<Bot>,
msg: &Message,
password: &str,
) -> crate::Result<Option<Message>> {
let is_valid = check_master_pass(password);
if !is_valid {
let msg = bot
.send_message(msg.chat.id, INVALID_MASTER_PASS_MESSAGE)
.await?;
return Ok(Some(msg));
}
Ok(None)
}
/// Handles GetNewMasterPass state
pub async fn get_new_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
next: super::PackagedHandler<String>,
) -> crate::Result<()> {
super::generic::generic(
bot,
msg,
db,
dialogue,
|bot, msg, _, password| Box::pin(check_new_master_pass(bot, msg, password)),
"Couldn't get the text of the message. Send the master password again",
next,
)
.await
}

View File

@ -4,6 +4,7 @@ mod generic;
mod get_existing_name;
mod get_login;
mod get_master_pass;
mod get_new_master_pass;
mod get_new_name;
mod get_password;
mod get_user;
@ -12,6 +13,7 @@ mod handler;
pub use get_existing_name::get_existing_name;
pub use get_login::get_login;
pub use get_master_pass::get_master_pass;
pub use get_new_master_pass::get_new_master_pass;
pub use get_new_name::get_new_name;
pub use get_password::get_password;
pub use get_user::get_user;
@ -27,6 +29,7 @@ pub enum State {
GetExistingName(PackagedHandler<String>),
GetNewName(PackagedHandler<String>),
GetMasterPass(PackagedHandler<String>),
GetNewMasterPass(PackagedHandler<String>),
GetLogin(PackagedHandler<String>),
GetPassword(PackagedHandler<String>),
GetUser(PackagedHandler<User>),