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
|
<?php
/**
* This class handles all authentication process.
*/
class FreshRSS_Auth {
/**
* Determines if user is connected.
*/
const DEFAULT_COOKIE_DURATION = 7776000;
/** @var bool */
private static $login_ok = false;
/**
* This method initializes authentication system.
*/
public static function init(): bool {
if (isset($_SESSION['REMOTE_USER']) && $_SESSION['REMOTE_USER'] !== httpAuthUser()) {
//HTTP REMOTE_USER has changed
self::removeAccess();
}
self::$login_ok = Minz_Session::param('loginOk', false);
$current_user = Minz_User::name();
if ($current_user === null) {
$current_user = FreshRSS_Context::$system_conf->default_user;
Minz_Session::_params([
Minz_User::CURRENT_USER => $current_user,
'csrf' => false,
]);
}
if (self::$login_ok) {
self::giveAccess();
} elseif (self::accessControl() && self::giveAccess()) {
FreshRSS_UserDAO::touch();
} else {
// Be sure all accesses are removed!
self::removeAccess();
}
return self::$login_ok;
}
/**
* This method checks if user is allowed to connect.
*
* Required session parameters are also set in this method (such as
* currentUser).
*
* @return bool true if user can be connected, false otherwise.
*/
private static function accessControl(): bool {
$auth_type = FreshRSS_Context::$system_conf->auth_type;
switch ($auth_type) {
case 'form':
$credentials = FreshRSS_FormAuth::getCredentialsFromCookie();
$current_user = '';
if (isset($credentials[1])) {
$current_user = trim($credentials[0]);
Minz_Session::_params([
Minz_User::CURRENT_USER => $current_user,
'passwordHash' => trim($credentials[1]),
'csrf' => false,
]);
}
return $current_user != '';
case 'http_auth':
$current_user = httpAuthUser();
if ($current_user == '') {
return false;
}
$login_ok = FreshRSS_UserDAO::exists($current_user);
if (!$login_ok && FreshRSS_Context::$system_conf->http_auth_auto_register) {
$email = null;
if (FreshRSS_Context::$system_conf->http_auth_auto_register_email_field !== '' &&
isset($_SERVER[FreshRSS_Context::$system_conf->http_auth_auto_register_email_field])) {
$email = $_SERVER[FreshRSS_Context::$system_conf->http_auth_auto_register_email_field];
}
$language = Minz_Translate::getLanguage(null, Minz_Request::getPreferredLanguages(), FreshRSS_Context::$system_conf->language);
Minz_Translate::init($language);
$login_ok = FreshRSS_user_Controller::createUser($current_user, $email, '', [
'language' => $language,
]);
}
if ($login_ok) {
Minz_Session::_params([
Minz_User::CURRENT_USER => $current_user,
'csrf' => false,
]);
}
return $login_ok;
case 'none':
return true;
default:
// TODO load extension
return false;
}
}
/**
* Gives access to the current user.
*/
public static function giveAccess(): bool {
FreshRSS_Context::initUser();
if (FreshRSS_Context::$user_conf == null) {
self::$login_ok = false;
return false;
}
switch (FreshRSS_Context::$system_conf->auth_type) {
case 'form':
self::$login_ok = Minz_Session::param('passwordHash') === FreshRSS_Context::$user_conf->passwordHash;
break;
case 'http_auth':
$current_user = Minz_User::name();
self::$login_ok = strcasecmp($current_user, httpAuthUser()) === 0;
break;
case 'none':
self::$login_ok = true;
break;
default:
// TODO: extensions
self::$login_ok = false;
}
Minz_Session::_params([
'loginOk' => self::$login_ok,
'REMOTE_USER' => httpAuthUser(),
]);
return self::$login_ok;
}
/**
* Returns if current user has access to the given scope.
*
* @param string $scope general (default) or admin
* @return boolean true if user has corresponding access, false else.
*/
public static function hasAccess($scope = 'general'): bool {
if (FreshRSS_Context::$user_conf == null) {
return false;
}
$currentUser = Minz_User::name();
$isAdmin = FreshRSS_Context::$user_conf->is_admin;
$default_user = FreshRSS_Context::$system_conf->default_user;
$ok = self::$login_ok;
switch ($scope) {
case 'general':
break;
case 'admin':
$ok &= $default_user === $currentUser || $isAdmin;
break;
default:
$ok = false;
}
return $ok;
}
/**
* Removes all accesses for the current user.
*/
public static function removeAccess(): void {
self::$login_ok = false;
Minz_Session::_params([
'loginOk' => false,
'csrf' => false,
'REMOTE_USER' => false,
]);
$username = '';
$token_param = Minz_Request::paramString('token');
if ($token_param != '') {
$username = Minz_Request::paramString('user');
if ($username != '') {
$conf = get_user_configuration($username);
if ($conf == null) {
$username = '';
}
}
}
if ($username == '') {
$username = FreshRSS_Context::$system_conf->default_user;
}
Minz_User::change($username);
switch (FreshRSS_Context::$system_conf->auth_type) {
case 'form':
Minz_Session::_param('passwordHash');
FreshRSS_FormAuth::deleteCookie();
break;
case 'http_auth':
case 'none':
// Nothing to do…
break;
default:
// TODO: extensions
}
}
/**
* Return if authentication is enabled on this instance of FRSS.
*/
public static function accessNeedsLogin(): bool {
return FreshRSS_Context::$system_conf->auth_type !== 'none';
}
/**
* Return if authentication requires a PHP action.
*/
public static function accessNeedsAction(): bool {
return FreshRSS_Context::$system_conf->auth_type === 'form';
}
public static function csrfToken(): string {
$csrf = Minz_Session::param('csrf');
if ($csrf == '') {
$salt = FreshRSS_Context::$system_conf->salt;
$csrf = sha1($salt . uniqid('' . mt_rand(), true));
Minz_Session::_param('csrf', $csrf);
}
return $csrf;
}
public static function isCsrfOk(?string $token = null): bool {
$csrf = Minz_Session::param('csrf');
if ($token === null) {
$token = $_POST['_csrf'] ?? '';
}
return $token != '' && $token === $csrf;
}
}
|