1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mouf\Security\Password; |
4
|
|
|
|
5
|
|
|
use Mouf\Utils\I18n\Fine\TranslatorInterface; |
6
|
|
|
|
7
|
|
|
class PasswordStrengthCheck implements \Mouf\Security\Password\Api\PasswordStrengthCheck |
8
|
|
|
{ |
9
|
|
|
private $minimumLength = 7; |
10
|
|
|
|
11
|
|
|
private $mustHaveUpperCase = true; |
12
|
|
|
|
13
|
|
|
private $mustHaveLowerCase = true; |
14
|
|
|
|
15
|
|
|
private $mustHaveNumber = true; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var TranslatorInterface |
19
|
|
|
*/ |
20
|
|
|
private $translationService; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* PasswordStrengthCheck constructor. |
24
|
|
|
* |
25
|
|
|
* @param TranslatorInterface $translationService |
26
|
|
|
*/ |
27
|
|
|
public function __construct(TranslatorInterface $translationService) |
28
|
|
|
{ |
29
|
|
|
$this->translationService = $translationService; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return int |
34
|
|
|
*/ |
35
|
|
|
public function getMinimumLength(): int |
36
|
|
|
{ |
37
|
|
|
return $this->minimumLength; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param int|null $minimumLength |
42
|
|
|
*/ |
43
|
|
|
public function setMinimumLength(int $minimumLength = null) |
44
|
|
|
{ |
45
|
|
|
$this->minimumLength = $minimumLength; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function isMustHaveUpperCase(): bool |
52
|
|
|
{ |
53
|
|
|
return $this->mustHaveUpperCase; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param bool $mustHaveUpperCase |
58
|
|
|
*/ |
59
|
|
|
public function setMustHaveUpperCase(bool $mustHaveUpperCase) |
60
|
|
|
{ |
61
|
|
|
$this->mustHaveUpperCase = $mustHaveUpperCase; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function isMustHaveLowerCase(): bool |
68
|
|
|
{ |
69
|
|
|
return $this->mustHaveLowerCase; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param bool $mustHaveLowerCase |
74
|
|
|
*/ |
75
|
|
|
public function setMustHaveLowerCase(bool $mustHaveLowerCase) |
76
|
|
|
{ |
77
|
|
|
$this->mustHaveLowerCase = $mustHaveLowerCase; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function isMustHaveNumber(): bool |
84
|
|
|
{ |
85
|
|
|
return $this->mustHaveNumber; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param bool $mustHaveNumber |
90
|
|
|
*/ |
91
|
|
|
public function setMustHaveNumber(bool $mustHaveNumber) |
92
|
|
|
{ |
93
|
|
|
$this->mustHaveNumber = $mustHaveNumber; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns true if the password is strong enough, false otherwise. |
98
|
|
|
* |
99
|
|
|
* @param string $password |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function checkPasswordStrength(string $password) : bool |
104
|
|
|
{ |
105
|
|
|
if ($this->minimumLength !== null && strlen($password) < $this->minimumLength) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($this->mustHaveLowerCase === true && !preg_match('#[a-z]+#', $password)) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($this->mustHaveUpperCase === true && !preg_match('#[A-Z]+#', $password)) { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($this->mustHaveUpperCase === true && !preg_match('#[0-9]+#', $password)) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns a list of rules that password must fullfil (in text). |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function getPasswordRules() : string |
130
|
|
|
{ |
131
|
|
|
if ($this->minimumLength === null && !$this->mustHaveUpperCase |
132
|
|
|
&& !$this->mustHaveLowerCase && !$this->mustHaveNumber) { |
133
|
|
|
return ''; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$base = $this->translationService->getTranslation('passwordservice.base_error_message'); |
137
|
|
|
$base .= '<ul>'; |
138
|
|
|
|
139
|
|
|
if ($this->minimumLength !== null) { |
140
|
|
|
$base .= '<li>'.$this->translationService->getTranslation('passwordservice.minimum_length', ['length' => $this->minimumLength]).'</li>'; |
141
|
|
|
} |
142
|
|
|
if ($this->mustHaveLowerCase === true) { |
143
|
|
|
$base .= '<li>'.$this->translationService->getTranslation('passwordservice.upper_case').'</li>'; |
144
|
|
|
} |
145
|
|
|
if ($this->mustHaveLowerCase === true) { |
146
|
|
|
$base .= '<li>'.$this->translationService->getTranslation('passwordservice.lower_case').'</li>'; |
147
|
|
|
} |
148
|
|
|
if ($this->mustHaveNumber === true) { |
149
|
|
|
$base .= '<li>'.$this->translationService->getTranslation('passwordservice.number_case').'</li>'; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$base .= '</ul>'; |
153
|
|
|
|
154
|
|
|
return $base; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|