1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Claudio Cardinale <[email protected]> |
5
|
|
|
* Date: 18/11/15 |
6
|
|
|
* Time: 16.49 |
7
|
|
|
* This program is free software; you can redistribute it and/or |
8
|
|
|
* modify it under the terms of the GNU General Public License |
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
10
|
|
|
* of the License, or (at your option) any later version. |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with this program; if not, write to the Free Software |
17
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Tymon\JWTAuth\Support\auth; |
21
|
|
|
|
22
|
|
|
use Illuminate\Cache\RateLimiter; |
23
|
|
|
use Illuminate\Http\JsonResponse; |
24
|
|
|
use Illuminate\Http\Request; |
25
|
|
|
use Illuminate\Support\Facades\Lang; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class ThrottlesLogins |
29
|
|
|
* @package Tymon\JWTAuth\Support\auth |
30
|
|
|
* @author Claudio Cardinale <[email protected]> |
31
|
|
|
* @copyright 2015 Claudio Cardinale |
32
|
|
|
* @version 1.0.0 |
33
|
|
|
*/ |
34
|
|
|
trait ThrottlesLogins |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Determine if the user has too many failed login attempts. |
38
|
|
|
* |
39
|
|
|
* @param \Illuminate\Http\Request $request |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
protected function hasTooManyLoginAttempts(Request $request) |
43
|
|
|
{ |
44
|
|
|
return app(RateLimiter::class)->tooManyAttempts( |
45
|
|
|
$this->getInputs($request).$request->ip(), |
46
|
|
|
$this->maxLoginAttempts(), $this->lockoutTime() / 60 |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Increment the login attempts for the user. |
52
|
|
|
* |
53
|
|
|
* @param \Illuminate\Http\Request $request |
54
|
|
|
* @return int |
55
|
|
|
*/ |
56
|
|
|
protected function incrementLoginAttempts(Request $request) |
57
|
|
|
{ |
58
|
|
|
app(RateLimiter::class)->hit( |
59
|
|
|
$this->getInputs($request).$request->ip() |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Determine how many retries are left for the user. |
65
|
|
|
* |
66
|
|
|
* @param \Illuminate\Http\Request $request |
67
|
|
|
* @return int |
68
|
|
|
*/ |
69
|
|
|
protected function retriesLeft(Request $request) |
70
|
|
|
{ |
71
|
|
|
$attempts = app(RateLimiter::class)->attempts( |
72
|
|
|
$this->getInputs($request).$request->ip() |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
return $this->maxLoginAttempts() - $attempts + 1; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Redirect the user after determining they are locked out. |
80
|
|
|
* |
81
|
|
|
* @param \Illuminate\Http\Request $request |
82
|
|
|
* @return \Illuminate\Http\RedirectResponse |
83
|
|
|
*/ |
84
|
|
|
protected function sendLockoutResponse(Request $request) |
85
|
|
|
{ |
86
|
|
|
$seconds = app(RateLimiter::class)->availableIn( |
87
|
|
|
$this->getInputs($request).$request->ip() |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$usernames = $this->loginUsername(); |
|
|
|
|
91
|
|
|
if(!is_array($usernames)) { |
92
|
|
|
$usernames = [$usernames]; |
93
|
|
|
} |
94
|
|
|
return new JsonResponse([ |
95
|
|
|
implode('.', $usernames) => [$this->getLockoutErrorMessage($seconds)], |
96
|
|
|
], 422); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the login lockout error message. |
101
|
|
|
* |
102
|
|
|
* @param int $seconds |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function getLockoutErrorMessage($seconds) |
106
|
|
|
{ |
107
|
|
|
return Lang::has('auth.throttle') |
108
|
|
|
? Lang::get('auth.throttle', ['seconds' => $seconds]) |
109
|
|
|
: 'Too many login attempts. Please try again in '.$seconds.' seconds.'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Clear the login locks for the given user credentials. |
114
|
|
|
* |
115
|
|
|
* @param \Illuminate\Http\Request $request |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
protected function clearLoginAttempts(Request $request) |
119
|
|
|
{ |
120
|
|
|
app(RateLimiter::class)->clear( |
121
|
|
|
$this->getInputs($request).$request->ip() |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the maximum number of login attempts for delaying further attempts. |
127
|
|
|
* |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
|
|
protected function maxLoginAttempts() |
131
|
|
|
{ |
132
|
|
|
return property_exists($this, 'maxLoginAttempts') ? $this->maxLoginAttempts : 5; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* The number of seconds to delay further login attempts. |
137
|
|
|
* |
138
|
|
|
* @return int |
139
|
|
|
*/ |
140
|
|
|
protected function lockoutTime() |
141
|
|
|
{ |
142
|
|
|
return property_exists($this, 'lockoutTime') ? $this->lockoutTime : 60; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* get username inputs as string |
148
|
|
|
* |
149
|
|
|
* @param Request $request |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
private function getInputs(Request $request) |
153
|
|
|
{ |
154
|
|
|
$usernames = $this->loginUsername(); |
|
|
|
|
155
|
|
|
if(!is_array($usernames)) { |
156
|
|
|
$usernames = [$usernames]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$ret = ''; |
160
|
|
|
foreach($usernames as $username) |
161
|
|
|
$ret .= $request->input($username).'.'; |
162
|
|
|
$ret = substr($ret, 0, -1); |
163
|
|
|
|
164
|
|
|
return $ret; |
165
|
|
|
} |
166
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.