Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] Remove Hash::check() for password verification #25677

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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