Improved master password validation

This commit is contained in:
2023-06-28 00:08:51 +03:00
parent bc56846843
commit 580641bcf4
3 changed files with 157 additions and 54 deletions

View File

@ -1,10 +1,36 @@
use crate::MainDialogue;
use cryptography::passwords::check_master_pass;
use cryptography::passwords::{check_master_pass, PasswordValidity};
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";
#[inline]
fn process_validity(validity: PasswordValidity) -> Result<(), String> {
if validity.is_empty() {
return Ok(());
}
let mut error_text = "Your master password is invalid:\n".to_owned();
if validity.contains(PasswordValidity::NO_LOWERCASE) {
error_text.push_str("\n* It doesn't have any lowercase characters")
}
if validity.contains(PasswordValidity::NO_UPPERCASE) {
error_text.push_str("\n* It doesn't have any uppercase characters")
}
if validity.contains(PasswordValidity::NO_NUMBER) {
error_text.push_str("\n* It doesn't have any numbers")
}
if validity.contains(PasswordValidity::NO_SPECIAL_CHARACTER) {
error_text.push_str("\n* It doesn't have any special characters")
}
if validity.contains(PasswordValidity::TOO_SHORT) {
error_text.push_str("\n* It is shorter than 8 characters")
}
error_text.push_str("\n\nModify your password and send it again");
Err(error_text)
}
/// Checks that the account with that name exists
#[inline]
@ -13,14 +39,15 @@ async fn check_new_master_pass(
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));
let validity = check_master_pass(password);
match process_validity(validity) {
Ok(()) => Ok(None),
Err(error_text) => {
let msg = bot.send_message(msg.chat.id, error_text).await?;
Ok(Some(msg))
}
}
Ok(None)
}
/// Handles GetNewMasterPass state