-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathNothingPersonalValidator.php
More file actions
210 lines (180 loc) · 6.88 KB
/
NothingPersonalValidator.php
File metadata and controls
210 lines (180 loc) · 6.88 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?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\Entities\User;
use CodeIgniter\Shield\Result;
/**
* Class NothingPersonalValidator
*
* Checks password does not contain any personal information
*/
class NothingPersonalValidator extends BaseValidator implements ValidatorInterface
{
/**
* Returns true if $password contains no part of the username
* or the user's email. Otherwise, it returns false.
* If true is returned the password will be passed to next validator.
* If false is returned the validation process will be immediately stopped.
*/
public function check(string $password, ?User $user = null): Result
{
$password = strtolower($password);
if ($valid = $this->isNotPersonal($password, $user) === true) {
$valid = $this->isNotSimilar($password, $user);
}
return new Result([
'success' => $valid,
'reason' => $this->error,
'extraInfo' => $this->suggestion,
]);
}
/**
* isNotPersonal()
*
* Looks for personal information in a password. The personal info used
* comes from CodeIgniter\Shield\Entities\User properties username and email.
*
* It is possible to include other fields as information sources.
* For instance, a project might require adding `firstname` and `lastname` properties
* to an extended version of the User class.
* The new fields can be included in personal information testing in by setting
* the `$personalFields` property in CodeIgniter\Shield\Config\Auth, e.g.
*
* public $personalFields = ['firstname', 'lastname'];
*
* isNotPersonal() returns true if no personal information can be found, or false
* if such info is found.
*/
protected function isNotPersonal(string $password, ?User $user): bool
{
$userName = strtolower($user->username ?? '');
$email = strtolower($user->email);
$valid = true;
// The most obvious transgressions
if ($password === $userName
|| $password === $email
|| $password === strrev($userName)) {
$valid = false;
}
// Parse out as many pieces as possible from username, password and email.
// Use the pieces as needles and haystacks and look every which way for matches.
if ($valid) {
// Take username apart for use as search needles
$needles = $this->strip_explode($userName);
// extract local-part and domain parts from email as separate needles
[$localPart, $domain] = explode('@', $email) + [1 => null];
// might be john.doe@example.com and we want all the needles we can get
$emailParts = $this->strip_explode($localPart);
if ($domain !== null && $domain !== '') {
$emailParts[] = $domain;
}
$needles = [...$needles, ...$emailParts];
// Get any other "personal" fields defined in config
$personalFields = $this->config->personalFields;
foreach ($personalFields as $value) {
if (! empty($user->{$value})) {
$needles[] = strtolower((string) $user->{$value});
}
}
$trivial = [
'a',
'an',
'and',
'as',
'at',
'but',
'for',
'if',
'in',
'not',
'of',
'or',
'so',
'the',
'then',
];
// Make password into haystacks
$haystacks = $this->strip_explode($password);
foreach ($haystacks as $haystack) {
if (empty($haystack) || in_array($haystack, $trivial, true) || mb_strlen($haystack, 'UTF-8') < 3) {
continue; // ignore trivial words
}
foreach ($needles as $needle) {
if (empty($needle) || in_array($needle, $trivial, true) || mb_strlen($needle, 'UTF-8') < 3) {
continue;
}
// look both ways in case password is subset of needle
if (strpos($haystack, $needle) !== false
|| strpos($needle, $haystack) !== false) {
$valid = false;
break 2;
}
}
}
}
if ($valid) {
return true;
}
$this->error = lang('Auth.errorPasswordPersonal');
$this->suggestion = lang('Auth.suggestPasswordPersonal');
return false;
}
/**
* notSimilar() uses $password and $userName to calculate a similarity value.
* Similarity values equal to, or greater than CodeIgniter\Shield\Config::maxSimilarity
* are rejected for being too much alike and false is returned.
* Otherwise, true is returned,
*
* A $maxSimilarity value of 0 (zero) returns true without making a comparison.
* In other words, 0 (zero) turns off similarity testing.
*/
protected function isNotSimilar(string $password, ?User $user): bool
{
if ($user->username === null) {
return true;
}
$maxSimilarity = (float) $this->config->maxSimilarity;
// sanity checking - working range 1-100, 0 is off
if ($maxSimilarity < 1) {
$maxSimilarity = 0;
} elseif ($maxSimilarity > 100) {
$maxSimilarity = 100;
}
if (! empty($maxSimilarity)) {
$userName = strtolower($user->username);
similar_text($password, $userName, $similarity);
if ($similarity >= $maxSimilarity) {
$this->error = lang('Auth.errorPasswordTooSimilar');
$this->suggestion = lang('Auth.suggestPasswordTooSimilar');
return false;
}
}
return true;
}
/**
* strip_explode($str)
*
* Replaces all non-word characters and underscores in $str with a space.
* Then it explodes that result using the space for a delimiter.
*
* @return array<int, string>
*/
protected function strip_explode(string $str): array
{
$stripped = preg_replace('/[\W_]+/', ' ', $str);
$parts = explode(' ', trim($stripped));
// If it's not already there put the untouched input at the top of the array
if (! in_array($str, $parts, true)) {
array_unshift($parts, $str);
}
return $parts;
}
}