Skip to content

Commit

Permalink
chore: Bumped
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Feb 25, 2025
1 parent c4b1e3f commit a604cb6
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 22 deletions.
4 changes: 3 additions & 1 deletion .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.2', '8.3']
php-version: ['8.1', '8.2', '8.3']
steps:
- uses: shivammathur/setup-php@v2
with:
Expand All @@ -35,5 +35,7 @@ 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: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test:
vendor/bin/phpcs --report=full --report-file=./report.txt -p ./src
vendor/bin/phpstan analyse -c phpstan.neon
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
}
],
"require": {
"php": ">=8.2",
"php": ">=8.1",
"psr/log": ">=1.1",
"ext-openssl": "*"
},
"require-dev": {
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2"
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
"psr-4": {
Expand All @@ -27,8 +27,8 @@
},
"extra": {
"branch-alias": {
"dev-main": "2.4.x-dev",
"dev-develop": "2.5.x-dev"
"dev-main": "2.3.x-dev",
"dev-develop": "2.4.x-dev"
}
}
}
14 changes: 14 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">

<arg name="basepath" value="."/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>
<arg name="extensions" value="php"/>

<rule ref="PSR12">
<exclude name="Generic.Files.LineLength"/>
</rule>
<file>./src</file>
</ruleset>
15 changes: 10 additions & 5 deletions src/PasswordGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ 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 = 16): string
public function generatePassword(int $length = 12)
{
$sets = [];
$sets[] = 'abcdefghjkmnpqrstuvwxyz';
$sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ';
$sets[] = '23456789';
$sets[] = '!@#$%&*?-';
$sets[] = '!@#$%&*?';

$all = '';
$password = '';
Expand All @@ -32,10 +35,12 @@ public function generatePassword(int $length = 16): string
}

$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)];
}

return str_shuffle($password);
$password = str_shuffle($password);

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

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

class RandomGenerator
{
public function __construct(protected readonly LoggerInterface $logger)
protected ?LoggerInterface $logger;
protected bool $useOpenSsl;

/**
* @param LoggerInterface|null $logger
*/
public function __construct(LoggerInterface $logger = null)
{
if (!function_exists('openssl_random_pseudo_bytes')) {
throw new \RuntimeException('You must enable the "openssl" extension for secure random number generation.');
$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;
}
}

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

if (false !== $bytes && true === $strong) {
return $bytes;
}

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

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

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

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

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

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

0 comments on commit a604cb6

Please sign in to comment.