|
1
|
|
|
<?php
|
|
2
|
|
|
/**
|
|
3
|
|
|
* @author Semih Serhat Karakaya <[email protected]>
|
|
4
|
|
|
* @copyright Copyright (c) 2017, ownCloud GmbH.
|
|
5
|
|
|
* @license AGPL-3.0
|
|
6
|
|
|
*
|
|
7
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
8
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
9
|
|
|
* as published by the Free Software Foundation.
|
|
10
|
|
|
*
|
|
11
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
|
|
* GNU Affero General Public License for more details.
|
|
15
|
|
|
*
|
|
16
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
18
|
|
|
*/
|
|
19
|
|
|
|
|
20
|
|
|
namespace OCA\Security;
|
|
21
|
|
|
|
|
22
|
|
|
use OC\HintException;
|
|
23
|
|
|
use OCP\IL10N;
|
|
24
|
|
|
|
|
25
|
|
|
class PasswordValidator {
|
|
26
|
|
|
|
|
27
|
|
|
/** @var SecurityConfig */
|
|
28
|
|
|
protected $config;
|
|
29
|
|
|
|
|
30
|
|
|
/** @var IL10N */
|
|
31
|
|
|
protected $l;
|
|
32
|
|
|
|
|
33
|
|
|
/**
|
|
34
|
|
|
* PasswordValidator constructor.
|
|
35
|
|
|
*
|
|
36
|
|
|
* @param SecurityConfig $config
|
|
37
|
|
|
* @param IL10N $l
|
|
38
|
|
|
*/
|
|
39
|
|
|
public function __construct(SecurityConfig $config, IL10N $l) {
|
|
40
|
|
|
$this->config = $config;
|
|
41
|
|
|
$this->l = $l;
|
|
42
|
|
|
}
|
|
43
|
|
|
|
|
44
|
|
|
/**
|
|
45
|
|
|
* validate the given password satisfies defined password policy
|
|
46
|
|
|
*
|
|
47
|
|
|
* @param string $password
|
|
48
|
|
|
* @throws HintException
|
|
49
|
|
|
*/
|
|
50
|
|
|
public function validate($password) {
|
|
51
|
|
|
$this->checkPasswordLength($password);
|
|
52
|
|
|
$this->checkNumericCharacters($password);
|
|
53
|
|
|
$this->checkUpperLowerCase($password);
|
|
54
|
|
|
$this->checkSpecialCharacters($password);
|
|
55
|
|
|
}
|
|
56
|
|
|
|
|
57
|
|
|
/**
|
|
58
|
|
|
* validate the given password satisfies defined min length
|
|
59
|
|
|
*
|
|
60
|
|
|
* @param string $password
|
|
61
|
|
|
* @throws HintException
|
|
62
|
|
|
*/
|
|
63
|
|
|
public function checkPasswordLength($password) {
|
|
64
|
|
|
$minPassLength = $this->config->getMinPasswordLength();
|
|
65
|
|
|
if(strlen($password) < $minPassLength) {
|
|
66
|
|
|
$message = 'Password needs to be at least ' . $minPassLength . ' characters long';
|
|
67
|
|
|
$hint = $this->l->t(
|
|
68
|
|
|
'Password needs to be at least %s characters long', [$minPassLength]
|
|
69
|
|
|
);
|
|
70
|
|
|
throw new HintException($message, $hint);
|
|
71
|
|
|
}
|
|
72
|
|
|
}
|
|
73
|
|
|
|
|
74
|
|
|
/**
|
|
75
|
|
|
* validate if password contains at least one upper and one lower case character
|
|
76
|
|
|
*
|
|
77
|
|
|
* @param string $password
|
|
78
|
|
|
* @throws HintException
|
|
79
|
|
|
*/
|
|
80
|
|
View Code Duplication |
public function checkUpperLowerCase($password) {
|
|
|
|
|
|
|
81
|
|
|
$enforceUpperLowerCase= $this->config->getIsUpperLowerCaseEnforced();
|
|
82
|
|
|
if($enforceUpperLowerCase === true && $this->hasUpperAndLowerCase($password) === false) {
|
|
83
|
|
|
$message = 'Password should contain at least one upper and one lower case character.';
|
|
84
|
|
|
$hint = $this->l->t(
|
|
85
|
|
|
'Password should contain at least one upper and one lower case character.'
|
|
86
|
|
|
);
|
|
87
|
|
|
throw new HintException($message, $hint);
|
|
88
|
|
|
}
|
|
89
|
|
|
}
|
|
90
|
|
|
|
|
91
|
|
|
/**
|
|
92
|
|
|
* validate the given password satisfies numeric character policy
|
|
93
|
|
|
*
|
|
94
|
|
|
* @param string $password
|
|
95
|
|
|
* @throws HintException
|
|
96
|
|
|
*/
|
|
97
|
|
View Code Duplication |
public function checkNumericCharacters($password) {
|
|
|
|
|
|
|
98
|
|
|
$enforceNumericCharacters = $this->config->getIsNumericCharactersEnforced();
|
|
99
|
|
|
if($enforceNumericCharacters === true && $this->hasNumericalCharacters($password) === false) {
|
|
100
|
|
|
$message = 'Password should contain at least one numerical character.';
|
|
101
|
|
|
$hint = $this->l->t(
|
|
102
|
|
|
'Password should contain at least one numerical character.'
|
|
103
|
|
|
);
|
|
104
|
|
|
throw new HintException($message, $hint);
|
|
105
|
|
|
}
|
|
106
|
|
|
}
|
|
107
|
|
|
|
|
108
|
|
|
/**
|
|
109
|
|
|
* check if password contains at least one special character
|
|
110
|
|
|
*
|
|
111
|
|
|
* @param string $password
|
|
112
|
|
|
* @throws HintException
|
|
113
|
|
|
*/
|
|
114
|
|
View Code Duplication |
public function checkSpecialCharacters($password) {
|
|
|
|
|
|
|
115
|
|
|
$enforceSpecialCharacters = $this->config->getIsSpecialCharactersEnforced();
|
|
116
|
|
|
if($enforceSpecialCharacters === true && $this->hasSpecialCharacter($password) === false) {
|
|
117
|
|
|
$message = 'Password should contain at least one special character.';
|
|
118
|
|
|
$hint = $this->l->t(
|
|
119
|
|
|
'Password should contain at least one special character.'
|
|
120
|
|
|
);
|
|
121
|
|
|
throw new HintException($message, $hint);
|
|
122
|
|
|
}
|
|
123
|
|
|
}
|
|
124
|
|
|
|
|
125
|
|
|
public function hasNumericalCharacters($password) {
|
|
126
|
|
|
return preg_match('/\d/', $password) === 1 ? true : false;
|
|
127
|
|
|
}
|
|
128
|
|
|
|
|
129
|
|
|
public function hasUpperAndLowerCase($password) {
|
|
130
|
|
|
$toLower = strtolower($password);
|
|
131
|
|
|
$toUpper = strtoupper($password);
|
|
132
|
|
|
return ($toLower !== $password && $toUpper !== $password) ? true : false;
|
|
133
|
|
|
}
|
|
134
|
|
|
|
|
135
|
|
|
public function hasSpecialCharacter($password) {
|
|
136
|
|
|
return (!ctype_alnum($password));
|
|
137
|
|
|
}
|
|
138
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.