1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
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
|
|
|
namespace Phauthentic\PasswordHasher; |
16
|
|
|
|
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Abstract password hashing class |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractPasswordHasher implements PasswordHasherInterface |
23
|
|
|
{ |
24
|
|
|
const SALT_BEFORE = 'before'; |
25
|
|
|
const SALT_AFTER = 'after'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Salt |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
protected $salt = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Position of the salt |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $saltPosition; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Sets the salt if any was used |
43
|
|
|
* |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
4 |
|
public function setSalt(string $salt, string $position = self::SALT_AFTER): self |
47
|
|
|
{ |
48
|
4 |
|
$this->checkSaltPositionArgument($position); |
49
|
3 |
|
$this->salt = $salt; |
50
|
3 |
|
$this->saltPosition = $position; |
51
|
|
|
|
52
|
3 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Checks the salt position argument |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
4 |
|
protected function checkSaltPositionArgument($position): void |
61
|
|
|
{ |
62
|
4 |
|
if (!in_array($position, [self::SALT_BEFORE, self::SALT_AFTER])) { |
63
|
1 |
|
throw new InvalidArgumentException(sprintf( |
64
|
1 |
|
'`%s` is an invalud argument, it has to be `` or ``', |
65
|
1 |
|
$position, |
66
|
1 |
|
self::SALT_BEFORE, |
67
|
1 |
|
self::SALT_AFTER |
68
|
|
|
)); |
69
|
|
|
} |
70
|
3 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Adds the salt to a password |
74
|
|
|
* |
75
|
|
|
* @param string $password Password to salt |
76
|
|
|
* @return string Salted password |
77
|
|
|
*/ |
78
|
11 |
|
protected function saltPassword(string $password): string |
79
|
|
|
{ |
80
|
11 |
|
if (empty($this->salt)) { |
81
|
11 |
|
return $password; |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
if ($this->saltPosition === self::SALT_BEFORE) { |
85
|
1 |
|
return $this->salt . $password; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
return $password . $this->salt; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns true if the password need to be rehashed, due to the password being |
93
|
|
|
* created with anything else than the passwords generated by this class. |
94
|
|
|
* |
95
|
|
|
* Returns true by default since the only implementation users should rely |
96
|
|
|
* on is the one provided by default in php 5.5+ or any compatible library |
97
|
|
|
* |
98
|
|
|
* @param string $password The password to verify |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
1 |
|
public function needsRehash(string $password): bool |
102
|
|
|
{ |
103
|
1 |
|
return password_needs_rehash($password, PASSWORD_DEFAULT); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|