1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
5
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org) |
12
|
|
|
* @link http://cakephp.org CakePHP(tm) Project |
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
declare(strict_types=1); |
17
|
|
|
|
18
|
|
|
namespace Phauthentic\PasswordHasher; |
19
|
|
|
|
20
|
|
|
use ArrayIterator; |
21
|
|
|
use RuntimeException; |
22
|
|
|
use Traversable; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Password hashing class that use weak hashing algorithms. This class is |
26
|
|
|
* intended only to be used with legacy databases where passwords have |
27
|
|
|
* not been migrated to a stronger algorithm yet. |
28
|
|
|
*/ |
29
|
|
|
class PasswordHasherCollection implements PasswordHasherCollectionInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* List of Hashers |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $hashers = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor |
40
|
|
|
* |
41
|
|
|
* @param iterable $hashers An iterable of password hashers |
42
|
|
|
*/ |
43
|
16 |
|
public function __construct(iterable $hashers = []) |
44
|
|
|
{ |
45
|
16 |
|
foreach ($hashers as $hasher) { |
46
|
8 |
|
$this->add($hasher); |
47
|
|
|
} |
48
|
16 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Adds a password hasher to the collection |
52
|
|
|
* |
53
|
|
|
* @param \Phauthentic\PasswordHasher\PasswordHasherInterface $hasher Hasher |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
10 |
|
public function add(PasswordHasherInterface $hasher): void |
57
|
|
|
{ |
58
|
10 |
|
$this->hashers[] = $hasher; |
59
|
10 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Retrieve an external iterator |
63
|
|
|
* |
64
|
|
|
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
65
|
|
|
* @return Traversable An instance of an object implementing <b>Iterator</b> or |
66
|
|
|
* <b>Traversable</b> |
67
|
|
|
*/ |
68
|
4 |
|
public function getIterator(): Traversable |
69
|
|
|
{ |
70
|
4 |
|
return new ArrayIterator($this->hashers); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Whether a offset exists |
75
|
|
|
* |
76
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
77
|
|
|
* @param mixed $offset <p> |
78
|
|
|
* An offset to check for. |
79
|
|
|
* </p> |
80
|
|
|
* @return bool true on success or false on failure. |
81
|
|
|
* </p> |
82
|
|
|
* <p> |
83
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
84
|
|
|
*/ |
85
|
4 |
|
public function offsetExists($offset) |
86
|
|
|
{ |
87
|
4 |
|
return isset($this->hashers[$offset]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Offset to retrieve |
92
|
|
|
* |
93
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
94
|
|
|
* @param mixed $offset <p> |
95
|
|
|
* The offset to retrieve. |
96
|
|
|
* </p> |
97
|
|
|
* @return mixed Can return all value types. |
98
|
|
|
*/ |
99
|
6 |
|
public function offsetGet($offset) |
100
|
|
|
{ |
101
|
6 |
|
if (isset($this->hashers[$offset])) { |
102
|
6 |
|
return $this->hashers[$offset]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Offset to set |
108
|
|
|
* |
109
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
110
|
|
|
* @param mixed $offset <p> |
111
|
|
|
* The offset to assign the value to. |
112
|
|
|
* </p> |
113
|
|
|
* @param mixed $value <p> |
114
|
|
|
* The value to set. |
115
|
|
|
* </p> |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
2 |
|
public function offsetSet($offset, $value) |
119
|
|
|
{ |
120
|
2 |
|
throw new RuntimeException('Use add()'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Offset to unset |
125
|
|
|
* |
126
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
127
|
|
|
* @param mixed $offset <p> |
128
|
|
|
* The offset to unset. |
129
|
|
|
* </p> |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
public function offsetUnset($offset) |
133
|
|
|
{ |
134
|
|
|
unset($this->hashers[$offset]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritDoc |
139
|
|
|
*/ |
140
|
12 |
|
public function count(): int |
141
|
|
|
{ |
142
|
12 |
|
return count($this->hashers); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|