Compare commits
No commits in common. "21bd01c3edcb25a8c158a8e5951185860433324e" and "a1bed9014d6fc680b1604fdf9804c748b01c8774" have entirely different histories.
21bd01c3ed
...
a1bed9014d
@ -9,10 +9,10 @@ from .. import cryptography, database
|
|||||||
from .utils import (
|
from .utils import (
|
||||||
accounts_to_json,
|
accounts_to_json,
|
||||||
base_handler,
|
base_handler,
|
||||||
check_account,
|
|
||||||
check_account_name,
|
check_account_name,
|
||||||
check_login,
|
check_login,
|
||||||
check_passwd,
|
check_passwd,
|
||||||
|
check_account,
|
||||||
get_all_accounts,
|
get_all_accounts,
|
||||||
json_to_accounts,
|
json_to_accounts,
|
||||||
send_tmp_message,
|
send_tmp_message,
|
||||||
@ -225,7 +225,7 @@ def _add_account5(
|
|||||||
name, login, passwd = data["name"], data["login"], data["passwd"]
|
name, login, passwd = data["name"], data["login"], data["passwd"]
|
||||||
|
|
||||||
enc_login, enc_pass, salt = cryptography.other_accounts.encrypt_account_info(
|
enc_login, enc_pass, salt = cryptography.other_accounts.encrypt_account_info(
|
||||||
login, passwd, text
|
login, passwd, text.encode("utf-8")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = database.add.add_account(
|
result = database.add.add_account(
|
||||||
@ -288,7 +288,7 @@ def _get_account3(
|
|||||||
engine, mes.from_user.id, name
|
engine, mes.from_user.id, name
|
||||||
)
|
)
|
||||||
login, passwd = cryptography.other_accounts.decrypt_account_info(
|
login, passwd = cryptography.other_accounts.decrypt_account_info(
|
||||||
enc_login, enc_pass, text, salt
|
enc_login, enc_pass, text.encode("utf-8"), salt
|
||||||
)
|
)
|
||||||
send_tmp_message(
|
send_tmp_message(
|
||||||
bot,
|
bot,
|
||||||
@ -456,7 +456,7 @@ def _import3(
|
|||||||
failed.append(name)
|
failed.append(name)
|
||||||
continue
|
continue
|
||||||
enc_login, enc_passwd, salt = cryptography.other_accounts.encrypt_account_info(
|
enc_login, enc_passwd, salt = cryptography.other_accounts.encrypt_account_info(
|
||||||
login, passwd, text
|
login, passwd, text.encode("utf-8")
|
||||||
)
|
)
|
||||||
result = database.add.add_account(
|
result = database.add.add_account(
|
||||||
engine, mes.from_user.id, name, salt, enc_login, enc_passwd
|
engine, mes.from_user.id, name, salt, enc_login, enc_passwd
|
||||||
|
@ -59,6 +59,7 @@ def get_all_accounts(
|
|||||||
engine: Engine, user_id: int, master_pass: str
|
engine: Engine, user_id: int, master_pass: str
|
||||||
) -> list[tuple[str, str, str]]:
|
) -> list[tuple[str, str, str]]:
|
||||||
accounts: list[tuple[str, str, str]] = []
|
accounts: list[tuple[str, str, str]] = []
|
||||||
|
master_pass = master_pass.encode("utf-8")
|
||||||
for account_name in database.get.get_accounts(engine, user_id):
|
for account_name in database.get.get_accounts(engine, user_id):
|
||||||
salt, enc_login, enc_passwd = database.get.get_account_info(
|
salt, enc_login, enc_passwd = database.get.get_account_info(
|
||||||
engine, user_id, account_name
|
engine, user_id, account_name
|
||||||
|
@ -3,14 +3,14 @@ import os
|
|||||||
from cryptography.exceptions import InvalidKey
|
from cryptography.exceptions import InvalidKey
|
||||||
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
|
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
|
||||||
|
|
||||||
MEMORY_USAGE = 2**14
|
_memory_use = 2**14
|
||||||
|
|
||||||
|
|
||||||
def _get_kdf(salt: bytes) -> Scrypt:
|
def _get_kdf(salt: bytes) -> Scrypt:
|
||||||
kdf = Scrypt(
|
kdf = Scrypt(
|
||||||
salt=salt,
|
salt=salt,
|
||||||
length=128,
|
length=128,
|
||||||
n=MEMORY_USAGE,
|
n=_memory_use,
|
||||||
r=8,
|
r=8,
|
||||||
p=1,
|
p=1,
|
||||||
)
|
)
|
||||||
|
@ -20,12 +20,12 @@ def _generate_key(salt: bytes, master_pass: bytes) -> bytes:
|
|||||||
|
|
||||||
|
|
||||||
def encrypt_account_info(
|
def encrypt_account_info(
|
||||||
login: str, passwd: str, master_pass: str
|
login: str, passwd: str, master_pass: bytes
|
||||||
) -> tuple[bytes, bytes, bytes]:
|
) -> tuple[bytes, bytes, bytes]:
|
||||||
"""Encrypts login and password of a user using their master password as a key.
|
"""Encrypts login and password of a user using their master password as a key.
|
||||||
Returns a tuple of encrypted login, password and salt"""
|
Returns a tuple of encrypted login, password and salt"""
|
||||||
salt = os.urandom(64)
|
salt = os.urandom(64)
|
||||||
key = _generate_key(salt, master_pass.encode("utf-8"))
|
key = _generate_key(salt, master_pass)
|
||||||
f = Fernet(key)
|
f = Fernet(key)
|
||||||
enc_login = f.encrypt(login.encode("utf-8"))
|
enc_login = f.encrypt(login.encode("utf-8"))
|
||||||
enc_passwd = f.encrypt(passwd.encode("utf-8"))
|
enc_passwd = f.encrypt(passwd.encode("utf-8"))
|
||||||
@ -33,11 +33,11 @@ def encrypt_account_info(
|
|||||||
|
|
||||||
|
|
||||||
def decrypt_account_info(
|
def decrypt_account_info(
|
||||||
enc_login: bytes, enc_pass: bytes, master_pass: str, salt: bytes
|
enc_login: bytes, enc_pass: bytes, master_pass: bytes, salt: bytes
|
||||||
) -> tuple[str, str]:
|
) -> tuple[str, str]:
|
||||||
"""Decrypts login and password using their master password as a key.
|
"""Decrypts login and password using their master password as a key.
|
||||||
Returns a tuple of decrypted login and password"""
|
Returns a tuple of decrypted login and password"""
|
||||||
key = _generate_key(salt, master_pass.encode("utf-8"))
|
key = _generate_key(salt, master_pass)
|
||||||
f = Fernet(key)
|
f = Fernet(key)
|
||||||
login_bytes = f.decrypt(enc_login)
|
login_bytes = f.decrypt(enc_login)
|
||||||
pass_bytes = f.decrypt(enc_pass)
|
pass_bytes = f.decrypt(enc_pass)
|
||||||
|
@ -13,7 +13,7 @@ def add_account(
|
|||||||
enc_login: bytes,
|
enc_login: bytes,
|
||||||
enc_pass: bytes,
|
enc_pass: bytes,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""Adds account to the database. Returns true on success, false otherwise"""
|
"""Adds account to db. Returns true, if on success"""
|
||||||
account = models.Account(
|
account = models.Account(
|
||||||
user_id=user_id, name=name, salt=salt, enc_login=enc_login, enc_pass=enc_pass
|
user_id=user_id, name=name, salt=salt, enc_login=enc_login, enc_pass=enc_pass
|
||||||
)
|
)
|
||||||
@ -28,7 +28,7 @@ def add_account(
|
|||||||
|
|
||||||
|
|
||||||
def add_master_pass(engine: Engine, user_id: int, salt: bytes, passwd: bytes) -> bool:
|
def add_master_pass(engine: Engine, user_id: int, salt: bytes, passwd: bytes) -> bool:
|
||||||
"""Adds master password the database. Returns true on success, false otherwise"""
|
"""Adds master password to db. Returns true, if on success"""
|
||||||
master_pass = models.MasterPass(user_id=user_id, salt=salt, passwd=passwd)
|
master_pass = models.MasterPass(user_id=user_id, salt=salt, passwd=passwd)
|
||||||
try:
|
try:
|
||||||
with sqlmodel.Session(engine) as session:
|
with sqlmodel.Session(engine) as session:
|
||||||
|
@ -7,7 +7,6 @@ from . import models
|
|||||||
def change_master_pass(
|
def change_master_pass(
|
||||||
engine: Engine, user_id: int, salt: bytes, passwd: bytes
|
engine: Engine, user_id: int, salt: bytes, passwd: bytes
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Changes master password and salt in the database"""
|
|
||||||
statement = (
|
statement = (
|
||||||
sqlmodel.update(models.MasterPass)
|
sqlmodel.update(models.MasterPass)
|
||||||
.where(models.MasterPass.user_id == user_id)
|
.where(models.MasterPass.user_id == user_id)
|
||||||
|
@ -5,7 +5,6 @@ from . import models
|
|||||||
|
|
||||||
|
|
||||||
def purge_accounts(engine: Engine, user_id: int) -> None:
|
def purge_accounts(engine: Engine, user_id: int) -> None:
|
||||||
"""Deletes all user's accounts"""
|
|
||||||
statement = sqlmodel.delete(models.Account).where(models.Account.user_id == user_id)
|
statement = sqlmodel.delete(models.Account).where(models.Account.user_id == user_id)
|
||||||
with sqlmodel.Session(engine) as session:
|
with sqlmodel.Session(engine) as session:
|
||||||
session.exec(statement)
|
session.exec(statement)
|
||||||
@ -13,7 +12,6 @@ def purge_accounts(engine: Engine, user_id: int) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def delete_master_pass(engine: Engine, user_id: int) -> None:
|
def delete_master_pass(engine: Engine, user_id: int) -> None:
|
||||||
"""Delets master password of the user"""
|
|
||||||
statement = sqlmodel.delete(models.MasterPass).where(
|
statement = sqlmodel.delete(models.MasterPass).where(
|
||||||
models.MasterPass.user_id == user_id
|
models.MasterPass.user_id == user_id
|
||||||
)
|
)
|
||||||
@ -23,7 +21,6 @@ def delete_master_pass(engine: Engine, user_id: int) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def delete_account(engine: Engine, user_id: int, name: str) -> None:
|
def delete_account(engine: Engine, user_id: int, name: str) -> None:
|
||||||
"""Deletes specific user account"""
|
|
||||||
statement = sqlmodel.delete(models.Account).where(
|
statement = sqlmodel.delete(models.Account).where(
|
||||||
models.Account.user_id == user_id, models.Account.name == name
|
models.Account.user_id == user_id, models.Account.name == name
|
||||||
)
|
)
|
||||||
|
@ -5,8 +5,7 @@ from . import models
|
|||||||
|
|
||||||
|
|
||||||
def get_master_pass(engine: Engine, user_id: int) -> tuple[bytes, bytes] | None:
|
def get_master_pass(engine: Engine, user_id: int) -> tuple[bytes, bytes] | None:
|
||||||
"""Gets master pass. Returns tuple of salt and password
|
"""Gets master pass. Returns tuple of salt and password"""
|
||||||
or None if it wasn't found"""
|
|
||||||
statement = sqlmodel.select(models.MasterPass).where(
|
statement = sqlmodel.select(models.MasterPass).where(
|
||||||
models.MasterPass.user_id == user_id
|
models.MasterPass.user_id == user_id
|
||||||
)
|
)
|
||||||
@ -28,8 +27,7 @@ def get_accounts(engine: Engine, user_id: int) -> list[str]:
|
|||||||
def get_account_info(
|
def get_account_info(
|
||||||
engine: Engine, user_id: int, name: str
|
engine: Engine, user_id: int, name: str
|
||||||
) -> tuple[bytes, bytes, bytes]:
|
) -> tuple[bytes, bytes, bytes]:
|
||||||
"""Gets account info. Returns tuple of salt, login and password
|
"""Gets account info. Returns tuple of salt, login and password"""
|
||||||
or None if it wasn't found"""
|
|
||||||
statement = sqlmodel.select(models.Account).where(
|
statement = sqlmodel.select(models.Account).where(
|
||||||
models.Account.user_id == user_id, models.Account.name == name
|
models.Account.user_id == user_id, models.Account.name == name
|
||||||
)
|
)
|
||||||
|
@ -5,11 +5,9 @@ from . import models
|
|||||||
|
|
||||||
|
|
||||||
def get_engine(host: str, user: str, passwd: str, db: str) -> Engine:
|
def get_engine(host: str, user: str, passwd: str, db: str) -> Engine:
|
||||||
"""Creates an engine for mariadb with pymysql as connector"""
|
|
||||||
engine = sqlmodel.create_engine(f"mariadb+pymysql://{user}:{passwd}@{host}/{db}")
|
engine = sqlmodel.create_engine(f"mariadb+pymysql://{user}:{passwd}@{host}/{db}")
|
||||||
return engine
|
return engine
|
||||||
|
|
||||||
|
|
||||||
def prepare(engine: Engine) -> None:
|
def prepare(engine: Engine) -> None:
|
||||||
"""Creates all tables, indexes and constrains in the database"""
|
|
||||||
sqlmodel.SQLModel.metadata.create_all(engine)
|
sqlmodel.SQLModel.metadata.create_all(engine)
|
||||||
|
Reference in New Issue
Block a user