Skip to content

Commit

Permalink
Merge branch '4.4' into 5.1
Browse files Browse the repository at this point in the history
* 4.4:
  Enable "native_constant_invocation" CS rule
  Make AbstractPhpFileCacheWarmer public
  • Loading branch information
nicolas-grekas committed Sep 2, 2020
2 parents 0b965da + 637ba4c commit ae0d567
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions Encoder/EncoderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private function getEncoderConfigFromAlgorithm(array $config): array

case 'bcrypt':
$config['algorithm'] = 'native';
$config['native_algorithm'] = PASSWORD_BCRYPT;
$config['native_algorithm'] = \PASSWORD_BCRYPT;

return $this->getEncoderConfigFromAlgorithm($config);

Expand Down Expand Up @@ -181,7 +181,7 @@ private function getEncoderConfigFromAlgorithm(array $config): array
$config['algorithm'] = 'sodium';
} elseif (\defined('PASSWORD_ARGON2I')) {
$config['algorithm'] = 'native';
$config['native_algorithm'] = PASSWORD_ARGON2I;
$config['native_algorithm'] = \PASSWORD_ARGON2I;
} else {
throw new LogicException(sprintf('Algorithm "argon2i" is not available. Either use %s"auto" or upgrade to PHP 7.2+ instead.', \defined('SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13') ? '"argon2id", ' : ''));
}
Expand All @@ -193,7 +193,7 @@ private function getEncoderConfigFromAlgorithm(array $config): array
$config['algorithm'] = 'sodium';
} elseif (\defined('PASSWORD_ARGON2ID')) {
$config['algorithm'] = 'native';
$config['native_algorithm'] = PASSWORD_ARGON2ID;
$config['native_algorithm'] = \PASSWORD_ARGON2ID;
} else {
throw new LogicException(sprintf('Algorithm "argon2id" is not available. Either use %s"auto", upgrade to PHP 7.3+ or use libsodium 1.0.15+ instead.', \defined('PASSWORD_ARGON2I') || $hasSodium ? '"argon2i", ' : ''));
}
Expand Down
16 changes: 8 additions & 8 deletions Encoder/NativePasswordEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class NativePasswordEncoder implements PasswordEncoderInterface, SelfSalti
{
private const MAX_PASSWORD_LENGTH = 4096;

private $algo = PASSWORD_BCRYPT;
private $algo = \PASSWORD_BCRYPT;
private $options;

/**
Expand All @@ -33,8 +33,8 @@ final class NativePasswordEncoder implements PasswordEncoderInterface, SelfSalti
public function __construct(int $opsLimit = null, int $memLimit = null, int $cost = null, string $algo = null)
{
$cost = $cost ?? 13;
$opsLimit = $opsLimit ?? max(4, \defined('SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE') ? SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE : 4);
$memLimit = $memLimit ?? max(64 * 1024 * 1024, \defined('SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE') ? SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE : 64 * 1024 * 1024);
$opsLimit = $opsLimit ?? max(4, \defined('SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE') ? \SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE : 4);
$memLimit = $memLimit ?? max(64 * 1024 * 1024, \defined('SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE') ? \SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE : 64 * 1024 * 1024);

if (3 > $opsLimit) {
throw new \InvalidArgumentException('$opsLimit must be 3 or greater.');
Expand All @@ -48,14 +48,14 @@ public function __construct(int $opsLimit = null, int $memLimit = null, int $cos
throw new \InvalidArgumentException('$cost must be in the range of 4-31.');
}

$algos = [1 => PASSWORD_BCRYPT, '2y' => PASSWORD_BCRYPT];
$algos = [1 => \PASSWORD_BCRYPT, '2y' => \PASSWORD_BCRYPT];

if (\defined('PASSWORD_ARGON2I')) {
$this->algo = $algos[2] = $algos['argon2i'] = (string) PASSWORD_ARGON2I;
$this->algo = $algos[2] = $algos['argon2i'] = (string) \PASSWORD_ARGON2I;
}

if (\defined('PASSWORD_ARGON2ID')) {
$this->algo = $algos[3] = $algos['argon2id'] = (string) PASSWORD_ARGON2ID;
$this->algo = $algos[3] = $algos['argon2id'] = (string) \PASSWORD_ARGON2ID;
}

if (null !== $algo) {
Expand All @@ -75,7 +75,7 @@ public function __construct(int $opsLimit = null, int $memLimit = null, int $cos
*/
public function encodePassword(string $raw, ?string $salt): string
{
if (\strlen($raw) > self::MAX_PASSWORD_LENGTH || ((string) PASSWORD_BCRYPT === $this->algo && 72 < \strlen($raw))) {
if (\strlen($raw) > self::MAX_PASSWORD_LENGTH || ((string) \PASSWORD_BCRYPT === $this->algo && 72 < \strlen($raw))) {
throw new BadCredentialsException('Invalid password.');
}

Expand All @@ -102,7 +102,7 @@ public function isPasswordValid(string $encoded, string $raw, ?string $salt): bo
return (72 >= \strlen($raw) || 0 !== strpos($encoded, '$2')) && password_verify($raw, $encoded);
}

if (\extension_loaded('sodium') && version_compare(SODIUM_LIBRARY_VERSION, '1.0.14', '>=')) {
if (\extension_loaded('sodium') && version_compare(\SODIUM_LIBRARY_VERSION, '1.0.14', '>=')) {
return sodium_crypto_pwhash_str_verify($encoded, $raw);
}

Expand Down
6 changes: 3 additions & 3 deletions Encoder/SodiumPasswordEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function __construct(int $opsLimit = null, int $memLimit = null)
throw new LogicException('Libsodium is not available. You should either install the sodium extension, upgrade to PHP 7.2+ or use a different encoder.');
}

$this->opsLimit = $opsLimit ?? max(4, \defined('SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE') ? SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE : 4);
$this->memLimit = $memLimit ?? max(64 * 1024 * 1024, \defined('SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE') ? SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE : 64 * 1024 * 1024);
$this->opsLimit = $opsLimit ?? max(4, \defined('SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE') ? \SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE : 4);
$this->memLimit = $memLimit ?? max(64 * 1024 * 1024, \defined('SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE') ? \SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE : 64 * 1024 * 1024);

if (3 > $this->opsLimit) {
throw new \InvalidArgumentException('$opsLimit must be 3 or greater.');
Expand All @@ -48,7 +48,7 @@ public function __construct(int $opsLimit = null, int $memLimit = null)

public static function isSupported(): bool
{
return version_compare(\extension_loaded('sodium') ? SODIUM_LIBRARY_VERSION : phpversion('libsodium'), '1.0.14', '>=');
return version_compare(\extension_loaded('sodium') ? \SODIUM_LIBRARY_VERSION : phpversion('libsodium'), '1.0.14', '>=');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Tests/Encoder/EncoderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ public function testMigrateFrom()
$this->assertInstanceOf(MigratingPasswordEncoder::class, $encoder);

$this->assertTrue($encoder->isPasswordValid((new SodiumPasswordEncoder())->encodePassword('foo', null), 'foo', null));
$this->assertTrue($encoder->isPasswordValid((new NativePasswordEncoder(null, null, null, PASSWORD_BCRYPT))->encodePassword('foo', null), 'foo', null));
$this->assertTrue($encoder->isPasswordValid((new NativePasswordEncoder(null, null, null, \PASSWORD_BCRYPT))->encodePassword('foo', null), 'foo', null));
$this->assertTrue($encoder->isPasswordValid($digest->encodePassword('foo', null), 'foo', null));
$this->assertStringStartsWith(SODIUM_CRYPTO_PWHASH_STRPREFIX, $encoder->encodePassword('foo', null));
$this->assertStringStartsWith(\SODIUM_CRYPTO_PWHASH_STRPREFIX, $encoder->encodePassword('foo', null));
}

public function testDefaultMigratingEncoders()
Expand Down
4 changes: 2 additions & 2 deletions Tests/Encoder/NativePasswordEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testNonArgonValidation()

public function testConfiguredAlgorithm()
{
$encoder = new NativePasswordEncoder(null, null, null, PASSWORD_BCRYPT);
$encoder = new NativePasswordEncoder(null, null, null, \PASSWORD_BCRYPT);
$result = $encoder->encodePassword('password', null);
$this->assertTrue($encoder->isPasswordValid($result, 'password', null));
$this->assertStringStartsWith('$2', $result);
Expand All @@ -84,7 +84,7 @@ public function testConfiguredAlgorithmWithLegacyConstValue()
public function testCheckPasswordLength()
{
$encoder = new NativePasswordEncoder(null, null, 4);
$result = password_hash(str_repeat('a', 72), PASSWORD_BCRYPT, ['cost' => 4]);
$result = password_hash(str_repeat('a', 72), \PASSWORD_BCRYPT, ['cost' => 4]);

$this->assertFalse($encoder->isPasswordValid($result, str_repeat('a', 73), 'salt'));
$this->assertTrue($encoder->isPasswordValid($result, str_repeat('a', 72), 'salt'));
Expand Down

0 comments on commit ae0d567

Please sign in to comment.