From c6382a6fe0c7e0e7d1bcb502fdcedbf3042f0a8a Mon Sep 17 00:00:00 2001 From: roadiz-ci Date: Fri, 6 Dec 2024 08:57:45 +0000 Subject: [PATCH] Merge branch release/v2.4.0 --- .github/workflows/run-test.yml | 4 +-- Makefile | 3 --- composer.json | 9 +++---- phpcs.xml.dist | 14 ----------- src/PasswordGenerator.php | 15 ++++-------- src/PasswordGeneratorInterface.php | 6 +---- src/RandomGenerator.php | 39 ++++++------------------------ src/SaltGenerator.php | 5 +--- src/SaltGeneratorInterface.php | 5 +--- src/TokenGenerator.php | 5 +--- src/TokenGeneratorInterface.php | 5 +--- 11 files changed, 22 insertions(+), 88 deletions(-) delete mode 100644 Makefile delete mode 100644 phpcs.xml.dist diff --git a/.github/workflows/run-test.yml b/.github/workflows/run-test.yml index d872a3a..2d69d18 100644 --- a/.github/workflows/run-test.yml +++ b/.github/workflows/run-test.yml @@ -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: @@ -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 diff --git a/Makefile b/Makefile deleted file mode 100644 index 5f4d6d2..0000000 --- a/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -test: - vendor/bin/phpcs --report=full --report-file=./report.txt -p ./src - vendor/bin/phpstan analyse -c phpstan.neon diff --git a/composer.json b/composer.json index e871618..5778d10 100644 --- a/composer.json +++ b/composer.json @@ -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": { @@ -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" } } } diff --git a/phpcs.xml.dist b/phpcs.xml.dist deleted file mode 100644 index da4bfdb..0000000 --- a/phpcs.xml.dist +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - ./src - diff --git a/src/PasswordGenerator.php b/src/PasswordGenerator.php index ef24af0..605e1a9 100644 --- a/src/PasswordGenerator.php +++ b/src/PasswordGenerator.php @@ -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 = ''; @@ -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); } } diff --git a/src/PasswordGeneratorInterface.php b/src/PasswordGeneratorInterface.php index 89ebbd5..88bbc53 100644 --- a/src/PasswordGeneratorInterface.php +++ b/src/PasswordGeneratorInterface.php @@ -6,9 +6,5 @@ interface PasswordGeneratorInterface { - /** - * @param int $length - * @return string - */ - public function generatePassword(int $length = 12); + public function generatePassword(int $length = 16): string; } diff --git a/src/RandomGenerator.php b/src/RandomGenerator.php index 0efe77e..7257851 100644 --- a/src/RandomGenerator.php +++ b/src/RandomGenerator.php @@ -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.'); } } diff --git a/src/SaltGenerator.php b/src/SaltGenerator.php index 55dd79d..be00380 100644 --- a/src/SaltGenerator.php +++ b/src/SaltGenerator.php @@ -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)), '{}', '-_'); } diff --git a/src/SaltGeneratorInterface.php b/src/SaltGeneratorInterface.php index 52f3b97..b117ef2 100644 --- a/src/SaltGeneratorInterface.php +++ b/src/SaltGeneratorInterface.php @@ -6,8 +6,5 @@ interface SaltGeneratorInterface { - /** - * @return string - */ - public function generateSalt(); + public function generateSalt(): string; } diff --git a/src/TokenGenerator.php b/src/TokenGenerator.php index 98f51e4..8535004 100644 --- a/src/TokenGenerator.php +++ b/src/TokenGenerator.php @@ -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()), '+/', '-_'), '='); } diff --git a/src/TokenGeneratorInterface.php b/src/TokenGeneratorInterface.php index 6e9c53f..6e5e278 100644 --- a/src/TokenGeneratorInterface.php +++ b/src/TokenGeneratorInterface.php @@ -6,8 +6,5 @@ interface TokenGeneratorInterface { - /** - * @return string - */ - public function generateToken(); + public function generateToken(): string; }