3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 namespace MediaWiki\Auth
;
28 * A pre-authentication provider to throttle authentication actions.
30 * Adding this provider will throttle account creations and primary authentication attempts
31 * (more specifically, any authentication that returns FAIL on failure). Secondary authentication
32 * cannot be easily throttled on a framework level (since it would typically return UI on failure);
33 * secondary providers are expected to do their own throttling.
37 class ThrottlePreAuthenticationProvider
extends AbstractPreAuthenticationProvider
{
39 protected $throttleSettings;
42 protected $accountCreationThrottle;
45 protected $passwordAttemptThrottle;
51 * @param array $params
52 * - accountCreationThrottle: (array) Condition array for the account creation throttle; an array
53 * of arrays in a format like $wgPasswordAttemptThrottle, passed to the Throttler constructor.
54 * - passwordAttemptThrottle: (array) Condition array for the password attempt throttle, in the
55 * same format as accountCreationThrottle.
56 * - cache: (BagOStuff) Where to store the throttle, defaults to the local cluster instance.
58 public function __construct( $params = [] ) {
59 $this->throttleSettings
= array_intersect_key( $params,
60 [ 'accountCreationThrottle' => true, 'passwordAttemptThrottle' => true ] );
61 $this->cache
= isset( $params['cache'] ) ?
$params['cache'] :
62 \ObjectCache
::getLocalClusterInstance();
65 public function setConfig( Config
$config ) {
66 parent
::setConfig( $config );
68 // @codeCoverageIgnoreStart
69 $this->throttleSettings +
= [
70 // @codeCoverageIgnoreEnd
71 'accountCreationThrottle' => [ [
72 'count' => $this->config
->get( 'AccountCreationThrottle' ),
75 'passwordAttemptThrottle' => $this->config
->get( 'PasswordAttemptThrottle' ),
78 if ( !empty( $this->throttleSettings
['accountCreationThrottle'] ) ) {
79 $this->accountCreationThrottle
= new Throttler(
80 $this->throttleSettings
['accountCreationThrottle'], [
81 'type' => 'acctcreate',
82 'cache' => $this->cache
,
86 if ( !empty( $this->throttleSettings
['passwordAttemptThrottle'] ) ) {
87 $this->passwordAttemptThrottle
= new Throttler(
88 $this->throttleSettings
['passwordAttemptThrottle'], [
90 'cache' => $this->cache
,
96 public function testForAccountCreation( $user, $creator, array $reqs ) {
97 if ( !$this->accountCreationThrottle ||
!$creator->isPingLimitable() ) {
98 return \StatusValue
::newGood();
101 $ip = $this->manager
->getRequest()->getIP();
103 if ( !\Hooks
::run( 'ExemptFromAccountCreationThrottle', [ $ip ] ) ) {
104 $this->logger
->debug( __METHOD__
. ": a hook allowed account creation w/o throttle\n" );
105 return \StatusValue
::newGood();
108 $result = $this->accountCreationThrottle
->increase( null, $ip, __METHOD__
);
110 return \StatusValue
::newFatal( 'acct_creation_throttle_hit', $result['count'] );
113 return \StatusValue
::newGood();
116 public function testForAuthentication( array $reqs ) {
117 if ( !$this->passwordAttemptThrottle
) {
118 return \StatusValue
::newGood();
121 $ip = $this->manager
->getRequest()->getIP();
123 $username = AuthenticationRequest
::getUsernameFromRequests( $reqs );
124 } catch ( \UnexpectedValueException
$e ) {
128 // Get everything this username could normalize to, and throttle each one individually.
129 // If nothing uses usernames, just throttle by IP.
130 $usernames = $this->manager
->normalizeUsername( $username );
132 foreach ( $usernames as $name ) {
133 $r = $this->passwordAttemptThrottle
->increase( $name, $ip, __METHOD__
);
134 if ( $r && ( !$result ||
$result['wait'] < $r['wait'] ) ) {
140 $message = wfMessage( 'login-throttled' )->durationParams( $result['wait'] );
141 return \StatusValue
::newFatal( $message );
143 $this->manager
->setAuthenticationSessionData( 'LoginThrottle',
144 [ 'users' => $usernames, 'ip' => $ip ] );
145 return \StatusValue
::newGood();
150 * @param null|\User $user
151 * @param AuthenticationResponse $response
153 public function postAuthentication( $user, AuthenticationResponse
$response ) {
154 if ( $response->status
!== AuthenticationResponse
::PASS
) {
156 } elseif ( !$this->passwordAttemptThrottle
) {
160 $data = $this->manager
->getAuthenticationSessionData( 'LoginThrottle' );
162 $this->logger
->error( 'throttler data not found for {user}', [ 'user' => $user->getName() ] );
166 foreach ( $data['users'] as $name ) {
167 $this->passwordAttemptThrottle
->clear( $name, $data['ip'] );