| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Password; |
| 24 |
|
| 25 |
use DomainException; |
| 26 |
use InvalidArgumentException; |
| 27 |
use MediaWiki\HookContainer\HookRunner; |
| 28 |
use MediaWiki\MediaWikiServices; |
| 29 |
use MediaWiki\Status\Status; |
| 30 |
use MediaWiki\User\UserIdentity; |
| 31 |
use StatusValue; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class UserPasswordPolicy { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private $policies; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private $policyCheckFunctions; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function __construct( array $policies, array $checks ) { |
| 59 |
if ( !isset( $policies['default'] ) ) { |
| 60 |
throw new InvalidArgumentException( |
| 61 |
'Must include a \'default\' password policy' |
| 62 |
); |
| 63 |
} |
| 64 |
$this->policies = $policies; |
| 65 |
|
| 66 |
foreach ( $checks as $statement => $check ) { |
| 67 |
if ( !is_callable( $check ) ) { |
| 68 |
throw new InvalidArgumentException( |
| 69 |
"Policy check functions must be callable. '$statement' isn't callable." |
| 70 |
); |
| 71 |
} |
| 72 |
$this->policyCheckFunctions[$statement] = $check; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function checkUserPassword( UserIdentity $user, $password ) { |
| 87 |
$effectivePolicy = $this->getPoliciesForUser( $user ); |
| 88 |
return $this->checkPolicies( |
| 89 |
$user, |
| 90 |
$password, |
| 91 |
$effectivePolicy, |
| 92 |
$this->policyCheckFunctions |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function checkUserPasswordForGroups( UserIdentity $user, $password, array $groups ) { |
| 110 |
$effectivePolicy = self::getPoliciesForGroups( |
| 111 |
$this->policies, |
| 112 |
$groups, |
| 113 |
$this->policies['default'] |
| 114 |
); |
| 115 |
return $this->checkPolicies( |
| 116 |
$user, |
| 117 |
$password, |
| 118 |
$effectivePolicy, |
| 119 |
$this->policyCheckFunctions |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
private function checkPolicies( UserIdentity $user, $password, $policies, $policyCheckFunctions ) { |
| 131 |
$status = Status::newGood( [] ); |
| 132 |
$forceChange = false; |
| 133 |
$suggestChangeOnLogin = false; |
| 134 |
$legacyUser = MediaWikiServices::getInstance() |
| 135 |
->getUserFactory() |
| 136 |
->newFromUserIdentity( $user ); |
| 137 |
foreach ( $policies as $policy => $settings ) { |
| 138 |
if ( !isset( $policyCheckFunctions[$policy] ) ) { |
| 139 |
throw new DomainException( "Invalid password policy config. No check defined for '$policy'." ); |
| 140 |
} |
| 141 |
if ( !is_array( $settings ) ) { |
| 142 |
|
| 143 |
$settings = [ 'value' => $settings ]; |
| 144 |
} |
| 145 |
if ( !array_key_exists( 'value', $settings ) ) { |
| 146 |
throw new DomainException( "Invalid password policy config. No value defined for '$policy'." ); |
| 147 |
} |
| 148 |
$value = $settings['value']; |
| 149 |
|
| 150 |
$policyStatus = call_user_func( |
| 151 |
$policyCheckFunctions[$policy], |
| 152 |
$value, |
| 153 |
$legacyUser, |
| 154 |
$password |
| 155 |
); |
| 156 |
|
| 157 |
if ( !$policyStatus->isGood() ) { |
| 158 |
if ( !empty( $settings['forceChange'] ) ) { |
| 159 |
$forceChange = true; |
| 160 |
} |
| 161 |
|
| 162 |
if ( !empty( $settings['suggestChangeOnLogin'] ) ) { |
| 163 |
$suggestChangeOnLogin = true; |
| 164 |
} |
| 165 |
} |
| 166 |
$status->merge( $policyStatus ); |
| 167 |
} |
| 168 |
|
| 169 |
if ( $status->isOK() ) { |
| 170 |
if ( $forceChange ) { |
| 171 |
$status->value['forceChange'] = true; |
| 172 |
} elseif ( $suggestChangeOnLogin ) { |
| 173 |
$status->value['suggestChangeOnLogin'] = true; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return $status; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public function getPoliciesForUser( UserIdentity $user ) { |
| 187 |
$services = MediaWikiServices::getInstance(); |
| 188 |
$effectivePolicy = self::getPoliciesForGroups( |
| 189 |
$this->policies, |
| 190 |
$services->getUserGroupManager() |
| 191 |
->getUserEffectiveGroups( $user ), |
| 192 |
$this->policies['default'] |
| 193 |
); |
| 194 |
|
| 195 |
$legacyUser = $services->getUserFactory() |
| 196 |
->newFromUserIdentity( $user ); |
| 197 |
( new HookRunner( $services->getHookContainer() ) )->onPasswordPoliciesForUser( $legacyUser, $effectivePolicy ); |
| 198 |
|
| 199 |
return $effectivePolicy; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public static function getPoliciesForGroups( array $policies, array $userGroups, |
| 211 |
array $defaultPolicy |
| 212 |
) { |
| 213 |
$effectivePolicy = $defaultPolicy; |
| 214 |
foreach ( $policies as $group => $policy ) { |
| 215 |
if ( in_array( $group, $userGroups ) ) { |
| 216 |
$effectivePolicy = self::maxOfPolicies( |
| 217 |
$effectivePolicy, |
| 218 |
$policy |
| 219 |
); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return $effectivePolicy; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public static function maxOfPolicies( array $p1, array $p2 ) { |
| 235 |
$ret = []; |
| 236 |
$keys = array_merge( array_keys( $p1 ), array_keys( $p2 ) ); |
| 237 |
foreach ( $keys as $key ) { |
| 238 |
if ( !isset( $p1[$key] ) ) { |
| 239 |
$ret[$key] = $p2[$key]; |
| 240 |
} elseif ( !isset( $p2[$key] ) ) { |
| 241 |
$ret[$key] = $p1[$key]; |
| 242 |
} elseif ( !is_array( $p1[$key] ) && !is_array( $p2[$key] ) ) { |
| 243 |
$ret[$key] = max( $p1[$key], $p2[$key] ); |
| 244 |
} else { |
| 245 |
if ( !is_array( $p1[$key] ) ) { |
| 246 |
$p1[$key] = [ 'value' => $p1[$key] ]; |
| 247 |
} elseif ( !is_array( $p2[$key] ) ) { |
| 248 |
$p2[$key] = [ 'value' => $p2[$key] ]; |
| 249 |
} |
| 250 |
$ret[$key] = self::maxOfPolicies( $p1[$key], $p2[$key] ); |
| 251 |
} |
| 252 |
} |
| 253 |
return $ret; |
| 254 |
} |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
class_alias( UserPasswordPolicy::class, 'UserPasswordPolicy' ); |