-
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
10 changed files
with
234 additions
and
41 deletions.
There are no files selected for viewing
166 changes: 166 additions & 0 deletions
166
app/Actions/Fortify/AdminRedirectIfTwoFactorAuthenticatable.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,166 @@ | ||
<?php | ||
|
||
namespace App\Actions\Fortify; | ||
|
||
use Illuminate\Auth\Events\Failed; | ||
use Illuminate\Contracts\Auth\StatefulGuard; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Session; | ||
use Illuminate\Validation\ValidationException; | ||
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged; | ||
use Laravel\Fortify\Fortify; | ||
use Laravel\Fortify\LoginRateLimiter; | ||
use Laravel\Fortify\TwoFactorAuthenticatable; | ||
|
||
class AdminRedirectIfTwoFactorAuthenticatable | ||
{ | ||
/** | ||
* The guard implementation. | ||
* | ||
* @var \Illuminate\Contracts\Auth\StatefulGuard | ||
*/ | ||
protected $guard,$model; | ||
|
||
/** | ||
* The login rate limiter instance. | ||
* | ||
* @var \Laravel\Fortify\LoginRateLimiter | ||
*/ | ||
protected $limiter; | ||
|
||
/** | ||
* Create a new controller instance. | ||
* | ||
* @param \Illuminate\Contracts\Auth\StatefulGuard $guard | ||
* @param \Laravel\Fortify\LoginRateLimiter $limiter | ||
* @return void | ||
*/ | ||
public function __construct(StatefulGuard $guard, LoginRateLimiter $limiter) | ||
{ | ||
$this->guard = $guard; | ||
$this->limiter = $limiter; | ||
$this->model = $this->guard->getProvider()->getModel(); | ||
} | ||
|
||
/** | ||
* Handle the incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param callable $next | ||
* @return mixed | ||
*/ | ||
public function handle($request, $next) | ||
{ | ||
$user = $this->validateCredentials($request); | ||
($this->model==='App\Models\Admin') ? Session::put('active_two_factor','admin') : Session::put('active_two_factor','web'); | ||
if (Fortify::confirmsTwoFactorAuthentication()) { | ||
if (optional($user)->two_factor_secret && | ||
!is_null(optional($user)->two_factor_confirmed_at) && | ||
in_array(TwoFactorAuthenticatable::class, class_uses_recursive($user))) { | ||
return $this->twoFactorChallengeResponse($request, $user); | ||
} else { | ||
return $next($request); | ||
} | ||
} | ||
|
||
if (optional($user)->two_factor_secret && | ||
in_array(TwoFactorAuthenticatable::class, class_uses_recursive($user))) { | ||
return $this->twoFactorChallengeResponse($request, $user); | ||
} | ||
|
||
return $next($request); | ||
} | ||
|
||
/** | ||
* Attempt to validate the incoming credentials. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return mixed | ||
*/ | ||
protected function validateCredentials($request) | ||
{ | ||
if (Fortify::$authenticateUsingCallback) { | ||
return tap(call_user_func(Fortify::$authenticateUsingCallback, $request), function ($user) use ($request) { | ||
if (!$user) { | ||
$this->fireFailedEvent($request); | ||
|
||
$this->throwFailedAuthenticationException($request); | ||
} | ||
}); | ||
} | ||
|
||
$model = $this->guard->getProvider()->getModel(); | ||
|
||
return tap($model::where(Fortify::username(), $request->{Fortify::username()})->first(), function ($user) use ($request) { | ||
if (!$user || !$this->guard->getProvider()->validateCredentials($user, ['password' => $request->password])) { | ||
$this->fireFailedEvent($request, $user); | ||
|
||
$this->throwFailedAuthenticationException($request); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Throw a failed authentication validation exception. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return void | ||
* | ||
* @throws \Illuminate\Validation\ValidationException | ||
*/ | ||
protected function throwFailedAuthenticationException($request) | ||
{ | ||
$this->limiter->increment($request); | ||
|
||
throw ValidationException::withMessages([ | ||
Fortify::username() => [trans('auth.failed')], | ||
]); | ||
} | ||
|
||
/** | ||
* Fire the failed authentication attempt event with the given arguments. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Illuminate\Contracts\Auth\Authenticatable|null $user | ||
* @return void | ||
*/ | ||
protected function fireFailedEvent($request, $user = null) | ||
{ | ||
event(new Failed(config('fortify.guard'), $user, [ | ||
Fortify::username() => $request->{Fortify::username()}, | ||
'password' => $request->password, | ||
])); | ||
} | ||
|
||
/** | ||
* Get the two factor authentication enabled response. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param mixed $user | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
*/ | ||
protected function twoFactorChallengeResponse($request, $user) | ||
{ | ||
$request->session()->put([ | ||
'login.id' => $user->getKey(), | ||
'login.remember' => $request->filled('remember'), | ||
]); | ||
|
||
TwoFactorAuthenticationChallenged::dispatch($user); | ||
$table = $user->getTable(); | ||
Session::put('table', $table); | ||
|
||
if ($user->getTable() == 'admins') { | ||
|
||
return $request->wantsJson() | ||
? response()->json(['admin.two_factor' => true]) | ||
: redirect()->route('admin.two-factor.login'); | ||
} else { | ||
|
||
return $request->wantsJson() | ||
? response()->json(['two_factor' => true]) | ||
: redirect()->route('two-factor.login'); | ||
} | ||
|
||
} | ||
} |
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
Oops, something went wrong.