Skip to content

Commit

Permalink
Merge branch release/v2.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Dec 6, 2024
1 parent bbd4f34 commit c6382a6
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 88 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['8.1', '8.2', '8.3']
php-version: ['8.2', '8.3']
steps:
- uses: shivammathur/setup-php@v2
with:
Expand All @@ -35,7 +35,5 @@ jobs:
${{ runner.os }}-php-${{ matrix.php-version }}-
- name: Install Dependencies
run: composer install --no-scripts --no-ansi --no-interaction --no-progress
- name: Run PHP Code Sniffer
run: vendor/bin/phpcs --extensions=php --warning-severity=0 --standard=PSR12 -p ./src
- name: Run PHPStan
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon
3 changes: 0 additions & 3 deletions Makefile

This file was deleted.

9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
}
],
"require": {
"php": ">=8.1",
"php": ">=8.2",
"psr/log": ">=1.1",
"ext-openssl": "*"
},
"require-dev": {
"phpstan/phpstan": "^1.5.3",
"squizlabs/php_codesniffer": "^3.5"
"phpstan/phpstan": "^1.5.3"
},
"autoload": {
"psr-4": {
Expand All @@ -27,8 +26,8 @@
},
"extra": {
"branch-alias": {
"dev-main": "2.3.x-dev",
"dev-develop": "2.4.x-dev"
"dev-main": "2.4.x-dev",
"dev-develop": "2.5.x-dev"
}
}
}
14 changes: 0 additions & 14 deletions phpcs.xml.dist

This file was deleted.

15 changes: 5 additions & 10 deletions src/PasswordGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +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.
*
* @param int $length
* @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 @@ -35,12 +32,10 @@ public function generatePassword(int $length = 12)
}

$all = \mb_str_split($all);
for ($i = 0; $i < $length - count($sets); $i++) {
for ($i = 0; $i < $length - count($sets); ++$i) {
$password .= $all[array_rand($all)];
}

$password = str_shuffle($password);

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

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

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

/**
* @param LoggerInterface|null $logger
*/
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.');
}
}

/**
* @param int $nbBytes
* @return string
*/
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 c6382a6

Please sign in to comment.