Skip to content

Commit

Permalink
[Security/Core] fix compat of NativePasswordEncoder with pre-PHP74 …
Browse files Browse the repository at this point in the history
…values of `PASSWORD_*` consts
  • Loading branch information
nicolas-grekas committed May 15, 2020
1 parent 9bcf9c1 commit df32171
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Expand Up @@ -24,7 +24,7 @@ final class NativePasswordEncoder implements PasswordEncoderInterface, SelfSalti
{
private const MAX_PASSWORD_LENGTH = 4096;

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

/**
Expand All @@ -48,7 +48,20 @@ public function __construct(int $opsLimit = null, int $memLimit = null, int $cos
throw new \InvalidArgumentException('$cost must be in the range of 4-31.');
}

$this->algo = (string) ($algo ?? (\defined('PASSWORD_ARGON2ID') ? PASSWORD_ARGON2ID : (\defined('PASSWORD_ARGON2I') ? PASSWORD_ARGON2I : PASSWORD_BCRYPT)));
$algos = [1 => PASSWORD_BCRYPT, '2y' => PASSWORD_BCRYPT];

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

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

if (null !== $algo) {
$this->algo = $algos[$algo] ?? $algo;
}

$this->options = [
'cost' => $cost,
'time_cost' => $opsLimit,
Expand Down
Expand Up @@ -73,6 +73,14 @@ public function testConfiguredAlgorithm()
$this->assertStringStartsWith('$2', $result);
}

public function testConfiguredAlgorithmWithLegacyConstValue()
{
$encoder = new NativePasswordEncoder(null, null, null, '1');
$result = $encoder->encodePassword('password', null);
$this->assertTrue($encoder->isPasswordValid($result, 'password', null));
$this->assertStringStartsWith('$2', $result);
}

public function testCheckPasswordLength()
{
$encoder = new NativePasswordEncoder(null, null, 4);
Expand Down

0 comments on commit df32171

Please sign in to comment.