From 8192c1942436ff467e87420adb23c64869887f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20Mo=C3=9Fhammer?= Date: Mon, 24 Jun 2013 18:46:45 +0200 Subject: [PATCH 1/2] Fix Authentication workflow - The authentication controller now uses the Authentication/Manager class, also there were some issues in the Session creation, this has been removed from the Bootstrap now, as the Controller must decide how to open a session (read-only or read/write). - The tests reflect a few chagnes, as the move from the CSRF token generation to the Formbuilder. - Notificaiton now doesn't use Zend Session refs #4340 --- .../controllers/AuthenticationController.php | 69 +++++++++++++++-- .../layouts/scripts/parts/topbar.phtml | 2 +- .../views/scripts/authentication/login.phtml | 11 ++- config/authentication.ini | 9 ++- library/Icinga/Application/Web.php | 11 --- library/Icinga/Authentication/Manager.php | 13 +++- library/Icinga/Authentication/PhpSession.php | 15 ++-- library/Icinga/Authentication/User.php | 16 ++-- library/Icinga/Protocol/Ldap/Connection.php | 40 +++++----- library/Icinga/Web/ActionController.php | 48 ------------ library/Icinga/Web/Notification.php | 7 +- .../application/views/helpers/QlinkTest.php | 2 +- .../Icinga/Web/ActionControllerTest.php | 74 ------------------- 13 files changed, 134 insertions(+), 183 deletions(-) diff --git a/application/controllers/AuthenticationController.php b/application/controllers/AuthenticationController.php index 593a15f22..315e74eaa 100644 --- a/application/controllers/AuthenticationController.php +++ b/application/controllers/AuthenticationController.php @@ -6,8 +6,9 @@ # namespace Icinga\Application\Controllers; use Icinga\Web\ActionController; -use Icinga\Authentication\Auth; -use Icinga\Web\Notification; +use Icinga\Authentication\Credentials as Credentials; +use Icinga\Authentication\Manager as AuthManager; +use Icinga\Form\Builder as FormBuilder; /** * Class AuthenticationController @@ -25,13 +26,67 @@ class AuthenticationController extends ActionController */ protected $modifiesSession = true; + private function getAuthForm() + { + return array( + 'username' => array( + 'text', + array( + 'label' => t('Username'), + 'required' => true, + ) + ), + 'password' => array( + 'password', + array( + 'label' => t('Password'), + 'required' => true + ) + ), + 'submit' => array( + 'submit', + array( + 'label' => t('Login'), + 'class' => 'pull-right' + ) + ) + ); + } + /** * */ public function loginAction() { $this->replaceLayout = true; - $this->view->form = $this->widget('form', array('name' => 'login')); + $credentials = new Credentials(); + $this->view->form = FormBuilder::fromArray( + $this->getAuthForm(), + array( + "CSRFProtection" => false, // makes no sense here + "model" => &$credentials + ) + ); + try { + $auth = AuthManager::getInstance(null, array( + "writeSession" => true + )); + if ($auth->isAuthenticated()) { + $this->redirectNow('index?_render=body'); + } + if ($this->getRequest()->isPost() && $this->view->form->isSubmitted()) { + $this->view->form->repopulate(); + if ($this->view->form->isValid()) { + if (!$auth->authenticate($credentials)) { + $this->view->form->getElement('password')->addError(t('Please provide a valid username and password')); + } else { + $this->redirectNow('index?_render=body'); + } + } + } + } catch (\Icinga\Exception\ConfigurationError $configError) { + $this->view->errorInfo = $configError->getMessage(); + } } /** @@ -39,11 +94,13 @@ class AuthenticationController extends ActionController */ public function logoutAction() { + $auth = AuthManager::getInstance(null, array( + "writeSession" => true + )); $this->replaceLayout = true; - Auth::getInstance()->forgetAuthentication(); - Notification::success('You have been logged out'); + $auth->removeAuthorization(); $this->_forward('login'); } } -// @codingStandardsIgnoreEnd \ No newline at end of file +// @codingStandardsIgnoreEnd diff --git a/application/layouts/scripts/parts/topbar.phtml b/application/layouts/scripts/parts/topbar.phtml index 961fa1da7..39e25a13a 100755 --- a/application/layouts/scripts/parts/topbar.phtml +++ b/application/layouts/scripts/parts/topbar.phtml @@ -12,7 +12,7 @@