3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Authentication\Validator
;
13 use Zend\Authentication\Adapter\ValidatableAdapterInterface
;
14 use Zend\Authentication\AuthenticationService
;
15 use Zend\Authentication\Result
;
16 use Zend\Authentication\Exception
;
17 use Zend\Stdlib\ArrayUtils
;
18 use Zend\Validator\AbstractValidator
;
21 * Authentication Validator
23 class Authentication
extends AbstractValidator
29 const IDENTITY_NOT_FOUND
= 'identityNotFound';
30 const IDENTITY_AMBIGUOUS
= 'identityAmbiguous';
31 const CREDENTIAL_INVALID
= 'credentialInvalid';
32 const UNCATEGORIZED
= 'uncategorized';
33 const GENERAL
= 'general';
39 protected $messageTemplates = array(
40 self
::IDENTITY_NOT_FOUND
=> 'Invalid identity',
41 self
::IDENTITY_AMBIGUOUS
=> 'Identity is ambiguous',
42 self
::CREDENTIAL_INVALID
=> 'Invalid password',
43 self
::UNCATEGORIZED
=> 'Authentication failed',
44 self
::GENERAL
=> 'Authentication failed',
48 * Authentication Adapter
49 * @var ValidatableAdapterInterface
60 * Credential (or field)
63 protected $credential;
66 * Authentication Service
67 * @var AuthenticationService
72 * Sets validator options
74 * @param mixed $options
76 public function __construct($options = null)
78 if ($options instanceof Traversable
) {
79 $options = ArrayUtils
::iteratorToArray($options);
82 if (is_array($options)) {
83 if (array_key_exists('adapter', $options)) {
84 $this->setAdapter($options['adapter']);
86 if (array_key_exists('identity', $options)) {
87 $this->setIdentity($options['identity']);
89 if (array_key_exists('credential', $options)) {
90 $this->setCredential($options['credential']);
92 if (array_key_exists('service', $options)) {
93 $this->setService($options['service']);
96 parent
::__construct($options);
102 * @return ValidatableAdapterInterface
104 public function getAdapter()
106 return $this->adapter
;
112 * @param ValidatableAdapterInterface $adapter
113 * @return Authentication
115 public function setAdapter(ValidatableAdapterInterface
$adapter)
117 $this->adapter
= $adapter;
127 public function getIdentity()
129 return $this->identity
;
135 * @param mixed $identity
136 * @return Authentication
138 public function setIdentity($identity)
140 $this->identity
= $identity;
150 public function getCredential()
152 return $this->credential
;
158 * @param mixed $credential
159 * @return Authentication
161 public function setCredential($credential)
163 $this->credential
= $credential;
171 * @return AuthenticationService
173 public function getService()
175 return $this->service
;
181 * @param AuthenticationService $service
182 * @return Authentication
184 public function setService(AuthenticationService
$service)
186 $this->service
= $service;
194 * @param mixed $value
195 * @param array $context
198 public function isValid($value = null, $context = null)
200 if ($value !== null) {
201 $this->setCredential($value);
204 if (($context !== null) && array_key_exists($this->identity
, $context)) {
205 $identity = $context[$this->identity
];
207 $identity = $this->identity
;
209 if (!$this->identity
) {
210 throw new Exception\
RuntimeException('Identity must be set prior to validation');
213 if (($context !== null) && array_key_exists($this->credential
, $context)) {
214 $credential = $context[$this->credential
];
216 $credential = $this->credential
;
219 if (!$this->adapter
) {
220 throw new Exception\
RuntimeException('Adapter must be set prior to validation');
222 $this->adapter
->setIdentity($identity);
223 $this->adapter
->setCredential($credential);
225 if (!$this->service
) {
226 throw new Exception\
RuntimeException('AuthenticationService must be set prior to validation');
228 $result = $this->service
->authenticate($this->adapter
);
230 if ($result->getCode() != Result
::SUCCESS
) {
231 switch ($result->getCode()) {
232 case Result
::FAILURE_IDENTITY_NOT_FOUND
:
233 $this->error(self
::IDENTITY_NOT_FOUND
);
235 case Result
::FAILURE_CREDENTIAL_INVALID
:
236 $this->error(self
::CREDENTIAL_INVALID
);
238 case Result
::FAILURE_IDENTITY_AMBIGUOUS
:
239 $this->error(self
::IDENTITY_AMBIGUOUS
);
241 case Result
::FAILURE_UNCATEGORIZED
:
242 $this->error(self
::UNCATEGORIZED
);
245 $this->error(self
::GENERAL
);