Skip to content

Commit e94a530

Browse files
committed
šŸ”’(backend) respect keycloak password policy when generating passwords
1 parent 7a0f6fb commit e94a530

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

ā€Žsrc/backend/core/services/identity/keycloak.pyā€Ž

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,25 +307,29 @@ def resync_all_mailboxes_to_keycloak():
307307

308308

309309
def generate_password(length=12):
310-
"""Generate a secure random password with at least one uppercase, one lowercase, and one digit."""
311-
if length < 3:
310+
"""
311+
Generate a secure random password with at least one uppercase, one lowercase, one digit, and one special character.
312+
"""
313+
if length < 12:
312314
raise ValueError(
313-
"Password length must be at least 3 to satisfy all requirements."
315+
"Password length must be at least 12 to satisfy all requirements."
314316
)
315317

316318
_upper = "ABCDEFGHJKLMNPQRTUVWXYZ"
317319
_lower = "abcdefghijkmnopqrstuvwxyz"
318320
_digits = "2346789"
321+
_special = "!@#$%&*?"
319322

320323
# Ensure at least one of each required character type
321324
password_chars = [
322325
secrets.choice(_upper),
323326
secrets.choice(_lower),
324327
secrets.choice(_digits),
328+
secrets.choice(_special),
325329
]
326330
# Fill the rest of the password length with random choices
327331
password_chars += [
328-
secrets.choice(_upper + _lower + _digits) for _ in range(length - 3)
332+
secrets.choice(_upper + _lower + _digits + _special) for _ in range(length - 4)
329333
]
330334
# Shuffle to avoid predictable positions
331335
secrets.SystemRandom().shuffle(password_chars)

0 commit comments

Comments
Ā (0)
⚔