MediaWiki  1.29.1
ThrottlePreAuthenticationProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Auth;
4 
5 use Wikimedia\TestingAccessWrapper;
6 
13  public function testConstructor() {
14  $provider = new ThrottlePreAuthenticationProvider();
15  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
16  $config = new \HashConfig( [
17  'AccountCreationThrottle' => [ [
18  'count' => 123,
19  'seconds' => 86400,
20  ] ],
21  'PasswordAttemptThrottle' => [ [
22  'count' => 5,
23  'seconds' => 300,
24  ] ],
25  ] );
26  $provider->setConfig( $config );
27  $this->assertSame( [
28  'accountCreationThrottle' => [ [ 'count' => 123, 'seconds' => 86400 ] ],
29  'passwordAttemptThrottle' => [ [ 'count' => 5, 'seconds' => 300 ] ]
30  ], $providerPriv->throttleSettings );
31  $accountCreationThrottle = TestingAccessWrapper::newFromObject(
32  $providerPriv->accountCreationThrottle );
33  $this->assertSame( [ [ 'count' => 123, 'seconds' => 86400 ] ],
34  $accountCreationThrottle->conditions );
35  $passwordAttemptThrottle = TestingAccessWrapper::newFromObject(
36  $providerPriv->passwordAttemptThrottle );
37  $this->assertSame( [ [ 'count' => 5, 'seconds' => 300 ] ],
38  $passwordAttemptThrottle->conditions );
39 
40  $provider = new ThrottlePreAuthenticationProvider( [
41  'accountCreationThrottle' => [ [ 'count' => 43, 'seconds' => 10000 ] ],
42  'passwordAttemptThrottle' => [ [ 'count' => 11, 'seconds' => 100 ] ],
43  ] );
44  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
45  $config = new \HashConfig( [
46  'AccountCreationThrottle' => [ [
47  'count' => 123,
48  'seconds' => 86400,
49  ] ],
50  'PasswordAttemptThrottle' => [ [
51  'count' => 5,
52  'seconds' => 300,
53  ] ],
54  ] );
55  $provider->setConfig( $config );
56  $this->assertSame( [
57  'accountCreationThrottle' => [ [ 'count' => 43, 'seconds' => 10000 ] ],
58  'passwordAttemptThrottle' => [ [ 'count' => 11, 'seconds' => 100 ] ],
59  ], $providerPriv->throttleSettings );
60 
61  $cache = new \HashBagOStuff();
62  $provider = new ThrottlePreAuthenticationProvider( [ 'cache' => $cache ] );
63  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
64  $provider->setConfig( new \HashConfig( [
65  'AccountCreationThrottle' => [ [ 'count' => 1, 'seconds' => 1 ] ],
66  'PasswordAttemptThrottle' => [ [ 'count' => 1, 'seconds' => 1 ] ],
67  ] ) );
68  $accountCreationThrottle = TestingAccessWrapper::newFromObject(
69  $providerPriv->accountCreationThrottle );
70  $this->assertSame( $cache, $accountCreationThrottle->cache );
71  $passwordAttemptThrottle = TestingAccessWrapper::newFromObject(
72  $providerPriv->passwordAttemptThrottle );
73  $this->assertSame( $cache, $passwordAttemptThrottle->cache );
74  }
75 
76  public function testDisabled() {
77  $provider = new ThrottlePreAuthenticationProvider( [
78  'accountCreationThrottle' => [],
79  'passwordAttemptThrottle' => [],
80  'cache' => new \HashBagOStuff(),
81  ] );
82  $provider->setLogger( new \Psr\Log\NullLogger() );
83  $provider->setConfig( new \HashConfig( [
84  'AccountCreationThrottle' => null,
85  'PasswordAttemptThrottle' => null,
86  ] ) );
87  $provider->setManager( AuthManager::singleton() );
88 
89  $this->assertEquals(
91  $provider->testForAccountCreation(
92  \User::newFromName( 'Created' ),
93  \User::newFromName( 'Creator' ),
94  []
95  )
96  );
97  $this->assertEquals(
99  $provider->testForAuthentication( [] )
100  );
101  }
102 
109  public function testTestForAccountCreation( $creatorname, $succeed, $hook ) {
110  $provider = new ThrottlePreAuthenticationProvider( [
111  'accountCreationThrottle' => [ [ 'count' => 2, 'seconds' => 86400 ] ],
112  'cache' => new \HashBagOStuff(),
113  ] );
114  $provider->setLogger( new \Psr\Log\NullLogger() );
115  $provider->setConfig( new \HashConfig( [
116  'AccountCreationThrottle' => null,
117  'PasswordAttemptThrottle' => null,
118  ] ) );
119  $provider->setManager( AuthManager::singleton() );
120 
121  $user = \User::newFromName( 'RandomUser' );
122  $creator = \User::newFromName( $creatorname );
123  if ( $hook ) {
124  $mock = $this->getMockBuilder( 'stdClass' )
125  ->setMethods( [ 'onExemptFromAccountCreationThrottle' ] )
126  ->getMock();
127  $mock->expects( $this->any() )->method( 'onExemptFromAccountCreationThrottle' )
128  ->will( $this->returnValue( false ) );
129  $this->mergeMwGlobalArrayValue( 'wgHooks', [
130  'ExemptFromAccountCreationThrottle' => [ $mock ],
131  ] );
132  }
133 
134  $this->assertEquals(
135  true,
136  $provider->testForAccountCreation( $user, $creator, [] )->isOK(),
137  'attempt #1'
138  );
139  $this->assertEquals(
140  true,
141  $provider->testForAccountCreation( $user, $creator, [] )->isOK(),
142  'attempt #2'
143  );
144  $this->assertEquals(
145  $succeed ? true : false,
146  $provider->testForAccountCreation( $user, $creator, [] )->isOK(),
147  'attempt #3'
148  );
149  }
150 
151  public static function provideTestForAccountCreation() {
152  return [
153  'Normal user' => [ 'NormalUser', false, false ],
154  'Sysop' => [ 'UTSysop', true, false ],
155  'Normal user with hook' => [ 'NormalUser', true, true ],
156  ];
157  }
158 
159  public function testTestForAuthentication() {
160  $provider = new ThrottlePreAuthenticationProvider( [
161  'passwordAttemptThrottle' => [ [ 'count' => 2, 'seconds' => 86400 ] ],
162  'cache' => new \HashBagOStuff(),
163  ] );
164  $provider->setLogger( new \Psr\Log\NullLogger() );
165  $provider->setConfig( new \HashConfig( [
166  'AccountCreationThrottle' => null,
167  'PasswordAttemptThrottle' => null,
168  ] ) );
169  $provider->setManager( AuthManager::singleton() );
170 
172  $req->username = 'SomeUser';
173  for ( $i = 1; $i <= 3; $i++ ) {
174  $status = $provider->testForAuthentication( [ $req ] );
175  $this->assertEquals( $i < 3, $status->isGood(), "attempt #$i" );
176  }
177  $this->assertCount( 1, $status->getErrors() );
178  $msg = new \Message( $status->getErrors()[0]['message'], $status->getErrors()[0]['params'] );
179  $this->assertEquals( 'login-throttled', $msg->getKey() );
180 
181  $provider->postAuthentication( \User::newFromName( 'SomeUser' ),
183  $this->assertFalse( $provider->testForAuthentication( [ $req ] )->isGood(), 'after FAIL' );
184 
185  $provider->postAuthentication( \User::newFromName( 'SomeUser' ),
187  $this->assertTrue( $provider->testForAuthentication( [ $req ] )->isGood(), 'after PASS' );
188 
189  $req1 = new UsernameAuthenticationRequest;
190  $req1->username = 'foo';
191  $req2 = new UsernameAuthenticationRequest;
192  $req2->username = 'bar';
193  $this->assertTrue( $provider->testForAuthentication( [ $req1, $req2 ] )->isGood() );
194 
196  $req->username = 'Some user';
197  $provider->testForAuthentication( [ $req ] );
198  $req->username = 'Some_user';
199  $provider->testForAuthentication( [ $req ] );
200  $req->username = 'some user';
201  $status = $provider->testForAuthentication( [ $req ] );
202  $this->assertFalse( $status->isGood(), 'denormalized usernames are normalized' );
203  }
204 
205  public function testPostAuthentication() {
206  $provider = new ThrottlePreAuthenticationProvider( [
207  'passwordAttemptThrottle' => [],
208  'cache' => new \HashBagOStuff(),
209  ] );
210  $provider->setLogger( new \TestLogger );
211  $provider->setConfig( new \HashConfig( [
212  'AccountCreationThrottle' => null,
213  'PasswordAttemptThrottle' => null,
214  ] ) );
215  $provider->setManager( AuthManager::singleton() );
216  $provider->postAuthentication( \User::newFromName( 'SomeUser' ),
218 
219  $provider = new ThrottlePreAuthenticationProvider( [
220  'passwordAttemptThrottle' => [ [ 'count' => 2, 'seconds' => 86400 ] ],
221  'cache' => new \HashBagOStuff(),
222  ] );
223  $logger = new \TestLogger( true );
224  $provider->setLogger( $logger );
225  $provider->setConfig( new \HashConfig( [
226  'AccountCreationThrottle' => null,
227  'PasswordAttemptThrottle' => null,
228  ] ) );
229  $provider->setManager( AuthManager::singleton() );
230  $provider->postAuthentication( \User::newFromName( 'SomeUser' ),
232  $this->assertSame( [
233  [ \Psr\Log\LogLevel::INFO, 'throttler data not found for {user}' ],
234  ], $logger->getBuffer() );
235  }
236 }
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
MediaWiki\Auth\ThrottlePreAuthenticationProviderTest\testDisabled
testDisabled()
Definition: ThrottlePreAuthenticationProviderTest.php:76
MediaWiki\Auth\ThrottlePreAuthenticationProviderTest\testConstructor
testConstructor()
Definition: ThrottlePreAuthenticationProviderTest.php:13
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:766
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1049
MediaWiki\Auth\ThrottlePreAuthenticationProviderTest\provideTestForAccountCreation
static provideTestForAccountCreation()
Definition: ThrottlePreAuthenticationProviderTest.php:151
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MediaWiki\Auth\ThrottlePreAuthenticationProviderTest\testTestForAuthentication
testTestForAuthentication()
Definition: ThrottlePreAuthenticationProviderTest.php:159
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
$req
this hook is for auditing only $req
Definition: hooks.txt:990
TestLogger
A logger that may be configured to either buffer logs or to print them to the output where PHPUnit wi...
Definition: TestLogger.php:34
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:556
MediaWiki\Auth\ThrottlePreAuthenticationProviderTest\testPostAuthentication
testPostAuthentication()
Definition: ThrottlePreAuthenticationProviderTest.php:205
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
MediaWiki\Auth\ThrottlePreAuthenticationProvider
A pre-authentication provider to throttle authentication actions.
Definition: ThrottlePreAuthenticationProvider.php:37
MediaWiki\Auth\UsernameAuthenticationRequest
AuthenticationRequest to ensure something with a username is present.
Definition: UsernameAuthenticationRequest.php:29
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:76
MediaWiki\Auth\ThrottlePreAuthenticationProviderTest
AuthManager Database MediaWiki\Auth\ThrottlePreAuthenticationProvider.
Definition: ThrottlePreAuthenticationProviderTest.php:12
$cache
$cache
Definition: mcc.php:33
MediaWiki\Auth\AuthManager\singleton
static singleton()
Get the global AuthManager.
Definition: AuthManager.php:146
MediaWiki\Auth\AuthenticationResponse\newFail
static newFail(Message $msg)
Definition: AuthenticationResponse.php:146
MediaWiki\$config
Config $config
Definition: MediaWiki.php:42
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1956
wfMessage
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
MediaWiki\Auth
Definition: AbstractAuthenticationProvider.php:22
MediaWiki\Auth\AuthenticationResponse\newPass
static newPass( $username=null)
Definition: AuthenticationResponse.php:134
MediaWiki\Auth\ThrottlePreAuthenticationProviderTest\testTestForAccountCreation
testTestForAccountCreation( $creatorname, $succeed, $hook)
provideTestForAccountCreation
Definition: ThrottlePreAuthenticationProviderTest.php:109