From b56ebd0b61b6189db739d0f0d32bb6c5f4387e7c Mon Sep 17 00:00:00 2001 From: StNicolay Date: Wed, 2 Nov 2022 23:11:29 +0300 Subject: [PATCH] Added account name and account credential checking --- src/bot/handlers.py | 9 +++++++++ src/bot/utils.py | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/bot/handlers.py b/src/bot/handlers.py index adf2714..816005e 100644 --- a/src/bot/handlers.py +++ b/src/bot/handlers.py @@ -9,6 +9,9 @@ from .. import cryptography, database from .utils import ( accounts_to_json, base_handler, + check_account_name, + check_login, + check_passwd, get_all_accounts, json_to_accounts, send_tmp_message, @@ -139,6 +142,8 @@ def _add_account2( if text == "/cancel": return send_tmp_message(bot, mes.chat.id, "Успешная отмена") + if not check_account_name(text): + return send_tmp_message(bot, mes.chat.id, "Не корректное название аккаунта") if text in database.get.get_accounts(engine, mes.from_user.id): return send_tmp_message( bot, mes.chat.id, "Аккаунт с таким именем уже существует" @@ -163,6 +168,8 @@ def _add_account3( text = mes.text.strip() if text == "/cancel": return send_tmp_message(bot, mes.chat.id, "Успешная отмена") + if not check_login(text): + return send_tmp_message(bot, mes.chat.id, "Не корректный логин") data["login"] = text @@ -184,6 +191,8 @@ def _add_account4( text = mes.text.strip() if text == "/cancel": return send_tmp_message(bot, mes.chat.id, "Успешная отмена") + if not check_passwd(text): + return send_tmp_message(bot, mes.chat.id, "Не корректный пароль") data["passwd"] = text diff --git a/src/bot/utils.py b/src/bot/utils.py index bf8f49b..0548eff 100644 --- a/src/bot/utils.py +++ b/src/bot/utils.py @@ -75,3 +75,23 @@ def accounts_to_json(accounts: list[tuple[str, str, str]]) -> io.StringIO: file = io.StringIO(_accounts_list_to_json(accounts)) file.name = "passwords.json" return file + + +def _base_check(val: str) -> bool: + "Returns false if finds new lines or backtick (`)" + return not ("\n" in val or "`" in val) + + +def check_account_name(name: str) -> bool: + "Returns true if account name is valid" + return _base_check(name) + + +def check_login(login: str) -> bool: + "Returns true if login is valid" + return _base_check(login) + + +def check_passwd(passwd: str) -> bool: + "Returns true if password is valid" + return _base_check(passwd)