Skip to content

[1.x] Retrieve user through provider #189

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

Merged
merged 1 commit into from
Jan 4, 2021
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
17 changes: 9 additions & 8 deletions src/Actions/RedirectIfTwoFactorAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Auth\Events\Failed;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use Laravel\Fortify\Fortify;
use Laravel\Fortify\LoginRateLimiter;
Expand Down Expand Up @@ -76,15 +75,17 @@ protected function validateCredentials($request)
});
}

$model = $this->guard->getProvider()->getModel();
$user = $this->guard->getProvider()->retrieveByCredentials(
$request->only(Fortify::username(), 'password')
);

return tap($model::where(Fortify::username(), $request->{Fortify::username()})->first(), function ($user) use ($request) {
if (! $user || ! Hash::check($request->password, $user->password)) {
$this->fireFailedEvent($request, $user);
if (! $user) {
$this->fireFailedEvent($request, $user);

$this->throwFailedAuthenticationException($request);
}
});
$this->throwFailedAuthenticationException($request);
}

return $user;
Comment on lines +78 to +88
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The verification might be not enough. retrieveByCredentials() does not expect password matches, only an email address does. Password hash validation is done by validateCredentials(), but its call was not included in the PR.

I think it is the cause of the PR reverted.

@taylorotwell Is it right?

}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Requests/TwoFactorLoginRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public function challengedUser()
return $this->challengedUser;
}

$model = app(StatefulGuard::class)->getProvider()->getModel();
$provider = app(StatefulGuard::class)->getProvider();

if (! $this->session()->has('login.id') ||
! $user = $model::find($this->session()->pull('login.id'))) {
! $user = $provider->retrieveById($this->session()->pull('login.id'))) {
throw new HttpResponseException(
app(FailedTwoFactorLoginResponse::class)->toResponse($this)
);
Expand Down