Skip to content

Commit

Permalink
[5.7] Remove Hash::check() for password verification (#25677)
Browse files Browse the repository at this point in the history
* [5.7] Remove depending on Hash::check() just for password verification.

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Fixes tests.

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Fixes styling.

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored and taylorotwell committed Sep 18, 2018
1 parent 8cb226e commit 5a622cc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
10 changes: 8 additions & 2 deletions src/Illuminate/Auth/DatabaseUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,14 @@ protected function getGenericUser($user)
*/
public function validateCredentials(UserContract $user, array $credentials)
{
return $this->hasher->check(
$credentials['password'], $user->getAuthPassword()
$hashed = $user->getAuthPassword();

if (strlen($hashed) === 0) {
return false;
}

return password_verify(
$credentials['password'], $hashed
);
}
}
7 changes: 6 additions & 1 deletion src/Illuminate/Auth/EloquentUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,13 @@ public function retrieveByCredentials(array $credentials)
public function validateCredentials(UserContract $user, array $credentials)
{
$plain = $credentials['password'];
$hashed = $user->getAuthPassword();

return $this->hasher->check($plain, $user->getAuthPassword());
if (strlen($hashed) === 0) {
return false;
}

return password_verify($plain, $hashed);
}

/**
Expand Down
17 changes: 14 additions & 3 deletions tests/Auth/AuthDatabaseUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,22 @@ public function testCredentialValidation()
{
$conn = m::mock('Illuminate\Database\Connection');
$hasher = m::mock('Illuminate\Contracts\Hashing\Hasher');
$hasher->shouldReceive('check')->once()->with('plain', 'hash')->andReturn(true);
$provider = new DatabaseUserProvider($conn, $hasher, 'foo');
$user = m::mock('Illuminate\Contracts\Auth\Authenticatable');
$user->shouldReceive('getAuthPassword')->once()->andReturn('hash');
$result = $provider->validateCredentials($user, ['password' => 'plain']);
$user->shouldReceive('getAuthPassword')->once()->andReturn('$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm');
$result = $provider->validateCredentials($user, ['password' => 'secret']);

$this->assertTrue($result);
}

public function testCredentialValidationUsingUnknownAlgorithm()
{
$conn = m::mock('Illuminate\Database\Connection');
$hasher = m::mock('Illuminate\Contracts\Hashing\Hasher');
$provider = new DatabaseUserProvider($conn, $hasher, 'foo');
$user = m::mock('Illuminate\Contracts\Auth\Authenticatable');
$user->shouldReceive('getAuthPassword')->once()->andReturn('$1$0590adc6$WVAjBIam8sJCgDieJGLey0');
$result = $provider->validateCredentials($user, ['password' => 's3cr3t']);

$this->assertTrue($result);
}
Expand Down
17 changes: 14 additions & 3 deletions tests/Auth/AuthEloquentUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,22 @@ public function testCredentialValidation()
{
$conn = m::mock('Illuminate\Database\Connection');
$hasher = m::mock('Illuminate\Contracts\Hashing\Hasher');
$hasher->shouldReceive('check')->once()->with('plain', 'hash')->andReturn(true);
$provider = new EloquentUserProvider($hasher, 'foo');
$user = m::mock('Illuminate\Contracts\Auth\Authenticatable');
$user->shouldReceive('getAuthPassword')->once()->andReturn('hash');
$result = $provider->validateCredentials($user, ['password' => 'plain']);
$user->shouldReceive('getAuthPassword')->once()->andReturn('$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm');
$result = $provider->validateCredentials($user, ['password' => 'secret']);

$this->assertTrue($result);
}

public function testCredentialValidationUsingUnknownAlgorithm()
{
$conn = m::mock('Illuminate\Database\Connection');
$hasher = m::mock('Illuminate\Contracts\Hashing\Hasher');
$provider = new EloquentUserProvider($hasher, 'foo');
$user = m::mock('Illuminate\Contracts\Auth\Authenticatable');
$user->shouldReceive('getAuthPassword')->once()->andReturn('$1$0590adc6$WVAjBIam8sJCgDieJGLey0');
$result = $provider->validateCredentials($user, ['password' => 's3cr3t']);

$this->assertTrue($result);
}
Expand Down

0 comments on commit 5a622cc

Please sign in to comment.