|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TechWilk\Rota; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use DateTimeZone; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use TechWilk\Rota\AuthProvider\CallbackInterface; |
|
12
|
|
|
use TechWilk\Rota\AuthProvider\UsernamePasswordInterface; |
|
13
|
|
|
use TechWilk\Rota\Exception\UnknownUserException; |
|
14
|
|
|
|
|
15
|
|
|
class Authentication |
|
16
|
|
|
{ |
|
17
|
|
|
protected $container; |
|
18
|
|
|
protected $authProvider; |
|
19
|
|
|
protected $routesWhitelist; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(ContainerInterface $container, AuthProviderInterface $authProvider, $routesWhitelist) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->container = $container; |
|
24
|
|
|
$this->authProvider = $authProvider; |
|
25
|
|
|
$this->routesWhitelist = $routesWhitelist; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
/* Skip auth if uri is whitelisted. */ |
|
31
|
|
|
if ($this->uriInWhitelist($request)) { |
|
32
|
|
|
$response = $next($request, $response); |
|
33
|
|
|
|
|
34
|
|
|
return $response; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if ($this->isUserLoggedIn()) { |
|
38
|
|
|
$response = $next($request, $response); |
|
39
|
|
|
} else { |
|
40
|
|
|
$_SESSION['urlRedirect'] = strval($request->getUri()); |
|
41
|
|
|
$router = $this->container->get('router'); |
|
42
|
|
|
|
|
43
|
|
|
return $response->withStatus(302)->withHeader('Location', $router->pathFor('login')); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $response; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function uriInWhitelist(ServerRequestInterface $request) |
|
50
|
|
|
{ |
|
51
|
|
|
$route = $request->getAttribute('route'); |
|
52
|
|
|
if (!isset($route)) { |
|
53
|
|
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return in_array($route->getName(), $this->routesWhitelist); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function isUserLoggedIn() |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
return isset($_SESSION['userId']); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function loginAttempt(EmailAddress $email, $password) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!$this->numberOfLoginAttemptsIsOk($email)) { |
|
67
|
|
|
throw new Exception('Too many attempts.'); |
|
68
|
|
|
return false; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($this->authProvider->checkCredentials($email, $password) !== true) { |
|
|
|
|
|
|
72
|
|
|
$this->logFailedLoginAttempt($email); |
|
73
|
|
|
|
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
switch ($this->authProvider->getAuthProviderSlug()) { |
|
78
|
|
|
case 'onebody': |
|
79
|
|
|
if (is_null($this->authProvider->getUserId())) { |
|
|
|
|
|
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
$socialAuth = SocialAuthQuery::create() |
|
83
|
|
|
->filterByPlatform($this->authProvider->getAuthProviderSlug()) |
|
84
|
|
|
->filterBySocialId($this->authProvider->getUserId()) |
|
|
|
|
|
|
85
|
|
|
->filterByRevoked(false) |
|
86
|
|
|
->findOne(); |
|
87
|
|
|
if (is_null($socialAuth)) { |
|
88
|
|
|
$user = UserQuery::create()->filterByEmail($email)->findOne(); |
|
89
|
|
|
if (!is_null($user)) { |
|
90
|
|
|
$socialAuth = new SocialAuth(); |
|
91
|
|
|
$socialAuth->setUser($user); |
|
92
|
|
|
$socialAuth->setPlatform('onebody'); |
|
93
|
|
|
$socialAuth->setSocialId($this->authProvider->getUserId()); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
if (!is_null($socialAuth)) { |
|
97
|
|
|
$socialAuth->setMeta($this->authProvider->getMeta()); |
|
|
|
|
|
|
98
|
|
|
$socialAuth->save(); |
|
99
|
|
|
$user = $socialAuth->getUser(); |
|
100
|
|
|
} |
|
101
|
|
|
break; |
|
102
|
|
|
default: |
|
103
|
|
|
$user = UserQuery::create()->filterByEmail($email)->findOne(); |
|
104
|
|
|
break; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->loginSuccess($user); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function loginSuccess(User $user) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
if (is_null($user)) { |
|
113
|
|
|
throw new UnknownUserException('User not found in the database.'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$_SESSION['userId'] = $user->getId(); |
|
117
|
|
|
$user->setLastLogin(new DateTime()); |
|
118
|
|
|
$user->save(); |
|
119
|
|
|
|
|
120
|
|
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
private function numberOfLoginAttemptsIsOk($username) |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
$numberOfAllowedAttempts = 8; |
|
126
|
|
|
$lockOutInterval = 15; // mins |
|
127
|
|
|
|
|
128
|
|
|
$date = new DateTime("-$lockOutInterval minutes"); |
|
129
|
|
|
$date->setTimezone(new DateTimeZone('UTC')); |
|
130
|
|
|
|
|
131
|
|
|
// check ip address |
|
132
|
|
|
if (isset($_SERVER['REMOTE_ADDR'])) { |
|
133
|
|
|
$loginFailures = LoginFailureQuery::create() |
|
134
|
|
|
->filterByIpAddress($_SERVER['REMOTE_ADDR']) |
|
135
|
|
|
->filterByTimestamp(['min' => $date]) |
|
136
|
|
|
->count(); |
|
137
|
|
|
|
|
138
|
|
|
if ($loginFailures >= $numberOfAllowedAttempts) { |
|
139
|
|
|
$this->logFailedLoginAttempt($username); |
|
140
|
|
|
|
|
141
|
|
|
return false; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
// check user account |
|
146
|
|
|
$loginFailures = LoginFailureQuery::create() |
|
147
|
|
|
->filterByUsername($username) |
|
148
|
|
|
->filterByTimestamp(['min' => $date]) |
|
149
|
|
|
->count(); |
|
150
|
|
|
|
|
151
|
|
|
if ($loginFailures >= $numberOfAllowedAttempts) { |
|
152
|
|
|
$this->logFailedLoginAttempt($username); |
|
153
|
|
|
|
|
154
|
|
|
return false; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return true; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
private function logFailedLoginAttempt($username) |
|
|
|
|
|
|
161
|
|
|
{ |
|
162
|
|
|
$f = new LoginFailure(); |
|
163
|
|
|
$f->setUsername($username); |
|
164
|
|
|
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0'; |
|
165
|
|
|
$f->setIpAddress($ip); |
|
166
|
|
|
$f->save(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function currentUser() |
|
|
|
|
|
|
170
|
|
|
{ |
|
171
|
|
|
$userId = $_SESSION['userId']; |
|
172
|
|
|
|
|
173
|
|
|
return UserQuery::create()->findPK($userId); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function getResetPasswordUrl() |
|
177
|
|
|
{ |
|
178
|
|
|
if ($this->authProvider instanceof UsernamePasswordInterface) { |
|
179
|
|
|
return $this->authProvider->getResetPasswordUrl(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return ''; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function getCallbackUrl() |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->authProvider->getCallbackUrl(); |
|
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public function verifyCallback($args) |
|
191
|
|
|
{ |
|
192
|
|
|
if ($this->authProvider->verifyCallback($args)) { |
|
|
|
|
|
|
193
|
|
|
switch ($this->authProvider->getAuthProviderSlug()) { |
|
194
|
|
|
case 'facebook': |
|
195
|
|
|
$socialAuth = SocialAuthQuery::create() |
|
196
|
|
|
->filterByPlatform($this->authProvider->getAuthProviderSlug()) |
|
197
|
|
|
->filterBySocialId($this->authProvider->getUserId()) |
|
|
|
|
|
|
198
|
|
|
->filterByRevoked(false) |
|
199
|
|
|
->findOne(); |
|
200
|
|
|
|
|
201
|
|
|
if (!is_null($socialAuth)) { |
|
202
|
|
|
//$socialAuth->setMeta($this->authProvider->getMeta()); |
|
|
|
|
|
|
203
|
|
|
//$socialAuth->save(); |
|
|
|
|
|
|
204
|
|
|
$user = $socialAuth->getUser(); |
|
205
|
|
|
} |
|
206
|
|
|
break; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $this->loginSuccess($user); |
|
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
return false; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
public function getAuthProviderSlug() |
|
216
|
|
|
{ |
|
217
|
|
|
return $this->authProvider->getAuthProviderSlug(); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function isCallback() |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->authProvider instanceof CallbackInterface; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
public function isCredential() |
|
226
|
|
|
{ |
|
227
|
|
|
return $this->authProvider instanceof UsernamePasswordInterface; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public function getSocialUserId() |
|
231
|
|
|
{ |
|
232
|
|
|
return $this->authProvider->getUserId(); |
|
|
|
|
|
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
public function getMeta() |
|
236
|
|
|
{ |
|
237
|
|
|
return $this->authProvider->getMeta(); |
|
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: