Compare commits
4 Commits
4702e048f3
...
267e54bb29
Author | SHA1 | Date | |
---|---|---|---|
|
267e54bb29 | ||
|
b1017082a9 | ||
|
39e86793a6 | ||
|
822061ae49 |
@ -31,4 +31,8 @@ def create_bot(token: str, engine: mariadb.Connection) -> telebot.TeleBot:
|
||||
functools.partial(handlers.reset_master_pass, bot, engine),
|
||||
commands=["reset_master_pass"],
|
||||
)
|
||||
bot.register_message_handler(
|
||||
functools.partial(handlers.delete_account, bot, engine),
|
||||
commands=["delete_account"],
|
||||
)
|
||||
return bot
|
||||
|
@ -26,7 +26,7 @@ def add_record(
|
||||
|
||||
master_password = data[4]
|
||||
master_password_from_db = database.get.get_master_pass(engine, mes.from_user.id)
|
||||
if master_password is None:
|
||||
if master_password_from_db is None:
|
||||
return _send_tmp_message(bot, mes.chat.id, "Нет мастер пароля")
|
||||
|
||||
master_salt, hash_pass = master_password_from_db
|
||||
@ -99,10 +99,14 @@ def get_account(
|
||||
if len(data) != 3:
|
||||
return _send_tmp_message(bot, 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, "Нет мастер пароля")
|
||||
master_salt, hash_pass = master_pass
|
||||
|
||||
if data[1] not in database.get.get_accounts(engine, mes.from_user.id):
|
||||
return _send_tmp_message(bot, mes.chat.id, "Нет такого аккаунта")
|
||||
|
||||
master_salt, hash_pass = database.get.get_master_pass(engine, mes.from_user.id)
|
||||
if (
|
||||
cryptography.master_pass.encrypt_master_pass_known_salt(data[2], master_salt)
|
||||
!= hash_pass
|
||||
@ -133,6 +137,13 @@ def reset_master_pass(
|
||||
bot: telebot.TeleBot, engine: Engine, mes: telebot.types.Message
|
||||
) -> None:
|
||||
data = shlex.split(mes.text)
|
||||
if len(data) != 2:
|
||||
return _send_tmp_message(bot, mes.chat.id, "Неправильное количество аргументов")
|
||||
|
||||
master_password_from_db = database.get.get_master_pass(engine, mes.from_user.id)
|
||||
if master_password_from_db is None:
|
||||
return _send_tmp_message(bot, mes.chat.id, "Нет мастер пароля")
|
||||
|
||||
master_password = data[1]
|
||||
enc_pass, salt = cryptography.master_pass.encrypt_master_pass(master_password)
|
||||
database.delete.purge_accounts(engine, mes.from_user.id)
|
||||
@ -140,3 +151,18 @@ def reset_master_pass(
|
||||
_send_tmp_message(
|
||||
bot, mes.chat.id, "Все ваши аккаунты удалены, а мастер пароль изменён"
|
||||
)
|
||||
|
||||
|
||||
def delete_account(
|
||||
bot: telebot.TeleBot, engine: Engine, mes: telebot.types.Message
|
||||
) -> None:
|
||||
data = shlex.split(mes.text)
|
||||
bot.delete_message(mes.chat.id, mes.id)
|
||||
|
||||
if len(data) != 2:
|
||||
return _send_tmp_message(bot, mes.chat.id, "Неправильное количество аргументов")
|
||||
|
||||
if data[1] not in database.get.get_accounts(engine, mes.from_user.id):
|
||||
return _send_tmp_message(bot, mes.chat.id, "Нет такого аккаунта")
|
||||
|
||||
database.delete.delete_account(engine, mes.from_user.id, data[1])
|
||||
|
@ -1,7 +1,5 @@
|
||||
import bcrypt
|
||||
|
||||
print("Hi")
|
||||
|
||||
|
||||
def encrypt_master_pass(passwd: str) -> tuple[bytes, bytes]:
|
||||
"""Hashes master password and return tuple of hashed password and salt"""
|
||||
|
@ -10,7 +10,7 @@ def change_master_pass(
|
||||
statement = sqlmodel.update(
|
||||
models.MasterPass,
|
||||
models.MasterPass.user_id == user_id,
|
||||
sqlmodel.values(salt=salt, passwd=passwd),
|
||||
{"salt": salt, "passwd": passwd},
|
||||
)
|
||||
with sqlmodel.Session(engine) as session:
|
||||
session.exec(statement)
|
||||
|
@ -16,3 +16,11 @@ def delete_master_pass(engine: Engine, user_id: int) -> None:
|
||||
)
|
||||
with sqlmodel.Session(engine) as session:
|
||||
session.exec(statement)
|
||||
|
||||
|
||||
def delete_account(engine: Engine, user_id: int, name: str) -> None:
|
||||
statement = sqlmodel.delete(models.Account).where(
|
||||
models.Account.user_id == user_id and models.Account.name == name
|
||||
)
|
||||
with sqlmodel.Session(engine) as session:
|
||||
session.exec(statement)
|
||||
|
@ -11,7 +11,6 @@ def get_master_pass(engine: Engine, user_id: int) -> tuple[bytes, bytes] | None:
|
||||
)
|
||||
with sqlmodel.Session(engine) as session:
|
||||
result = session.exec(statement).first()
|
||||
print(result)
|
||||
if result is None:
|
||||
return
|
||||
return (result.salt, result.passwd)
|
||||
|
@ -8,7 +8,6 @@ def get_engine(host: str, user: str, passwd: str, db: str) -> Engine:
|
||||
engine = sqlmodel.create_engine(
|
||||
f"mariadb+mariadbconnector://{user}:{passwd}@{host}/{db}"
|
||||
)
|
||||
print(type(engine))
|
||||
return engine
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user