Add password strength check (#983)
* Add password strength check * Delete time.sleep and log, use the select class instead * Ignore W503 in flake8 * Solve some problems of the password strength check algorithm Part of the code was written by @phisch. This code tries to measure the strength of the user's password and suggests that the password is weak. * rename function * Restore the flake8 configuration file to its previous state
This commit is contained in:
parent
f06aabb4d4
commit
7e19bf6e2e
2
.flake8
2
.flake8
|
|
@ -7,4 +7,4 @@ max-line-length = 236
|
|||
show-source = True
|
||||
statistics = True
|
||||
per-file-ignores = __init__.py:F401,F403,F405 simple_menu.py:C901,W503 guided.py:C901
|
||||
builtins = _
|
||||
builtins = _
|
||||
|
|
@ -99,20 +99,46 @@ def do_countdown() -> bool:
|
|||
|
||||
return True
|
||||
|
||||
def check_password_strong(passwd :str) -> bool:
|
||||
|
||||
symbol_count = 0
|
||||
if any(character.isdigit() for character in passwd):
|
||||
symbol_count += 10
|
||||
if any(character.isupper() for character in passwd):
|
||||
symbol_count += 26
|
||||
if any(character.islower() for character in passwd):
|
||||
symbol_count += 26
|
||||
if any(not character.isalnum() for character in passwd):
|
||||
symbol_count += 40
|
||||
|
||||
if symbol_count ** len(passwd) < 10e20:
|
||||
|
||||
prompt = _("The password you are using seems to be weak,")
|
||||
prompt += _("are you sure you want to use it?")
|
||||
|
||||
choice = Menu(prompt, ["yes", "no"], default_option="yes").run()
|
||||
return choice == "yes"
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def get_password(prompt :str = '') -> Optional[str]:
|
||||
if not prompt:
|
||||
prompt = _("Enter a password: ")
|
||||
|
||||
while passwd := getpass.getpass(prompt):
|
||||
|
||||
if len(passwd.strip()) <= 0:
|
||||
break
|
||||
|
||||
if not check_password_strong(passwd):
|
||||
continue
|
||||
|
||||
passwd_verification = getpass.getpass(prompt=_('And one more time for verification: '))
|
||||
if passwd != passwd_verification:
|
||||
log(' * Passwords did not match * ', fg='red')
|
||||
continue
|
||||
|
||||
if len(passwd.strip()) <= 0:
|
||||
break
|
||||
|
||||
return passwd
|
||||
return None
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue