MediaWiki master
PasswordPolicyChecks.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Password;
24
27use Wikimedia\CommonPasswords\CommonPasswords;
28
41
49 public static function checkMinimalPasswordLength( $policyVal, UserIdentity $user, $password ) {
50 $status = Status::newGood();
51 if ( $policyVal > strlen( $password ) ) {
52 $status->error( 'passwordtooshort', $policyVal );
53 }
54 return $status;
55 }
56
66 public static function checkMinimumPasswordLengthToLogin( $policyVal, UserIdentity $user, $password ) {
67 $status = Status::newGood();
68 if ( $policyVal > strlen( $password ) ) {
69 $status->fatal( 'passwordtooshort', $policyVal );
70 }
71 return $status;
72 }
73
82 public static function checkMaximalPasswordLength( $policyVal, UserIdentity $user, $password ) {
83 $status = Status::newGood();
84 if ( $policyVal < strlen( $password ) ) {
85 $status->fatal( 'passwordtoolong', $policyVal );
86 }
87 return $status;
88 }
89
98 $policyVal,
99 UserIdentity $user,
100 $password
101 ) {
102 $status = Status::newGood();
103 $username = $user->getName();
104 if ( $policyVal && stripos( $username, $password ) !== false ) {
105 $status->error( 'password-substring-username-match' );
106 }
107 return $status;
108 }
109
117 public static function checkPasswordCannotMatchDefaults( $policyVal, UserIdentity $user, $password ) {
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( $policyVal, UserIdentity $user, $password ) {
160 $status = Status::newGood();
161 if ( $policyVal && CommonPasswords::isCommon( $password ) ) {
162 $status->error( 'passwordincommonlist' );
163 }
164
165 return $status;
166 }
167
168}
Functions to check passwords against a policy requirement.
static checkMaximalPasswordLength( $policyVal, UserIdentity $user, $password)
Check password is shorter than the maximum, fatal.
static checkPasswordCannotBeSubstringInUsername( $policyVal, UserIdentity $user, $password)
Check if a password is a (case-insensitive) substring within the username.
static checkMinimumPasswordLengthToLogin( $policyVal, UserIdentity $user, $password)
Check password is longer than the minimum, fatal.
static checkMinimalPasswordLength( $policyVal, UserIdentity $user, $password)
Check password is longer than the minimum, not fatal.
static checkPasswordNotInCommonList( $policyVal, UserIdentity $user, $password)
Ensure the password isn't in the list of common passwords by the wikimedia/common-passwords library,...
static checkPasswordCannotMatchDefaults( $policyVal, UserIdentity $user, $password)
Check if username and password are on a list of past MediaWiki default passwords.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Interface for objects representing user identity.