From d68a7bb6e86ca93cbbf7964d7bf00849c675df92 Mon Sep 17 00:00:00 2001 From: StNicolay Date: Tue, 15 Nov 2022 15:39:38 +0300 Subject: [PATCH] Moved check of generated password into separate function --- src/bot/utils.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/bot/utils.py b/src/bot/utils.py index b1db49f..b958a10 100644 --- a/src/bot/utils.py +++ b/src/bot/utils.py @@ -109,17 +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) +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: """Generates password of length 32""" choices = SystemRandom().choices while True: passwd = "".join(choices(PASSWORD_CHARS, k=32)) - # If there is at least one lowercase character, uppercase character - # 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) - ): + if _check_gened_password(passwd): return passwd