MediaWiki master
PasswordPolicyChecks.php
Go to the documentation of this file.
1<?php
9declare( strict_types = 1 );
10
11namespace MediaWiki\Password;
12
15use Wikimedia\CommonPasswords\CommonPasswords;
16
29
37 public static function checkMinimalPasswordLength( int $policyVal, UserIdentity $user, string $password ): Status {
38 $status = Status::newGood();
39 if ( $policyVal > strlen( $password ) ) {
40 $status->error( 'passwordtooshort', $policyVal );
41 }
42 return $status;
43 }
44
54 public static function checkMinimumPasswordLengthToLogin(
55 int $policyVal,
56 UserIdentity $user,
57 string $password
58 ): Status {
59 $status = Status::newGood();
60 if ( $policyVal > strlen( $password ) ) {
61 $status->fatal( 'passwordtooshort', $policyVal );
62 }
63 return $status;
64 }
65
74 public static function checkMaximalPasswordLength(
75 int $policyVal,
76 UserIdentity $user,
77 string $password
78 ): Status {
79 $status = Status::newGood();
80 if ( $policyVal < strlen( $password ) ) {
81 $status->fatal( 'passwordtoolong', $policyVal );
82 }
83 return $status;
84 }
85
94 bool $policyVal,
95 UserIdentity $user,
96 string $password
97 ): Status {
98 $status = Status::newGood();
99 $username = $user->getName();
100 if ( $policyVal && stripos( $username, $password ) !== false ) {
101 $status->error( 'password-substring-username-match' );
102 }
103 return $status;
104 }
105
113 public static function checkPasswordCannotMatchDefaults(
114 bool $policyVal,
115 UserIdentity $user,
116 string $password
117 ): Status {
118 static $blockedLogins = [
119 // r75589
120 'Useruser' => 'Passpass',
121 'Useruser1' => 'Passpass1',
122 // r75605
123 'Apitestsysop' => 'testpass',
124 'Apitestuser' => 'testpass',
125 ];
126
127 $status = Status::newGood();
128 $username = $user->getName();
129 if ( $policyVal ) {
130 if (
131 isset( $blockedLogins[$username] ) &&
132 hash_equals( $blockedLogins[$username], $password )
133 ) {
134 $status->error( 'password-login-forbidden' );
135 }
136
137 // Example from ApiChangeAuthenticationRequest
138 if ( hash_equals( 'ExamplePassword', $password ) ) {
139 $status->error( 'password-login-forbidden' );
140 }
141 }
142 return $status;
143 }
144
159 public static function checkPasswordNotInCommonList(
160 bool $policyVal,
161 UserIdentity $user,
162 string $password
163 ): Status {
164 $status = Status::newGood();
165 if ( $policyVal && CommonPasswords::isCommon( $password ) ) {
166 $status->error( 'passwordincommonlist' );
167 }
168
169 return $status;
170 }
171
172}
Functions to check passwords against a policy requirement.
static checkPasswordCannotMatchDefaults(bool $policyVal, UserIdentity $user, string $password)
Check if username and password are on a list of past MediaWiki default passwords.
static checkPasswordNotInCommonList(bool $policyVal, UserIdentity $user, string $password)
Ensure the password isn't in the list of common passwords by the wikimedia/common-passwords library,...
static checkMinimumPasswordLengthToLogin(int $policyVal, UserIdentity $user, string $password)
Check password is longer than the minimum, fatal.
static checkMaximalPasswordLength(int $policyVal, UserIdentity $user, string $password)
Check password is shorter than the maximum, fatal.
static checkMinimalPasswordLength(int $policyVal, UserIdentity $user, string $password)
Check password is longer than the minimum, not fatal.
static checkPasswordCannotBeSubstringInUsername(bool $policyVal, UserIdentity $user, string $password)
Check if a password is a (case-insensitive) substring within the username.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
fatal( $message,... $parameters)
Add an error and set OK to false, indicating that the operation as a whole was fatal.
error( $message,... $parameters)
Add an error, do not set fatal flag This can be used for non-fatal errors.
Interface for objects representing user identity.