Created
December 18, 2012 01:27
-
-
Save anonymous/4324164 to your computer and use it in GitHub Desktop.
ZF2 Authentication Validator
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 | |
/** | |
* Zend Framework 2 | |
* | |
* @category Zend | |
* @package Validator | |
*/ | |
namespace Zend\Validator; | |
use Traversable; | |
use Zend\Authentication\Adapter\AdapterInterface; | |
use Zend\Authentication\AuthenticationService; | |
use Zend\Authentication\Result; | |
use Zend\Stdlib\ArrayUtils; | |
use Zend\Validator\AbstractValidator; | |
/** | |
* Authentication Validator | |
* | |
* @category Zend | |
* @package Validator | |
*/ | |
class Authentication extends AbstractValidator | |
{ | |
/** | |
* Error codes | |
* @const string | |
*/ | |
const IDENTITY_NOT_FOUND = 'identityNotFound'; | |
const IDENTITY_AMBIGUOUS = 'identityAmbiguous'; | |
const CREDENTIAL_INVALID = 'credentialInvalid'; | |
const UNCATEGORIZED = 'uncategorized'; | |
const GENERAL = 'general'; | |
/** | |
* Error Messages | |
* @var array | |
*/ | |
protected $messageTemplates = array( | |
self::IDENTITY_NOT_FOUND => 'Invalid identity', | |
self::IDENTITY_AMBIGUOUS => 'Identity is ambiguous', | |
self::CREDENTIAL_INVALID => 'Invalid password', | |
self::UNCATEGORIZED => 'Authentication failed', | |
self::GENERAL => 'Authentication failed', | |
); | |
/** | |
* Authentication Adapter | |
* @var Zend\Authentication\Adapter\Adapter | |
*/ | |
protected $adapter; | |
/** | |
* Credential (or field) | |
* @var string | |
*/ | |
protected $credential; | |
/** | |
* Authentication Service | |
* @var Zend\Authentication\AuthenticationService | |
*/ | |
protected $service; | |
/** | |
* Sets validator options | |
* | |
* @param mixed $options | |
*/ | |
public function __construct($options = null) | |
{ | |
if ($options instanceof Traversable) { | |
$options = ArrayUtils::iteratorToArray($options); | |
} | |
if (is_array($options)) { | |
if (array_key_exists('adapter', $options)) { | |
$this->setAdapter($options['adapter']); | |
} | |
if (array_key_exists('credential', $options)) { | |
$this->setCredential($options['credential']); | |
} | |
if (array_key_exists('service', $options)) { | |
$this->setService($options['service']); | |
} | |
} | |
parent::__construct($options); | |
} | |
/** | |
* Get Adapter | |
* | |
* @return Zend\Authentication\Adapter\AdapterInterface | |
*/ | |
public function getAdapter() | |
{ | |
return $this->adapter; | |
} | |
/** | |
* Set Adapter | |
* | |
* @param Zend\Authentication\Adapter\AdapterInterface $adapter | |
* @return Authentication | |
*/ | |
public function setAdapter(AdapterInterface $adapter) | |
{ | |
$this->adapter = $adapter; | |
return $this; | |
} | |
/** | |
* Get Credential | |
* | |
* @return string | |
*/ | |
public function getCredential() | |
{ | |
return $this->credential; | |
} | |
/** | |
* Set Credential | |
* | |
* @param string $credential | |
* @return Authentication | |
*/ | |
public function setCredential($credential) | |
{ | |
if (!is_string($credential) || empty($credential)) { | |
throw new Exception\InvalidArgumentException('$credential must be a non-empty string'); | |
} | |
$this->credential = $credential; | |
return $this; | |
} | |
/** | |
* Get Service | |
* | |
* @return Zend\Authentication\AuthenticationService | |
*/ | |
public function getService() | |
{ | |
return $this->service; | |
} | |
/** | |
* Set Service | |
* | |
* @param Zend\Authentication\AuthenticationService $service | |
* @return Authentication | |
*/ | |
public function setService(AuthenticationService $service) | |
{ | |
$this->service = $service; | |
return $this; | |
} | |
/** | |
* Is Valid | |
* | |
* @param mixed $value | |
* @param array $context | |
* @return bool | |
*/ | |
public function isValid($value, $context = null) | |
{ | |
$this->setValue($value); | |
if (!$this->credential) { | |
throw new Exception\RuntimeException('Credential must be set prior to validation'); | |
} | |
if (($context !== null) && array_key_exists($this->credential, $context)) { | |
$credential = $context[$this->credential]; | |
} else { | |
$credential = $this->credential; | |
} | |
if (!is_string($credential) || empty($credential)) { | |
$this->error(self::UNCATEGORIZED); | |
return false; | |
} | |
if (!$this->adapter) { | |
throw new Exception\RuntimeException('Adapter must be set prior to validation'); | |
} | |
$this->adapter->setCredentials($credential, $value); | |
if (!$this->service) { | |
throw new Exception\RuntimeException('AuthenticationService must be set prior to validation'); | |
} | |
$result = $this->service->authenticate($this->adapter); | |
if ($result->getCode() != Result::SUCCESS) { | |
switch ($result->getCode()) { | |
case Result::FAILURE_IDENTITY_NOT_FOUND: | |
$this->error(self::IDENTITY_NOT_FOUND); | |
break; | |
case Result::FAILURE_CREDENTIAL_INVALID: | |
$this->error(self::CREDENTIAL_INVALID); | |
break; | |
case Result::FAILURE_IDENTITY_AMBIGUOUS: | |
$this->error(self::IDENTITY_AMBIGUOUS); | |
break; | |
case Result::FAILURE_UNCATEGORIZED: | |
$this->error(self::UNCATEGORIZED); | |
break; | |
default: | |
$this->error(self::GENERAL); | |
} | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment