1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\Auth\Services\Throttling; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Cache\Repository as Cache; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Login throttler using cache. |
9
|
|
|
* |
10
|
|
|
* @author Andrea Marco Sartori |
11
|
|
|
*/ |
12
|
|
|
class CachingThrottler implements ThrottlerInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Andrea Marco Sartori |
17
|
|
|
* @var Illuminate\Contracts\Cache\Repository $cache Cache repository. |
18
|
|
|
*/ |
19
|
|
|
protected $cache; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Andrea Marco Sartori |
23
|
|
|
* @var string $key Source where attempts come from. |
24
|
|
|
*/ |
25
|
|
|
protected $key; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author Andrea Marco Sartori |
29
|
|
|
* @var string $lockOutKey Key to store lock out time in. |
30
|
|
|
*/ |
31
|
|
|
protected $lockOutKey; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set the dependencies. |
35
|
|
|
* |
36
|
|
|
* @author Andrea Marco Sartori |
37
|
|
|
* @param Illuminate\Contracts\Cache\Repository $cache |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Cache $cache) |
41
|
|
|
{ |
42
|
|
|
$this->cache = $cache; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set univocal source where attempts come from. |
47
|
|
|
* |
48
|
|
|
* @author Andrea Marco Sartori |
49
|
|
|
* @return boolean |
50
|
|
|
*/ |
51
|
|
|
public function setSource($source) |
52
|
|
|
{ |
53
|
|
|
$this->key = $source; |
54
|
|
|
|
55
|
|
|
$this->lockOutKey = "{$source}:lockout"; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Determine whether the user has been locked out. |
60
|
|
|
* |
61
|
|
|
* @author Andrea Marco Sartori |
62
|
|
|
* @return boolean |
63
|
|
|
*/ |
64
|
|
|
public function lockedOut() |
65
|
|
|
{ |
66
|
|
|
return $this->cache->has($this->lockOutKey); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Retrieve the number of remaining seconds before the next attempt. |
71
|
|
|
* |
72
|
|
|
* @author Andrea Marco Sartori |
73
|
|
|
* @return integer |
74
|
|
|
*/ |
75
|
|
|
public function getRemainingSeconds() |
76
|
|
|
{ |
77
|
|
|
return $this->cache->get($this->lockOutKey) - time(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Increment the number of failed attempts. |
82
|
|
|
* |
83
|
|
|
* @author Andrea Marco Sartori |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function incrementAttempts() |
87
|
|
|
{ |
88
|
|
|
$this->cache->add($this->key, 0, $this->getExpiry()); |
89
|
|
|
|
90
|
|
|
$this->cache->increment($this->key); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Retrieve the minutes after which keys expire. |
95
|
|
|
* |
96
|
|
|
* @author Andrea Marco Sartori |
97
|
|
|
* @return integer |
98
|
|
|
*/ |
99
|
|
|
private function getExpiry() |
100
|
|
|
{ |
101
|
|
|
return (int) $this->getDelay() / 60; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Retrieve the seconds to wait before the next attempt. |
106
|
|
|
* |
107
|
|
|
* @author Andrea Marco Sartori |
108
|
|
|
* @return integer |
109
|
|
|
*/ |
110
|
|
|
private function getDelay() |
111
|
|
|
{ |
112
|
|
|
return config('_auth.login.throttling.delay'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Determine whether a user has performed too many attempts. |
117
|
|
|
* |
118
|
|
|
* @author Andrea Marco Sartori |
119
|
|
|
* @return boolean |
120
|
|
|
*/ |
121
|
|
|
public function tooManyAttempts() |
122
|
|
|
{ |
123
|
|
|
$maximum = config('_auth.login.throttling.max_attempts'); |
124
|
|
|
|
125
|
|
|
return $this->cache->get($this->key, 0) > $maximum; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Lock a user out. |
130
|
|
|
* |
131
|
|
|
* @author Andrea Marco Sartori |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
public function lockOut() |
135
|
|
|
{ |
136
|
|
|
$this->resetAttempts(); |
137
|
|
|
|
138
|
|
|
$this->cache->add($this->lockOutKey, $this->getDelay() + time(), $this->getExpiry()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Reset the attempts counter. |
143
|
|
|
* |
144
|
|
|
* @author Andrea Marco Sartori |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
public function resetAttempts() |
148
|
|
|
{ |
149
|
|
|
$this->cache->forget($this->key); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|