Skip to content

Commit

Permalink
tweak how rate limiting is implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 30, 2020
1 parent 0c7b533 commit 8609af2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 5 additions & 1 deletion routes/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
}

$limiter = config('fortify.limiters.login');
$twoFactorLimiter = config('fortify.limiters.two-factor');

Route::post('/login', [AuthenticatedSessionController::class, 'store'])
->middleware(array_filter([
Expand Down Expand Up @@ -126,7 +127,10 @@
}

Route::post('/two-factor-challenge', [TwoFactorAuthenticatedSessionController::class, 'store'])
->middleware(['guest']);
->middleware(array_filter([
'guest',
$twoFactorLimiter ? 'throttle:'.$twoFactorLimiter : null,
]));

$twoFactorMiddleware = Features::optionEnabled(Features::twoFactorAuthentication(), 'confirmPassword')
? ['auth', 'password.confirm']
Expand Down
10 changes: 10 additions & 0 deletions stubs/FortifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\Actions\Fortify\ResetUserPassword;
use App\Actions\Fortify\UpdateUserPassword;
use App\Actions\Fortify\UpdateUserProfileInformation;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Laravel\Fortify\Fortify;

Expand All @@ -32,5 +34,13 @@ public function boot()
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);

RateLimiter::for('login', function (Request $request) {
return Limit::perMinute(5)->by($request->email.$request->ip());
});

RateLimiter::for('two-factor', function (Request $request) {
return Limit::perMinute(5)->by($request->session()->get('login.id'));
});
}
}
3 changes: 2 additions & 1 deletion stubs/fortify.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@
*/

'limiters' => [
'login' => null,
'login' => 'login',
'two-factor' => 'two-factor',
],

/*
Expand Down

0 comments on commit 8609af2

Please sign in to comment.