Skip to content

Commit 1c065e7

Browse files
committed
Merge pull request #21 from garethellis36/bugfix/php7-warnings
Bug fix - PHP7 warnings
2 parents 50fdd7d + 8dc2822 commit 1c065e7

File tree

5 files changed

+49
-23
lines changed

5 files changed

+49
-23
lines changed

.travis.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ php:
55
- 5.4
66
- 5.5
77
- 5.6
8+
- 7.0
89

910
before_script:
1011
- composer self-update
1112
- composer install --prefer-dist
1213

13-
script: phpunit -c travis.phpunit.xml
14-
15-
after_script:
16-
- wget https://scrutinizer-ci.com/ocular.phar
17-
- php ocular.phar code-coverage:upload --format=php-clover /tmp/jeremykendall/password-validator/coverage.xml
14+
script: phpunit -c phpunit.xml.dist

src/JeremyKendall/Password/Decorator/UpgradeDecorator.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class UpgradeDecorator extends AbstractDecorator
2323
*/
2424
protected $validationCallback;
2525

26+
const DEFAULT_REHASH_COST = 4;
27+
2628
/**
2729
* Public constructor
2830
*
@@ -48,12 +50,33 @@ public function isValid($password, $passwordHash, $legacySalt = null, $identity
4850
);
4951

5052
if ($isValid === true) {
51-
$passwordHash = password_hash($password, PASSWORD_DEFAULT, array(
52-
'cost' => 4,
53-
'salt' => 'CostAndSaltForceRehash',
54-
));
53+
$passwordHash = $this->createHashWhichWillForceRehashInValidator($password);
5554
}
5655

5756
return $this->validator->isValid($password, $passwordHash, $legacySalt, $identity);
5857
}
58+
59+
/**
60+
* This method returns an upgraded password, one that is hashed by the
61+
* password_hash method in such a way that it forces the PasswordValidator
62+
* to rehash the password. This results in PasswordValidator::isValid()
63+
* returning a Result::$code of Result::SUCCESS_PASSWORD_REHASHED,
64+
* notifying the StorageDecorator or custom application code that the
65+
* returned password hash should be persisted.
66+
*
67+
* @param string $password Password to upgrade
68+
*
69+
* @return string Hashed password
70+
*/
71+
private function createHashWhichWillForceRehashInValidator($password)
72+
{
73+
$cost = static::DEFAULT_REHASH_COST;
74+
$options = $this->getOptions();
75+
76+
if (isset($options['cost']) && (int) $options['cost'] === $cost) {
77+
$cost++;
78+
}
79+
80+
return password_hash($password, PASSWORD_DEFAULT, array('cost' => $cost));
81+
}
5982
}

tests/JeremyKendall/Password/Tests/Decorator/IntegrationTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,22 @@ public function testLegacyPasswordIsValidUpgradedRehashedStored2()
9191
);
9292
$this->assertStringStartsWith('$2y$10$', $result->getPassword());
9393
}
94+
95+
public function testLegacyPasswordIsValidUpgradedRehashedWhenClientCodeUsesSameParametersAsUpgradeDecorator()
96+
{
97+
$validator = new UpgradeDecorator(
98+
new PasswordValidator(),
99+
$this->callback
100+
);
101+
$password = 'password';
102+
$hash = hash('sha512', $password);
103+
$validator->setOptions(array('cost' => UpgradeDecorator::DEFAULT_REHASH_COST));
104+
$result = $validator->isValid($password, $hash);
105+
$this->assertTrue($result->isValid(), "Failed asserting that result is valid");
106+
$this->assertEquals(
107+
ValidationResult::SUCCESS_PASSWORD_REHASHED,
108+
$result->getCode(),
109+
"Failed asserting that password was rehashed"
110+
);
111+
}
94112
}

tests/JeremyKendall/Password/Tests/Decorator/KarptoniteRehashUpgradeDecoratorTest.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@ protected function setUp()
6767

6868
public function testRehashingPasswordHashesScenarioCredentialIsValid()
6969
{
70-
$upgradeValidatorRehash = password_hash(
71-
$this->plainTextPassword,
72-
PASSWORD_DEFAULT,
73-
array(
74-
'cost' => 4,
75-
'salt' => 'CostAndSaltForceRehash',
76-
)
77-
);
7870
$finalValidatorRehash = password_hash($this->plainTextPassword, PASSWORD_DEFAULT);
7971

8072
$validResult = new ValidationResult(
@@ -84,7 +76,7 @@ public function testRehashingPasswordHashesScenarioCredentialIsValid()
8476

8577
$this->decoratedValidator->expects($this->once())
8678
->method('isValid')
87-
->with($this->plainTextPassword, $upgradeValidatorRehash, $this->legacySalt)
79+
->with($this->plainTextPassword, $this->isType('string'), $this->legacySalt)
8880
->will($this->returnValue($validResult));
8981

9082
$result = $this->decorator->isValid(

tests/JeremyKendall/Password/Tests/Decorator/UpgradeDecoratorTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ public function testPasswordValidAndPasswordRehashed()
4646
{
4747
$password = 'password';
4848
$passwordHash = hash('sha512', $password);
49-
$upgradeRehash = password_hash($password, PASSWORD_DEFAULT, array(
50-
'cost' => 4,
51-
'salt' => 'CostAndSaltForceRehash',
52-
));
5349

5450
$validatorRehash = password_hash($password, PASSWORD_DEFAULT);
5551

@@ -60,7 +56,7 @@ public function testPasswordValidAndPasswordRehashed()
6056

6157
$this->decoratedValidator->expects($this->once())
6258
->method('isValid')
63-
->with($password, $upgradeRehash)
59+
->with($password, $this->isType('string'))
6460
->will($this->returnValue($result));
6561

6662
$result = $this->decorator->isValid($password, $passwordHash);

0 commit comments

Comments
 (0)