Skip to content
This repository was archived by the owner on Apr 19, 2025. It is now read-only.

Commit 62c5a9e

Browse files
committed
Modify throttling flow; lockout login procedure in response to hitting two-factor authentication limit
1 parent 7f9c829 commit 62c5a9e

File tree

5 files changed

+31
-24
lines changed

5 files changed

+31
-24
lines changed

src/Http/Controllers/ThrottlesTwoFactorAuths.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
use Illuminate\Auth\Events\Lockout;
66
use Illuminate\Cache\RateLimiter;
7+
use Illuminate\Foundation\Auth\ThrottlesLogins;
78
use Illuminate\Http\Request;
9+
use Illuminate\Support\Str;
810

911
trait ThrottlesTwoFactorAuths
1012
{
13+
use ThrottlesLogins;
14+
1115
/**
1216
* Determine if the user has too many failed two-factor authentiction attempts.
1317
*
@@ -16,9 +20,7 @@ trait ThrottlesTwoFactorAuths
1620
*/
1721
protected function hasTooManyTwoFactorAuthAttempts(Request $request)
1822
{
19-
return $this->limiter()->tooManyAttempts(
20-
$this->throttleKey($request), 3, 1
21-
);
23+
return self::hasTooManyLoginAttempts($request);
2224
}
2325

2426
/**
@@ -29,7 +31,7 @@ protected function hasTooManyTwoFactorAuthAttempts(Request $request)
2931
*/
3032
protected function incrementTwoFactorAuthAttempts(Request $request)
3133
{
32-
$this->limiter()->hit($this->throttleKey($request));
34+
self::incrementLoginAttempts($request);
3335
}
3436

3537
/**
@@ -44,15 +46,19 @@ protected function sendLockoutResponse(Request $request)
4446
$this->throttleKey($request)
4547
);
4648

47-
$message = __('two-factor-auth.throttle', ['seconds' => $seconds]);
49+
$message = __('twofactor-auth::twofactor-auth.throttle', ['seconds' => $seconds]);
4850

49-
$errors = [$this->fieldname() => $message];
51+
$errors = [$this->username() => $message];
5052

5153
if ($request->expectsJson()) {
5254
return response()->json($errors, 423);
5355
}
5456

55-
return redirect()->back()->withErrors($errors);
57+
return redirect()->to('/login')
58+
->withInput(
59+
array_only($request->session()->get('two-factor:auth'), [$this->username(), 'remember'])
60+
)
61+
->withErrors($errors);
5662
}
5763

5864
/**
@@ -85,7 +91,7 @@ protected function fireLockoutEvent(Request $request)
8591
*/
8692
protected function throttleKey(Request $request)
8793
{
88-
return $request->session()->get('two-factor:auth:id').'|'.$request->ip();
94+
return Str::lower($request->session()->get('two-factor:auth')[$this->username()]).'|'.$request->ip();
8995
}
9096

9197
/**

src/Http/Controllers/TwoFactorAuthenticatesUsers.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ public function showTwoFactorForm()
3333
*/
3434
public function verifyToken(VerifySMSToken $request)
3535
{
36-
// If the class is using the ThrottlesLogins trait, we can automatically throttle
37-
// the two-factor authentication attempts for this application. We'll key this by
38-
// the user id in the session storage and the IP address of the client making
39-
// these requests into this application.
36+
// If the class is using the ThrottlesTwoFactorAuths trait, we can automatically
37+
// throttle the two-factor authentication attempts for this application.
38+
// We'll key this by the username in the session storage and the IP address
39+
// of the client making these requests into this application.
4040
if ($this->hasTooManyTwoFactorAuthAttempts($request)) {
4141
$this->fireLockoutEvent($request);
4242

@@ -77,7 +77,6 @@ protected function attemptTwoFactorAuth(Request $request)
7777
$user = User::findOrFail($request->session()->get('two-factor:auth:id'));
7878

7979
if (resolve(TwoFactorProvider::class)->verify($user, $request->input('token'))) {
80-
$request->session()->forget('two-factor:auth:id');
8180
auth()->login($user); // If SMS code validation passes, login user
8281

8382
return true;
@@ -98,6 +97,8 @@ protected function sendTwoFactorAuthResponse(Request $request)
9897

9998
$this->clearTwoFactorAuthAttempts($request);
10099

100+
$request->session()->forget('two-factor:auth');
101+
101102
return redirect()->intended($this->redirectPath());
102103
}
103104

@@ -109,7 +110,7 @@ protected function sendTwoFactorAuthResponse(Request $request)
109110
*/
110111
protected function sendFailedTwoFactorAuthResponse(Request $request)
111112
{
112-
$errors = [$this->fieldname() => __('two-factor-auth.failed')];
113+
$errors = ['token' => __('twofactor-auth::twofactor-auth.failed')];
113114

114115
if ($request->expectsJson()) {
115116
return response()->json($errors, 422);
@@ -127,22 +128,22 @@ protected function sendFailedTwoFactorAuthResponse(Request $request)
127128
*/
128129
protected function sendKillTwoFactorAuthResponse(Request $request)
129130
{
130-
$errors = [$this->fieldname() => __('two-factor-auth.expired')];
131+
$errors = [$this->username() => __('twofactor-auth::twofactor-auth.expired')];
131132

132133
if ($request->expectsJson()) {
133134
return response()->json($errors, 401);
134135
}
135136

136-
return redirect()->back()->withErrors($errors);
137+
return redirect()->to('/login')->withErrors($errors);
137138
}
138139

139140
/**
140-
* Get the input field identifier to be used by the controller.
141+
* Get the login username to be used by the controller.
141142
*
142143
* @return string
143144
*/
144-
public function fieldname()
145+
public function username()
145146
{
146-
return 'token';
147+
return 'email';
147148
}
148149
}

src/Http/Requests/VerifySMSToken.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class VerifySMSToken extends FormRequest
1313
*/
1414
public function authorize()
1515
{
16-
if ($this->session()->has('two-factor:auth:id')) {
16+
if ($this->session()->has('two-factor:auth')) {
1717
return true;
1818
}
1919

src/resources/lang/en/twofactor-auth.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
'send' => 'Send Token',
1919

2020
'failed' => 'Invalid authentication token provided.',
21-
'throttle' => 'Too many authentication attempts. You will be redirected to the login page. Please try again in :seconds seconds.',
22-
'expired' => 'The authentication token has expired. You will be redirected to the login page.',
21+
'throttle' => 'Too many two-factor authentication token attempts. Please try again in :seconds seconds.',
22+
'expired' => 'The two-factor authentication token has expired.',
2323

2424
];

src/resources/lang/nl/twofactor-auth.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
'send' => 'Verstuur Token',
1919

2020
'failed' => 'Ongeldige authenticatie token aangeleverd.',
21-
'throttle' => 'Te veel authenticatie pogingen. Je wordt automatisch teruggestuurd naar de login pagina. Probeer het alsjeblieft opnieuw over :seconds seconden.',
22-
'expired' => 'De authenticatie token is verlopen. Je wordt automatisch teruggestuurd naar de login pagina.',
21+
'throttle' => 'Te veel authenticatie token pogingen. Probeer het alsjeblieft opnieuw over :seconds seconden.',
22+
'expired' => 'De authenticatie token is verlopen.',
2323

2424
];

0 commit comments

Comments
 (0)