Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.41% |
104 / 109 |
|
77.27% |
17 / 22 |
CRAP | |
0.00% |
0 / 1 |
| TempUserCreator | |
95.41% |
104 / 109 |
|
77.27% |
17 / 22 |
39 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
6 | |||
| isEnabled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isKnown | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isAutoCreateAction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| shouldAutoCreate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isTempName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isReservedName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPlaceholderName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMatchPattern | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMatchPatterns | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMatchCondition | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getExpireAfterDays | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getNotifyBeforeExpirationDays | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| attemptAutoCreate | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
4 | |||
| acquireName | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
4 | |||
| getSerialProvider | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| createSerialProvider | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
3.00 | |||
| getSerialMapping | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| createSerialMapping | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
3.00 | |||
| acquireAndStashName | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| getStashedName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\User\TempUser; |
| 4 | |
| 5 | use MediaWiki\Auth\AuthManager; |
| 6 | use MediaWiki\Auth\Throttler; |
| 7 | use MediaWiki\Permissions\Authority; |
| 8 | use MediaWiki\Registration\ExtensionRegistry; |
| 9 | use MediaWiki\Request\WebRequest; |
| 10 | use MediaWiki\Session\Session; |
| 11 | use MediaWiki\User\CentralId\CentralIdLookup; |
| 12 | use MediaWiki\User\UserFactory; |
| 13 | use MediaWiki\User\UserRigorOptions; |
| 14 | use MediaWiki\Utils\MWTimestamp; |
| 15 | use UnexpectedValueException; |
| 16 | use Wikimedia\IPUtils; |
| 17 | use Wikimedia\ObjectFactory\ObjectFactory; |
| 18 | use Wikimedia\Rdbms\IExpression; |
| 19 | use Wikimedia\Rdbms\IReadableDatabase; |
| 20 | |
| 21 | /** |
| 22 | * Service for temporary user creation. For convenience this also proxies the |
| 23 | * TempUserConfig methods. |
| 24 | * |
| 25 | * This is separate from TempUserConfig to avoid dependency loops. Special pages |
| 26 | * and actions are free to use this class, but services should take it as a |
| 27 | * constructor parameter only if necessary. |
| 28 | * |
| 29 | * @since 1.39 |
| 30 | */ |
| 31 | class TempUserCreator implements TempUserConfig { |
| 32 | private readonly array $serialProviderConfig; |
| 33 | private readonly array $serialMappingConfig; |
| 34 | private ?SerialProvider $serialProvider; |
| 35 | private ?SerialMapping $serialMapping; |
| 36 | |
| 37 | /** ObjectFactory specs for the core serial providers */ |
| 38 | private const SERIAL_PROVIDERS = [ |
| 39 | 'local' => [ |
| 40 | 'class' => LocalSerialProvider::class, |
| 41 | 'services' => [ 'DBLoadBalancerFactory' ], |
| 42 | ] |
| 43 | ]; |
| 44 | |
| 45 | /** ObjectFactory specs for the core serial maps */ |
| 46 | private const SERIAL_MAPPINGS = [ |
| 47 | 'readable-numeric' => [ |
| 48 | 'class' => ReadableNumericSerialMapping::class, |
| 49 | ], |
| 50 | 'plain-numeric' => [ |
| 51 | 'class' => PlainNumericSerialMapping::class, |
| 52 | ], |
| 53 | 'localized-numeric' => [ |
| 54 | 'class' => LocalizedNumericSerialMapping::class, |
| 55 | 'services' => [ 'LanguageFactory' ], |
| 56 | ], |
| 57 | 'filtered-radix' => [ |
| 58 | 'class' => FilteredRadixSerialMapping::class, |
| 59 | ], |
| 60 | 'scramble' => [ |
| 61 | 'class' => ScrambleMapping::class, |
| 62 | ] |
| 63 | ]; |
| 64 | |
| 65 | public function __construct( |
| 66 | private readonly RealTempUserConfig $config, |
| 67 | private readonly ObjectFactory $objectFactory, |
| 68 | private readonly UserFactory $userFactory, |
| 69 | private readonly AuthManager $authManager, |
| 70 | private readonly CentralIdLookup $centralIdLookup, |
| 71 | private readonly Throttler $tempAccountCreationThrottler, |
| 72 | private readonly Throttler $tempAccountNameAcquisitionThrottler, |
| 73 | ) { |
| 74 | $this->serialProviderConfig = $config->getSerialProviderConfig(); |
| 75 | $this->serialMappingConfig = $config->getSerialMappingConfig(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Acquire a serial number, create the corresponding user and log in. |
| 80 | * |
| 81 | * @param string|null $name Previously acquired name |
| 82 | * @param WebRequest $request Request details, used for throttling |
| 83 | * @param string[] $tags Change tags to apply to the autocreation log. |
| 84 | * @return CreateStatus |
| 85 | */ |
| 86 | public function create( ?string $name, WebRequest $request, array $tags = [] ): CreateStatus { |
| 87 | $status = new CreateStatus; |
| 88 | |
| 89 | // Check name acquisition rate limits first. |
| 90 | if ( $name === null ) { |
| 91 | $name = $this->acquireName( $request->getIP() ); |
| 92 | if ( $name === null ) { |
| 93 | // If the $name remains null after calling ::acquireName, then |
| 94 | // we cannot generate a username and therefore cannot create a user. |
| 95 | // This could also happen if acquiring the name was rate limited |
| 96 | // In this case return a CreateStatus indicating no user was created. |
| 97 | // TODO: Create a custom message to support workflows related to T357802 |
| 98 | return CreateStatus::newFatal( 'temp-user-unable-to-acquire' ); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Check temp account creation rate limits. |
| 103 | // For IPv6 IPs, the throttle should apply to the /64 range |
| 104 | $ipToThrottle = $request->getIP(); |
| 105 | if ( IPUtils::isIPv6( $ipToThrottle ) ) { |
| 106 | // Normalize to the beginning of the range |
| 107 | [ $ipHex ] = IPUtils::parseRange( $ipToThrottle . '/64' ); |
| 108 | $ipToThrottle = IPUtils::formatHex( $ipHex ) . '/64'; |
| 109 | } |
| 110 | $result = $this->tempAccountCreationThrottler->increase( |
| 111 | null, $ipToThrottle, 'TempUserCreator' ); |
| 112 | if ( $result ) { |
| 113 | $message = wfMessage( 'acct_creation_throttle_hit-temp' )->params( $result['count'] ) |
| 114 | ->durationParams( $result['wait'] ); |
| 115 | $status->fatal( $message ); |
| 116 | return $status; |
| 117 | } |
| 118 | |
| 119 | $createStatus = $this->attemptAutoCreate( $name, false, $tags ); |
| 120 | |
| 121 | if ( $createStatus->isOK() ) { |
| 122 | // The temporary account name didn't already exist, so now attempt to login |
| 123 | // using ::attemptAutoCreate as there isn't a public method to just login. |
| 124 | $this->attemptAutoCreate( $name, true ); |
| 125 | } |
| 126 | return $createStatus; |
| 127 | } |
| 128 | |
| 129 | /** @inheritDoc */ |
| 130 | public function isEnabled() { |
| 131 | return $this->config->isEnabled(); |
| 132 | } |
| 133 | |
| 134 | /** @inheritDoc */ |
| 135 | public function isKnown() { |
| 136 | return $this->config->isKnown(); |
| 137 | } |
| 138 | |
| 139 | /** @inheritDoc */ |
| 140 | public function isAutoCreateAction( string $action ) { |
| 141 | return $this->config->isAutoCreateAction( $action ); |
| 142 | } |
| 143 | |
| 144 | /** @inheritDoc */ |
| 145 | public function shouldAutoCreate( Authority $authority, string $action ) { |
| 146 | return $this->config->shouldAutoCreate( $authority, $action ); |
| 147 | } |
| 148 | |
| 149 | /** @inheritDoc */ |
| 150 | public function isTempName( string $name ) { |
| 151 | return $this->config->isTempName( $name ); |
| 152 | } |
| 153 | |
| 154 | /** @inheritDoc */ |
| 155 | public function isReservedName( string $name ) { |
| 156 | return $this->config->isReservedName( $name ); |
| 157 | } |
| 158 | |
| 159 | public function getPlaceholderName(): string { |
| 160 | return $this->config->getPlaceholderName(); |
| 161 | } |
| 162 | |
| 163 | public function getMatchPattern(): Pattern { |
| 164 | return $this->config->getMatchPattern(); |
| 165 | } |
| 166 | |
| 167 | public function getMatchPatterns(): array { |
| 168 | return $this->config->getMatchPatterns(); |
| 169 | } |
| 170 | |
| 171 | public function getMatchCondition( IReadableDatabase $db, string $field, string $op ): IExpression { |
| 172 | return $this->config->getMatchCondition( $db, $field, $op ); |
| 173 | } |
| 174 | |
| 175 | public function getExpireAfterDays(): ?int { |
| 176 | return $this->config->getExpireAfterDays(); |
| 177 | } |
| 178 | |
| 179 | public function getNotifyBeforeExpirationDays(): ?int { |
| 180 | return $this->config->getNotifyBeforeExpirationDays(); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Attempts to auto create a temporary user using |
| 185 | * AuthManager::autoCreateUser, and optionally log them |
| 186 | * in if $login is true. |
| 187 | * |
| 188 | * @param string $name |
| 189 | * @param bool $login Whether to also log the user in to this temporary account. |
| 190 | * @param string[] $tags Change tags to apply to the autocreation log. |
| 191 | * @return CreateStatus |
| 192 | */ |
| 193 | private function attemptAutoCreate( string $name, bool $login = false, array $tags = [] ): CreateStatus { |
| 194 | $createStatus = new CreateStatus; |
| 195 | // Verify the $name is usable. |
| 196 | $user = $this->userFactory->newFromName( $name, UserRigorOptions::RIGOR_USABLE ); |
| 197 | if ( !$user ) { |
| 198 | $createStatus->fatal( 'internalerror_info', |
| 199 | 'Unable to create user with automatically generated name' ); |
| 200 | return $createStatus; |
| 201 | } |
| 202 | $status = $this->authManager->autoCreateUser( |
| 203 | $user, AuthManager::AUTOCREATE_SOURCE_TEMP, $login, true, null, $tags |
| 204 | ); |
| 205 | $createStatus->merge( $status ); |
| 206 | // If a userexists warning is a part of the status, then |
| 207 | // add the fatal error temp-user-unable-to-acquire. |
| 208 | if ( $createStatus->hasMessage( 'userexists' ) ) { |
| 209 | $createStatus->fatal( 'temp-user-unable-to-acquire' ); |
| 210 | } |
| 211 | if ( $createStatus->isOK() ) { |
| 212 | $createStatus->value = $user; |
| 213 | } |
| 214 | return $createStatus; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Acquire a new username and return it. Permanently reserve the ID in |
| 219 | * the database. |
| 220 | * |
| 221 | * @param string $ip The IP address associated with this name acquisition request. |
| 222 | * @return string|null The username, or null if the auto-generated username is |
| 223 | * already in use, or if the attempt trips the TempAccountNameAcquisitionThrottle limits. |
| 224 | */ |
| 225 | private function acquireName( string $ip ): ?string { |
| 226 | if ( $this->tempAccountNameAcquisitionThrottler->increase( |
| 227 | null, $ip, 'TempUserCreator' |
| 228 | ) ) { |
| 229 | return null; |
| 230 | } |
| 231 | $year = null; |
| 232 | if ( $this->serialProviderConfig['useYear'] ?? false ) { |
| 233 | $year = MWTimestamp::getInstance()->format( 'Y' ); |
| 234 | } |
| 235 | // Check if the temporary account name is already in use as the ID provided |
| 236 | // may not be properly collision safe (T353390) |
| 237 | $index = $this->getSerialProvider()->acquireIndex( (int)$year ); |
| 238 | $serialId = $this->getSerialMapping()->getSerialIdForIndex( $index ); |
| 239 | $username = $this->config->getGeneratorPattern()->generate( $serialId, $year ); |
| 240 | |
| 241 | // Because the ::acquireIndex method may not always return a unique index, |
| 242 | // make sure that the temporary account name does not already exist. This |
| 243 | // is needed because of the problems discussed in T353390. |
| 244 | // The problems discussed at that task should not require the use of a primary lookup. |
| 245 | $centralId = $this->centralIdLookup->centralIdFromName( |
| 246 | $username, |
| 247 | CentralIdLookup::AUDIENCE_RAW |
| 248 | ); |
| 249 | if ( !$centralId ) { |
| 250 | // If no user exists with this name centrally, then return the $username. |
| 251 | return $username; |
| 252 | } |
| 253 | return null; |
| 254 | } |
| 255 | |
| 256 | private function getSerialProvider(): SerialProvider { |
| 257 | $this->serialProvider ??= $this->createSerialProvider(); |
| 258 | return $this->serialProvider; |
| 259 | } |
| 260 | |
| 261 | private function createSerialProvider(): SerialProvider { |
| 262 | $type = $this->serialProviderConfig['type']; |
| 263 | if ( isset( self::SERIAL_PROVIDERS[$type] ) ) { |
| 264 | $spec = self::SERIAL_PROVIDERS[$type]; |
| 265 | } else { |
| 266 | $extensionProviders = ExtensionRegistry::getInstance() |
| 267 | ->getAttribute( 'TempUserSerialProviders' ); |
| 268 | if ( isset( $extensionProviders[$type] ) ) { |
| 269 | $spec = $extensionProviders[$type]; |
| 270 | } else { |
| 271 | throw new UnexpectedValueException( __CLASS__ . ": unknown serial provider \"$type\"" ); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 276 | return $this->objectFactory->createObject( |
| 277 | $spec, |
| 278 | [ |
| 279 | 'assertClass' => SerialProvider::class, |
| 280 | 'extraArgs' => [ $this->serialProviderConfig ] |
| 281 | ] |
| 282 | ); |
| 283 | } |
| 284 | |
| 285 | private function getSerialMapping(): SerialMapping { |
| 286 | $this->serialMapping ??= $this->createSerialMapping(); |
| 287 | return $this->serialMapping; |
| 288 | } |
| 289 | |
| 290 | private function createSerialMapping(): SerialMapping { |
| 291 | $type = $this->serialMappingConfig['type']; |
| 292 | if ( isset( self::SERIAL_MAPPINGS[$type] ) ) { |
| 293 | $spec = self::SERIAL_MAPPINGS[$type]; |
| 294 | } else { |
| 295 | $extensionMappings = ExtensionRegistry::getInstance() |
| 296 | ->getAttribute( 'TempUserSerialMappings' ); |
| 297 | if ( isset( $extensionMappings[$type] ) ) { |
| 298 | $spec = $extensionMappings[$type]; |
| 299 | } else { |
| 300 | throw new UnexpectedValueException( __CLASS__ . ": unknown serial mapping \"$type\"" ); |
| 301 | } |
| 302 | } |
| 303 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 304 | return $this->objectFactory->createObject( |
| 305 | $spec, |
| 306 | [ |
| 307 | 'assertClass' => SerialMapping::class, |
| 308 | 'extraArgs' => [ $this->serialMappingConfig ] |
| 309 | ] |
| 310 | ); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Permanently acquire a username, stash it in a session, and return it. |
| 315 | * Do not create the user. |
| 316 | * |
| 317 | * If this method was called before with the same session ID, return the |
| 318 | * previously stashed username instead of acquiring a new one. |
| 319 | * |
| 320 | * @param Session $session |
| 321 | * @return string|null The username, or null if no username could be acquired |
| 322 | */ |
| 323 | public function acquireAndStashName( Session $session ) { |
| 324 | $name = $session->get( 'TempUser:name' ); |
| 325 | if ( $name !== null ) { |
| 326 | return $name; |
| 327 | } |
| 328 | $name = $this->acquireName( $session->getRequest()->getIP() ); |
| 329 | if ( $name !== null ) { |
| 330 | $session->set( 'TempUser:name', $name ); |
| 331 | $session->save(); |
| 332 | } |
| 333 | return $name; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Return a possible acquired and stashed username in a session. |
| 338 | * Do not acquire or create the user. |
| 339 | * |
| 340 | * If this method is called with the same session ID as function acquireAndStashName(), |
| 341 | * it returns the previously stashed username. |
| 342 | * |
| 343 | * @since 1.41 |
| 344 | * @param Session $session |
| 345 | * @return ?string The username, if it was already acquired |
| 346 | */ |
| 347 | public function getStashedName( Session $session ): ?string { |
| 348 | return $session->get( 'TempUser:name' ); |
| 349 | } |
| 350 | } |