1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* sysPass |
4
|
|
|
* |
5
|
|
|
* @author nuxsmin |
6
|
|
|
* @link https://syspass.org |
7
|
|
|
* @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org |
8
|
|
|
* |
9
|
|
|
* This file is part of sysPass. |
10
|
|
|
* |
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU General Public License |
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace SP\Mvc\Controller\Validators; |
26
|
|
|
|
27
|
|
|
use SP\Core\Exceptions\ValidationException; |
28
|
|
|
use SP\DataModel\ItemPreset\Password; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class PasswordValidator |
32
|
|
|
* |
33
|
|
|
* @package SP\Mvc\Controller |
34
|
|
|
*/ |
35
|
|
|
final class PasswordValidator implements ValidatorInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var Password |
39
|
|
|
*/ |
40
|
|
|
private $password; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* PasswordValidator constructor. |
44
|
|
|
* |
45
|
|
|
* @param Password $password |
46
|
|
|
*/ |
47
|
|
|
public function __construct(Password $password) |
48
|
|
|
{ |
49
|
|
|
$this->password = $password; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Password $password |
54
|
|
|
* |
55
|
|
|
* @return PasswordValidator |
56
|
|
|
*/ |
57
|
|
|
public static function factory(Password $password) |
58
|
|
|
{ |
59
|
|
|
return new self($password); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $string |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
* @throws ValidationException |
67
|
|
|
*/ |
68
|
|
|
public function validate(string $string): bool |
69
|
|
|
{ |
70
|
|
|
if (mb_strlen($string) < $this->password->getLength()) { |
71
|
|
|
throw new ValidationException(sprintf(__('Password needs to be %d characters long'), $this->password->getLength())); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$regex = $this->password->getRegex(); |
75
|
|
|
|
76
|
|
|
if (!empty($this->password->getRegex()) && !Validator::matchRegex($string, $regex)) { |
77
|
|
|
throw new ValidationException(__u('Password does not contain the required characters'), ValidationException::ERROR, $regex); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($this->password->isUseLetters()) { |
81
|
|
|
if (!Validator::hasLetters($string)) { |
82
|
|
|
throw new ValidationException(__u('Password needs to contain letters')); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($this->password->isUseLower() && !Validator::hasLower($string)) { |
86
|
|
|
throw new ValidationException(__u('Password needs to contain lower case letters')); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($this->password->isUseUpper() && !Validator::hasUpper($string)) { |
90
|
|
|
throw new ValidationException(__u('Password needs to contain upper case letters')); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($this->password->isUseNumbers() && !Validator::hasNumbers($string)) { |
95
|
|
|
throw new ValidationException(__u('Password needs to contain numbers')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($this->password->isUseSymbols() && !Validator::hasSymbols($string)) { |
99
|
|
|
throw new ValidationException(__u('Password needs to contain symbols')); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
} |