/
backend_login.php
297 lines (255 loc) · 10.7 KB
/
backend_login.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
* @package redaxo\core\login
*
* @method null|rex_user getUser()
* @method null|rex_user getImpersonator()
*/
class rex_backend_login extends rex_login
{
public const SYSTEM_ID = 'backend_login';
private const SESSION_PASSWORD_CHANGE_REQUIRED = 'password_change_required';
/** @var string */
private $tableName;
/** @var bool|null */
private $stayLoggedIn;
/** @var rex_backend_password_policy */
private $passwordPolicy;
public function __construct()
{
parent::__construct();
$tableName = rex::getTablePrefix() . 'user';
$this->setSqlDb(1);
$this->setSystemId(self::SYSTEM_ID);
$this->setSessionDuration(rex::getProperty('session_duration'));
$qry = 'SELECT * FROM ' . $tableName;
$this->setUserQuery($qry . ' WHERE id = :id AND status = 1');
$this->setImpersonateQuery($qry . ' WHERE id = :id');
$this->passwordPolicy = rex_backend_password_policy::factory();
$loginPolicy = $this->getLoginPolicy();
// XXX because with concat the time into the sql query, users of this class should use checkLogin() immediately after creating the object.
$qry .= ' WHERE
status = 1
AND login = :login
AND login_tries < '. $loginPolicy->getMaxTriesUntilBlock() .'
AND (
login_tries < ' . $loginPolicy->getMaxTriesUntilDelay() . '
OR
login_tries >= ' . $loginPolicy->getMaxTriesUntilDelay() . ' AND lasttrydate < "' . rex_sql::datetime(time() - $loginPolicy->getReloginDelay()) . '"
)';
if ($blockAccountAfter = $this->passwordPolicy->getBlockAccountAfter()) {
$datetime = (new DateTimeImmutable())->sub($blockAccountAfter);
$qry .= ' AND password_changed > "'.$datetime->format(rex_sql::FORMAT_DATETIME).'"';
}
$this->setLoginQuery($qry);
$this->tableName = $tableName;
}
/**
* @param bool $stayLoggedIn
* @return void
*/
public function setStayLoggedIn($stayLoggedIn = false)
{
if (!$this->getLoginPolicy()->isStayLoggedInEnabled()) {
$stayLoggedIn = false;
}
$this->stayLoggedIn = $stayLoggedIn;
}
public function checkLogin()
{
$sql = rex_sql::factory();
$userId = $this->getSessionVar(rex_login::SESSION_USER_ID);
$cookiename = self::getStayLoggedInCookieName();
$loggedInViaCookie = false;
if ($cookiekey = rex_cookie($cookiename, 'string')) {
if (!$userId) {
$sql->setQuery('SELECT id, password FROM ' . rex::getTable('user') . ' WHERE cookiekey = ? LIMIT 1', [$cookiekey]);
if (1 == $sql->getRows()) {
$this->setSessionVar(rex_login::SESSION_USER_ID, $sql->getValue('id'));
$this->setSessionVar(rex_login::SESSION_PASSWORD, $sql->getValue('password'));
self::setStayLoggedInCookie($cookiekey);
$loggedInViaCookie = true;
} else {
self::deleteStayLoggedInCookie();
}
}
$this->setSessionVar(rex_login::SESSION_LAST_ACTIVITY, time());
}
$check = parent::checkLogin();
if ($check) {
// gelungenen versuch speichern | login_tries = 0
if ('' != $this->userLogin || !$userId) {
self::regenerateSessionId();
$params = [];
$add = '';
if ($this->stayLoggedIn || $cookiekey) {
$cookiekey = (string) $this->user->getValue('cookiekey');
if (!$cookiekey) {
$cookiekey = sha1($this->systemId . time() . $this->userLogin);
$add = 'cookiekey = ?, ';
$params[] = $cookiekey;
}
self::setStayLoggedInCookie($cookiekey);
}
if (self::passwordNeedsRehash($this->user->getValue('password'))) {
$add .= 'password = ?, ';
$params[] = self::passwordHash($this->userPassword, true);
}
array_push($params, rex_sql::datetime(), rex_sql::datetime(), session_id(), $this->userLogin);
$sql->setQuery('UPDATE ' . $this->tableName . ' SET ' . $add . 'login_tries=0, lasttrydate=?, lastlogin=?, session_id=? WHERE login=? LIMIT 1', $params);
rex_user_session::getInstance()->storeCurrentSession();
}
assert($this->user instanceof rex_sql);
$this->user = rex_user::fromSql($this->user);
if ($this->impersonator instanceof rex_sql) {
$this->impersonator = rex_user::fromSql($this->impersonator);
}
if ($loggedInViaCookie || $this->userLogin) {
if ($this->user->getValue('password_change_required')) {
$this->setSessionVar(self::SESSION_PASSWORD_CHANGE_REQUIRED, true);
} elseif ($forceRenewAfter = $this->passwordPolicy->getForceRenewAfter()) {
$datetime = (new DateTimeImmutable())->sub($forceRenewAfter);
if (strtotime($this->user->getValue('password_changed')) < $datetime->getTimestamp()) {
$this->setSessionVar(self::SESSION_PASSWORD_CHANGE_REQUIRED, true);
}
}
}
rex_user_session::getInstance()->updateLastActivity();
} else {
// fehlversuch speichern | login_tries++
if ('' != $this->userLogin) {
$sql->setQuery('SELECT login_tries FROM ' . $this->tableName . ' WHERE login=? LIMIT 1', [$this->userLogin]);
if ($sql->getRows() > 0) {
$loginPolify = $this->getLoginPolicy();
$loginTries = $sql->getValue('login_tries');
$this->increaseLoginTries();
if ($loginTries >= $loginPolify->getMaxTriesUntilDelay() - 1) {
$time = $loginPolify->getReloginDelay();
$hours = floor($time / 3600);
$mins = floor(($time - ($hours * 3600)) / 60);
$secs = $time % 60;
$formatted = ($hours ? $hours . 'h ' : '') . ($hours || $mins ? $mins . 'min ' : '') . $secs . 's';
$this->message .= ' ' . rex_i18n::rawMsg('login_wait', '<strong data-time="' . $time . '">' . $formatted . '</strong>');
}
}
}
}
// check if session was killed only if the user is logged in
if ($check) {
$sql->setQuery('SELECT 1 FROM '.rex::getTable('user_session').' where session_id = ?', [session_id()]);
if (0 === $sql->getRows()) {
$check = false;
$this->message = rex_i18n::msg('login_session_expired');
rex_csrf_token::removeAll();
}
}
if ($this->isLoggedOut() && '' != $userId) {
$sql->setQuery('UPDATE ' . $this->tableName . ' SET session_id="" WHERE id=? LIMIT 1', [$userId]);
self::deleteStayLoggedInCookie();
rex_user_session::getInstance()->clearCurrentSession();
}
return $check;
}
public function increaseLoginTries(): void
{
$sql = rex_sql::factory();
$sql->setQuery('UPDATE ' . $this->tableName . ' SET login_tries=login_tries+1,session_id="",lasttrydate=? WHERE login=? LIMIT 1', [rex_sql::datetime(), $this->userLogin]);
}
public function requiresPasswordChange(): bool
{
return (bool) $this->getSessionVar(self::SESSION_PASSWORD_CHANGE_REQUIRED, false);
}
/**
* @param null|string $passwordHash Passing `null` or ommitting this param is DEPRECATED
*/
public function changedPassword(
#[\SensitiveParameter]
?string $passwordHash = null
): void {
$this->setSessionVar(self::SESSION_PASSWORD_CHANGE_REQUIRED, false);
if (null !== $passwordHash) {
parent::changedPassword($passwordHash);
}
}
/**
* @return void
*/
public static function deleteSession()
{
self::startSession();
unset($_SESSION[static::getSessionNamespace()][self::SYSTEM_ID]);
self::deleteStayLoggedInCookie();
rex_csrf_token::removeAll();
}
private static function setStayLoggedInCookie(string $cookiekey): void
{
$sessionConfig = rex::getProperty('session', [])['backend']['cookie'] ?? [];
rex_response::sendCookie(self::getStayLoggedInCookieName(), $cookiekey, [
'expires' => strtotime('+1 year'),
'secure' => $sessionConfig['secure'] ?? false,
'samesite' => $sessionConfig['samesite'] ?? 'lax',
]);
}
private static function deleteStayLoggedInCookie()
{
rex_response::sendCookie(self::getStayLoggedInCookieName(), '');
}
/**
* @return string
*/
private static function getStayLoggedInCookieName()
{
return 'rex_user_' . sha1(rex::getProperty('instname'));
}
/**
* @return bool
*/
public static function hasSession()
{
// try to fast-fail, so we dont need to start a session in all cases (which would require a session lock...)
if (!isset($_COOKIE[session_name()])) {
return false;
}
self::startSession();
return ($_SESSION[static::getSessionNamespace()][self::SYSTEM_ID][rex_login::SESSION_USER_ID] ?? 0) > 0;
}
/**
* Creates the user object if it does not already exist.
*
* Helpful if you want to check permissions of the backend user in frontend.
* If you only want to know if there is any backend session, use {@link rex_backend_login::hasSession()}.
*
* @return rex_user|null
*/
public static function createUser()
{
if (!self::hasSession()) {
return null;
}
if ($user = rex::getUser()) {
return $user;
}
$login = new self();
rex::setProperty('login', $login);
if ($login->checkLogin()) {
$user = $login->getUser();
rex::setProperty('user', $user);
return $user;
}
return null;
}
/**
* returns the backends session namespace.
*
* @return string
*/
protected static function getSessionNamespace()
{
return rex::getProperty('instname'). '_backend';
}
public function getLoginPolicy(): rex_login_policy
{
$loginPolicy = (array) rex::getProperty('backend_login_policy', []);
return new rex_login_policy($loginPolicy);
}
}