update pwd gen for better security

This commit is contained in:
Charles 2025-12-10 12:04:10 +01:00
parent 76b3af7f2e
commit 07bd064faa
1 changed files with 10 additions and 11 deletions

View File

@ -16,6 +16,7 @@ use Doctrine\ORM\EntityNotFoundException;
use Exception; use Exception;
use League\Bundle\OAuth2ServerBundle\Model\AccessToken; use League\Bundle\OAuth2ServerBundle\Model\AccessToken;
use Random\RandomException; use Random\RandomException;
use RuntimeException;
use SebastianBergmann\CodeCoverage\Util\DirectoryCouldNotBeCreatedException; use SebastianBergmann\CodeCoverage\Util\DirectoryCouldNotBeCreatedException;
use Symfony\Bundle\SecurityBundle\Security; use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\File\Exception\FileException; use Symfony\Component\HttpFoundation\File\Exception\FileException;
@ -46,16 +47,7 @@ class UserService
*/ */
public function generateRandomPassword(): string public function generateRandomPassword(): string
{ {
$length = 50; // Length of the password return bin2hex(random_bytes(32));
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+';
$charactersLength = strlen($characters);
$randomPassword = '';
for ($i = 0; $i < $length; $i++) {
$randomPassword .= $characters[random_int(0, $charactersLength - 1)];
}
return $randomPassword;
} }
@ -478,7 +470,14 @@ class UserService
$user->setEmail(trim($user->getEmail())); $user->setEmail(trim($user->getEmail()));
if($setPassword) { if($setPassword) {
//FOR SETTING A DEFAULT RANDOM PASSWORD OF 50 CHARACTERS until user set his own password //FOR SETTING A DEFAULT RANDOM PASSWORD OF 50 CHARACTERS until user set his own password
$user->setPassword($this->generateRandomPassword()); try {
$user->setPassword(bin2hex(random_bytes(50)));
} catch (RandomException $e) {
$this->loggerService->logError('Error generating random password: ' . $e->getMessage(), [
'target_user_id' => $user->getId(),
]);
throw new RuntimeException('Error generating random password: ' . $e->getMessage());
}
} }
if($picture) { if($picture) {