fix(password_generation): replace pwgen with secrets module for secure password generation
This commit is contained in:
@ -6,6 +6,8 @@ import json
|
|||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
from dotenv import dotenv_values
|
from dotenv import dotenv_values
|
||||||
import re
|
import re
|
||||||
|
import secrets
|
||||||
|
import string
|
||||||
|
|
||||||
import traffic
|
import traffic
|
||||||
|
|
||||||
@ -121,16 +123,13 @@ def run_cmd(command: list[str]) -> str:
|
|||||||
|
|
||||||
def generate_password() -> str:
|
def generate_password() -> str:
|
||||||
'''
|
'''
|
||||||
Generates a random password using pwgen for user.
|
Generates a secure, random alphanumeric password.
|
||||||
Could raise subprocess.CalledProcessError
|
|
||||||
'''
|
'''
|
||||||
try:
|
try:
|
||||||
return subprocess.check_output(['pwgen', '-s', '32', '1'], shell=False).decode().strip()
|
alphabet = string.ascii_letters + string.digits
|
||||||
except (subprocess.CalledProcessError, FileNotFoundError):
|
return ''.join(secrets.choice(alphabet) for _ in range(32))
|
||||||
try:
|
|
||||||
return subprocess.check_output(['cat', '/proc/sys/kernel/random/uuid'], shell=False).decode().strip()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise PasswordGenerationError(f"Failed to generate password: {e}")
|
raise PasswordGenerationError(f"Failed to generate password using secrets module: {e}")
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user