Major refactor of the code

A lot of function are now using classes instead of parameters or tuples
isort was added to the dev requirements
Comments were adjusted
This commit is contained in:
2022-12-25 18:01:12 +03:00
parent bbc9650357
commit 3f744723a9
12 changed files with 195 additions and 186 deletions

View File

@ -5,7 +5,7 @@ import time
import telebot
from sqlalchemy.future import Engine
from .. import encryption, database, generate_password
from .. import database, encryption, generate_password
from ..account_checks import (
check_account,
check_account_name,
@ -13,6 +13,7 @@ from ..account_checks import (
check_password,
)
from ..account_parsing import accounts_to_json, json_to_accounts
from ..classes import DecryptedAccount
Message = telebot.types.Message
@ -31,9 +32,9 @@ def _send_tmp_message(
def _base_handler(
bot: telebot.TeleBot, mes: Message, prev_mes: Message | None = None
) -> None:
bot.delete_message(mes.chat.id, mes.id)
bot.delete_message(mes.chat.id, mes.id, timeout=5)
if prev_mes is not None:
bot.delete_message(prev_mes.chat.id, prev_mes.id)
bot.delete_message(prev_mes.chat.id, prev_mes.id, timeout=5)
def get_accounts(bot: telebot.TeleBot, engine: Engine, mes: Message) -> None:
@ -68,11 +69,11 @@ def delete_all(bot: telebot.TeleBot, engine: Engine, mes: Message) -> None:
"Отправьте YES для подтверждения",
)
bot.register_next_step_handler(
mes, functools.partial(_delete_all, bot, engine, bot_mes)
mes, functools.partial(_delete_all2, bot, engine, bot_mes)
)
def _delete_all(
def _delete_all2(
bot: telebot.TeleBot, engine: Engine, prev_mes: Message, mes: Message
) -> None:
_base_handler(bot, mes, prev_mes)
@ -115,15 +116,12 @@ def _set_master_pass2(
if text == "/cancel":
return _send_tmp_message(bot, mes.chat.id, "Успешная отмена")
password_hash, master_salt = encryption.master_pass.encrypt_master_pass(
master_password = encryption.master_pass.encrypt_master_pass(
mes.from_user.id,
text,
)
database.add.add_master_pass(
engine,
mes.from_user.id,
master_salt,
password_hash,
)
database.add.add_master_pass(engine, master_password)
_send_tmp_message(bot, mes.chat.id, "Успех")
del mes, text
gc.collect()
@ -135,13 +133,16 @@ def reset_master_pass(
mes: Message,
) -> None:
_base_handler(bot, mes)
if database.get.get_master_pass(engine, mes.from_user.id) is None:
return _send_tmp_message(bot, mes.chat.id, "Мастер пароль не задан")
bot_mes = bot.send_message(
mes.chat.id,
"Отправьте новый мастер пароль, осторожно, все текущие аккаунты "
"будут удалены навсегда",
)
bot.register_next_step_handler(
mes, functools.partial(_reset_master_pass2, bot, engine, bot_mes)
)
@ -155,9 +156,13 @@ def _reset_master_pass2(
if text == "/cancel":
return _send_tmp_message(bot, mes.chat.id, "Успешная отмена")
hash_, salt = encryption.master_pass.encrypt_master_pass(text)
master_password = encryption.master_pass.encrypt_master_pass(
mes.from_user.id,
text,
)
database.delete.purge_accounts(engine, mes.from_user.id)
database.change.change_master_pass(engine, mes.from_user.id, salt, hash_)
database.change.change_master_pass(engine, master_password)
_send_tmp_message(
bot, mes.chat.id, "Все ваши аккаунты удалены, а мастер пароль изменён"
)
@ -267,24 +272,30 @@ def _add_account5(
if text == "/cancel":
return _send_tmp_message(bot, mes.chat.id, "Успешная отмена")
salt, hash_ = database.get.get_master_pass(engine, mes.from_user.id)
if not encryption.master_pass.check_master_pass(text, hash_, salt):
master_password = database.get.get_master_pass(engine, mes.from_user.id)
if not encryption.master_pass.check_master_pass(text, master_password):
return _send_tmp_message(
bot,
mes.chat.id,
"Не подходит главный пароль",
)
name, login, passwd = data["name"], data["login"], data["passwd"]
# name, login, passwd = data["name"], data["login"], data["passwd"]
account = DecryptedAccount(
user_id=mes.from_user.id,
name=data["name"],
login=data["login"],
password=data["passwd"],
)
enc_login, enc_pass, salt = encryption.other_accounts.encrypt(
login,
passwd,
encrypted_account = encryption.other_accounts.encrypt(
account,
text,
)
result = database.add.add_account(
engine, mes.from_user.id, name, salt, enc_login, enc_pass
engine,
encrypted_account,
)
_send_tmp_message(
@ -293,19 +304,19 @@ def _add_account5(
"Успех" if result else "Произошла не предвиденная ошибка",
)
del data, name, login, passwd, enc_login
del data, account
gc.collect()
def get_account(bot: telebot.TeleBot, engine: Engine, mes: Message) -> None:
_base_handler(bot, mes)
bot_mes = bot.send_message(mes.chat.id, "Отправьте название аккаунта")
master_pass = database.get.get_master_pass(engine, mes.from_user.id)
if master_pass is None:
return _send_tmp_message(bot, mes.chat.id, "Нет мастер пароля")
bot_mes = bot.send_message(mes.chat.id, "Отправьте название аккаунта")
bot.register_next_step_handler(
mes, functools.partial(_get_account2, bot, engine, bot_mes)
)
@ -340,36 +351,28 @@ def _get_account3(
if text == "/cancel":
return _send_tmp_message(bot, mes.chat.id, "Успешная отмена")
master_salt, hash_pass = database.get.get_master_pass(
master_password = database.get.get_master_pass(
engine,
mes.from_user.id,
)
if not encryption.master_pass.check_master_pass(
text,
hash_pass,
master_salt,
):
if not encryption.master_pass.check_master_pass(text, master_password):
return _send_tmp_message(bot, mes.chat.id, "Не подходит мастер пароль")
salt, enc_login, enc_pass = database.get.get_account_info(
engine, mes.from_user.id, name
)
login, passwd = encryption.other_accounts.decrypt(
enc_login,
enc_pass,
account = database.get.get_account_info(engine, mes.from_user.id, name)
account = encryption.other_accounts.decrypt(
account,
text,
salt,
)
_send_tmp_message(
bot,
mes.chat.id,
f"Логин:\n`{login}`\nПароль:\n`{passwd}`\nНажмите на логин "
"или пароль, чтобы скопировать",
f"Логин:\n`{account.login}`\nПароль:\n`{account.password}`\nНажмите "
"на логин или пароль, чтобы скопировать",
30,
)
del text, mes, passwd, login
del text, mes
gc.collect()
@ -455,15 +458,11 @@ def _export2(
if text == "/cancel":
return _send_tmp_message(bot, mes.chat.id, "Успешная отмена")
master_salt, hash_pass = database.get.get_master_pass(
master_password = database.get.get_master_pass(
engine,
mes.from_user.id,
)
if not encryption.master_pass.check_master_pass(
text,
hash_pass,
master_salt,
):
if not encryption.master_pass.check_master_pass(text, master_password):
return _send_tmp_message(bot, mes.chat.id, "Не подходит мастер пароль")
accounts = database.get.get_all_accounts(engine, mes.from_user.id)
@ -474,7 +473,7 @@ def _export2(
del text, accounts, json_io
gc.collect()
time.sleep(30)
bot.delete_message(bot_mes.chat.id, bot_mes.id)
bot.delete_message(bot_mes.chat.id, bot_mes.id, timeout=5)
def import_accounts(
@ -513,13 +512,16 @@ def _import2(
mes.chat.id,
"Вы должны отправить документ",
)
if mes.document.file_size > 102_400: # If file size is bigger that 100 MB
if mes.document.file_size > 102_400: # If file size is bigger than 100 MB
return _send_tmp_message(bot, mes.chat.id, "Файл слишком большой")
file_info = bot.get_file(mes.document.file_id)
downloaded_file = bot.download_file(file_info.file_path)
try:
accounts = json_to_accounts(downloaded_file.decode("utf-8"))
accounts = json_to_accounts(
downloaded_file.decode("utf-8"),
mes.from_user.id,
)
except Exception:
return _send_tmp_message(
bot,
@ -537,7 +539,7 @@ def _import3(
bot: telebot.TeleBot,
engine: Engine,
prev_mes: Message,
accounts: list[tuple[str, str, str]],
accounts: list[DecryptedAccount],
mes: Message,
) -> None:
_base_handler(bot, mes, prev_mes)
@ -545,40 +547,33 @@ def _import3(
if text == "/cancel":
return _send_tmp_message(bot, mes.chat.id, "Успешная отмена")
master_salt, hash_pass = database.get.get_master_pass(
master_password = database.get.get_master_pass(
engine,
mes.from_user.id,
)
if not encryption.master_pass.check_master_pass(
text,
hash_pass,
master_salt,
):
if not encryption.master_pass.check_master_pass(text, master_password):
return _send_tmp_message(bot, mes.chat.id, "Не подходит мастер пароль")
# List of names of accounts, which failed to be added to the database
# or failed the tests
failed: list[str] = []
for account in accounts:
name, login, passwd = account
if not check_account(name, login, passwd):
failed.append(name)
if not check_account(account):
failed.append(account.name)
continue
enc_login, enc_passwd, salt = encryption.other_accounts.encrypt(
login,
passwd,
account = encryption.other_accounts.encrypt(
account,
text,
)
result = database.add.add_account(
engine, mes.from_user.id, name, salt, enc_login, enc_passwd
)
result = database.add.add_account(engine, account)
if not result:
failed.append(name)
failed.append(account.name)
if failed:
mes_text = "Не удалось добавить:\n" + "\n".join(failed)
else:
mes_text = "Успех"
_send_tmp_message(bot, mes.chat.id, mes_text, 10)
del text, mes, accounts
gc.collect()