This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 2auth reset password workflow (#133)
- Loading branch information
1 parent
5167331
commit 18d5797
Showing
11 changed files
with
486 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/Http/Controllers/TwoFactorAuthenticatedPasswordResetController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ARKEcosystem\Fortify\Http\Controllers; | ||
|
||
use ARKEcosystem\Fortify\Http\Requests\TwoFactorResetPasswordRequest; | ||
use Illuminate\Http\Exceptions\HttpResponseException; | ||
use Illuminate\Routing\Controller; | ||
use Laravel\Fortify\Contracts\FailedTwoFactorLoginResponse; | ||
|
||
class TwoFactorAuthenticatedPasswordResetController extends Controller | ||
{ | ||
/** | ||
* Show the two factor authentication challenge view. | ||
* | ||
* @param TwoFactorResetPasswordRequest $request | ||
* @param string $token | ||
* | ||
* @return mixed | ||
*/ | ||
public function create(TwoFactorResetPasswordRequest $request, string $token) | ||
{ | ||
if (! $request->hasChallengedUser()) { | ||
throw new HttpResponseException(redirect()->route('login')); | ||
} | ||
|
||
if (! $request->hasValidToken()) { | ||
throw new HttpResponseException(redirect()->route('login')->withErrors(['email' => trans('fortify::validation.password_reset_link_invalid')])); | ||
} | ||
|
||
return view('ark-fortify::auth.two-factor-challenge', [ | ||
'token' => $token, | ||
'resetPassword' => true, | ||
'email' => $request->challengedUser()->email, | ||
]); | ||
} | ||
|
||
/** | ||
* Validates the 2fa code and shows the reset password form. | ||
* | ||
* @param TwoFactorResetPasswordRequest $request | ||
* | ||
* @return mixed | ||
*/ | ||
public function store(TwoFactorResetPasswordRequest $request) | ||
{ | ||
$user = $request->challengedUser(); | ||
|
||
if (! $request->hasValidToken()) { | ||
throw new HttpResponseException(redirect()->route('login')->withErrors(['email' => trans('fortify::validation.password_reset_link_invalid')])); | ||
} | ||
|
||
if ($code = $request->validRecoveryCode()) { | ||
$user->replaceRecoveryCode($code); | ||
} elseif (! $request->hasValidCode()) { | ||
return app(FailedTwoFactorLoginResponse::class); | ||
} | ||
|
||
return view('ark-fortify::auth.reset-password'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ARKEcosystem\Fortify\Http\Requests; | ||
|
||
use Illuminate\Contracts\Auth\PasswordBroker; | ||
use Illuminate\Contracts\Auth\StatefulGuard; | ||
use Illuminate\Http\Exceptions\HttpResponseException; | ||
use Laravel\Fortify\Contracts\FailedTwoFactorLoginResponse; | ||
use Laravel\Fortify\Http\Requests\TwoFactorLoginRequest; | ||
|
||
class TwoFactorResetPasswordRequest extends TwoFactorLoginRequest | ||
{ | ||
/** | ||
* Determine if the reset token is valid. | ||
* | ||
* @return bool | ||
*/ | ||
public function hasValidToken() | ||
{ | ||
$user = $this->challengedUser(); | ||
|
||
return $user && app(PasswordBroker::class)->tokenExists($user, $this->route('token')); | ||
} | ||
|
||
/** | ||
* Determine if there is a challenged user in the current session. | ||
* | ||
* @return bool | ||
*/ | ||
public function hasChallengedUser() | ||
{ | ||
$model = app(StatefulGuard::class)->getProvider()->getModel(); | ||
|
||
return $this->has('email') && | ||
$model::whereEmail($this->get('email'))->exists(); | ||
} | ||
|
||
/** | ||
* Get the user that is attempting the two factor challenge. | ||
* | ||
* @return mixed | ||
*/ | ||
public function challengedUser() | ||
{ | ||
if ($this->challengedUser) { | ||
return $this->challengedUser; | ||
} | ||
|
||
$model = app(StatefulGuard::class)->getProvider()->getModel(); | ||
|
||
if (! $this->has('email') || | ||
! $user = $model::whereEmail($this->get('email'))->first()) { | ||
throw new HttpResponseException( | ||
app(FailedTwoFactorLoginResponse::class)->toResponse($this) | ||
); | ||
} | ||
|
||
return $this->challengedUser = $user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.