-
Notifications
You must be signed in to change notification settings - Fork 271
Add Two Factor Authentication for React Starter Kit #156
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
Open
pushpak1300
wants to merge
18
commits into
main
Choose a base branch
from
feat/add_two_factor_auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
429e5ec
feat: add two-factor authentication
pushpak1300 ebfcf9e
Merge branch 'main' into feat/add_two_factor_auth
pushpak1300 a822d3d
feat: update package-lock
pushpak1300 ee5f641
feat: add tests
pushpak1300 29c0df1
linting changes
pushpak1300 6840944
Merge branch 'main' into feat/add_two_factor_auth
pushpak1300 bf996b7
feat: enhance two-factor authentication implementation and improve co…
pushpak1300 38766c1
Update two-factor authentication implementation and refactor related …
pushpak1300 39bd4bd
Update two-factor authentication implementation and refactor related …
pushpak1300 07d1019
Formatting
pushpak1300 ee8bcf3
Formatting Recovery Code
pushpak1300 94f117f
refactor
pushpak1300 d1a9b23
Fix Test
pushpak1300 e2972bb
Formatting
pushpak1300 ab5140c
Simplify Inertia Form
pushpak1300 b612ae5
Refactor Test
pushpak1300 5c38d18
Fix Issue
pushpak1300 5397f80
Remove explicit typing
pushpak1300 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
/storage/*.key | ||
/storage/pail | ||
/vendor | ||
.DS_Store | ||
.env | ||
.env.backup | ||
.env.production | ||
|
This file contains hidden or 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
41 changes: 0 additions & 41 deletions
41
app/Http/Controllers/Auth/ConfirmablePasswordController.php
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
app/Http/Controllers/Concerns/ConfirmsTwoFactorAuthentication.php
This file contains hidden or 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,71 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Concerns; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
use Laravel\Fortify\Actions\DisableTwoFactorAuthentication; | ||
use Laravel\Fortify\Features; | ||
|
||
trait ConfirmsTwoFactorAuthentication | ||
{ | ||
/** | ||
* Validate the two-factor authentication state for the request. | ||
*/ | ||
protected function validateTwoFactorAuthenticationState(Request $request): void | ||
{ | ||
if (! Features::optionEnabled(Features::twoFactorAuthentication(), 'confirm')) { | ||
return; | ||
} | ||
|
||
$currentTime = time(); | ||
|
||
// Notate totally disabled state in session... | ||
if ($this->twoFactorAuthenticationDisabled($request)) { | ||
$request->session()->put('two_factor_empty_at', $currentTime); | ||
} | ||
|
||
// If was previously totally disabled this session but is now confirming, notate time... | ||
if ($this->hasJustBegunConfirmingTwoFactorAuthentication($request)) { | ||
$request->session()->put('two_factor_confirming_at', $currentTime); | ||
} | ||
|
||
// If the profile is reloaded and is not confirmed but was previously in confirming state, disable... | ||
if ($this->neverFinishedConfirmingTwoFactorAuthentication($request, $currentTime)) { | ||
app(DisableTwoFactorAuthentication::class)(Auth::user()); | ||
|
||
$request->session()->put('two_factor_empty_at', $currentTime); | ||
$request->session()->remove('two_factor_confirming_at'); | ||
} | ||
} | ||
|
||
/** | ||
* Determine if two-factor authentication is totally disabled. | ||
*/ | ||
protected function twoFactorAuthenticationDisabled(Request $request): bool | ||
{ | ||
return is_null($request->user()->two_factor_secret) && | ||
is_null($request->user()->two_factor_confirmed_at); | ||
} | ||
|
||
/** | ||
* Determine if two-factor authentication is just now being confirmed within the last request cycle. | ||
*/ | ||
protected function hasJustBegunConfirmingTwoFactorAuthentication(Request $request): bool | ||
{ | ||
return ! is_null($request->user()->two_factor_secret) && | ||
is_null($request->user()->two_factor_confirmed_at) && | ||
$request->session()->has('two_factor_empty_at') && | ||
is_null($request->session()->get('two_factor_confirming_at')); | ||
} | ||
|
||
/** | ||
* Determine if two-factor authentication was never totally confirmed once confirmation started. | ||
*/ | ||
protected function neverFinishedConfirmingTwoFactorAuthentication(Request $request, int $currentTime): bool | ||
{ | ||
return ! array_key_exists('code', $request->session()->getOldInput()) && | ||
is_null($request->user()->two_factor_confirmed_at) && | ||
$request->session()->get('two_factor_confirming_at', 0) != $currentTime; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
app/Http/Controllers/Settings/TwoFactorAuthenticationController.php
This file contains hidden or 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,47 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Settings; | ||
|
||
use App\Http\Controllers\Concerns\ConfirmsTwoFactorAuthentication; | ||
use App\Http\Controllers\Controller; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response as HttpResponse; | ||
use Illuminate\Routing\Controllers\HasMiddleware; | ||
use Illuminate\Routing\Controllers\Middleware; | ||
use Inertia\Inertia; | ||
use Inertia\Response; | ||
use Laravel\Fortify\Features; | ||
|
||
class TwoFactorAuthenticationController extends Controller implements HasMiddleware | ||
{ | ||
use ConfirmsTwoFactorAuthentication; | ||
|
||
/** | ||
* Get the middleware that should be assigned to the controller. | ||
*/ | ||
public static function middleware(): array | ||
{ | ||
return Features::optionEnabled(Features::twoFactorAuthentication(), 'confirmPassword') | ||
? [new Middleware('password.confirm', only: ['show'])] | ||
: []; | ||
} | ||
|
||
/** | ||
* Show the user's two-factor authentication settings page. | ||
*/ | ||
public function show(Request $request): Response | ||
{ | ||
abort_if( | ||
! Features::enabled(Features::twoFactorAuthentication()), | ||
HttpResponse::HTTP_FORBIDDEN, | ||
'Two factor authentication is disabled.' | ||
); | ||
|
||
$this->validateTwoFactorAuthenticationState($request); | ||
|
||
return Inertia::render('settings/two-factor', [ | ||
'requiresConfirmation' => Features::optionEnabled(Features::twoFactorAuthentication(), 'confirm'), | ||
'twoFactorEnabled' => $request->user()->hasEnabledTwoFactorAuthentication(), | ||
]); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,39 @@ | ||
<?php | ||
|
||
namespace App\Providers; | ||
|
||
use Illuminate\Cache\RateLimiting\Limit; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\RateLimiter; | ||
use Illuminate\Support\ServiceProvider; | ||
use Inertia\Inertia; | ||
use Laravel\Fortify\Fortify; | ||
|
||
class FortifyServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register any application services. | ||
*/ | ||
public function register(): void | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Bootstrap any application services. | ||
*/ | ||
public function boot(): void | ||
{ | ||
Fortify::twoFactorChallengeView(function () { | ||
return Inertia::render('auth/two-factor-challenge'); | ||
}); | ||
|
||
Fortify::confirmPasswordView(function () { | ||
return Inertia::render('auth/confirm-password'); | ||
}); | ||
|
||
RateLimiter::for('two-factor', function (Request $request) { | ||
pushpak1300 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Limit::perMinute(5)->by($request->session()->get('login.id')); | ||
}); | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
return [ | ||
App\Providers\AppServiceProvider::class, | ||
App\Providers\FortifyServiceProvider::class, | ||
]; |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.