Skip to content

Commit

Permalink
refactor: Removed base RozierApp class when possible, added LogTrail …
Browse files Browse the repository at this point in the history
…service for publishing message in session and logger
  • Loading branch information
roadiz-ci committed Dec 3, 2024
1 parent f55afa2 commit e0b9c17
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 53 deletions.
12 changes: 4 additions & 8 deletions src/PasswordGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ class PasswordGenerator extends RandomGenerator implements PasswordGeneratorInte
* one uppercase letter, one digit, and one special character. The remaining characters
* in the password are chosen at random from those four sets.
*
* The available characters in each set are user friendly - there are no ambiguous
* The available characters in each set are user-friendly - there are no ambiguous
* characters such as i, l, 1, o, 0, etc.
*
* @return string
*
* @see https://gist.github.com/tylerhall/521810
*/
public function generatePassword(int $length = 12)
public function generatePassword(int $length = 16): string
{
$sets = [];
$sets[] = 'abcdefghjkmnpqrstuvwxyz';
$sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ';
$sets[] = '23456789';
$sets[] = '!@#$%&*?';
$sets[] = '!@#$%&*?-';

$all = '';
$password = '';
Expand All @@ -38,8 +36,6 @@ public function generatePassword(int $length = 12)
$password .= $all[array_rand($all)];
}

$password = str_shuffle($password);

return $password;
return str_shuffle($password);
}
}
5 changes: 1 addition & 4 deletions src/PasswordGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@

interface PasswordGeneratorInterface
{
/**
* @return string
*/
public function generatePassword(int $length = 12);
public function generatePassword(int $length = 16): string;
}
32 changes: 7 additions & 25 deletions src/RandomGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,22 @@

class RandomGenerator
{
protected ?LoggerInterface $logger;
protected bool $useOpenSsl;

public function __construct(?LoggerInterface $logger = null)
public function __construct(protected readonly LoggerInterface $logger)
{
$this->logger = $logger;
// determine whether to use OpenSSL
if (defined('PHP_WINDOWS_VERSION_BUILD') && version_compare(PHP_VERSION, '5.3.4', '<')) {
$this->useOpenSsl = false;
} elseif (!function_exists('openssl_random_pseudo_bytes')) {
if (null !== $this->logger) {
$this->logger->notice('It is recommended that you enable the "openssl" extension for random number generation.');
}
$this->useOpenSsl = false;
} else {
$this->useOpenSsl = true;
if (!function_exists('openssl_random_pseudo_bytes')) {
throw new \RuntimeException('You must enable the "openssl" extension for secure random number generation.');
}
}

public function getRandomNumber(int $nbBytes = 32): string
{
// try OpenSSL
if ($this->useOpenSsl) {
$bytes = \openssl_random_pseudo_bytes($nbBytes, $strong);

if (false !== $bytes && true === $strong) {
return $bytes;
}
$bytes = \openssl_random_pseudo_bytes($nbBytes, $strong);

if (null !== $this->logger) {
$this->logger->info('OpenSSL did not produce a secure random number.');
}
if (false !== $bytes && true === $strong) {
return $bytes;
}

return hash('sha256', uniqid((string) mt_rand(), true), true);
throw new \RuntimeException('Unable to generate a secure random number.');
}
}
5 changes: 1 addition & 4 deletions src/SaltGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

class SaltGenerator extends RandomGenerator implements SaltGeneratorInterface
{
/**
* @return string
*/
public function generateSalt()
public function generateSalt(): string
{
return strtr(base64_encode($this->getRandomNumber(24)), '{}', '-_');
}
Expand Down
5 changes: 1 addition & 4 deletions src/SaltGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@

interface SaltGeneratorInterface
{
/**
* @return string
*/
public function generateSalt();
public function generateSalt(): string;
}
5 changes: 1 addition & 4 deletions src/TokenGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

class TokenGenerator extends RandomGenerator implements TokenGeneratorInterface
{
/**
* @return string
*/
public function generateToken()
public function generateToken(): string
{
return rtrim(strtr(base64_encode($this->getRandomNumber()), '+/', '-_'), '=');
}
Expand Down
5 changes: 1 addition & 4 deletions src/TokenGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@

interface TokenGeneratorInterface
{
/**
* @return string
*/
public function generateToken();
public function generateToken(): string;
}

0 comments on commit e0b9c17

Please sign in to comment.