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
;
24 use MediaWiki\MediaWikiServices
;
27 * Functions to check passwords against a policy requirement
30 class PasswordPolicyChecks
{
33 * Check password is longer than minimum, not fatal
34 * @param int $policyVal minimal length
36 * @param string $password
37 * @return Status error if $password is shorter than $policyVal
39 public static function checkMinimalPasswordLength( $policyVal, User
$user, $password ) {
40 $status = Status
::newGood();
41 if ( $policyVal > strlen( $password ) ) {
42 $status->error( 'passwordtooshort', $policyVal );
48 * Check password is longer than minimum, fatal
49 * @param int $policyVal minimal length
51 * @param string $password
52 * @return Status fatal if $password is shorter than $policyVal
54 public static function checkMinimumPasswordLengthToLogin( $policyVal, User
$user, $password ) {
55 $status = Status
::newGood();
56 if ( $policyVal > strlen( $password ) ) {
57 $status->fatal( 'passwordtooshort', $policyVal );
63 * Check password is shorter than maximum, fatal
64 * @param int $policyVal maximum length
66 * @param string $password
67 * @return Status fatal if $password is shorter than $policyVal
69 public static function checkMaximalPasswordLength( $policyVal, User
$user, $password ) {
70 $status = Status
::newGood();
71 if ( $policyVal < strlen( $password ) ) {
72 $status->fatal( 'passwordtoolong', $policyVal );
78 * Check if username and password match
79 * @param bool $policyVal true to force compliance.
81 * @param string $password
82 * @return Status error if username and password match, and policy is true
84 public static function checkPasswordCannotMatchUsername( $policyVal, User
$user, $password ) {
85 $status = Status
::newGood();
86 $username = $user->getName();
87 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
89 $policyVal && $contLang->lc( $password ) === $contLang->lc( $username )
91 $status->error( 'password-name-match' );
97 * Check if username and password are on a blacklist
98 * @param bool $policyVal true to force compliance.
100 * @param string $password
101 * @return Status error if username and password match, and policy is true
103 public static function checkPasswordCannotMatchBlacklist( $policyVal, User
$user, $password ) {
104 static $blockedLogins = [
105 'Useruser' => 'Passpass', 'Useruser1' => 'Passpass1', # r75589
106 'Apitestsysop' => 'testpass', 'Apitestuser' => 'testpass' # r75605
109 $status = Status
::newGood();
110 $username = $user->getName();
112 if ( isset( $blockedLogins[$username] ) && $password == $blockedLogins[$username] ) {
113 $status->error( 'password-login-forbidden' );
116 // Example from ApiChangeAuthenticationRequest
117 if ( $password === 'ExamplePassword' ) {
118 $status->error( 'password-login-forbidden' );
125 * Ensure that password isn't in top X most popular passwords
127 * @param int $policyVal Cut off to use. Will automatically shrink to the max
128 * supported for error messages if set to more than max number of passwords on file,
129 * so you can use the PHP_INT_MAX constant here safely.
131 * @param string $password
135 public static function checkPopularPasswordBlacklist( $policyVal, User
$user, $password ) {
136 global $wgPopularPasswordFile, $wgSitename;
137 $status = Status
::newGood();
138 if ( $policyVal > 0 ) {
139 $langEn = Language
::factory( 'en' );
140 $passwordKey = $langEn->lc( trim( $password ) );
142 // People often use the name of the current site, which won't be
143 // in the common password file. Also check '' for people who use
145 $sitename = $langEn->lc( trim( $wgSitename ) );
146 $hardcodedCommonPasswords = [ '', 'wiki', 'mediawiki', $sitename ];
147 if ( in_array( $passwordKey, $hardcodedCommonPasswords ) ) {
148 $status->error( 'passwordtoopopular' );
152 // This could throw an exception, but there's not a good way
153 // of failing gracefully, if say the file is missing, so just
154 // let the exception fall through.
155 // Format of cdb file is mapping password => popularity rank.
156 // See maintenance/createCommonPasswordCdb.php
157 $db = CdbReader
::open( $wgPopularPasswordFile );
159 $res = $db->get( $passwordKey );
160 if ( $res && (int)$res <= $policyVal ) {
161 // Note: If you want to find the true number of common
162 // passwords stored (for reporting the error), you have to take
163 // the max of the policyVal and $db->get( '_TOTALENTRIES' ).
164 $status->error( 'passwordtoopopular' );