d7aee5bd5c8c8f36f7831598de89449e79bcef86
[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 * @param int $policyVal minimal length
49 * @param User $user
50 * @param string $password
51 * @return Status fatal if $password is shorter than $policyVal
52 */
53 public static function checkMinimumPasswordLengthToLogin( $policyVal, User $user, $password ) {
54 $status = Status::newGood();
55 if ( $policyVal > strlen( $password ) ) {
56 $status->fatal( 'passwordtooshort', $policyVal );
57 }
58 return $status;
59 }
60
61 /**
62 * Check password is shorter than maximum, fatal
63 * @param int $policyVal maximum length
64 * @param User $user
65 * @param string $password
66 * @return Status fatal if $password is shorter than $policyVal
67 */
68 public static function checkMaximalPasswordLength( $policyVal, User $user, $password ) {
69 $status = Status::newGood();
70 if ( $policyVal < strlen( $password ) ) {
71 $status->fatal( 'passwordtoolong', $policyVal );
72 }
73 return $status;
74 }
75
76 /**
77 * Check if username and password match
78 * @param bool $policyVal true to force compliance.
79 * @param User $user
80 * @param string $password
81 * @return Status error if username and password match, and policy is true
82 */
83 public static function checkPasswordCannotMatchUsername( $policyVal, User $user, $password ) {
84 global $wgContLang;
85 $status = Status::newGood();
86 $username = $user->getName();
87 if ( $policyVal && $wgContLang->lc( $password ) === $wgContLang->lc( $username ) ) {
88 $status->error( 'password-name-match' );
89 }
90 return $status;
91 }
92
93 /**
94 * Check if username and password are on a blacklist
95 * @param bool $policyVal true to force compliance.
96 * @param User $user
97 * @param string $password
98 * @return Status error if username and password match, and policy is true
99 */
100 public static function checkPasswordCannotMatchBlacklist( $policyVal, User $user, $password ) {
101 static $blockedLogins = [
102 'Useruser' => 'Passpass', 'Useruser1' => 'Passpass1', # r75589
103 'Apitestsysop' => 'testpass', 'Apitestuser' => 'testpass' # r75605
104 ];
105
106 $status = Status::newGood();
107 $username = $user->getName();
108 if ( $policyVal
109 && isset( $blockedLogins[$username] )
110 && $password == $blockedLogins[$username]
111 ) {
112 $status->error( 'password-login-forbidden' );
113 }
114 return $status;
115 }
116
117 /**
118 * Ensure that password isn't in top X most popular passwords
119 *
120 * @param int $policyVal 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.
123 * @param User $user
124 * @param string $password
125 * @since 1.27
126 * @return Status
127 */
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 ) );
134
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
137 // just whitespace.
138 $sitename = $langEn->lc( trim( $wgSitename ) );
139 $hardcodedCommonPasswords = [ '', 'wiki', 'mediawiki', $sitename ];
140 if ( in_array( $passwordKey, $hardcodedCommonPasswords ) ) {
141 $status->error( 'passwordtoopopular' );
142 return $status;
143 }
144
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 );
151
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' );
158 }
159 }
160 return $status;
161 }
162
163 }