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
23 use \Cdb\Reader
as CdbReader
;
26 * Functions to check passwords against a policy requirement
29 class PasswordPolicyChecks
{
32 * Check password is longer than minimum, not fatal
33 * @param int $policyVal minimal length
35 * @param string $password
36 * @return Status error if $password is shorter than $policyVal
38 public static function checkMinimalPasswordLength( $policyVal, User
$user, $password ) {
39 $status = Status
::newGood();
40 if ( $policyVal > strlen( $password ) ) {
41 $status->error( 'passwordtooshort', $policyVal );
47 * Check password is longer than minimum, fatal
48 * @param int $policyVal minimal length
50 * @param string $password
51 * @return Status fatal if $password is shorter than $policyVal
53 public static function checkMinimumPasswordLengthToLogin( $policyVal, User
$user, $password ) {
54 $status = Status
::newGood();
55 if ( $policyVal > strlen( $password ) ) {
56 $status->fatal( 'passwordtooshort', $policyVal );
62 * Check password is shorter than maximum, fatal
63 * @param int $policyVal maximum length
65 * @param string $password
66 * @return Status fatal if $password is shorter than $policyVal
68 public static function checkMaximalPasswordLength( $policyVal, User
$user, $password ) {
69 $status = Status
::newGood();
70 if ( $policyVal < strlen( $password ) ) {
71 $status->fatal( 'passwordtoolong', $policyVal );
77 * Check if username and password match
78 * @param bool $policyVal true to force compliance.
80 * @param string $password
81 * @return Status error if username and password match, and policy is true
83 public static function checkPasswordCannotMatchUsername( $policyVal, User
$user, $password ) {
85 $status = Status
::newGood();
86 $username = $user->getName();
87 if ( $policyVal && $wgContLang->lc( $password ) === $wgContLang->lc( $username ) ) {
88 $status->error( 'password-name-match' );
94 * Check if username and password are on a blacklist
95 * @param bool $policyVal true to force compliance.
97 * @param string $password
98 * @return Status error if username and password match, and policy is true
100 public static function checkPasswordCannotMatchBlacklist( $policyVal, User
$user, $password ) {
101 static $blockedLogins = array(
102 'Useruser' => 'Passpass', 'Useruser1' => 'Passpass1', # r75589
103 'Apitestsysop' => 'testpass', 'Apitestuser' => 'testpass' # r75605
106 $status = Status
::newGood();
107 $username = $user->getName();
109 && isset( $blockedLogins[$username] )
110 && $password == $blockedLogins[$username]
112 $status->error( 'password-login-forbidden' );
118 * Ensure that password isn't in top X most popular passwords
120 * @param $policyVal int Cut off to use. Will automatically shrink to the max
121 * supported for error messages if set to more than max number of passwords on file,
122 * so you can use the PHP_INT_MAX constant here safely.
124 * @param $password String
128 public static function checkPopularPasswordBlacklist( $policyVal, User
$user, $password ) {
129 global $wgPopularPasswordFile, $wgSitename;
130 $status = Status
::newGood();
131 if ( $policyVal > 0 ) {
132 $langEn = Language
::factory( 'en' );
133 $passwordKey = $langEn->lc( trim( $password ) );
135 // People often use the name of the current site, which won't be
136 // in the common password file. Also check '' for people who use
138 $sitename = $langEn->lc( trim( $wgSitename ) );
139 $hardcodedCommonPasswords = array( '', 'wiki', 'mediawiki', $sitename );
140 if ( in_array( $passwordKey, $hardcodedCommonPasswords ) ) {
141 $status->error( 'passwordtoopopular' );
145 // This could throw an exception, but there's not a good way
146 // of failing gracefully, if say the file is missing, so just
147 // let the exception fall through.
148 // Format of cdb file is mapping password => popularity rank.
149 // See maintenance/createCommonPasswordCdb.php
150 $db = CdbReader
::open( $wgPopularPasswordFile );
152 $res = $db->get( $passwordKey );
153 if ( $res && (int)$res <= $policyVal ) {
154 // Note: If you want to find the true number of common
155 // passwords stored (for reporting the error), you have to take
156 // the max of the policyVal and $db->get( '_TOTALENTRIES' ).
157 $status->error( 'passwordtoopopular' );