MediaWiki REL1_33
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 = $params['cache'] ?? \ObjectCache::getLocalClusterInstance();
62 }
63
64 public function setConfig( Config $config ) {
65 parent::setConfig( $config );
66
67 $accountCreationThrottle = $this->config->get( 'AccountCreationThrottle' );
68 // Handle old $wgAccountCreationThrottle format (number of attempts per 24 hours)
71 'count' => $accountCreationThrottle,
72 'seconds' => 86400,
73 ] ];
74 }
75
76 // @codeCoverageIgnoreStart
77 $this->throttleSettings += [
78 // @codeCoverageIgnoreEnd
79 'accountCreationThrottle' => $accountCreationThrottle,
80 'passwordAttemptThrottle' => $this->config->get( 'PasswordAttemptThrottle' ),
81 ];
82
83 if ( !empty( $this->throttleSettings['accountCreationThrottle'] ) ) {
84 $this->accountCreationThrottle = new Throttler(
85 $this->throttleSettings['accountCreationThrottle'], [
86 'type' => 'acctcreate',
87 'cache' => $this->cache,
88 ]
89 );
90 }
91 if ( !empty( $this->throttleSettings['passwordAttemptThrottle'] ) ) {
92 $this->passwordAttemptThrottle = new Throttler(
93 $this->throttleSettings['passwordAttemptThrottle'], [
94 'type' => 'password',
95 'cache' => $this->cache,
96 ]
97 );
98 }
99 }
100
101 public function testForAccountCreation( $user, $creator, array $reqs ) {
102 if ( !$this->accountCreationThrottle || !$creator->isPingLimitable() ) {
103 return \StatusValue::newGood();
104 }
105
106 $ip = $this->manager->getRequest()->getIP();
107
108 if ( !\Hooks::run( 'ExemptFromAccountCreationThrottle', [ $ip ] ) ) {
109 $this->logger->debug( __METHOD__ . ": a hook allowed account creation w/o throttle\n" );
110 return \StatusValue::newGood();
111 }
112
113 $result = $this->accountCreationThrottle->increase( null, $ip, __METHOD__ );
114 if ( $result ) {
115 $message = wfMessage( 'acct_creation_throttle_hit' )->params( $result['count'] )
116 ->durationParams( $result['wait'] );
117 return \StatusValue::newFatal( $message );
118 }
119
120 return \StatusValue::newGood();
121 }
122
123 public function testForAuthentication( array $reqs ) {
124 if ( !$this->passwordAttemptThrottle ) {
125 return \StatusValue::newGood();
126 }
127
128 $ip = $this->manager->getRequest()->getIP();
129 try {
131 } catch ( \UnexpectedValueException $e ) {
132 $username = '';
133 }
134
135 // Get everything this username could normalize to, and throttle each one individually.
136 // If nothing uses usernames, just throttle by IP.
137 $usernames = $this->manager->normalizeUsername( $username );
138 $result = false;
139 foreach ( $usernames as $name ) {
140 $r = $this->passwordAttemptThrottle->increase( $name, $ip, __METHOD__ );
141 if ( $r && ( !$result || $result['wait'] < $r['wait'] ) ) {
142 $result = $r;
143 }
144 }
145
146 if ( $result ) {
147 $message = wfMessage( 'login-throttled' )->durationParams( $result['wait'] );
148 return \StatusValue::newFatal( $message );
149 } else {
150 $this->manager->setAuthenticationSessionData( 'LoginThrottle',
151 [ 'users' => $usernames, 'ip' => $ip ] );
152 return \StatusValue::newGood();
153 }
154 }
155
161 if ( $response->status !== AuthenticationResponse::PASS ) {
162 return;
163 } elseif ( !$this->passwordAttemptThrottle ) {
164 return;
165 }
166
167 $data = $this->manager->getAuthenticationSessionData( 'LoginThrottle' );
168 if ( !$data ) {
169 // this can occur when login is happening via AuthenticationRequest::$loginRequest
170 // so testForAuthentication is skipped
171 $this->logger->info( 'throttler data not found for {user}', [ 'user' => $user->getName() ] );
172 return;
173 }
174
175 foreach ( $data['users'] as $name ) {
176 $this->passwordAttemptThrottle->clear( $name, $data['ip'] );
177 }
178 }
179}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:58
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 use $formDescriptor instead 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:782
this hook is for auditing only $response
Definition hooks.txt:780
returning false will NOT prevent logging $e
Definition hooks.txt:2175
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
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
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$params