Improve documentation for the MinimumPasswordLengthToLogin policy
[lhc/web/wiklou.git] / includes / password / PasswordPolicyChecks.php
1 <?php
2 /**
3 * Password policy checks
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 use Cdb\Reader as CdbReader;
24
25 /**
26 * Functions to check passwords against a policy requirement
27 * @since 1.26
28 */
29 class PasswordPolicyChecks {
30
31 /**
32 * Check password is longer than minimum, not fatal
33 * @param int $policyVal minimal length
34 * @param User $user
35 * @param string $password
36 * @return Status error if $password is shorter than $policyVal
37 */
38 public static function checkMinimalPasswordLength( $policyVal, User $user, $password ) {
39 $status = Status::newGood();
40 if ( $policyVal > strlen( $password ) ) {
41 $status->error( 'passwordtooshort', $policyVal );
42 }
43 return $status;
44 }
45
46 /**
47 * Check password is longer than minimum, fatal.
48 * Intended for locking out users with passwords too short to trust, requiring them
49 * to recover their account by some other means.
50 * @param int $policyVal minimal length
51 * @param User $user
52 * @param string $password
53 * @return Status fatal if $password is shorter than $policyVal
54 */
55 public static function checkMinimumPasswordLengthToLogin( $policyVal, User $user, $password ) {
56 $status = Status::newGood();
57 if ( $policyVal > strlen( $password ) ) {
58 $status->fatal( 'passwordtooshort', $policyVal );
59 }
60 return $status;
61 }
62
63 /**
64 * Check password is shorter than maximum, fatal
65 * @param int $policyVal maximum length
66 * @param User $user
67 * @param string $password
68 * @return Status fatal if $password is shorter than $policyVal
69 */
70 public static function checkMaximalPasswordLength( $policyVal, User $user, $password ) {
71 $status = Status::newGood();
72 if ( $policyVal < strlen( $password ) ) {
73 $status->fatal( 'passwordtoolong', $policyVal );
74 }
75 return $status;
76 }
77
78 /**
79 * Check if username and password match
80 * @param bool $policyVal true to force compliance.
81 * @param User $user
82 * @param string $password
83 * @return Status error if username and password match, and policy is true
84 */
85 public static function checkPasswordCannotMatchUsername( $policyVal, User $user, $password ) {
86 global $wgContLang;
87 $status = Status::newGood();
88 $username = $user->getName();
89 if ( $policyVal && $wgContLang->lc( $password ) === $wgContLang->lc( $username ) ) {
90 $status->error( 'password-name-match' );
91 }
92 return $status;
93 }
94
95 /**
96 * Check if username and password are on a blacklist
97 * @param bool $policyVal true to force compliance.
98 * @param User $user
99 * @param string $password
100 * @return Status error if username and password match, and policy is true
101 */
102 public static function checkPasswordCannotMatchBlacklist( $policyVal, User $user, $password ) {
103 static $blockedLogins = [
104 'Useruser' => 'Passpass', 'Useruser1' => 'Passpass1', # r75589
105 'Apitestsysop' => 'testpass', 'Apitestuser' => 'testpass' # r75605
106 ];
107
108 $status = Status::newGood();
109 $username = $user->getName();
110 if ( $policyVal ) {
111 if ( isset( $blockedLogins[$username] ) && $password == $blockedLogins[$username] ) {
112 $status->error( 'password-login-forbidden' );
113 }
114
115 // Example from ApiChangeAuthenticationRequest
116 if ( $password === 'ExamplePassword' ) {
117 $status->error( 'password-login-forbidden' );
118 }
119 }
120 return $status;
121 }
122
123 /**
124 * Ensure that password isn't in top X most popular passwords
125 *
126 * @param int $policyVal Cut off to use. Will automatically shrink to the max
127 * supported for error messages if set to more than max number of passwords on file,
128 * so you can use the PHP_INT_MAX constant here safely.
129 * @param User $user
130 * @param string $password
131 * @since 1.27
132 * @return Status
133 */
134 public static function checkPopularPasswordBlacklist( $policyVal, User $user, $password ) {
135 global $wgPopularPasswordFile, $wgSitename;
136 $status = Status::newGood();
137 if ( $policyVal > 0 ) {
138 $langEn = Language::factory( 'en' );
139 $passwordKey = $langEn->lc( trim( $password ) );
140
141 // People often use the name of the current site, which won't be
142 // in the common password file. Also check '' for people who use
143 // just whitespace.
144 $sitename = $langEn->lc( trim( $wgSitename ) );
145 $hardcodedCommonPasswords = [ '', 'wiki', 'mediawiki', $sitename ];
146 if ( in_array( $passwordKey, $hardcodedCommonPasswords ) ) {
147 $status->error( 'passwordtoopopular' );
148 return $status;
149 }
150
151 // This could throw an exception, but there's not a good way
152 // of failing gracefully, if say the file is missing, so just
153 // let the exception fall through.
154 // Format of cdb file is mapping password => popularity rank.
155 // See maintenance/createCommonPasswordCdb.php
156 $db = CdbReader::open( $wgPopularPasswordFile );
157
158 $res = $db->get( $passwordKey );
159 if ( $res && (int)$res <= $policyVal ) {
160 // Note: If you want to find the true number of common
161 // passwords stored (for reporting the error), you have to take
162 // the max of the policyVal and $db->get( '_TOTALENTRIES' ).
163 $status->error( 'passwordtoopopular' );
164 }
165 }
166 return $status;
167 }
168
169 }