1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/**
* @see https://github.com/laminas/laminas-authentication for the canonical source repository
* @copyright https://github.com/laminas/laminas-authentication/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-authentication/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Authentication\Validator;
use Laminas\Authentication\Adapter\ValidatableAdapterInterface;
use Laminas\Authentication\AuthenticationService;
use Laminas\Authentication\Exception;
use Laminas\Authentication\Result;
use Laminas\Stdlib\ArrayUtils;
use Laminas\Validator\AbstractValidator;
use Traversable;
use function is_array;
use function is_string;
/**
* Authentication 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';
/**
* Authentication\Result codes mapping
* @const array
*/
const CODE_MAP = [
Result::FAILURE_IDENTITY_NOT_FOUND => self::IDENTITY_NOT_FOUND,
Result::FAILURE_CREDENTIAL_INVALID => self::CREDENTIAL_INVALID,
Result::FAILURE_IDENTITY_AMBIGUOUS => self::IDENTITY_AMBIGUOUS,
Result::FAILURE_UNCATEGORIZED => self::UNCATEGORIZED,
];
/**
* Authentication\Result codes mapping configurable overrides
* @var string[]
*/
protected $codeMap = [];
/**
* Error Messages
* @var array
*/
protected $messageTemplates = [
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 null|ValidatableAdapterInterface
*/
protected $adapter;
/**
* Identity (or field)
* @var string
*/
protected $identity;
/**
* Credential (or field)
* @var string
*/
protected $credential;
/**
* Authentication Service
* @var null|AuthenticationService
*/
protected $service;
/**
* Sets validator options
*
* @param array|Traversable $options
*/
public function __construct($options = null)
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
}
if (is_array($options)) {
if (isset($options['adapter'])) {
$this->setAdapter($options['adapter']);
}
if (isset($options['identity'])) {
$this->setIdentity($options['identity']);
}
if (isset($options['credential'])) {
$this->setCredential($options['credential']);
}
if (isset($options['service'])) {
$this->setService($options['service']);
}
if (isset($options['code_map'])) {
foreach ($options['code_map'] as $code => $template) {
if (empty($template) || ! is_string($template)) {
throw new Exception\InvalidArgumentException(
'Message key in code_map option must be a non-empty string'
);
}
if (! isset($this->messageTemplates[$template])) {
$this->messageTemplates[$template] = $this->messageTemplates[static::GENERAL];
}
$this->codeMap[(int) $code] = $template;
}
}
}
parent::__construct($options);
}
/**
* Get Adapter
*
* @return null|ValidatableAdapterInterface
*/
public function getAdapter()
{
return $this->adapter;
}
/**
* Set Adapter
*
* @param ValidatableAdapterInterface $adapter
* @return self Provides a fluent interface
*/
public function setAdapter(ValidatableAdapterInterface $adapter)
{
$this->adapter = $adapter;
return $this;
}
/**
* Get Identity
*
* @return mixed
*/
public function getIdentity()
{
return $this->identity;
}
/**
* Set Identity
*
* @param mixed $identity
* @return self Provides a fluent interface
*/
public function setIdentity($identity)
{
$this->identity = $identity;
return $this;
}
/**
* Get Credential
*
* @return mixed
*/
public function getCredential()
{
return $this->credential;
}
/**
* Set Credential
*
* @param mixed $credential
* @return self Provides a fluent interface
*/
public function setCredential($credential)
{
$this->credential = $credential;
return $this;
}
/**
* Get Service
*
* @return null|AuthenticationService
*/
public function getService()
{
return $this->service;
}
/**
* Set Service
*
* @param AuthenticationService $service
* @return self Provides a fluent interface
*/
public function setService(AuthenticationService $service)
{
$this->service = $service;
return $this;
}
/**
* Returns true if and only if authentication result is valid
*
* If authentication result fails validation, then this method returns false, and
* getMessages() will return an array of messages that explain why the
* validation failed.
*
* @param null|mixed $value OPTIONAL Credential (or field)
* @param null|array $context OPTIONAL Authentication data (identity and/or credential)
* @return bool
* @throws Exception\RuntimeException
*/
public function isValid($value = null, $context = null)
{
if ($value !== null) {
$this->setCredential($value);
}
if ($this->identity === null) {
throw new Exception\RuntimeException('Identity must be set prior to validation');
}
$identity = ($context !== null) && array_key_exists($this->identity, $context)
? $context[$this->identity]
: $this->identity;
if ($this->credential === null) {
throw new Exception\RuntimeException('Credential must be set prior to validation');
}
$credential = ($context !== null) && array_key_exists($this->credential, $context)
? $context[$this->credential]
: $this->credential;
if (! $this->service) {
throw new Exception\RuntimeException('AuthenticationService must be set prior to validation');
}
$adapter = $this->adapter ?: $this->getAdapterFromAuthenticationService();
$adapter->setIdentity($identity);
$adapter->setCredential($credential);
$result = $this->service->authenticate($adapter);
if ($result->isValid()) {
return true;
}
$messageKey = $this->mapResultCodeToMessageKey($result->getCode());
$this->error($messageKey);
return false;
}
/**
* @param int $code Authentication result code
* @return string Message key that should be used for the code
*/
protected function mapResultCodeToMessageKey($code)
{
if (isset($this->codeMap[$code])) {
return $this->codeMap[$code];
}
if (array_key_exists($code, static::CODE_MAP)) {
return static::CODE_MAP[$code];
}
return self::GENERAL;
}
/**
* @return ValidatableAdapterInterface
* @throws Exception\RuntimeException if no adapter present in
* authentication service
* @throws Exception\RuntimeException if adapter present in authentication
* service is not a ValidatableAdapterInterface instance
*/
private function getAdapterFromAuthenticationService()
{
if (! $this->service) {
throw new Exception\RuntimeException('Adapter must be set prior to validation');
}
$adapter = $this->service->getAdapter();
if (! $adapter) {
throw new Exception\RuntimeException('Adapter must be set prior to validation');
}
if (! $adapter instanceof ValidatableAdapterInterface) {
throw new Exception\RuntimeException(sprintf(
'Adapter must be an instance of %s; %s given',
ValidatableAdapterInterface::class,
is_object($adapter) ? get_class($adapter) : gettype($adapter)
));
}
return $adapter;
}
}