Skip to content

Instantly share code, notes, and snippets.

@vojtech-dobes
Created August 31, 2012 12:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vojtech-dobes/3552238 to your computer and use it in GitHub Desktop.
Save vojtech-dobes/3552238 to your computer and use it in GitHub Desktop.
Multiple ways of authentication in Nette
services:
authenticator:
class: VojtechDobes\NetteSecurity\MultiAuthenticator
setup:
- addAuthenticator( simple, @nette.authenticator )
- addAuthenticator( twitter, SomeCustomTwitterAuthenticator( key, secretKey ) )
<?php
$this->user->login('simple', 'username', '****');
$this->user->login('twitter');
<?php
namespace VojtechDobes\NetteSecurity;
use Nette\InvalidArgumentException;
use Nette\Security\IAuthenticator;
use Nette\Security\IIdentity;
/**
* Provides unified access to multiple authenticators
*
* @author Vojtěch Dobeš
*/
class MultiAuthenticator implements IAuthenticator
{
/** @var callable[] */
private $authenticators = array();
/**
* Registers authenticator
*
* @param string
* @param IAuthenticator|callable
* @return MultiAuthenticator provides a fluent interface
*/
public function addAuthenticator($key, $authenticator)
{
if ($authenticator instanceof IAuthenticator) {
$this->authenticators[$key] = array($authenticator, 'authenticate');
} elseif (is_callable($authenticator)) {
$this->authenticators[$key] = $authenticator;
} else {
throw new InvalidArgumentException('Authenticator must be callable or instance of IAuthenticator.');
}
return $this;
}
/**
* Tries to authenticate via authenticator named as first argument
*
* @param array
* @return IIdentity
*/
public function authenticate(array $args)
{
$key = array_shift($args);
if (!isset($this->authenticators[$key])) {
throw new InvalidArgumentException("Authenticator named '$key' is not registered.");
}
return call_user_func($this->authenticators[$key], $args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment