diff --git a/src/__init__.py b/src/__init__.py index a4fd0a9..2ef97ef 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -2,14 +2,7 @@ import os from dotenv import load_dotenv -from . import ( - account_checks, - account_parsing, - bot, - cryptography, - database, - gen_password, -) +from . import account_checks, account_parsing, bot, cryptography, database, gen_password __all__ = [ "account_checks", diff --git a/src/bot/__init__.py b/src/bot/__init__.py index ef8dedd..0c97fa4 100644 --- a/src/bot/__init__.py +++ b/src/bot/__init__.py @@ -3,10 +3,9 @@ import functools import telebot from sqlalchemy.future import Engine -from .. import utils from . import handlers -__all__ = ["handlers", "utils"] +__all__ = ["handlers"] def create_bot(token: str, engine: Engine) -> telebot.TeleBot: diff --git a/src/bot/handlers.py b/src/bot/handlers.py index 53773ed..43093bf 100644 --- a/src/bot/handlers.py +++ b/src/bot/handlers.py @@ -14,7 +14,6 @@ from ..account_checks import ( ) from ..account_parsing import accounts_to_json, json_to_accounts from ..gen_password import gen_password -from ..utils import get_all_accounts Message = telebot.types.Message @@ -399,7 +398,8 @@ def _export2( if not cryptography.master_pass.check_master_pass(text, hash_pass, master_salt): return _send_tmp_message(bot, mes.chat.id, "Не подходит мастер пароль") - accounts = get_all_accounts(engine, mes.from_user.id, text) + accounts = database.get.get_all_accounts(engine, mes.from_user.id) + accounts = cryptography.other_accounts.decrypt_multiple(accounts, text) json_io = accounts_to_json(accounts) bot_mes = bot.send_document(mes.chat.id, json_io) diff --git a/src/cryptography/other_accounts.py b/src/cryptography/other_accounts.py index 26a97e8..0f2df4b 100644 --- a/src/cryptography/other_accounts.py +++ b/src/cryptography/other_accounts.py @@ -1,5 +1,6 @@ import base64 import os +from typing import Iterator from cryptography.fernet import Fernet from cryptography.hazmat.backends import default_backend @@ -47,3 +48,15 @@ def decrypt_account_info( login = f.decrypt(base64.urlsafe_b64encode(enc_login)).decode("utf-8") password = f.decrypt(base64.urlsafe_b64encode(enc_pass)).decode("utf-8") return (login, password) + + +def decrypt_multiple( + accounts: Iterator[tuple[str, bytes, bytes, bytes]], master_pass: str +) -> Iterator[tuple[str, str, str]]: + """Gets an iterator of tuples, where values represent account's name, salt, + encrypted login and encrypted password. + Return an iterator of names, logins and passwords as a tuple""" + for account in accounts: + name, salt, enc_login, enc_passwd = account + login, passwd = decrypt_account_info(enc_login, enc_passwd, master_pass, salt) + yield (name, login, passwd) diff --git a/src/utils.py b/src/utils.py deleted file mode 100644 index e93f35b..0000000 --- a/src/utils.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import Iterator - -from sqlalchemy.future import Engine - -from . import cryptography, database - - -def get_all_accounts( - engine: Engine, user_id: int, master_pass: str -) -> Iterator[tuple[str, str, str]]: - for account in database.get.get_all_accounts(engine, user_id): - name, salt, enc_login, enc_passwd = account - login, passwd = cryptography.other_accounts.decrypt_account_info( - enc_login, enc_passwd, master_pass, salt - ) - yield (name, login, passwd)