|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Devlop\SecurePasswordRule; |
| 6 | + |
| 7 | +use Dont\JustDont; |
| 8 | +use Illuminate\Contracts\Validation\Rule; |
| 9 | + |
| 10 | +class SecurePasswordRule implements Rule |
| 11 | +{ |
| 12 | + use JustDont; |
| 13 | + |
| 14 | + /** |
| 15 | + * The minimum length |
| 16 | + */ |
| 17 | + protected int $minLength = 6; |
| 18 | + |
| 19 | + /** |
| 20 | + * Require the use of at least X letters |
| 21 | + */ |
| 22 | + protected int $minLetters = 0; |
| 23 | + |
| 24 | + /** |
| 25 | + * Require the use of at least X lowercase letters |
| 26 | + */ |
| 27 | + protected int $minLowercaseLetters = 0; |
| 28 | + |
| 29 | + /** |
| 30 | + * Require the use of at least X uppercase letters |
| 31 | + */ |
| 32 | + protected int $minUppercaseLetters = 0; |
| 33 | + |
| 34 | + /** |
| 35 | + * Require the use of at least X numbers |
| 36 | + */ |
| 37 | + protected int $minNumbers = 0; |
| 38 | + |
| 39 | + /** |
| 40 | + * Require the use of at least X special characters |
| 41 | + */ |
| 42 | + protected int $minSpecial = 0; |
| 43 | + |
| 44 | + /** |
| 45 | + * No more than X of consecutive whitespace characters (null to disable check) |
| 46 | + */ |
| 47 | + protected ?int $maxConsecutiveWhitespace = 2; |
| 48 | + |
| 49 | + /** |
| 50 | + * No more than X of consecutive identical characters (null to disable check) |
| 51 | + */ |
| 52 | + protected ?int $maxConsecutiveIdentical = null; |
| 53 | + |
| 54 | + /** |
| 55 | + * The errors for the password being tested |
| 56 | + * |
| 57 | + * @var array<string> |
| 58 | + */ |
| 59 | + protected array $errors = []; |
| 60 | + |
| 61 | + /** |
| 62 | + * Create a new rule instance. |
| 63 | + * |
| 64 | + * @return void |
| 65 | + */ |
| 66 | + public function __construct() |
| 67 | + { |
| 68 | + // |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Determine if the validation rule passes. |
| 73 | + * |
| 74 | + * @param string $attribute |
| 75 | + * @param mixed $value |
| 76 | + * @return bool |
| 77 | + */ |
| 78 | + public function passes($attribute, $value) : bool |
| 79 | + { |
| 80 | + if (mb_strlen($value) < $this->minLength) { |
| 81 | + // must be of a minimum length |
| 82 | + $this->errors[] = $this->minLengthMessage( |
| 83 | + 'The password must be at least :min characters.', |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + if (preg_match_all('/\p{L}/', $value) < $this->minLetters) { |
| 88 | + $this->errors[] = $this->minLettersMessage( |
| 89 | + 'The password must contain letters.|The password must contain at least :min letters.', |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + if (preg_match_all('/\p{Ll}/', $value) < $this->minLowercaseLetters) { |
| 94 | + $this->errors[] = $this->minLowercaseLettersMessage( |
| 95 | + 'The password must contain lowercase letters.|The password must contain at least :min lowercase letters.', |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + if (preg_match_all('/\p{Lu}/', $value) < $this->minUppercaseLetters) { |
| 100 | + $this->errors[] = $this->minUppercaseLettersMessage( |
| 101 | + 'The password must contain uppercase letters.|The password must contain at least :min uppercase letters.', |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + if (preg_match_all('/\p{N}/', $value) < $this->minNumbers) { |
| 106 | + $this->errors[] = $this->minNumbersMessage( |
| 107 | + 'The password must contain numbers.|The password must contain at least :min numbers.', |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + if (preg_match_all('/[^\p{L}\p{N}]/', $value) < $this->minSpecial) { |
| 112 | + $this->errors[] = $this->minSpecialMessage( |
| 113 | + 'The password must contain special characters.|The password must contain at least :min special characters.', |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + // TODO: add check disallowing whitespace totally |
| 118 | + |
| 119 | + if ($this->maxConsecutiveWhitespace !== null && preg_match('/\s{' . max(0, $this->maxConsecutiveWhitespace) . ',}/', $value) === 1) { |
| 120 | + $this->errors[] = $this->maxConsecutiveWhitespaceMessage( |
| 121 | + 'The password can not contain more than :max consecutive space.|The password can not contain more than :max consecutive spaces.', |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + if ($this->maxConsecutiveIdentical !== null && preg_match('/(.)\1{' . max(0, $this->maxConsecutiveIdentical) . ',}/', $value) === 1) { |
| 126 | + $this->errors[] = $this->maxConsecutiveIdenticalMessage( |
| 127 | + 'The password can not contain more than :max consecutive identical character.|The password can not contain more than :max consecutive identical characters.', |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + return count($this->errors) === 0; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Get the validation error message. |
| 136 | + */ |
| 137 | + public function message() : string |
| 138 | + { |
| 139 | + return __('The password is not good enough: :error', [ |
| 140 | + 'error' => $this->errors[0] ?? '', |
| 141 | + ]); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Get the validation error message for $minLength |
| 146 | + */ |
| 147 | + protected function minLengthMessage(string $default) : string |
| 148 | + { |
| 149 | + return trans_choice($default, $this->minLetters, [ |
| 150 | + 'min' => $this->minLetters, |
| 151 | + ]); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Get the validation error message for $minLetters |
| 156 | + */ |
| 157 | + protected function minLettersMessage(string $default) : string |
| 158 | + { |
| 159 | + return trans_choice($default, $this->minLetters, [ |
| 160 | + 'min' => $this->minLetters, |
| 161 | + ]); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Get the validation error message for $minLowercaseLetters |
| 166 | + */ |
| 167 | + protected function minLowercaseLettersMessage(string $default) : string |
| 168 | + { |
| 169 | + return trans_choice($default, $this->minLowercaseLetters, [ |
| 170 | + 'min' => $this->minLowercaseLetters, |
| 171 | + ]); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Get the validation error message for $minUppercaseLetters |
| 176 | + */ |
| 177 | + protected function minUppercaseLettersMessage(string $default) : string |
| 178 | + { |
| 179 | + return trans_choice($default, $this->minUppercaseLetters, [ |
| 180 | + 'min' => $this->minUppercaseLetters, |
| 181 | + ]); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Get the validation error message for $minNumbers |
| 186 | + */ |
| 187 | + protected function minNumbersMessage(string $default) : string |
| 188 | + { |
| 189 | + return trans_choice($default, $this->minNumbers, [ |
| 190 | + 'min' => $this->minNumbers, |
| 191 | + ]); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Get the validation error message for $minSpecial |
| 196 | + */ |
| 197 | + protected function minSpecialMessage(string $default) : string |
| 198 | + { |
| 199 | + return trans_choice($default, $this->minSpecial, [ |
| 200 | + 'min' => $this->minSpecial, |
| 201 | + ]); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Get the validation error message for $maxConsecutiveWhitespaceMessage |
| 206 | + */ |
| 207 | + protected function maxConsecutiveWhitespaceMessage(string $default) : string |
| 208 | + { |
| 209 | + return trans_choice($default, $this->maxConsecutiveWhitespace, [ |
| 210 | + 'max' => $this->maxConsecutiveWhitespace, |
| 211 | + ]); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Get the validation error message for $maxConsecutiveIdentical |
| 216 | + */ |
| 217 | + protected function maxConsecutiveIdenticalMessage(string $default) : string |
| 218 | + { |
| 219 | + return trans_choice($default, $this->maxConsecutiveIdentical, [ |
| 220 | + 'max' => $this->maxConsecutiveIdentical, |
| 221 | + ]); |
| 222 | + } |
| 223 | +} |
0 commit comments