1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) |
5
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
6
|
|
|
* |
7
|
|
|
* Licensed under The MIT License |
8
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
10
|
|
|
* |
11
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
12
|
|
|
* @link https://cakephp.org CakePHP(tm) Project |
13
|
|
|
* @since 1.0.0 |
14
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
declare(strict_types=1); |
18
|
|
|
|
19
|
|
|
namespace Phauthentic\Authentication\Identifier; |
20
|
|
|
|
21
|
|
|
use ArrayAccess; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Callback Identifier |
25
|
|
|
*/ |
26
|
|
|
class CollectionIdentifier implements IdentifierInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Errors |
30
|
|
|
* |
31
|
|
|
* @var array<mixed, mixed> |
32
|
|
|
*/ |
33
|
|
|
protected array $errors = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Identifier Collection |
37
|
|
|
* |
38
|
|
|
* @var iterable<\Phauthentic\Authentication\Identifier\IdentifierInterface> |
39
|
|
|
*/ |
40
|
|
|
protected iterable $collection; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor |
44
|
|
|
* |
45
|
|
|
* @param iterable<\Phauthentic\Authentication\Identifier\IdentifierInterface> $collection Identifier collection. |
46
|
|
|
*/ |
47
|
|
|
public function __construct(iterable $collection) |
48
|
|
|
{ |
49
|
|
|
$this->collection = $collection; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get errors |
54
|
|
|
* |
55
|
|
|
* @return array<mixed, mixed> |
56
|
|
|
*/ |
57
|
|
|
public function getErrors(): array |
58
|
|
|
{ |
59
|
|
|
return $this->errors; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Identifies an user or service by the passed credentials |
64
|
|
|
* |
65
|
|
|
* @param array<string, string> $credentials Authentication credentials |
66
|
|
|
* @return \ArrayAccess|null |
67
|
|
|
*/ |
68
|
|
|
public function identify(array $credentials): ?ArrayAccess |
69
|
|
|
{ |
70
|
|
|
/** @var \Phauthentic\Authentication\Identifier\IdentifierInterface $identifier */ |
71
|
|
|
foreach ($this->collection as $identifier) { |
72
|
|
|
$result = $identifier->identify($credentials); |
73
|
|
|
if ($result) { |
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
|
|
$this->errors[get_class($identifier)] = $identifier->getErrors(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|