| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Auth; |
| 23 |
|
| 24 |
use MediaWiki\MainConfigNames; |
| 25 |
use MediaWiki\MediaWikiServices; |
| 26 |
use MediaWiki\User\User; |
| 27 |
use Wikimedia\ObjectCache\BagOStuff; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class ThrottlePreAuthenticationProvider extends AbstractPreAuthenticationProvider { |
| 40 |
|
| 41 |
protected $throttleSettings; |
| 42 |
|
| 43 |
|
| 44 |
protected $accountCreationThrottle; |
| 45 |
|
| 46 |
|
| 47 |
protected $passwordAttemptThrottle; |
| 48 |
|
| 49 |
|
| 50 |
protected $cache; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function __construct( $params = [] ) { |
| 61 |
$this->throttleSettings = array_intersect_key( $params, |
| 62 |
[ 'accountCreationThrottle' => true, 'passwordAttemptThrottle' => true ] ); |
| 63 |
$services = MediaWikiServices::getInstance(); |
| 64 |
$this->cache = $params['cache'] ?? $services->getObjectCacheFactory() |
| 65 |
->getLocalClusterInstance(); |
| 66 |
} |
| 67 |
|
| 68 |
protected function postInitSetup() { |
| 69 |
$accountCreationThrottle = $this->config->get( MainConfigNames::AccountCreationThrottle ); |
| 70 |
|
| 71 |
if ( !is_array( $accountCreationThrottle ) ) { |
| 72 |
$accountCreationThrottle = [ [ |
| 73 |
'count' => $accountCreationThrottle, |
| 74 |
'seconds' => 86400, |
| 75 |
] ]; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
$this->throttleSettings += [ |
| 80 |
|
| 81 |
'accountCreationThrottle' => $accountCreationThrottle, |
| 82 |
'passwordAttemptThrottle' => |
| 83 |
$this->config->get( MainConfigNames::PasswordAttemptThrottle ), |
| 84 |
]; |
| 85 |
|
| 86 |
if ( !empty( $this->throttleSettings['accountCreationThrottle'] ) ) { |
| 87 |
$this->accountCreationThrottle = new Throttler( |
| 88 |
$this->throttleSettings['accountCreationThrottle'], [ |
| 89 |
'type' => 'acctcreate', |
| 90 |
'cache' => $this->cache, |
| 91 |
] |
| 92 |
); |
| 93 |
} |
| 94 |
if ( !empty( $this->throttleSettings['passwordAttemptThrottle'] ) ) { |
| 95 |
$this->passwordAttemptThrottle = new Throttler( |
| 96 |
$this->throttleSettings['passwordAttemptThrottle'], [ |
| 97 |
'type' => 'password', |
| 98 |
'cache' => $this->cache, |
| 99 |
] |
| 100 |
); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public function testForAccountCreation( $user, $creator, array $reqs ) { |
| 105 |
if ( !$this->accountCreationThrottle || !$creator->isPingLimitable() ) { |
| 106 |
return \StatusValue::newGood(); |
| 107 |
} |
| 108 |
|
| 109 |
$ip = $this->manager->getRequest()->getIP(); |
| 110 |
|
| 111 |
if ( !$this->getHookRunner()->onExemptFromAccountCreationThrottle( $ip ) ) { |
| 112 |
$this->logger->debug( __METHOD__ . ": a hook allowed account creation w/o throttle" ); |
| 113 |
return \StatusValue::newGood(); |
| 114 |
} |
| 115 |
|
| 116 |
$result = $this->accountCreationThrottle->increase( null, $ip, __METHOD__ ); |
| 117 |
if ( $result ) { |
| 118 |
$message = wfMessage( 'acct_creation_throttle_hit' )->params( $result['count'] ) |
| 119 |
->durationParams( $result['wait'] ); |
| 120 |
return \StatusValue::newFatal( $message ); |
| 121 |
} |
| 122 |
|
| 123 |
return \StatusValue::newGood(); |
| 124 |
} |
| 125 |
|
| 126 |
public function testForAuthentication( array $reqs ) { |
| 127 |
if ( !$this->passwordAttemptThrottle ) { |
| 128 |
return \StatusValue::newGood(); |
| 129 |
} |
| 130 |
|
| 131 |
$ip = $this->manager->getRequest()->getIP(); |
| 132 |
try { |
| 133 |
$username = AuthenticationRequest::getUsernameFromRequests( $reqs ); |
| 134 |
} catch ( \UnexpectedValueException $e ) { |
| 135 |
$username = null; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
if ( $username !== null ) { |
| 141 |
$usernames = $this->manager->normalizeUsername( $username ); |
| 142 |
} else { |
| 143 |
$usernames = [ null ]; |
| 144 |
} |
| 145 |
$result = false; |
| 146 |
foreach ( $usernames as $name ) { |
| 147 |
$r = $this->passwordAttemptThrottle->increase( $name, $ip, __METHOD__ ); |
| 148 |
if ( $r && ( !$result || $result['wait'] < $r['wait'] ) ) { |
| 149 |
$result = $r; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if ( $result ) { |
| 154 |
$message = wfMessage( 'login-throttled' )->durationParams( $result['wait'] ); |
| 155 |
return \StatusValue::newFatal( $message ); |
| 156 |
} else { |
| 157 |
$this->manager->setAuthenticationSessionData( 'LoginThrottle', |
| 158 |
[ 'users' => $usernames, 'ip' => $ip ] ); |
| 159 |
return \StatusValue::newGood(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public function postAuthentication( $user, AuthenticationResponse $response ) { |
| 168 |
if ( $response->status !== AuthenticationResponse::PASS ) { |
| 169 |
return; |
| 170 |
} elseif ( !$this->passwordAttemptThrottle ) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
$data = $this->manager->getAuthenticationSessionData( 'LoginThrottle' ); |
| 175 |
if ( !$data ) { |
| 176 |
|
| 177 |
|
| 178 |
$this->logger->info( 'throttler data not found for {user}', [ 'user' => $user->getName() ] ); |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
foreach ( $data['users'] as $name ) { |
| 183 |
$this->passwordAttemptThrottle->clear( $name, $data['ip'] ); |
| 184 |
} |
| 185 |
} |
| 186 |
} |