Skip to content

Instantly share code, notes, and snippets.

@weierophinney
Created April 23, 2015 20:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weierophinney/fce00a22f779f1ce4ae7 to your computer and use it in GitHub Desktop.
Save weierophinney/fce00a22f779f1ce4ae7 to your computer and use it in GitHub Desktop.
Example of a composite authentication adapter for zf-mvc-auth
<?php
use Zend\Http\Request;
use Zend\Http\Response;;
use ZF\MvcAuth\Authentication\AdapterInterface;
use ZF\MvcAuth\Identity\IdentityInterface;
use ZF\MvcAuth\MvcAuthEvent;
class CompositeAdapter implements AdapterInterface
{
private $adapters = [];
private $matched;
public function attach(AdapterInterface $adapter)
{
if (in_array($adapter, $this->adapters, true)) {
return;
}
$this->adapters[] = $adapter;
}
public function provides()
{
return array_reduce($this->adapters, function ($carry, $adapter) {
$provides = array_merge($carry, $adapter->provides());
return array_unique($provides);
}, []);
}
public function matches($type)
{
$this->matched = null;
foreach ($this->adapters as $adapter) {
$match = $adapter->matches($type);
if ($match) {
$this->matched = $adapter;
return true;
}
}
return false;
}
public function getTypeFromRequest(Request $request)
{
foreach ($this->adapters as $adapter) {
$type = $adapter->getTypeFromRequest($request);
if (false !== $type) {
return $type;
}
}
return false;
}
public function preAuth(Request $request, Response $response)
{
foreach ($this->adapters as $adapter) {
$result = $adapter->preAuth($request, $response);
if ($result instanceof Response) {
return $result;
}
}
}
public function authenticate(Request $request, Response $response, MvcAuthEvent $mvcAuthEvent)
{
if (! $this->matched) {
return false;
}
// Reset $matched so that subsequent calls fail
$adapter = $this->matched;
$this->matched = null;
return $adapter->authenticate($request, $response, $mvcAuthEvent);
}
}
<?php
use Zend\ServiceManager\DelegatorFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
/**
* Delegator factory for adding the CompositeAdapter.
*
* This delegator factory assumes that you've registered the CompositeAdapter
* as the service 'CompositeAdapter'.
*
* Attach it to the DefaultAuthenticationListener using:
*
* ```php
* 'service_manager' => array(
* 'delegators' => array(
* 'ZF\MvcAuth\Authentication\DefaultAuthenticationListener' => array(
* 'CompositeAdapterDelegatorFactory',
* ),
* ),
* ),
* ```
*/
class CompositeAdapterDelegatorFactory implements DelegatorFactoryInterface
{
public function createDelegatorWithName(
ServiceLocatorInterface $services,
$name,
$requestedName,
$callback
) {
$listener = $callback();
$composite = $services->get('CompositeAdapter');
$listener->attach($composite);
return $listener;
}
}
<?php
use ZF\MvcAuth\Factory\AuthenticationHttpAdapterFactory;
use ZF\MvcAuth\Factory\AuthenticationOAuth2AdapterFactory;
class CompositeAdapterFactory
{
public function __invoke($services)
{
$config = $services->get('Config');
$adapters = [];
/*
* You could put the key somewhere else. The idea, though, is that each
* named adapter matches a key under zf-mvc-auth.authentication.adapters. The
* primary problem with this approach is that you'll get two instances
* for adapters you attach to the composite adapter. One alternative is
* to use the approach in ZF\MvcAuth\Factory\AuthenticationAdapterDelegatorFactory
* and define the configuration for the attached adapters within this
* array, and instantiate them here only. Of course, that means you can
* only configure those adapters manually.
*/
if (isset($config['zf-mvc-auth']['authentication']['composite'])
&& is_array($config['zf-mvc-auth']['authentication']['composite'])
) {
$adapters = $config['zf-mvc-auth']['authentication']['composite'];
}
$composite = new CompositeAdapter();
foreach ($adapters as $adapterName) {
$adapter = $this->getAdapter(
$adapterName,
$config['zf-mvc-auth']['authentication']['adapters'],
$services
);
if (! $adapter) {
continue;
}
$composite->attach($adapter);
}
return $composite;
}
private function getAdapter($adapterName, array $config, $services)
{
if (! isset $config[$adapterName]
|| ! isset($config[$adapterName]['adapter'])
|| ! is_string($config[$adapterName]['adapter'])
) {
return false;
}
$config = $config[$adapterName];
switch ($config['adapter']) {
case 'ZF\MvcAuth\Authentication\HttpAdapter':
return AuthenticationHttpAdapterFactory::factory($adapterName, $config, $services);
case 'ZF\MvcAuth\Authentication\OAuth2Adapter':
return AuthenticationOAuth2AdapterFactory::factory($adapterName, $config, $services);
default:
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment