MediaWiki REL1_31
ThrottlePreAuthenticationProvider.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Auth;
23
24use BagOStuff;
25use Config;
26
40
43
46
48 protected $cache;
49
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();
63 }
64
65 public function setConfig( Config $config ) {
66 parent::setConfig( $config );
67
68 $accountCreationThrottle = $this->config->get( 'AccountCreationThrottle' );
69 // Handle old $wgAccountCreationThrottle format (number of attempts per 24 hours)
70 if ( !is_array( $accountCreationThrottle ) ) {
72 'count' => $accountCreationThrottle,
73 'seconds' => 86400,
74 ] ];
75 }
76
77 // @codeCoverageIgnoreStart
78 $this->throttleSettings += [
79 // @codeCoverageIgnoreEnd
80 'accountCreationThrottle' => $accountCreationThrottle,
81 'passwordAttemptThrottle' => $this->config->get( 'PasswordAttemptThrottle' ),
82 ];
83
84 if ( !empty( $this->throttleSettings['accountCreationThrottle'] ) ) {
85 $this->accountCreationThrottle = new Throttler(
86 $this->throttleSettings['accountCreationThrottle'], [
87 'type' => 'acctcreate',
88 'cache' => $this->cache,
89 ]
90 );
91 }
92 if ( !empty( $this->throttleSettings['passwordAttemptThrottle'] ) ) {
93 $this->passwordAttemptThrottle = new Throttler(
94 $this->throttleSettings['passwordAttemptThrottle'], [
95 'type' => 'password',
96 'cache' => $this->cache,
97 ]
98 );
99 }
100 }
101
102 public function testForAccountCreation( $user, $creator, array $reqs ) {
103 if ( !$this->accountCreationThrottle || !$creator->isPingLimitable() ) {
104 return \StatusValue::newGood();
105 }
106
107 $ip = $this->manager->getRequest()->getIP();
108
109 if ( !\Hooks::run( 'ExemptFromAccountCreationThrottle', [ $ip ] ) ) {
110 $this->logger->debug( __METHOD__ . ": a hook allowed account creation w/o throttle\n" );
111 return \StatusValue::newGood();
112 }
113
114 $result = $this->accountCreationThrottle->increase( null, $ip, __METHOD__ );
115 if ( $result ) {
116 $message = wfMessage( 'acct_creation_throttle_hit' )->params( $result['count'] )
117 ->durationParams( $result['wait'] );
118 return \StatusValue::newFatal( $message );
119 }
120
121 return \StatusValue::newGood();
122 }
123
124 public function testForAuthentication( array $reqs ) {
125 if ( !$this->passwordAttemptThrottle ) {
126 return \StatusValue::newGood();
127 }
128
129 $ip = $this->manager->getRequest()->getIP();
130 try {
132 } catch ( \UnexpectedValueException $e ) {
133 $username = '';
134 }
135
136 // Get everything this username could normalize to, and throttle each one individually.
137 // If nothing uses usernames, just throttle by IP.
138 $usernames = $this->manager->normalizeUsername( $username );
139 $result = false;
140 foreach ( $usernames as $name ) {
141 $r = $this->passwordAttemptThrottle->increase( $name, $ip, __METHOD__ );
142 if ( $r && ( !$result || $result['wait'] < $r['wait'] ) ) {
143 $result = $r;
144 }
145 }
146
147 if ( $result ) {
148 $message = wfMessage( 'login-throttled' )->durationParams( $result['wait'] );
149 return \StatusValue::newFatal( $message );
150 } else {
151 $this->manager->setAuthenticationSessionData( 'LoginThrottle',
152 [ 'users' => $usernames, 'ip' => $ip ] );
153 return \StatusValue::newGood();
154 }
155 }
156
162 if ( $response->status !== AuthenticationResponse::PASS ) {
163 return;
164 } elseif ( !$this->passwordAttemptThrottle ) {
165 return;
166 }
167
168 $data = $this->manager->getAuthenticationSessionData( 'LoginThrottle' );
169 if ( !$data ) {
170 // this can occur when login is happening via AuthenticationRequest::$loginRequest
171 // so testForAuthentication is skipped
172 $this->logger->info( 'throttler data not found for {user}', [ 'user' => $user->getName() ] );
173 return;
174 }
175
176 foreach ( $data['users'] as $name ) {
177 $this->passwordAttemptThrottle->clear( $name, $data['ip'] );
178 }
179 }
180}
interface is intended to be more or less compatible with the PHP memcached client.
Definition BagOStuff.php:47
A base class that implements some of the boilerplate for a PreAuthenticationProvider.
static getUsernameFromRequests(array $reqs)
Get the username from the set of requests.
This is a value object to hold authentication response data.
const PASS
Indicates that the authentication succeeded.
A pre-authentication provider to throttle authentication actions.
testForAccountCreation( $user, $creator, array $reqs)
Determine whether an account creation may begin.
testForAuthentication(array $reqs)
Determine whether an authentication may begin.
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
this hook is for auditing only or null if authentication failed before getting that far $username
Definition hooks.txt:785
this hook is for auditing only $response
Definition hooks.txt:783
returning false will NOT prevent logging $e
Definition hooks.txt:2176
Interface for configuration instances.
Definition Config.php:28
you have access to all of the normal MediaWiki so you can get a DB use the cache
$params