Skip to content
Snippets Groups Projects
Commit f39f81e2 authored by Demian Katz's avatar Demian Katz
Browse files

Set up plugin manager for authentication handlers; updated unit test for MultiAuth accordingly.

parent 691bf315
No related merge requests found
...@@ -60,6 +60,22 @@ $config = array( ...@@ -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( 'controllers' => array(
'invokables' => array( 'invokables' => array(
'admin' => 'VuFind\Controller\AdminController', 'admin' => 'VuFind\Controller\AdminController',
......
...@@ -44,7 +44,7 @@ use VuFind\Config\Reader as ConfigReader, ...@@ -44,7 +44,7 @@ use VuFind\Config\Reader as ConfigReader,
*/ */
class Manager implements ServiceLocatorAwareInterface class Manager implements ServiceLocatorAwareInterface
{ {
protected $auth; protected $auth = false;
protected $config; protected $config;
protected $session; protected $session;
protected $ilsAccount = false; protected $ilsAccount = false;
...@@ -56,12 +56,24 @@ class Manager implements ServiceLocatorAwareInterface ...@@ -56,12 +56,24 @@ class Manager implements ServiceLocatorAwareInterface
public function __construct() public function __construct()
{ {
$this->config = ConfigReader::getConfig(); $this->config = ConfigReader::getConfig();
$this->auth = Factory::getAuth(
$this->config->Authentication->method, $this->config
);
$this->session = new SessionContainer('Account'); $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? * Does the current configuration support account creation?
* *
...@@ -69,7 +81,7 @@ class Manager implements ServiceLocatorAwareInterface ...@@ -69,7 +81,7 @@ class Manager implements ServiceLocatorAwareInterface
*/ */
public function supportsCreation() public function supportsCreation()
{ {
return $this->auth->supportsCreation(); return $this->getAuth()->supportsCreation();
} }
/** /**
...@@ -83,7 +95,7 @@ class Manager implements ServiceLocatorAwareInterface ...@@ -83,7 +95,7 @@ class Manager implements ServiceLocatorAwareInterface
*/ */
public function getSessionInitiator($target) public function getSessionInitiator($target)
{ {
return $this->auth->getSessionInitiator($target); return $this->getAuth()->getSessionInitiator($target);
} }
/** /**
...@@ -93,7 +105,7 @@ class Manager implements ServiceLocatorAwareInterface ...@@ -93,7 +105,7 @@ class Manager implements ServiceLocatorAwareInterface
*/ */
public function getAuthClass() public function getAuthClass()
{ {
return get_class($this->auth); return get_class($this->getAuth());
} }
/** /**
...@@ -134,7 +146,7 @@ class Manager implements ServiceLocatorAwareInterface ...@@ -134,7 +146,7 @@ class Manager implements ServiceLocatorAwareInterface
{ {
// Perform authentication-specific cleanup and modify redirect URL if // Perform authentication-specific cleanup and modify redirect URL if
// necessary. // necessary.
$url = $this->auth->logout($url); $url = $this->getAuth()->logout($url);
// Clear out cached ILS connection. // Clear out cached ILS connection.
$this->ilsAccount = false; $this->ilsAccount = false;
...@@ -173,7 +185,7 @@ class Manager implements ServiceLocatorAwareInterface ...@@ -173,7 +185,7 @@ class Manager implements ServiceLocatorAwareInterface
*/ */
public function checkForExpiredCredentials() public function checkForExpiredCredentials()
{ {
if ($this->isLoggedIn() && $this->auth->isExpired()) { if ($this->isLoggedIn() && $this->getAuth()->isExpired()) {
$this->logout(null, false); $this->logout(null, false);
return true; return true;
} }
...@@ -203,7 +215,7 @@ class Manager implements ServiceLocatorAwareInterface ...@@ -203,7 +215,7 @@ class Manager implements ServiceLocatorAwareInterface
*/ */
public function create($request) public function create($request)
{ {
$user = $this->auth->create($request); $user = $this->getAuth()->create($request);
$this->updateSession($user); $this->updateSession($user);
return $user; return $user;
} }
...@@ -222,7 +234,7 @@ class Manager implements ServiceLocatorAwareInterface ...@@ -222,7 +234,7 @@ class Manager implements ServiceLocatorAwareInterface
{ {
// Perform authentication: // Perform authentication:
try { try {
$user = $this->auth->authenticate($request); $user = $this->getAuth()->authenticate($request);
} catch (AuthException $e) { } catch (AuthException $e) {
// Pass authentication exceptions through unmodified // Pass authentication exceptions through unmodified
throw $e; throw $e;
......
...@@ -26,7 +26,9 @@ ...@@ -26,7 +26,9 @@
* @link http://vufind.org/wiki/building_an_authentication_handler Wiki * @link http://vufind.org/wiki/building_an_authentication_handler Wiki
*/ */
namespace VuFind\Auth; namespace VuFind\Auth;
use VuFind\Exception\Auth as AuthException; use VuFind\Exception\Auth as AuthException,
Zend\ServiceManager\ServiceLocatorAwareInterface,
Zend\ServiceManager\ServiceLocatorInterface;
/** /**
* MultiAuth Authentication plugin * MultiAuth Authentication plugin
...@@ -61,7 +63,7 @@ use VuFind\Exception\Auth as AuthException; ...@@ -61,7 +63,7 @@ use VuFind\Exception\Auth as AuthException;
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/building_an_authentication_handler Wiki * @link http://vufind.org/wiki/building_an_authentication_handler Wiki
*/ */
class MultiAuth extends AbstractBase class MultiAuth extends AbstractBase implements ServiceLocatorAwareInterface
{ {
protected $filters = array(); protected $filters = array();
protected $methods = array(); protected $methods = array();
...@@ -172,9 +174,12 @@ class MultiAuth extends AbstractBase ...@@ -172,9 +174,12 @@ class MultiAuth extends AbstractBase
*/ */
protected function authUser($request) protected function authUser($request)
{ {
$manager = $this->getServiceLocator();
// Try authentication methods until we find one that works: // Try authentication methods until we find one that works:
foreach ($this->methods as $method) { foreach ($this->methods as $method) {
$authenticator = Factory::getAuth(trim($method), $this->getConfig()); $authenticator = $manager->get($method);
$authenticator->setConfig($this->getConfig());
try { try {
$user = $authenticator->authenticate($request); $user = $authenticator->authenticate($request);
...@@ -200,4 +205,27 @@ class MultiAuth extends AbstractBase ...@@ -200,4 +205,27 @@ class MultiAuth extends AbstractBase
} }
return $user; 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
<?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
<?php <?php
/** /**
* Factory class for constructing authentication modules. * Session handler plugin manager
* *
* PHP version 5 * PHP version 5
* *
...@@ -20,59 +20,32 @@ ...@@ -20,59 +20,32 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* *
* @category VuFind2 * @category VuFind2
* @package Authentication * @package Session_Handlers
* @author Franck Borel <franck.borel@gbv.de>
* @author Demian Katz <demian.katz@villanova.edu> * @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/building_an_authentication_handler Wiki * @link http://vufind.org/wiki/creating_a_session_handler Wiki
*/ */
namespace VuFind\Auth; namespace VuFind\Auth;
use VuFind\Exception\Auth as AuthException;
/** /**
* Factory class for constructing authentication modules. * Session handler plugin manager
* *
* @category VuFind2 * @category VuFind2
* @package Authentication * @package Session_Handlers
* @author Franck Borel <franck.borel@gbv.de>
* @author Demian Katz <demian.katz@villanova.edu> * @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/building_an_authentication_handler Wiki * @link http://vufind.org/wiki/creating_a_session_handler Wiki
*/ */
class Factory class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
{ {
/** /**
* Initialize an authentication module. * Return the name of the base class or interface that plug-ins must conform
* to.
* *
* @param string $authNHandler The name of the module to initialize. * @return string
* @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) protected function getExpectedInterface()
{ {
// Special handling for authentication classes that don't conform to the return 'VuFind\Auth\AbstractBase';
// 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
...@@ -88,6 +88,11 @@ class Bootstrap ...@@ -88,6 +88,11 @@ class Bootstrap
$serviceManager = $app->getServiceManager(); $serviceManager = $app->getServiceManager();
$config = $app->getConfig(); $config = $app->getConfig();
$serviceManager->setService(
'AuthHandlerManager', new \VuFind\Auth\PluginManager(
new ServiceManagerConfig($config['auth_handler_manager'])
)
);
$serviceManager->setService( $serviceManager->setService(
'SessionHandlerManager', new \VuFind\Session\PluginManager( 'SessionHandlerManager', new \VuFind\Session\PluginManager(
new ServiceManagerConfig($config['session_handler_manager']) new ServiceManagerConfig($config['session_handler_manager'])
......
...@@ -51,8 +51,16 @@ class MultiAuthTest extends \VuFind\Tests\TestCase ...@@ -51,8 +51,16 @@ class MultiAuthTest extends \VuFind\Tests\TestCase
if (null === $config) { if (null === $config) {
$config = $this->getAuthConfig(); $config = $this->getAuthConfig();
} }
$serviceLocator = new \VuFind\Auth\PluginManager(
new \Zend\ServiceManager\Config(
array(
'abstract_factories' => array('VuFind\Auth\PluginFactory'),
)
)
);
$obj = new MultiAuth(); $obj = new MultiAuth();
$obj->setConfig($config); $obj->setConfig($config);
$obj->setServiceLocator($serviceLocator);
return $obj; return $obj;
} }
...@@ -102,6 +110,24 @@ class MultiAuthTest extends \VuFind\Tests\TestCase ...@@ -102,6 +110,24 @@ class MultiAuthTest extends \VuFind\Tests\TestCase
return $request; 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. * Test login with blank username.
* *
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment