Skip to content

Commit

Permalink
Implemented FallbackPasswordHasher::needsRehash()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 3, 2014
1 parent c283491 commit 260d046
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/Controller/Component/Auth/FallbackPasswordHasher.php
Expand Up @@ -82,4 +82,15 @@ public function check($password, $hashedPassword) {
return false;
}

/**
* Returns true if the password need to be rehashed, with the first hasher present
* in the list of hashers
*
* @param string $password The password to verify
* @return boolean
*/
public function needsRehash($password) {
return $this->_hashers[0]->needsRehash($password);
}

}
Expand Up @@ -14,20 +14,20 @@
*/
namespace Cake\Test\TestCase\Controller\Component\Auth;

use Cake\Controller\Component\Auth\FallbackPasswordHasher;
use Cake\Controller\Component\Auth\SimplePasswordHasher;
use Cake\Controller\Component\Auth\WeakPasswordHasher;
use Cake\Controller\Component\Auth\FallbackPasswordHasher;
use Cake\TestSuite\TestCase;

/**
* Test case for SimplePasswordHasher
* Test case for FallbackPasswordHasher
*
*/
class FallbackPasswordHasherTest extends TestCase {


/**
* Tests that only the first hasher is user for hashig a password
* Tests that only the first hasher is user for hashing a password
*
* @return void
*/
Expand All @@ -42,7 +42,7 @@ public function testHash() {
}

/**
* Tests that the check mehthod will chek with configured hashers until a match
* Tests that the check method will check with configured hashers until a match
* is found
*
* @return void
Expand All @@ -57,4 +57,20 @@ public function testCheck() {
$this->assertTrue($hasher->check('foo', $hash));
$this->assertTrue($hasher->check('foo', $otherHash));
}

/**
* Tests that the password only needs to be re-built according to the first hasher
*
* @return void
*/
public function testNeedsRehash() {
$hasher = new FallbackPasswordHasher(['hashers' => ['Simple', 'Weak']]);
$weak = new WeakPasswordHasher();
$otherHash = $weak->hash('foo');
$this->assertTrue($hasher->needsRehash($otherHash));

$simple = new SimplePasswordHasher();
$hash = $simple->hash('foo');
$this->assertFalse($hasher->needsRehash($hash));
}
}

0 comments on commit 260d046

Please sign in to comment.