Created
August 31, 2012 12:40
-
-
Save vojtech-dobes/3552238 to your computer and use it in GitHub Desktop.
Multiple ways of authentication in Nette
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
authenticator: | |
class: VojtechDobes\NetteSecurity\MultiAuthenticator | |
setup: | |
- addAuthenticator( simple, @nette.authenticator ) | |
- addAuthenticator( twitter, SomeCustomTwitterAuthenticator( key, secretKey ) ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$this->user->login('simple', 'username', '****'); | |
$this->user->login('twitter'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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