diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index c9503d8d92d7da63b012f3f7406d44323f0485d3..7b6bd58ca79f15e6c74c6f81eb174fb4cb46b451 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -60,6 +60,22 @@ $config = array( ) ), ), + 'auth_handler_manager' => array( + 'abstract_factories' => array('VuFind\Auth\PluginFactory'), + 'invokables' => array( + 'database' => 'VuFind\Auth\Database', + 'ils' => 'VuFind\Auth\ILS', + 'ldap' => 'VuFind\Auth\LDAP', + 'multiauth' => 'VuFind\Auth\MultiAuth', + 'shibboleth' => 'VuFind\Auth\Shibboleth', + 'sip2' => 'VuFind\Auth\SIP2', + ), + 'aliases' => array( + // for legacy 1.x compatibility + 'db' => 'Database', + 'sip' => 'Sip2', + ), + ), 'controllers' => array( 'invokables' => array( 'admin' => 'VuFind\Controller\AdminController', diff --git a/module/VuFind/src/VuFind/Auth/Factory.php b/module/VuFind/src/VuFind/Auth/Factory.php deleted file mode 100644 index f2404893ad4c4acdc80fefdd7a69fb5d5d2589c0..0000000000000000000000000000000000000000 --- a/module/VuFind/src/VuFind/Auth/Factory.php +++ /dev/null @@ -1,78 +0,0 @@ -<?php -/** - * Factory class for constructing authentication modules. - * - * PHP version 5 - * - * Copyright (C) Villanova University 2010. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * @category VuFind2 - * @package Authentication - * @author Franck Borel <franck.borel@gbv.de> - * @author Demian Katz <demian.katz@villanova.edu> - * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License - * @link http://vufind.org/wiki/building_an_authentication_handler Wiki - */ -namespace VuFind\Auth; -use VuFind\Exception\Auth as AuthException; - -/** - * Factory class for constructing authentication modules. - * - * @category VuFind2 - * @package Authentication - * @author Franck Borel <franck.borel@gbv.de> - * @author Demian Katz <demian.katz@villanova.edu> - * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License - * @link http://vufind.org/wiki/building_an_authentication_handler Wiki - */ -class Factory -{ - /** - * Initialize an authentication module. - * - * @param string $authNHandler The name of the module to initialize. - * @param object $config Optional configuration object to pass through - * (loads default configuration if none specified). - * - * @throws AuthException - * @return object - */ - static function getAuth($authNHandler, $config = null) - { - // Special handling for authentication classes that don't conform to the - // standard pattern (for legacy support): - if ($authNHandler == 'DB') { - $authNHandler = 'Database'; - } else if ($authNHandler == 'SIP') { - $authNHandler = 'SIP2'; - } - - // Load up the handler if a legal name has been supplied. - $className = 'VuFind\Auth\\' . $authNHandler; - if (class_exists($className)) { - $obj = new $className(); - if (null !== $config) { - $obj->setConfig($config); - } - return $obj; - } else { - throw new AuthException( - 'Authentication handler ' . $authNHandler . ' does not exist!' - ); - } - } -} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Auth/Manager.php b/module/VuFind/src/VuFind/Auth/Manager.php index 9e2203835ab5bb1ca937a36569bcded01370e117..1ee745a40f045984d64bdfa737b27a734d387666 100644 --- a/module/VuFind/src/VuFind/Auth/Manager.php +++ b/module/VuFind/src/VuFind/Auth/Manager.php @@ -44,7 +44,7 @@ use VuFind\Config\Reader as ConfigReader, */ class Manager implements ServiceLocatorAwareInterface { - protected $auth; + protected $auth = false; protected $config; protected $session; protected $ilsAccount = false; @@ -56,12 +56,24 @@ class Manager implements ServiceLocatorAwareInterface public function __construct() { $this->config = ConfigReader::getConfig(); - $this->auth = Factory::getAuth( - $this->config->Authentication->method, $this->config - ); $this->session = new SessionContainer('Account'); } + /** + * Get the authentication handler. + * + * @return AbstractBase + */ + protected function getAuth() + { + if (!$this->auth) { + $manager = $this->getServiceLocator()->get('AuthHandlerManager'); + $this->auth = $manager->get($this->config->Authentication->method); + $this->auth->setConfig($this->config); + } + return $this->auth; + } + /** * Does the current configuration support account creation? * @@ -69,7 +81,7 @@ class Manager implements ServiceLocatorAwareInterface */ public function supportsCreation() { - return $this->auth->supportsCreation(); + return $this->getAuth()->supportsCreation(); } /** @@ -83,7 +95,7 @@ class Manager implements ServiceLocatorAwareInterface */ public function getSessionInitiator($target) { - return $this->auth->getSessionInitiator($target); + return $this->getAuth()->getSessionInitiator($target); } /** @@ -93,7 +105,7 @@ class Manager implements ServiceLocatorAwareInterface */ public function getAuthClass() { - return get_class($this->auth); + return get_class($this->getAuth()); } /** @@ -134,7 +146,7 @@ class Manager implements ServiceLocatorAwareInterface { // Perform authentication-specific cleanup and modify redirect URL if // necessary. - $url = $this->auth->logout($url); + $url = $this->getAuth()->logout($url); // Clear out cached ILS connection. $this->ilsAccount = false; @@ -173,7 +185,7 @@ class Manager implements ServiceLocatorAwareInterface */ public function checkForExpiredCredentials() { - if ($this->isLoggedIn() && $this->auth->isExpired()) { + if ($this->isLoggedIn() && $this->getAuth()->isExpired()) { $this->logout(null, false); return true; } @@ -203,7 +215,7 @@ class Manager implements ServiceLocatorAwareInterface */ public function create($request) { - $user = $this->auth->create($request); + $user = $this->getAuth()->create($request); $this->updateSession($user); return $user; } @@ -222,7 +234,7 @@ class Manager implements ServiceLocatorAwareInterface { // Perform authentication: try { - $user = $this->auth->authenticate($request); + $user = $this->getAuth()->authenticate($request); } catch (AuthException $e) { // Pass authentication exceptions through unmodified throw $e; diff --git a/module/VuFind/src/VuFind/Auth/MultiAuth.php b/module/VuFind/src/VuFind/Auth/MultiAuth.php index 3a604ffa0bc4b9bcf1cf12ff9a83fb76a79c5771..bfa4cc02e0ac45ebb48202a051d3396ea6ef9c73 100644 --- a/module/VuFind/src/VuFind/Auth/MultiAuth.php +++ b/module/VuFind/src/VuFind/Auth/MultiAuth.php @@ -26,7 +26,9 @@ * @link http://vufind.org/wiki/building_an_authentication_handler Wiki */ namespace VuFind\Auth; -use VuFind\Exception\Auth as AuthException; +use VuFind\Exception\Auth as AuthException, + Zend\ServiceManager\ServiceLocatorAwareInterface, + Zend\ServiceManager\ServiceLocatorInterface; /** * MultiAuth Authentication plugin @@ -61,7 +63,7 @@ use VuFind\Exception\Auth as AuthException; * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link http://vufind.org/wiki/building_an_authentication_handler Wiki */ -class MultiAuth extends AbstractBase +class MultiAuth extends AbstractBase implements ServiceLocatorAwareInterface { protected $filters = array(); protected $methods = array(); @@ -172,9 +174,12 @@ class MultiAuth extends AbstractBase */ protected function authUser($request) { + $manager = $this->getServiceLocator(); + // Try authentication methods until we find one that works: foreach ($this->methods as $method) { - $authenticator = Factory::getAuth(trim($method), $this->getConfig()); + $authenticator = $manager->get($method); + $authenticator->setConfig($this->getConfig()); try { $user = $authenticator->authenticate($request); @@ -200,4 +205,27 @@ class MultiAuth extends AbstractBase } return $user; } + + /** + * Set the service locator. + * + * @param ServiceLocatorInterface $serviceLocator Locator to register + * + * @return Manager + */ + public function setServiceLocator(ServiceLocatorInterface $serviceLocator) + { + $this->serviceLocator = $serviceLocator; + return $this; + } + + /** + * Get the service locator. + * + * @return \Zend\ServiceManager\ServiceLocatorInterface + */ + public function getServiceLocator() + { + return $this->serviceLocator; + } } \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Auth/PluginFactory.php b/module/VuFind/src/VuFind/Auth/PluginFactory.php new file mode 100644 index 0000000000000000000000000000000000000000..818617d0d6d189e7ed728d4111311c322f5dde13 --- /dev/null +++ b/module/VuFind/src/VuFind/Auth/PluginFactory.php @@ -0,0 +1,48 @@ +<?php +/** + * Auth handler plugin factory + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Session_Handlers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/creating_a_session_handler Wiki + */ +namespace VuFind\Auth; + +/** + * Auth handler plugin factory + * + * @category VuFind2 + * @package Session_Handlers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/creating_a_session_handler Wiki + */ +class PluginFactory extends \VuFind\ServiceManager\AbstractPluginFactory +{ + /** + * Constructor + */ + public function __construct() + { + $this->defaultNamespace = 'VuFind\Auth'; + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Auth/PluginManager.php b/module/VuFind/src/VuFind/Auth/PluginManager.php new file mode 100644 index 0000000000000000000000000000000000000000..e7f5e71c4d25957003db8d1c9fade8661f96d908 --- /dev/null +++ b/module/VuFind/src/VuFind/Auth/PluginManager.php @@ -0,0 +1,51 @@ +<?php +/** + * Session handler plugin manager + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Session_Handlers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/creating_a_session_handler Wiki + */ +namespace VuFind\Auth; + +/** + * Session handler plugin manager + * + * @category VuFind2 + * @package Session_Handlers + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/creating_a_session_handler Wiki + */ +class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager +{ + /** + * Return the name of the base class or interface that plug-ins must conform + * to. + * + * @return string + */ + protected function getExpectedInterface() + { + return 'VuFind\Auth\AbstractBase'; + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Bootstrap.php b/module/VuFind/src/VuFind/Bootstrap.php index 73562479761fc4f5dbc17eef52ebab08b0fbdbeb..1119988764905a7891ce79fde3e2abdf0cb8d1aa 100644 --- a/module/VuFind/src/VuFind/Bootstrap.php +++ b/module/VuFind/src/VuFind/Bootstrap.php @@ -88,6 +88,11 @@ class Bootstrap $serviceManager = $app->getServiceManager(); $config = $app->getConfig(); + $serviceManager->setService( + 'AuthHandlerManager', new \VuFind\Auth\PluginManager( + new ServiceManagerConfig($config['auth_handler_manager']) + ) + ); $serviceManager->setService( 'SessionHandlerManager', new \VuFind\Session\PluginManager( new ServiceManagerConfig($config['session_handler_manager']) diff --git a/module/VuFind/tests/Auth/MultiAuthTest.php b/module/VuFind/tests/Auth/MultiAuthTest.php index 0e741bc49a58271ebb7417c693a2fb33d86745cd..41251995d671c76f6adda2d05a0bdea0edd41bcb 100644 --- a/module/VuFind/tests/Auth/MultiAuthTest.php +++ b/module/VuFind/tests/Auth/MultiAuthTest.php @@ -51,8 +51,16 @@ class MultiAuthTest extends \VuFind\Tests\TestCase if (null === $config) { $config = $this->getAuthConfig(); } + $serviceLocator = new \VuFind\Auth\PluginManager( + new \Zend\ServiceManager\Config( + array( + 'abstract_factories' => array('VuFind\Auth\PluginFactory'), + ) + ) + ); $obj = new MultiAuth(); $obj->setConfig($config); + $obj->setServiceLocator($serviceLocator); return $obj; } @@ -102,6 +110,24 @@ class MultiAuthTest extends \VuFind\Tests\TestCase return $request; } + /** + * Test login with handler configured to load a class which does not conform + * to the appropriate authentication interface. (We'll use \VuFind\Cart as an + * arbitrary inappropriate class). + * + * @return void + */ + public function testLoginWithBadClass() + { + $this + ->setExpectedException('Zend\ServiceManager\Exception\RuntimeException'); + $config = $this->getAuthConfig(); + $config->MultiAuth->method_order = 'VuFind\Cart,Database'; + + $request = $this->getLoginRequest(); + $this->getAuthObject($config)->authenticate($request); + } + /** * Test login with blank username. *