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