-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Auth.php
270 lines (224 loc) · 8.66 KB
/
Auth.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
267
268
269
270
<?php
namespace Directus\API\Routes\A1;
use Directus\Application\Route;
use Directus\Database\TableGateway\DirectusActivityTableGateway;
use Directus\Database\TableGateway\DirectusGroupsTableGateway;
use Directus\Database\TableGateway\DirectusUsersTableGateway;
use Directus\Mail\Mail;
use Directus\Services\AuthService;
use Directus\Util\DateUtils;
use Directus\Util\StringUtils;
use Directus\View\JsonView;
use Zend\Db\TableGateway\TableGateway;
class Auth extends Route
{
public function requestToken()
{
$response = [
'success' => false,
'error' => [
'message' => __t('incorrect_email_or_password')
]
];
$request = $this->app->request();
$email = $request->post('email');
$password = $request->post('password');
if ($email && $password) {
$authService = new AuthService($this->app);
$accessToken = $authService->requestToken($email, $password);
if ($accessToken) {
unset($response['error']);
$response['success'] = true;
$response['data'] = [
'token' => $accessToken
];
}
}
return $this->app->response($response);
}
public function login()
{
$app = $this->app;
$auth = $this->app->container->get('auth');
$ZendDb = $this->app->container->get('zenddb');
$acl = $this->app->container->get('acl');
$response = [
'error' => [
'message' => __t('incorrect_email_or_password')
],
'success' => false,
];
if ($auth->loggedIn()) {
$response['success'] = true;
unset($response['error']);
return $this->app->response($response);
}
$req = $app->request();
$email = $req->post('email');
$password = $req->post('password');
$Users = new DirectusUsersTableGateway($ZendDb, null);
$user = $Users->findOneBy('email', $email);
if (!$user) {
return $this->app->response($response);
}
// ------------------------------
// Check if group needs whitelist
$groupId = $user['group'];
$directusGroupsTableGateway = new DirectusGroupsTableGateway($ZendDb, null);
if (!$directusGroupsTableGateway->acceptIP($groupId, $app->request->getIp())) {
return $this->app->response([
'success' => false,
'error' => [
'message' => 'Request not allowed from IP address',
]
]);
}
// =============================================================================
// Fetch information about the latest version to the admin
// when they first log in.
// =============================================================================
if (is_null($user['last_login']) && $user['group'] == 1) {
$_SESSION['first_version_check'] = true;
}
// @todo: Login should fail on correct information when user is not active.
$response['success'] = $auth->login($user['id'], $user['password'], $user['salt'], $password);
// When the credentials are correct but the user is Inactive
$isUserActive = false;
// TODO: Add a method in RowGateway to check whether the user is active or not
// TODO: Add information about the user status
if ($user['status'] == STATUS_ACTIVE_NUM) {
$isUserActive = true;
}
if ($response['success'] && !$isUserActive) {
$auth->logout();
$response['success'] = false;
$response['error']['message'] = __t('login_error_user_is_not_active');
return $this->app->response($response);
}
if ($response['success']) {
// Set logged user to the ACL
$acl->setUserId($user['id']);
$acl->setGroupId($user['group']);
$app->hookEmitter->run('directus.authenticated', [$app, $user]);
$app->hookEmitter->run('directus.authenticated.admin', [$app, $user]);
unset($response['message']);
$response['last_page'] = json_decode($user['last_page']);
$userSession = $auth->getUserInfo();
$set = [
'ip' => get_request_ip(),
'last_login' => DateUtils::now(),
'access_token' => $userSession['access_token']
];
$where = ['id' => $user['id']];
$updateResult = $Users->update($set, $where);
$Activity = new DirectusActivityTableGateway($ZendDb, null);
$Activity->recordLogin($user['id']);
}
return $this->app->response($response);
}
public function logout($inactive = null)
{
$app = $this->app;
$auth = $app->container->get('auth');
if ($auth->loggedIn()) {
$auth->logout();
}
if ($inactive) {
$app->redirect(get_directus_path('/login.php?inactive=1'));
} else {
$app->redirect(get_directus_path('/login.php'));
}
}
public function resetPassword($token)
{
$app = $this->app;
$auth = $app->container->get('auth');
$ZendDb = $app->container->get('zenddb');
$DirectusUsersTableGateway = new DirectusUsersTableGateway($ZendDb, null);
$user = $DirectusUsersTableGateway->findOneBy('reset_token', $token);
if (!$user) {
$app->halt(200, __t('password_reset_incorrect_token'));
}
$expirationDate = new \DateTime($user['reset_expiration'], new \DateTimeZone('UTC'));
if (DateUtils::hasPassed($expirationDate)) {
$app->halt(200, __t('password_reset_expired_token'));
}
$password = StringUtils::randomString();
$set = [];
// @NOTE: this is not being used for hashing the password anymore
$set['salt'] = StringUtils::randomString();
$set['password'] = $auth->hashPassword($password, $set['salt']);
$set['reset_token'] = '';
// Skip ACL
$DirectusUsersTableGateway = new \Zend\Db\TableGateway\TableGateway('directus_users', $ZendDb);
$affectedRows = $DirectusUsersTableGateway->update($set, ['id' => $user['id']]);
if (1 !== $affectedRows) {
$app->halt(200, __t('password_reset_error'));
}
send_forgot_password_email($user, $password);
$app->halt(200, __t('password_reset_new_temporary_password_sent'));
}
public function forgotPassword()
{
$app = $this->app;
$ZendDb = $app->container->get('zenddb');
$email = $app->request()->post('email');
if (!isset($email)) {
return $this->app->response([
'success' => false,
'error' => [
'message' => __t('password_forgot_invalid_email')
]
]);
}
$DirectusUsersTableGateway = new DirectusUsersTableGateway($ZendDb, null);
$user = $DirectusUsersTableGateway->findOneBy('email', $email);
if (false === $user) {
return $this->app->response([
'success' => false,
'error' => [
'message' => __t('password_forgot_no_account_found')
]
]);
}
$set = [];
$set['reset_token'] = StringUtils::randomString(30);
$set['reset_expiration'] = DateUtils::inDays(2);
// Skip ACL
$DirectusUsersTableGateway = new \Zend\Db\TableGateway\TableGateway('directus_users', $ZendDb);
$affectedRows = $DirectusUsersTableGateway->update($set, ['id' => $user['id']]);
if (1 !== $affectedRows) {
return $this->app->response([
'success' => false
]);
}
send_reset_password_email($user, $set['reset_token']);
return $this->app->response([
'success' => true
]);
}
public function permissions()
{
$acl = $this->app->container->get('acl');
return $this->app->response([
'data' => $acl->getGroupPrivileges()
]);
}
public function session()
{
return $this->app->response($_SESSION);
}
public function clearSession()
{
$_SESSION = [];
if (ini_get('session.use_cookies')) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params['path'], $params['domain'],
$params['secure'], $params['httponly']
);
}
session_destroy();
return $this->app->response($_SESSION);
}
}