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

Commit 5b3ed7c

Browse files
committed
Small refactors. Release v2 which is only compatible with Laravel version >= 5.5
1 parent ce16fef commit 5b3ed7c

File tree

4 files changed

+27
-36
lines changed

4 files changed

+27
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Two-Factor-Authentication
2-
A two-factor authentication package for Laravel >= 5.4
2+
A two-factor authentication package for Laravel >= 5.5
33

44
## Description
55
This is a two-factor authentication package for *Laravel*. It is heavily inspired by the [Laravel Two-Factor Authentication](https://github.com/srmklive/laravel-twofactor-authentication) package. The main differences between this package and the aforementioned package are:
66

77
- This package currently only works with the *MessageBird Verify* api or the `'null'` driver that goes through all the steps of the two-factor authentication process without actually doing any real verification. This could be useful for testing purposes.
88
- This package uses throttling to limit the number of unsuccessful authentication attempts in a certain amount of time.
9-
- This package is only guaranteed to work with Laravel >= 5.4. Prior versions have not been tested.
9+
- The current version of this package is only guaranteed to work with Laravel >= 5.5. Version 1.* of this package works with Laravel 5.4. Versions of Laravel prior to 5.4 have not been tested.
1010

1111
## Installation
1212
1 To install using *Composer* run:
@@ -120,7 +120,7 @@ and lastly
120120
*/
121121
private function registerUserAndSendToken(User $user)
122122
{
123-
// Custom, provider dependend logic for sending an authentication token
123+
// Custom, provider dependend logic for sending an authentication token
124124
// to the user. In the case of MessageBird Verify this could simply be
125125
// resolve(TwoFactorProvider::class)->sendSMSToken($this->user)
126126
// Here we assume this function is called from a queue'd job
@@ -142,6 +142,20 @@ class TwoFactorAuthController extends Controller
142142
{
143143
use TwoFactorAuthenticatesUsers;
144144

145+
/**
146+
* The maximum number of attempts to allow.
147+
*
148+
* @var int
149+
*/
150+
protected $maxAttempts = 5;
151+
152+
/**
153+
* The number of minutes to throttle for.
154+
*
155+
* @var int
156+
*/
157+
protected $decayMinutes = 1;
158+
145159
/**
146160
* Where to redirect users after two-factor authentication passes.
147161
*

src/Http/Controllers/ThrottlesTwoFactorAuths.php

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace MichaelDzjap\TwoFactorAuth\Http\Controllers;
44

5-
use Illuminate\Auth\Events\Lockout;
6-
use Illuminate\Cache\RateLimiter;
75
use Illuminate\Foundation\Auth\ThrottlesLogins;
86
use Illuminate\Http\Request;
97
use Illuminate\Support\Str;
@@ -69,18 +67,7 @@ protected function sendLockoutResponse(Request $request)
6967
*/
7068
protected function clearTwoFactorAuthAttempts(Request $request)
7169
{
72-
$this->limiter()->clear($this->throttleKey($request));
73-
}
74-
75-
/**
76-
* Fire an event when a lockout occurs.
77-
*
78-
* @param \Illuminate\Http\Request $request
79-
* @return void
80-
*/
81-
protected function fireLockoutEvent(Request $request)
82-
{
83-
event(new Lockout($request));
70+
self::clearLoginAttempts($request);
8471
}
8572

8673
/**
@@ -93,14 +80,4 @@ protected function throttleKey(Request $request)
9380
{
9481
return Str::lower($request->session()->get('two-factor:auth')[$this->username()]).'|'.$request->ip();
9582
}
96-
97-
/**
98-
* Get the rate limiter instance.
99-
*
100-
* @return \Illuminate\Cache\RateLimiter
101-
*/
102-
protected function limiter()
103-
{
104-
return app(RateLimiter::class);
105-
}
10683
}

src/Http/Controllers/TwoFactorAuthenticatesUsers.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\User;
66
use Illuminate\Foundation\Auth\RedirectsUsers;
77
use Illuminate\Http\Request;
8+
use Illuminate\Validation\ValidationException;
89
use MichaelDzjap\TwoFactorAuth\Contracts\TwoFactorProvider;
910
use MichaelDzjap\TwoFactorAuth\Exceptions\TokenAlreadyProcessedException;
1011
use MichaelDzjap\TwoFactorAuth\Exceptions\TokenExpiredException;
@@ -103,20 +104,18 @@ protected function sendTwoFactorAuthResponse(Request $request)
103104
}
104105

105106
/**
106-
* Get the failed two-factor authentication response instance.
107+
* Throw a validation exception when two-factor authentication attempt fails.
108+
* NOTE: Throwing a validation exception is cleaner than redirecting, but
109+
* we can only do it here because we don't need to redirect to the login route.
107110
*
108111
* @param \Illuminate\Http\Request $request
109-
* @return \Illuminate\Http\RedirectResponse
112+
* @throws \Illuminate\Validation\ValidationException
110113
*/
111114
protected function sendFailedTwoFactorAuthResponse(Request $request)
112115
{
113-
$errors = ['token' => __('twofactor-auth::twofactor-auth.failed')];
114-
115-
if ($request->expectsJson()) {
116-
return response()->json($errors, 422);
117-
}
118-
119-
return redirect()->back()->withErrors($errors);
116+
throw ValidationException::withMessages([
117+
'token' => [__('twofactor-auth::twofactor-auth.failed')],
118+
]);
120119
}
121120

122121
/**

0 commit comments

Comments
 (0)