-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of Admin Two Factor Authentication Guard
- Loading branch information
Showing
18 changed files
with
368 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace App\Actions\Fortify; | ||
|
||
use Illuminate\Contracts\Auth\StatefulGuard; | ||
use Illuminate\Support\Facades\Session; | ||
use Laravel\Fortify\LoginRateLimiter; | ||
|
||
class PrepareAuthenticatedSession | ||
{ | ||
/** | ||
* The guard implementation. | ||
* | ||
* @var \Illuminate\Contracts\Auth\StatefulGuard | ||
*/ | ||
protected $guard; | ||
|
||
/** | ||
* The login rate limiter instance. | ||
* | ||
* @var \Laravel\Fortify\LoginRateLimiter | ||
*/ | ||
protected $limiter; | ||
|
||
|
||
/** | ||
* Create a new class instance. | ||
* | ||
* @param \Laravel\Fortify\LoginRateLimiter $limiter | ||
* @return void | ||
*/ | ||
public function __construct(StatefulGuard $guard,LoginRateLimiter $limiter) | ||
{ | ||
$this->guard = $guard; | ||
$this->limiter = $limiter; | ||
} | ||
|
||
/** | ||
* Handle the incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param callable $next | ||
* @return mixed | ||
*/ | ||
public function handle($request, $next) | ||
{ | ||
$request->session()->regenerate(); | ||
|
||
$this->limiter->clear($request); | ||
|
||
|
||
return $next($request); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
namespace App\Actions\Fortify\Requests; | ||
|
||
use App\Http\Controllers\AdminController; | ||
use Illuminate\Contracts\Auth\StatefulGuard; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Http\Exceptions\HttpResponseException; | ||
use Laravel\Fortify\Contracts\FailedTwoFactorLoginResponse; | ||
use Laravel\Fortify\Contracts\TwoFactorAuthenticationProvider; | ||
|
||
class TwoFactorLoginRequest extends FormRequest | ||
{ | ||
/** | ||
* The user attempting the two factor challenge. | ||
* | ||
* @var mixed | ||
*/ | ||
protected $challengedUser; | ||
|
||
/** | ||
* Indicates if the user wished to be remembered after login. | ||
* | ||
* @var bool | ||
*/ | ||
protected $remember; | ||
|
||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'code' => 'nullable|string', | ||
'recovery_code' => 'nullable|string', | ||
]; | ||
} | ||
|
||
/** | ||
* Determine if the request has a valid two factor code. | ||
* | ||
* @return bool | ||
*/ | ||
public function hasValidCode() | ||
{ | ||
return $this->code && tap(app(TwoFactorAuthenticationProvider::class)->verify( | ||
decrypt($this->challengedUser()->two_factor_secret), $this->code | ||
), function ($result) { | ||
if ($result) { | ||
$this->session()->forget('login.id'); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Get the valid recovery code if one exists on the request. | ||
* | ||
* @return string|null | ||
*/ | ||
public function validRecoveryCode() | ||
{ | ||
if (! $this->recovery_code) { | ||
return; | ||
} | ||
|
||
return tap(collect($this->challengedUser()->recoveryCodes())->first(function ($code) { | ||
return hash_equals($this->recovery_code, $code) ? $code : null; | ||
}), function ($code) { | ||
if ($code) { | ||
$this->session()->forget('login.id'); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Determine if there is a challenged user in the current session. | ||
* | ||
* @return bool | ||
*/ | ||
public function hasChallengedUser() | ||
{ | ||
if ($this->challengedUser) { | ||
return true; | ||
} | ||
|
||
// $model = app(StatefulGuard::class)->getProvider()->getModel(); | ||
$model = 'App\Models\Admin'; | ||
|
||
return $this->session()->has('login.id') && | ||
$model::find($this->session()->get('login.id')); | ||
} | ||
|
||
/** | ||
* 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(); | ||
$model = 'App\Models\Admin'; | ||
if (! $this->session()->has('login.id') || | ||
! $user = $model::find($this->session()->get('login.id'))) { | ||
throw new HttpResponseException( | ||
app(FailedTwoFactorLoginResponse::class)->toResponse($this) | ||
); | ||
} | ||
|
||
return $this->challengedUser = $user; | ||
} | ||
|
||
/** | ||
* Determine if the user wanted to be remembered after login. | ||
* | ||
* @return bool | ||
*/ | ||
public function remember() | ||
{ | ||
if (! $this->remember) { | ||
$this->remember = $this->session()->pull('login.remember', false); | ||
} | ||
|
||
return $this->remember; | ||
} | ||
} |
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.