openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

commit a8555cdcda61e3f80b7cbd3e4c51b49c6195057d
parent ba48428434ff86e6477fb91bd5f189ec1f261c3d
Author: Jan Dankert <develop@jandankert.de>
Date:   Mon, 16 Nov 2020 13:21:57 +0100

Code cleanup: Externalize calling the auth modules.

Diffstat:
Mmodules/cms/action/LoginAction.class.php | 92++++++++++++++-----------------------------------------------------------------
Mmodules/cms/auth/CookieAuth.class.php | 2--
Mmodules/cms/auth/DatabaseAuth.class.php | 6+-----
Mmodules/cms/auth/HttpAuth.class.php | 2+-
Mmodules/cms/auth/IdentAuth.class.php | 2+-
Mmodules/cms/auth/InternalAuth.class.php | 10+++++-----
Mmodules/cms/auth/RememberAuth.class.php | 2+-
Mmodules/cms/auth/SSLAuth.class.php | 2+-
Mmodules/cms/auth/SingleSignonAuth.class.php | 2--
Mmodules/cms/ui/action/IndexAction.class.php | 25++-----------------------
10 files changed, 28 insertions(+), 117 deletions(-)

diff --git a/modules/cms/action/LoginAction.class.php b/modules/cms/action/LoginAction.class.php @@ -4,6 +4,7 @@ namespace cms\action; use cms\auth\Auth; +use cms\auth\AuthRunner; use cms\base\Configuration; use cms\base\DB; use cms\base\Startup; @@ -279,29 +280,7 @@ class LoginAction extends BaseAction $this->setTemplateVar('send_password',$loginConfig->get('send_password')); // Versuchen, einen Benutzernamen zu ermitteln, der im Eingabeformular vorausgewählt wird. - $modules = $securityConfig->subset('preselect')->get('modules',[]); - - $username = ''; - - foreach( $modules as $module) - { - $moduleClass = Auth::NS.'\\'.$module.'Auth'; - - if ( ! class_exists($moduleClass)) { - Logger::warn("module is not availble: ".$moduleClass ); - continue; - } - - /** @var \cms\auth\Auth $auth */ - $auth = new $moduleClass; - $username = $auth->username(); - - if ( $username ) - { - Logger::debug('Preselecting User '.$username.' from '.$module.'Auth'); - break; // Benutzername gefunden. - } - } + $username = AuthRunner::getUsername('preselect'); $this->setTemplateVar('login_name',$username); @@ -398,7 +377,7 @@ class LoginAction extends BaseAction */ function loginPost() { - Session::setUser(''); // Altes Login entfernen. + Session::setUser(null); // Altes Login entfernen. if ( Configuration::subset('login')->is('nologin',false ) ) throw new SecurityException('login disabled'); @@ -460,48 +439,17 @@ class LoginAction extends BaseAction $this->setCookie('or_username',$loginName ); $this->setCookie('or_dbid' ,$this->getRequestVar('dbid')); - // Authentifizierungs-Module. - $modules = Configuration::subset(['security','authenticate'])->get('modules',[] ); - - $loginOk = false; - $mustChangePassword = false; - $tokenFailed = false; - $lastModule = null; - // Jedes Authentifizierungsmodul durchlaufen, bis ein Login erfolgreich ist. - foreach( $modules as $module) - { - $moduleClass = Auth::NS.'\\' . $module . 'Auth'; - $auth = new $moduleClass; - Logger::info('Trying to login with module '.$moduleClass); - $loginStatus = $auth->login( $loginName,$loginPassword, $token ); - $loginOk = $loginStatus === true || $loginStatus === Auth::STATUS_SUCCESS; - - if ( $loginStatus === Auth::STATUS_PW_EXPIRED ) - $mustChangePassword = true; - if ( $loginStatus === Auth::STATUS_TOKEN_NEEDED ) - $tokenFailed = true; - - if ( $loginOk ) - { - Logger::info('Login successful for '.$loginName); - $lastModule = $module; - - break; // Login erfolgreich, erstes Modul gewinnt. - } - } - - /* - $loginOk = $this->checkLogin( $loginName, - $loginPassword, - $newPassword1, - $newPassword2 ); - */ - - + $result = AuthRunner::checkLogin('authenticate',$loginName,$loginPassword, $token ); + + $mustChangePassword = ( $result === Auth::STATUS_PW_EXPIRED ); + $tokenFailed = ( $result === Auth::STATUS_TOKEN_NEEDED ); + $loginOk = ( $result === Auth::STATUS_SUCCESS ); + if ( $loginOk ) { - + Logger::info('Login successful for '.$loginName); + try { // Benutzer über den Benutzernamen laden. @@ -603,35 +551,26 @@ class LoginAction extends BaseAction /** - * Benutzer meldet sich ab. + * Logout current user. */ public function logoutPost() { - $user = Session::getUser(); - if ( is_object($user) ) - $this->setTemplateVar('login_username',$user->name); - if ( Configuration::subset('security')->is('renew_session_logout',false) ) $this->recreateSession(); - // Login-Token löschen: - // Wenn der Benutzer sich abmelden will, dann soll auch die automatische - // Anmeldung deaktiviert werden. - - // Bestehendes Login-Token aus dem Cookie lesen und aus der Datenbank löschen. + // Reading the login token cookie list( $selector,$token ) = array_pad( explode('.',@$_COOKIE['or_token']),2,''); + // Logout forces the removal of all login tokens if ( $selector ) $this->currentUser->deleteLoginToken( $selector ); // Cookie mit Logintoken löschen. $this->setCookie('or_token' ,null ); - //session_unset(); Session::setUser(null); - $this->addNotice('user', 0, $user->name, 'LOGOUT_OK', Action::NOTICE_OK); - + $this->addNoticeFor( $this->currentUser, Messages::LOGOUT_OK ); } @@ -641,6 +580,7 @@ class LoginAction extends BaseAction */ function logoutView() { + // There is no view for this action. } diff --git a/modules/cms/auth/CookieAuth.class.php b/modules/cms/auth/CookieAuth.class.php @@ -30,4 +30,3 @@ class CookieAuth implements Auth } -?>- \ No newline at end of file diff --git a/modules/cms/auth/DatabaseAuth.class.php b/modules/cms/auth/DatabaseAuth.class.php @@ -31,11 +31,10 @@ class DatabaseAuth implements Auth $sql->setString('username', $user); $sql->setString('password', hash($algo, $password)); $row = $sql->getRow(); - $ok = !empty($row); // noch nicht implementiert: $authdb->close(); - return $ok ? Auth::STATUS_SUCCESS : Auth::STATUS_FAILED; + return $row ? Auth::STATUS_SUCCESS : Auth::STATUS_FAILED; } public function username() @@ -44,5 +43,3 @@ class DatabaseAuth implements Auth } } - -?>- \ No newline at end of file diff --git a/modules/cms/auth/HttpAuth.class.php b/modules/cms/auth/HttpAuth.class.php @@ -35,7 +35,7 @@ class HttpAuth implements Auth { $http = new Http( Configuration::get(['security','http','url'])); $http->method = 'HEAD'; - $http->setBasicAuthentication($this->name, $password); + $http->setBasicAuthentication($user, $password); $ok = $http->request(); diff --git a/modules/cms/auth/IdentAuth.class.php b/modules/cms/auth/IdentAuth.class.php @@ -50,7 +50,7 @@ class IdentAuth implements Auth */ public function login($user, $password, $token) { - return Auth::STATUS_FAILED; + return null; } } diff --git a/modules/cms/auth/InternalAuth.class.php b/modules/cms/auth/InternalAuth.class.php @@ -39,12 +39,12 @@ SQL // Benutzer ist nicht vorhanden. // Trotzdem das Kennwort hashen, um Timingattacken zu verhindern. $unusedHash = Password::hash(User::pepperPassword($password), Password::bestAlgoAvailable()); - return false; + return null; } // Pruefen ob Kennwort mit Datenbank uebereinstimmt. if (!Password::check(User::pepperPassword($password), $row_user['password_hash'], $row_user['password_algo'])) { - return false; + return Auth::STATUS_FAILED; } // Behandeln von Klartext-Kennwoertern (Igittigitt). @@ -64,7 +64,7 @@ SQL // Wenn das kennwort abgelaufen ist, kann es eine bestimmte Dauer noch benutzt und geändert werden. // Nach Ablauf dieser Dauer wird das Login abgelehnt. if ($row_user['password_expires'] + (Configuration::subset('security')->get('deny_after_expiration_duration',72) * 60 * 60) < time()) - return false; // Abgelaufenes Kennwort wird nicht mehr akzeptiert. + return Auth::STATUS_FAILED; // Abgelaufenes Kennwort wird nicht mehr akzeptiert. else return Auth::STATUS_PW_EXPIRED; // Kennwort ist abgelaufen, kann aber noch geändert werden. } @@ -73,7 +73,7 @@ SQL $user = new User($row_user['id']); $user->load(); if (Password::getTOTPCode($user->otpSecret) == $token) - return true; + return Auth::STATUS_SUCCESS; else return Auth::STATUS_TOKEN_NEEDED; } @@ -83,7 +83,7 @@ SQL } // Benutzer wurde erfolgreich authentifiziert. - return true; + return Auth::STATUS_SUCCESS; } public function username() diff --git a/modules/cms/auth/RememberAuth.class.php b/modules/cms/auth/RememberAuth.class.php @@ -77,7 +77,7 @@ SQL */ public function login($user, $password, $token) { - return false; + return null; } } diff --git a/modules/cms/auth/SSLAuth.class.php b/modules/cms/auth/SSLAuth.class.php @@ -30,7 +30,7 @@ class SSLAuth implements Auth */ public function login($user, $password, $token) { - return false; + return ( $this->username() == $user ) ? Auth::STATUS_SUCCESS : null; } } diff --git a/modules/cms/auth/SingleSignonAuth.class.php b/modules/cms/auth/SingleSignonAuth.class.php @@ -25,4 +25,3 @@ class SingleSignonAuth implements Auth } } -?>- \ No newline at end of file diff --git a/modules/cms/ui/action/IndexAction.class.php b/modules/cms/ui/action/IndexAction.class.php @@ -5,6 +5,7 @@ namespace cms\ui\action; use cms\action\Action; use cms\action\RequestParams; use cms\auth\Auth; +use cms\auth\AuthRunner; use cms\base\Configuration; use cms\base\Configuration as C; use cms\base\Startup; @@ -329,29 +330,7 @@ class IndexAction extends Action private function tryAutoLogin() { - $modules = C::subset( ['security','autologin'] )->get('modules',[] ); - $username = null; - - foreach( $modules as $module) - { - Logger::debug( 'Auto-Login module: '.$module ); - $moduleClass = Auth::NS. '\\'.$module.'Auth'; - $auth = new $moduleClass; - /* @type $auth Auth */ - try { - $username = $auth->username(); - } - catch( Exception $e ) { - Logger::warn( 'Error in auth-module '.$module.":\n".$e->__toString() ); - // Ignore this and continue with next module. - } - - if ( $username ) - { - Logger::debug('Auto-Login for User '.$username.' with auth-module '.$module); - break; // Benutzername gefunden. - } - } + $username = AuthRunner::getUsername('autologin'); if ( $username ) {