-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathCompositionValidator.php
More file actions
58 lines (50 loc) · 1.73 KB
/
Copy pathCompositionValidator.php
File metadata and controls
58 lines (50 loc) · 1.73 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
<?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\Passwords;
use CodeIgniter\Shield\Authentication\AuthenticationException;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Result;
/**
* Class CompositionValidator
*
* Checks the general makeup of the password.
*
* While older composition checks might have included different character
* groups that you had to include, current NIST standards prefer to simply
* set a minimum length and a long maximum (128+ chars).
*
* @see https://pages.nist.gov/800-63-3/sp800-63b.html#sec5
*/
class CompositionValidator extends BaseValidator implements ValidatorInterface
{
/**
* Returns true when the password passes this test.
* The password will be passed to any remaining validators.
* False will immediately stop validation process
*/
public function check(string $password, ?User $user = null): Result
{
if ($this->config->minimumPasswordLength === 0) {
throw AuthenticationException::forUnsetPasswordLength();
}
$passed = mb_strlen($password, 'UTF-8') >= $this->config->minimumPasswordLength;
if (! $passed) {
return new Result([
'success' => false,
'reason' => lang('Auth.errorPasswordLength', [$this->config->minimumPasswordLength]),
'extraInfo' => lang('Auth.suggestPasswordLength'),
]);
}
return new Result([
'success' => true,
]);
}
}