-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathPasswords.php
More file actions
145 lines (126 loc) · 3.74 KB
/
Copy pathPasswords.php
File metadata and controls
145 lines (126 loc) · 3.74 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Shield\Authentication;
use CodeIgniter\Shield\Authentication\Passwords\ValidatorInterface;
use CodeIgniter\Shield\Config\Auth;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Result;
/**
* Class Passwords
*
* Provides a central location to handle password
* related tasks like hashing, verifying, validating, etc.
*/
class Passwords
{
public function __construct(protected Auth $config)
{
}
/**
* Hash a password.
*
* @return false|string|null
*/
public function hash(string $password)
{
return password_hash($password, $this->config->hashAlgorithm, $this->getHashOptions());
}
private function getHashOptions(): array
{
if (
(defined('PASSWORD_ARGON2I') && $this->config->hashAlgorithm === PASSWORD_ARGON2I)
|| (defined('PASSWORD_ARGON2ID') && $this->config->hashAlgorithm === PASSWORD_ARGON2ID)
) {
return [
'memory_cost' => $this->config->hashMemoryCost,
'time_cost' => $this->config->hashTimeCost,
'threads' => $this->config->hashThreads,
];
}
return [
'cost' => $this->config->hashCost,
];
}
/**
* Hash a password.
*
* @return false|string|null
*
* @deprecated This is only for backward compatibility.
*/
public function hashDanger(string $password)
{
return password_hash(
base64_encode(
hash('sha384', $password, true),
),
$this->config->hashAlgorithm,
$this->getHashOptions(),
);
}
/**
* Verifies a password against a previously hashed password.
*
* @param string $password The password we're checking
* @param string $hash The previously hashed password
*/
public function verify(string $password, string $hash): bool
{
return password_verify($password, $hash);
}
/**
* Checks to see if a password should be rehashed.
*/
public function needsRehash(string $hashedPassword): bool
{
return password_needs_rehash($hashedPassword, $this->config->hashAlgorithm, $this->getHashOptions());
}
/**
* Checks a password against all of the Validators specified
* in `$passwordValidators` setting in Config\Auth.php.
*
* @throws AuthenticationException
*/
public function check(string $password, ?User $user = null): Result
{
if (null === $user) {
throw AuthenticationException::forNoEntityProvided();
}
$password = trim($password);
if ($password === '') {
return new Result([
'success' => false,
'reason' => lang('Auth.errorPasswordEmpty'),
]);
}
foreach ($this->config->passwordValidators as $className) {
/** @var ValidatorInterface $class */
$class = new $className($this->config);
$result = $class->check($password, $user);
if (! $result->isOK()) {
return $result;
}
}
return new Result([
'success' => true,
]);
}
/**
* Returns the validation rule for max length.
*/
public static function getMaxLengthRule(): string
{
if (config('Auth')->hashAlgorithm === PASSWORD_BCRYPT) {
return 'max_byte[72]';
}
return 'max_length[255]';
}
}