|
|
@ -11,6 +11,15 @@ from sqlalchemy.future import Engine
|
|
|
|
from .. import cryptography, database
|
|
|
|
from .. import cryptography, database
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FORBIDDEN_CHARS = frozenset("`\n")
|
|
|
|
|
|
|
|
PUNCTUATION = frozenset(string.punctuation).difference(FORBIDDEN_CHARS)
|
|
|
|
|
|
|
|
PASSWORD_CHARS = tuple(
|
|
|
|
|
|
|
|
frozenset(string.ascii_letters + string.digits).difference(FORBIDDEN_CHARS)
|
|
|
|
|
|
|
|
| PUNCTUATION
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
Message = telebot.types.Message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _Account(pydantic.BaseModel):
|
|
|
|
class _Account(pydantic.BaseModel):
|
|
|
|
name: str
|
|
|
|
name: str
|
|
|
|
login: str
|
|
|
|
login: str
|
|
|
@ -38,9 +47,6 @@ def json_to_accounts(json_: str) -> list[tuple[str, str, str]]:
|
|
|
|
return [i.as_tuple() for i in accounts.accounts]
|
|
|
|
return [i.as_tuple() for i in accounts.accounts]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Message = telebot.types.Message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_tmp_message(
|
|
|
|
def send_tmp_message(
|
|
|
|
bot: telebot.TeleBot, chat_id: telebot.types.Message, text: str, timeout: int = 5
|
|
|
|
bot: telebot.TeleBot, chat_id: telebot.types.Message, text: str, timeout: int = 5
|
|
|
|
) -> None:
|
|
|
|
) -> None:
|
|
|
@ -78,9 +84,9 @@ def accounts_to_json(accounts: list[tuple[str, str, str]]) -> io.StringIO:
|
|
|
|
return file
|
|
|
|
return file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _base_check(val: str) -> bool:
|
|
|
|
def _base_check(val: str, /) -> bool:
|
|
|
|
"Returns false if finds new lines or backtick (`)"
|
|
|
|
"Returns false if finds new lines or backtick (`)"
|
|
|
|
return not ("\n" in val or "`" in val)
|
|
|
|
return not any(i in FORBIDDEN_CHARS for i in val)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_account_name(name: str) -> bool:
|
|
|
|
def check_account_name(name: str) -> bool:
|
|
|
@ -103,20 +109,23 @@ def check_account(name: str, login: str, passwd: str) -> bool:
|
|
|
|
return check_account_name(name) and check_login(login) and check_passwd(passwd)
|
|
|
|
return check_account_name(name) and check_login(login) and check_passwd(passwd)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _check_gened_password(passwd: str, /) -> bool:
|
|
|
|
|
|
|
|
"""Retuns true if generated password is valid,
|
|
|
|
|
|
|
|
false otherwise.
|
|
|
|
|
|
|
|
Password is valid if there is at least one lowercase character,
|
|
|
|
|
|
|
|
uppercase character and one punctuation character"""
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
|
|
any(c.islower() for c in passwd)
|
|
|
|
|
|
|
|
and any(c.isupper() for c in passwd)
|
|
|
|
|
|
|
|
and any(c.isdigit() for c in passwd)
|
|
|
|
|
|
|
|
and any(c in PUNCTUATION for c in passwd)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def gen_passwd() -> str:
|
|
|
|
def gen_passwd() -> str:
|
|
|
|
"""Generates password of length 32"""
|
|
|
|
"""Generates password of length 32"""
|
|
|
|
choices = SystemRandom().choices
|
|
|
|
choices = SystemRandom().choices
|
|
|
|
# Remove backtick and pipe from pucntuation
|
|
|
|
|
|
|
|
punctuation = set(string.punctuation).difference("`|")
|
|
|
|
|
|
|
|
chars = tuple(string.ascii_letters + string.digits + "".join(punctuation))
|
|
|
|
|
|
|
|
while True:
|
|
|
|
while True:
|
|
|
|
passwd = "".join(choices(chars, k=32))
|
|
|
|
passwd = "".join(choices(PASSWORD_CHARS, k=32))
|
|
|
|
# If there is at least one lowercase character, uppercase character
|
|
|
|
if _check_gened_password(passwd):
|
|
|
|
# and one punctuation character
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
|
|
any(c.islower() for c in passwd)
|
|
|
|
|
|
|
|
and any(c.isupper() for c in passwd)
|
|
|
|
|
|
|
|
and any(c.isdigit() for c in passwd)
|
|
|
|
|
|
|
|
and any(c in punctuation for c in passwd)
|
|
|
|
|
|
|
|
):
|
|
|
|
|
|
|
|
return passwd
|
|
|
|
return passwd
|
|
|
|