17 use Symfony\Component\HttpFoundation\Session\Session;
18 use Symfony\Component\HttpFoundation\RedirectResponse;
19 use League\OAuth2\Client\Provider\GenericProvider;
21 define(
"TITLE_CORE_AUTH", _(
"Login"));
25 public static $origReferer;
33 private $authExternal;
38 $this->Title = TITLE_CORE_AUTH;
39 $this->PluginLevel = 1000;
41 parent::__construct();
44 $this->
dbManager = $container->get(
"db.manager");
45 $this->userDao = $container->get(
'dao.user');
46 $this->session = $container->get(
'session');
55 return self::$origReferer;
67 return $this->userDao->updateUserTable();
83 if (!$this->session->isStarted()) {
84 $this->session->setName(
'Login');
85 $this->session->start();
89 if ($this->authExternal !==
false && $this->authExternal[
'useAuthExternal']) {
90 $this->
checkUsernameAndPassword($this->authExternal[
'loginAuthExternal'], $this->authExternal[
'passwordAuthExternal']);
93 if (array_key_exists(
'selectMemberGroup', $_POST)) {
94 $selectedGroupId = intval($_POST[
'selectMemberGroup']);
95 $this->userDao->setDefaultGroupMembership(intval($_SESSION[Auth::USER_ID]), $selectedGroupId);
96 $_SESSION[Auth::GROUP_ID] = $selectedGroupId;
97 $this->session->set(Auth::GROUP_ID, $selectedGroupId);
98 $SysConf[
'auth'][Auth::GROUP_ID] = $selectedGroupId;
101 if (array_key_exists(Auth::USER_ID, $_SESSION)) {
102 $SysConf[
'auth'][Auth::USER_ID] = $_SESSION[Auth::USER_ID];
104 if (array_key_exists(Auth::GROUP_ID, $_SESSION)) {
105 $SysConf[
'auth'][Auth::GROUP_ID] = $_SESSION[Auth::GROUP_ID];
110 if (!empty($_SESSION[
'time']) && @$_SESSION[
'time'] + (60 * 480) < $Now) {
114 $_SESSION[
'time'] = $Now;
115 if (empty($_SESSION[
'ip'])) {
116 $_SESSION[
'ip'] = $this->
getIP();
117 }
else if ((@$_SESSION[
'checkip'] == 1) && (@$_SESSION[
'ip'] != $this->
getIP())) {
120 $_SESSION[
'ip'] = $this->
getIP();
123 if (@$_SESSION[Auth::USER_NAME]) {
125 if (empty($_SESSION[
'time_check'])) {
126 $_SESSION[
'time_check'] = time() + (480 * 60);
128 if (time() >= @$_SESSION[
'time_check']) {
129 $userName = @$_SESSION[Auth::USER_NAME];
130 $row = $this->userDao->getUserAndDefaultGroupByUserName($userName, @$_SESSION[
'oauthCheck']);
132 if (empty($row[
'user_pass'])) {
142 plugin_disable($_SESSION[Auth::USER_LEVEL]);
143 $this->
State = PLUGIN_STATE_READY;
156 if (empty($userRow)) {
157 $username =
'Default User';
158 $userRow = $this->userDao->getUserAndDefaultGroupByUserName($username);
161 $_SESSION[Auth::USER_ID] = $userRow[
'user_pk'];
162 $SysConf[
'auth'][Auth::USER_ID] = $userRow[
'user_pk'];
163 $this->session->set(Auth::USER_ID, $userRow[
'user_pk']);
164 $_SESSION[Auth::USER_NAME] = $userRow[
'user_name'];
165 $this->session->set(Auth::USER_NAME, $userRow[
'user_name']);
166 $_SESSION[
'Folder'] = $userRow[
'root_folder_fk'];
167 $_SESSION[Auth::USER_LEVEL] = $userRow[
'user_perm'];
168 $this->session->set(Auth::USER_LEVEL, $userRow[
'user_perm']);
169 $_SESSION[
'UserEmail'] = $userRow[
'user_email'];
170 $_SESSION[
'UserEnote'] = $userRow[
'email_notify'];
171 $_SESSION[Auth::GROUP_ID] = $userRow[
'group_fk'];
172 $SysConf[
'auth'][Auth::GROUP_ID] = $userRow[
'group_fk'];
173 $this->session->set(Auth::GROUP_ID, $userRow[
'group_fk']);
174 $_SESSION[
'GroupName'] = $userRow[
'group_name'];
176 $_SESSION[
'oauthCheck'] = $userRow[
'oauth'];
178 if (array_key_exists(Menu::BANNER_COOKIE, $_COOKIE)) {
179 $_COOKIE[Menu::BANNER_COOKIE] = 0;
181 setcookie(Menu::BANNER_COOKIE,
"", time() - 3600);
193 $Vars = array(
'HTTP_CLIENT_IP',
'HTTP_X_COMING_FROM',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED');
194 foreach ($Vars as $V) {
195 if (!empty($_SERVER[$V])) {
196 return ($_SERVER[$V]);
199 return (@$_SERVER[
'REMOTE_ADDR']);
209 $this->vars[
'loginProvider'] =
"password";
210 if (array_key_exists(
'AUTHENTICATION', $SysConf) &&
211 array_key_exists(
'provider', $SysConf[
'AUTHENTICATION'])) {
212 $this->vars[
'loginProvider'] = $SysConf[
'AUTHENTICATION'][
'provider'];
218 if (empty($timezone) || strpos($timezone,
"Unknown") ==
true) {
219 $timezone = date_default_timezone_get();
221 $_SESSION[
'timezone'] = $timezone;
226 if (array_key_exists(
'http_proxy', $SysConf[
'FOSSOLOGY']) &&
227 ! empty($SysConf[
'FOSSOLOGY'][
'http_proxy'])) {
228 $proxy = $SysConf[
'FOSSOLOGY'][
'http_proxy'];
230 if (array_key_exists(
'https_proxy', $SysConf[
'FOSSOLOGY']) &&
231 ! empty($SysConf[
'FOSSOLOGY'][
'https_proxy'])) {
232 $proxy = $SysConf[
'FOSSOLOGY'][
'https_proxy'];
235 if (! empty($providerCheck)) {
236 $provider =
new GenericProvider([
237 "clientId" => $SysConf[
'SYSCONFIG'][
'OidcAppId'],
238 "clientSecret" => $SysConf[
'SYSCONFIG'][
'OidcSecret'],
239 "redirectUri" => $SysConf[
'SYSCONFIG'][
'OidcRedirectURL'],
240 "urlAuthorize" => $SysConf[
'SYSCONFIG'][
'OidcAuthorizeURL'],
241 "urlAccessToken" => $SysConf[
'SYSCONFIG'][
'OidcAccessTokenURL'],
242 "urlResourceOwnerDetails" => $SysConf[
'SYSCONFIG'][
'OidcResourceURL'],
245 $authorizationUrl = $provider->getAuthorizationUrl([
246 "scope" => [
'email openid']
248 $_SESSION[
'oauth2state'] = $provider->getState();
249 $_SESSION[
'HTTP_REFERER'] = $referrer;
250 header(
'Location: ' . $authorizationUrl);
254 if (empty($referrer) && array_key_exists(
'HTTP_REFERER', $_SESSION)) {
255 $referrer = $_SESSION[
'HTTP_REFERER'];
256 }
else if (empty($referrer)) {
260 if (array_key_exists(
"oauthemail", $_SESSION)) {
261 $getEmail = $_SESSION[
'oauthemail'];
262 unset($_SESSION[
'oauthemail']);
264 $referrerQuery = parse_url($referrer,PHP_URL_QUERY);
265 if ($referrerQuery) {
267 parse_str($referrerQuery,$params);
268 if (array_key_exists(
'mod', $params) && $params[
'mod'] == $this->Name) {
272 if (!empty($getEmail) && empty($userName)) {
278 if (empty($referrer)) {
279 if (plugin_find_id(
'browse') < 0) {
280 $newReferrer =
Traceback_uri() .
'?mod=' .
'browse' .
'&oauth=true';
282 return new RedirectResponse($newReferrer);
284 return new RedirectResponse($referrer);
288 $initPluginId = plugin_find_id(
"init");
289 if ($initPluginId >= 0) {
291 $this->vars[
'info'] = $Plugins[$initPluginId]->infoFirstTimeUsage();
294 if (isset($_SERVER[
'HTTPS']) && $_SERVER[
'HTTPS'] !=
"off") {
295 $this->vars[
'protocol'] =
"HTTPS";
297 $this->vars[
'protocol'] = preg_replace(
"@/.*@",
"", @$_SERVER[
'SERVER_PROTOCOL']);
300 $this->vars[
'referrer'] = $referrer;
301 $this->vars[
'loginFailure'] = !empty($userName) || !empty($password);
302 if (!empty($userName) && $userName!=
'Default User') {
303 $this->vars[
'userName'] = $userName;
305 if (!empty($SysConf[
'SYSCONFIG'][
'OidcAppName'])) {
306 $this->vars[
'providerExist'] = $SysConf[
'SYSCONFIG'][
'OidcAppName'];
308 $this->vars[
'providerExist'] = 0;
310 return $this->
render(
'login.html.twig',$this->vars);
318 if (array_key_exists(
'User', $_SESSION) && $_SESSION[
'User'] !=
"Default User") {
320 if (!empty($SysConf[
'SYSCONFIG'][
'OidcLogoutURL'])) {
321 $uri = $SysConf[
'SYSCONFIG'][
'OidcLogoutURL'];
326 $_SESSION[
'oauth2state'] =
"";
327 header(
"Location: $uri");
330 parent::OutputOpen();
347 $options = array(
'cost' => 10);
350 if ($this->authExternal !==
false && $this->authExternal[
'useAuthExternal']) {
351 $username = $this->authExternal[
'loginAuthExternal'];
354 $this->userDao->getUserAndDefaultGroupByUserName($username);
355 }
catch (Exception $e) {
358 if (! $user_exists && $GLOBALS[
'SysConf'][
'EXT_AUTH'][
'CONF_EXT_AUTH_NEW_USER_AUTO_CREATE']) {
360 $User =
trim(str_replace(
"'",
"''", $this->authExternal[
'loginAuthExternal']));
361 $Pass = $this->authExternal[
'passwordAuthExternal'] ;
362 $Hash = password_hash($Pass, PASSWORD_DEFAULT, $options);
363 $Desc = $this->authExternal[
'descriptionAuthExternal'];
367 $Email = $this->authExternal[
'emailAuthExternal'];
369 $agentList = $GLOBALS[
'SysConf'][
'EXT_AUTH'][
'CONF_EXT_AUTH_NEW_USER_AGENT_LIST'];
370 add_user($User, $Desc, $Hash, $Perm, $Email, $Email_notify,
371 $GLOBALS[
'SysConf'][
'SYSCONFIG'][
'UploadVisibility'], $agentList, $Folder);
375 $authProvider =
"password";
376 if (array_key_exists(
'AUTHENTICATION', $SysConf) &&
377 array_key_exists(
'provider', $SysConf[
'AUTHENTICATION'])) {
378 $authProvider = $SysConf[
'AUTHENTICATION'][
'provider'];
381 if (empty($userName) || $userName ==
'Default User') {
385 $row = $this->userDao->getUserAndDefaultGroupByUserName($userName, $oauth);
386 }
catch (Exception $e) {
390 if (empty($row[
'user_name'])) {
395 if (!$isRest && $authProvider !=
"password") {
401 if (! empty($row[
'user_pass'])) {
402 $options = array(
'cost' => 10);
404 if (password_verify($password, $row[
'user_pass'])) {
405 if (password_needs_rehash($row[
'user_pass'], PASSWORD_DEFAULT, $options)) {
406 $newHash = password_hash($password, PASSWORD_DEFAULT, $options);
410 }
else if (! empty($row[
'user_seed'])) {
411 $passwordHash = sha1($row[
'user_seed'] . $password);
413 if (strcmp($passwordHash, $row[
'user_pass']) == 0) {
414 $newHash = password_hash($password, PASSWORD_DEFAULT, $options);
421 }
else if (!empty($password)) {
427 if (!$this->userDao->isUserActive($userName)) {
429 $this->vars[
'userInactive'] =
true;
436 $_SESSION[
'time_check'] = time() + (480 * 60);
438 if (
"X" . $row[
'user_perm'] ==
"X") {
441 $_SESSION[Auth::USER_LEVEL] = $row[
'user_perm'];
446 $_SESSION[
'NoPopup'] = 1;
448 $_SESSION[
'NoPopup'] = 0;
451 $this->userDao->updateUserLastConnection($row[
'user_pk']);
This is the Plugin class. All plugins should:
render($templateName, $vars=null)
Contains the constants and helpers for authentication of user.
Install()
Only used during installation. This may be called multiple times. Used to ensure the DB has the right...
getIP()
Retrieve the user's IP address. Some proxy systems pass forwarded IP address info....
Output()
This is only called when the user logs out.
staticValue()
getter to retreive value of static var
checkUsernameAndPassword($userName, $password, $oauth=false, $isRest=false)
OutputOpen()
perform logout
PostInitialize()
This is where the magic for Authentication happens.
updateSession($userRow, $oauth=false)
Set $_SESSION and $SysConf user variables.
__construct()
base constructor. Most plugins will just use this
siteminder_check()
Check if SiteMinder is enabled.
auth_external_check()
Check if the external HTTP authentication is enabled. The mapping variables should be configured in f...
Traceback_uri()
Get the URI without query to this location.
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
GetArrayVal($Key, $Arr)
Get the value from a array(map)
update_password_hash($User, $Hash)
Update user password hash.
add_user($User, $Desc, $Hash, $Perm, $Email, $Email_notify, $Upload_visibility, $agentList, $Folder, $default_bucketpool_fk='')
Add a user.
char * trim(char *ptext)
Trimming whitespace.
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
fo_dbManager * dbManager
fo_dbManager object