|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Devpri\Tinre\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Auth\Events\Lockout; |
|
6
|
|
|
use Illuminate\Cache\RateLimiter; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Http\Response; |
|
9
|
|
|
use Illuminate\Support\Facades\Lang; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
use Illuminate\Validation\ValidationException; |
|
12
|
|
|
|
|
13
|
|
|
trait ThrottlesLogins |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Determine if the user has too many failed login attempts. |
|
17
|
|
|
* |
|
18
|
|
|
* @param \Illuminate\Http\Request $request |
|
19
|
|
|
* @return bool |
|
20
|
|
|
*/ |
|
21
|
3 |
|
protected function hasTooManyLoginAttempts(Request $request) |
|
22
|
|
|
{ |
|
23
|
3 |
|
return $this->limiter()->tooManyAttempts( |
|
24
|
3 |
|
$this->throttleKey($request), |
|
25
|
3 |
|
$this->maxAttempts() |
|
26
|
|
|
); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Increment the login attempts for the user. |
|
31
|
|
|
* |
|
32
|
|
|
* @param \Illuminate\Http\Request $request |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
2 |
|
protected function incrementLoginAttempts(Request $request) |
|
36
|
|
|
{ |
|
37
|
2 |
|
$this->limiter()->hit( |
|
38
|
2 |
|
$this->throttleKey($request), |
|
39
|
2 |
|
$this->decayMinutes() * 60 |
|
40
|
|
|
); |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Redirect the user after determining they are locked out. |
|
45
|
|
|
* |
|
46
|
|
|
* @param \Illuminate\Http\Request $request |
|
47
|
|
|
* @return void |
|
48
|
|
|
* |
|
49
|
|
|
* @throws \Illuminate\Validation\ValidationException |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function sendLockoutResponse(Request $request) |
|
52
|
|
|
{ |
|
53
|
|
|
$seconds = $this->limiter()->availableIn( |
|
54
|
|
|
$this->throttleKey($request) |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
throw ValidationException::withMessages([ |
|
58
|
|
|
'email' => [Lang::get('auth.throttle', [ |
|
59
|
|
|
'seconds' => $seconds, |
|
60
|
|
|
'minutes' => ceil($seconds / 60), |
|
61
|
|
|
])], |
|
62
|
|
|
])->status(Response::HTTP_TOO_MANY_REQUESTS); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Clear the login locks for the given user credentials. |
|
67
|
|
|
* |
|
68
|
|
|
* @param \Illuminate\Http\Request $request |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
1 |
|
protected function clearLoginAttempts(Request $request) |
|
72
|
|
|
{ |
|
73
|
1 |
|
$this->limiter()->clear($this->throttleKey($request)); |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Fire an event when a lockout occurs. |
|
78
|
|
|
* |
|
79
|
|
|
* @param \Illuminate\Http\Request $request |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function fireLockoutEvent(Request $request) |
|
83
|
|
|
{ |
|
84
|
|
|
event(new Lockout($request)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get the throttle key for the given request. |
|
89
|
|
|
* |
|
90
|
|
|
* @param \Illuminate\Http\Request $request |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
3 |
|
protected function throttleKey(Request $request) |
|
94
|
|
|
{ |
|
95
|
3 |
|
return Str::lower($request->input('email')).'|'.$request->ip(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get the rate limiter instance. |
|
100
|
|
|
* |
|
101
|
|
|
* @return \Illuminate\Cache\RateLimiter |
|
102
|
|
|
*/ |
|
103
|
3 |
|
protected function limiter() |
|
104
|
|
|
{ |
|
105
|
3 |
|
return app(RateLimiter::class); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get the maximum number of attempts to allow. |
|
110
|
|
|
* |
|
111
|
|
|
* @return int |
|
112
|
|
|
*/ |
|
113
|
3 |
|
public function maxAttempts() |
|
114
|
|
|
{ |
|
115
|
3 |
|
return property_exists($this, 'maxAttempts') ? $this->maxAttempts : 5; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the number of minutes to throttle for. |
|
120
|
|
|
* |
|
121
|
|
|
* @return int |
|
122
|
|
|
*/ |
|
123
|
2 |
|
public function decayMinutes() |
|
124
|
|
|
{ |
|
125
|
2 |
|
return property_exists($this, 'decayMinutes') ? $this->decayMinutes : 1; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|