3 * Password policy checks
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Functions to check passwords against a policy requirement
27 class PasswordPolicyChecks
{
30 * Check password is longer than minimum, not fatal
31 * @param int $policyVal minimal length
33 * @param string $password
34 * @return Status error if $password is shorter than $policyVal
36 public static function checkMinimalPasswordLength( $policyVal, User
$user, $password ) {
37 $status = Status
::newGood();
38 if ( $policyVal > strlen( $password ) ) {
39 $status->error( 'passwordtooshort', $policyVal );
45 * Check password is longer than minimum, fatal
46 * @param int $policyVal minimal length
48 * @param string $password
49 * @return Status fatal if $password is shorter than $policyVal
51 public static function checkMinimumPasswordLengthToLogin( $policyVal, User
$user, $password ) {
52 $status = Status
::newGood();
53 if ( $policyVal > strlen( $password ) ) {
54 $status->fatal( 'passwordtooshort', $policyVal );
60 * Check password is shorter than maximum, fatal
61 * @param int $policyVal maximum length
63 * @param string $password
64 * @return Status fatal if $password is shorter than $policyVal
66 public static function checkMaximalPasswordLength( $policyVal, User
$user, $password ) {
67 $status = Status
::newGood();
68 if ( $policyVal < strlen( $password ) ) {
69 $status->fatal( 'passwordtoolong', $policyVal );
75 * Check if username and password match
76 * @param bool $policyVal true to force compliance.
78 * @param string $password
79 * @return Status error if username and password match, and policy is true
81 public static function checkPasswordCannotMatchUsername( $policyVal, User
$user, $password ) {
83 $status = Status
::newGood();
84 $username = $user->getName();
85 if ( $policyVal && $wgContLang->lc( $password ) === $wgContLang->lc( $username ) ) {
86 $status->error( 'password-name-match' );
92 * Check if username and password are on a blacklist
93 * @param bool $policyVal true to force compliance.
95 * @param string $password
96 * @return Status error if username and password match, and policy is true
98 public static function checkPasswordCannotMatchBlacklist( $policyVal, User
$user, $password ) {
99 static $blockedLogins = array(
100 'Useruser' => 'Passpass', 'Useruser1' => 'Passpass1', # r75589
101 'Apitestsysop' => 'testpass', 'Apitestuser' => 'testpass' # r75605
104 $status = Status
::newGood();
105 $username = $user->getName();
107 && isset( $blockedLogins[$username] )
108 && $password == $blockedLogins[$username]
110 $status->error( 'password-login-forbidden' );