modify random password generator function make_password() to always include at least one of the possible string groups (ascii_letters, digits, special characters etc.)

This commit is contained in:
rdzantoine.pro@gmail.com
2025-06-23 10:03:06 +02:00
committed by Antoine Nguyen
parent f2697b6c92
commit 3c9cc5a42e
+18 -4
View File
@@ -294,13 +294,27 @@ def make_password():
"""Create a random password."""
length = int(param_tools.get_global_parameter("random_password_length", app="core"))
allow_special_characters = bool(param_tools.get_global_parameter("allow_special_characters", app="core"))
possible_chars = string.ascii_letters + string.digits
possible_chars_types = [string.ascii_letters, string.digits]
all_possible_chars = ''
certain_chars = ''
for i in range(10): # limit tries
if allow_special_characters: # add special characters in random passwd
possible_chars += string.punctuation
possible_chars_types.append(string.punctuation)
# make it so there is always one character of each type in password
# and then will randomize their placement
for possible_chars in possible_chars_types:
all_possible_chars += possible_chars
certain_chars += random.SystemRandom().choice(possible_chars)
password = "".join(
random.SystemRandom().choice(possible_chars) for _ in range(length)
)
random.SystemRandom().choice(all_possible_chars) \
for _ in range(length - len(possible_chars_types))
) + certain_chars
li_password = list(password)
random.shuffle(li_password)
password = "".join(li_password)
try:
password_validation.validate_password(password)
except ValidationError: