Skip to content

Commit 3a7a769

Browse files
committed
Secure login access
1 parent 0a7af98 commit 3a7a769

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

app/Http/Controllers/Auth/AuthController.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Http\Requests\Auth\LoginRequest;
99
use App\Http\Requests\Auth\RegisterRequest;
1010
use App\Repositories\UserRepository;
11+
use App\Services\MaxValueDelay;
1112

1213
class AuthController extends Controller {
1314

@@ -43,19 +44,27 @@ public function __construct(Guard $auth)
4344
* @param App\Http\Requests\LoginRequest $request
4445
* @return Response
4546
*/
46-
public function postLogin(LoginRequest $request)
47+
public function postLogin(LoginRequest $request, MaxValueDelay $maxValueDelay)
4748
{
4849
$logValue = $request->input('log');
4950

50-
$logAccess = filter_var($logValue, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
51+
if($maxValueDelay->check($logValue))
52+
{
53+
return redirect('/auth/login')
54+
->with('error', trans('front/login.maxattempt'));
55+
}
5156

57+
$logAccess = filter_var($logValue, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
58+
5259
$credentials = [$logAccess => $logValue, 'password' => $request->input('password')];
5360

5461
if ($this->auth->attempt($credentials, $request->has('memory')))
5562
{
5663
return redirect('/');
5764
}
5865

66+
$maxValueDelay->increment($logValue);
67+
5968
return redirect('/auth/login')
6069
->with('error', trans('front/login.credentials'))
6170
->withInput($request->only('email'));

app/Services/MaxValueDelay.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php namespace App\Services;
2+
3+
use Cache;
4+
5+
class MaxValueDelay {
6+
7+
/**
8+
* Time delay in minutes.
9+
*
10+
* @var int
11+
*/
12+
protected $timeRepeat = 4;
13+
14+
/**
15+
* Max repeat.
16+
*
17+
* @var int
18+
*/
19+
protected $max = 2;
20+
21+
/**
22+
* Add or increment a key in cache.
23+
*
24+
* @return void
25+
*/
26+
public function increment($key)
27+
{
28+
if(!Cache::add($key, 0, $this->timeRepeat))
29+
{
30+
Cache::increment($key);
31+
}
32+
}
33+
34+
/**
35+
* Check for max value.
36+
*
37+
* @return bool
38+
*/
39+
public function check($key)
40+
{
41+
return Cache::get($key) == $this->max;
42+
}
43+
44+
}

resources/lang/en/front/login.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
'register-info' => 'To register quickly just click on the button !',
1212
'registering' => 'I subscribe',
1313
'credentials' => 'These credentials do not match our records.',
14-
'log' => 'Your email or your user name'
14+
'log' => 'Your email or your user name',
15+
'maxattempt' => 'You have reached the maximum number of login attempts. Try again in a few minutes.'
1516
];

resources/lang/fr/front/login.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
'register-info' => 'Vous pouvez vous inscrire rapidement et gratuitement et pouvoir ainsi laisser des commentaires en cliquant sur le bouton ci-dessous.',
1212
'registering' => 'Je m\'inscris',
1313
'credentials' => 'Ces informations ne correspondent pas à celles que nous avons dans notre base de données.',
14-
'log' => 'Votre email ou votre nom d\'utilisateur'
14+
'log' => 'Votre email ou votre nom d\'utilisateur',
15+
'maxattempt' => 'Vous avez atteint le nombre maximum de tentatives de connexion. Réessayez dans quelques minutes.'
1516
];

0 commit comments

Comments
 (0)