/
Authenticator.php
266 lines (243 loc) · 7.49 KB
/
Authenticator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* Definition of Authenticator.php
*
* @copyright 2015-today Justso GmbH
* @author j.schirrmacher@justso.de
*/
namespace justso\justauth;
use justso\justapi\NotFoundException;
use justso\justapi\RequestHelper;
use justso\justapi\SystemEnvironmentInterface;
/**
* Class Authenticator
*/
class Authenticator
{
/**
* @var SystemEnvironmentInterface
*/
protected $env;
/**
* @var Session
*/
private $session;
/**
* @var array
*/
private $authConf = null;
private $errors = [];
public function __construct(SystemEnvironmentInterface $env)
{
$this->env = $env;
$this->session = $env->getDIC()->get('Auth.Session');
}
/**
* Authenticates a user via the provided credentials.
* If the 'auto-register' auth config option is set, unknown e-mail addresses are used to create new users.
*/
public function auth()
{
$userRepository = $this->getUserRepository();
$request = $this->env->getRequestHelper();
$user = null;
try {
$user = $userRepository->getByEmail($request->getEMailParam('email'));
$password = $request->getParam('password', '', true);
if ($password === '') {
$this->requestActivation($user, $request);
} elseif ($user->checkPassword($password)) {
$this->session->loginUser($user);
} else {
$user = null;
$this->errors[] = 'auth-failed';
}
} catch (NotFoundException $e) {
if ($this->getAuthConf('auto-register')) {
/** @var UserInterface $user */
$user = $this->env->getDIC()->get('UserInterface');
$user->setFromRequest($request);
$userRepository->persist($user);
if ($this->getAuthConf('needs-activation')) {
$this->requestActivation($user, $request);
} else {
$user->setActive(true);
}
if ($this->getAuthConf('login-new-users')) {
$this->session->loginUser($user, true);
}
} else {
$this->errors[] = 'auth-failed';
}
}
return $user;
}
/**
* Checks if the user is authenticated.
*
* @return bool
*/
public function isAuth()
{
return $this->session->isAuth() && !($this->getAuthConf('needs-activation') && $this->isActivationPending());
}
/**
* Checks the given access code, and if it is valid, the user is authenticated and activated.
*
* @param string $code
* @return string The destination string which was registered for the token.
* @throws NotFoundException if the token is not valid.
*/
public function activate($code)
{
$userRepository = $this->getUserRepository();
$user = $userRepository->getByAccessCode($code);
$this->getLoginNotificator()->activateUser($user);
$url = $user->getDestination();
//$user->setToken(null);
//$user->setDestination(null);
$user->setActive(true);
$userRepository->persist($user);
$this->session->loginUser($user);
return $url;
}
/**
* Sends an activation link to the specified user.
*
* @param UserInterface $user
* @param RequestHelper $request
*/
private function requestActivation(UserInterface $user, RequestHelper $request)
{
$userRepository = $this->getUserRepository();
$code = bin2hex(openssl_random_pseudo_bytes(20));
$user->setToken($code);
$user->setDestination($request->getParam('page', '', true));
$userRepository->persist($user);
$this->session->loginUser($user, true);
$link = $this->env->getBootstrap()->getApiUrl() . '/activate?c=' . $code;
$this->getLoginNotificator()->sendActivation($user, $code, $link, $request);
}
/**
* Returns the value of the specified authentication configuration value if it is set or the $default.
*
* @param string $name
* @param mixed $default
* @return mixed
*/
protected function getAuthConf($name, $default = false)
{
if ($this->authConf === null) {
$config = $this->env->getBootstrap()->getConfiguration();
if (!isset($config['auth'])) {
$this->authConf = [
'auto-register' => true,
'needs-activation' => true,
];
} else {
$this->authConf = $config['auth'];
}
}
if (isset($this->authConf[$name])) {
return $this->authConf[$name];
} else {
return $default;
}
}
/**
* @return int|null
*/
public function getUserId()
{
$user = $this->session->getCurrentUser();
return $user !== null ? $user->getId() : null;
}
/**
* Returns the user object for the current user from the repository.
*
* @return UserInterface
*/
public function getUser()
{
$user = $this->session->getCurrentUser();
if ($user === null) {
return null;
}
if ($this->session->isCloned()) {
/** @var UserRepositoryInterface $userRepo */
$userRepo = $this->env->getDIC()->get('UserRepositoryInterface');
$user = $userRepo->getById($user->getId());
}
return $user;
}
/**
* If a user is authenticated, the return value specifies if the user has just registered.
* If no user is authenticated, the return value is null.
*
* @return bool|null
*/
public function isNewUser()
{
return $this->session->hasJustRegistered();
}
/**
* If a user is authenticated, the return value specifies if there is an activation pending.
* If no user is authenticated, the return value is null.
*
* @return bool|null
*/
public function isActivationPending()
{
$user = $this->session->getCurrentUser();
return $user !== null ? !$user->isActive() : null;
}
/**
* @return UserRepositoryInterface
*/
private function getUserRepository()
{
return $this->env->getDIC()->get('UserRepositoryInterface');
}
/**
* @return LoginNotificatorInterface
*/
private function getLoginNotificator()
{
return $this->env->getDIC()->get('LoginNotificatorInterface');
}
/**
* Returns an information structure containing data about the current user
*
* @return array
*/
public function getAuthInfo()
{
$activationPending = $this->isActivationPending();
$newUser = $this->isNewUser() && $this->getAuthConf('login-new-users');
$result = [
'errors' => $this->errors,
'userid' => null
];
$this->errors = [];
if ($newUser) {
$result['new_user'] = true;
}
if ($activationPending) {
$result['pending_activation'] = true;
}
if (!$activationPending || $newUser) {
$result['userid'] = $this->getUserId();
if ($this->getUserId()) {
$result['username'] = $this->getUser()->getFullName();
}
}
return $result;
}
/**
* Log out current user
*/
public function logout()
{
$this->session->logoutCurrentUser();
}
}