MediaWiki  1.27.2
ThrottlePreAuthenticationProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Auth;
4 
11  public function testConstructor() {
12  $provider = new ThrottlePreAuthenticationProvider();
13  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
14  $config = new \HashConfig( [
15  'AccountCreationThrottle' => 123,
16  'PasswordAttemptThrottle' => [ [
17  'count' => 5,
18  'seconds' => 300,
19  ] ],
20  ] );
21  $provider->setConfig( $config );
22  $this->assertSame( [
23  'accountCreationThrottle' => [ [ 'count' => 123, 'seconds' => 86400 ] ],
24  'passwordAttemptThrottle' => [ [ 'count' => 5, 'seconds' => 300 ] ]
25  ], $providerPriv->throttleSettings );
26  $accountCreationThrottle = \TestingAccessWrapper::newFromObject(
27  $providerPriv->accountCreationThrottle );
28  $this->assertSame( [ [ 'count' => 123, 'seconds' => 86400 ] ],
29  $accountCreationThrottle->conditions );
30  $passwordAttemptThrottle = \TestingAccessWrapper::newFromObject(
31  $providerPriv->passwordAttemptThrottle );
32  $this->assertSame( [ [ 'count' => 5, 'seconds' => 300 ] ],
33  $passwordAttemptThrottle->conditions );
34 
35  $provider = new ThrottlePreAuthenticationProvider( [
36  'accountCreationThrottle' => [ [ 'count' => 43, 'seconds' => 10000 ] ],
37  'passwordAttemptThrottle' => [ [ 'count' => 11, 'seconds' => 100 ] ],
38  ] );
39  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
40  $config = new \HashConfig( [
41  'AccountCreationThrottle' => 123,
42  'PasswordAttemptThrottle' => [ [
43  'count' => 5,
44  'seconds' => 300,
45  ] ],
46  ] );
47  $provider->setConfig( $config );
48  $this->assertSame( [
49  'accountCreationThrottle' => [ [ 'count' => 43, 'seconds' => 10000 ] ],
50  'passwordAttemptThrottle' => [ [ 'count' => 11, 'seconds' => 100 ] ],
51  ], $providerPriv->throttleSettings );
52 
53  $cache = new \HashBagOStuff();
54  $provider = new ThrottlePreAuthenticationProvider( [ 'cache' => $cache ] );
55  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
56  $provider->setConfig( new \HashConfig( [
57  'AccountCreationThrottle' => [ [ 'count' => 1, 'seconds' => 1 ] ],
58  'PasswordAttemptThrottle' => [ [ 'count' => 1, 'seconds' => 1 ] ],
59  ] ) );
60  $accountCreationThrottle = \TestingAccessWrapper::newFromObject(
61  $providerPriv->accountCreationThrottle );
62  $this->assertSame( $cache, $accountCreationThrottle->cache );
63  $passwordAttemptThrottle = \TestingAccessWrapper::newFromObject(
64  $providerPriv->passwordAttemptThrottle );
65  $this->assertSame( $cache, $passwordAttemptThrottle->cache );
66  }
67 
68  public function testDisabled() {
69  $provider = new ThrottlePreAuthenticationProvider( [
70  'accountCreationThrottle' => [],
71  'passwordAttemptThrottle' => [],
72  'cache' => new \HashBagOStuff(),
73  ] );
74  $provider->setLogger( new \Psr\Log\NullLogger() );
75  $provider->setConfig( new \HashConfig( [
76  'AccountCreationThrottle' => null,
77  'PasswordAttemptThrottle' => null,
78  ] ) );
79  $provider->setManager( AuthManager::singleton() );
80 
81  $this->assertEquals(
83  $provider->testForAccountCreation(
84  \User::newFromName( 'Created' ),
85  \User::newFromName( 'Creator' ),
86  []
87  )
88  );
89  $this->assertEquals(
91  $provider->testForAuthentication( [] )
92  );
93  }
94 
101  public function testTestForAccountCreation( $creatorname, $succeed, $hook ) {
102  $provider = new ThrottlePreAuthenticationProvider( [
103  'accountCreationThrottle' => [ [ 'count' => 2, 'seconds' => 86400 ] ],
104  'cache' => new \HashBagOStuff(),
105  ] );
106  $provider->setLogger( new \Psr\Log\NullLogger() );
107  $provider->setConfig( new \HashConfig( [
108  'AccountCreationThrottle' => null,
109  'PasswordAttemptThrottle' => null,
110  ] ) );
111  $provider->setManager( AuthManager::singleton() );
112 
113  $user = \User::newFromName( 'RandomUser' );
114  $creator = \User::newFromName( $creatorname );
115  if ( $hook ) {
116  $mock = $this->getMock( 'stdClass', [ 'onExemptFromAccountCreationThrottle' ] );
117  $mock->expects( $this->any() )->method( 'onExemptFromAccountCreationThrottle' )
118  ->will( $this->returnValue( false ) );
119  $this->mergeMwGlobalArrayValue( 'wgHooks', [
120  'ExemptFromAccountCreationThrottle' => [ $mock ],
121  ] );
122  }
123 
124  $this->assertEquals(
126  $provider->testForAccountCreation( $user, $creator, [] ),
127  'attempt #1'
128  );
129  $this->assertEquals(
131  $provider->testForAccountCreation( $user, $creator, [] ),
132  'attempt #2'
133  );
134  $this->assertEquals(
135  $succeed ? \StatusValue::newGood() : \StatusValue::newFatal( 'acct_creation_throttle_hit', 2 ),
136  $provider->testForAccountCreation( $user, $creator, [] ),
137  'attempt #3'
138  );
139  }
140 
141  public static function provideTestForAccountCreation() {
142  return [
143  'Normal user' => [ 'NormalUser', false, false ],
144  'Sysop' => [ 'UTSysop', true, false ],
145  'Normal user with hook' => [ 'NormalUser', true, true ],
146  ];
147  }
148 
149  public function testTestForAuthentication() {
150  $provider = new ThrottlePreAuthenticationProvider( [
151  'passwordAttemptThrottle' => [ [ 'count' => 2, 'seconds' => 86400 ] ],
152  'cache' => new \HashBagOStuff(),
153  ] );
154  $provider->setLogger( new \Psr\Log\NullLogger() );
155  $provider->setConfig( new \HashConfig( [
156  'AccountCreationThrottle' => null,
157  'PasswordAttemptThrottle' => null,
158  ] ) );
159  $provider->setManager( AuthManager::singleton() );
160 
162  $req->username = 'SomeUser';
163  for ( $i = 1; $i <= 3; $i++ ) {
164  $status = $provider->testForAuthentication( [ $req ] );
165  $this->assertEquals( $i < 3, $status->isGood(), "attempt #$i" );
166  }
167  $this->assertCount( 1, $status->getErrors() );
168  $msg = new \Message( $status->getErrors()[0]['message'], $status->getErrors()[0]['params'] );
169  $this->assertEquals( 'login-throttled', $msg->getKey() );
170 
171  $provider->postAuthentication( \User::newFromName( 'SomeUser' ),
173  $this->assertFalse( $provider->testForAuthentication( [ $req ] )->isGood(), 'after FAIL' );
174 
175  $provider->postAuthentication( \User::newFromName( 'SomeUser' ),
177  $this->assertTrue( $provider->testForAuthentication( [ $req ] )->isGood(), 'after PASS' );
178 
179  $req1 = new UsernameAuthenticationRequest;
180  $req1->username = 'foo';
181  $req2 = new UsernameAuthenticationRequest;
182  $req2->username = 'bar';
183  $this->assertTrue( $provider->testForAuthentication( [ $req1, $req2 ] )->isGood() );
184 
186  $req->username = 'Some user';
187  $provider->testForAuthentication( [ $req ] );
188  $req->username = 'Some_user';
189  $provider->testForAuthentication( [ $req ] );
190  $req->username = 'some user';
191  $status = $provider->testForAuthentication( [ $req ] );
192  $this->assertFalse( $status->isGood(), 'denormalized usernames are normalized' );
193  }
194 
195  public function testPostAuthentication() {
196  $provider = new ThrottlePreAuthenticationProvider( [
197  'passwordAttemptThrottle' => [],
198  'cache' => new \HashBagOStuff(),
199  ] );
200  $provider->setLogger( new \TestLogger );
201  $provider->setConfig( new \HashConfig( [
202  'AccountCreationThrottle' => null,
203  'PasswordAttemptThrottle' => null,
204  ] ) );
205  $provider->setManager( AuthManager::singleton() );
206  $provider->postAuthentication( \User::newFromName( 'SomeUser' ),
208 
209  $provider = new ThrottlePreAuthenticationProvider( [
210  'passwordAttemptThrottle' => [ [ 'count' => 2, 'seconds' => 86400 ] ],
211  'cache' => new \HashBagOStuff(),
212  ] );
213  $logger = new \TestLogger( true );
214  $provider->setLogger( $logger );
215  $provider->setConfig( new \HashConfig( [
216  'AccountCreationThrottle' => null,
217  'PasswordAttemptThrottle' => null,
218  ] ) );
219  $provider->setManager( AuthManager::singleton() );
220  $provider->postAuthentication( \User::newFromName( 'SomeUser' ),
222  $this->assertSame( [
223  [ \Psr\Log\LogLevel::ERROR, 'throttler data not found for {user}' ],
224  ], $logger->getBuffer() );
225  }
226 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:568
Config $config
Definition: MediaWiki.php:37
mergeMwGlobalArrayValue($name, $values)
Merges the given values into a MW global array variable.
testTestForAccountCreation($creatorname, $succeed, $hook)
provideTestForAccountCreation
static newFatal($message)
Factory function for fatal errors.
Definition: StatusValue.php:63
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
A pre-authentication provider to throttle authentication actions.
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:1798
A logger that may be configured to either buffer logs or to print them to the output where PHPUnit wi...
Definition: TestLogger.php:34
static singleton()
Get the global AuthManager.
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 an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing 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 unsetoffset-wrap String Wrap the message in html(usually something like"&lt
$cache
Definition: mcc.php:33
static newGood($value=null)
Factory function for good results.
Definition: StatusValue.php:76
AuthenticationRequest to ensure something with a username is present.
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 local account $user
Definition: hooks.txt:242
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
this hook is for auditing only $req
Definition: hooks.txt:965
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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:1004
static newFromObject($object)
Return the same object, without access restrictions.
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
AuthManager Database MediaWiki\Auth\ThrottlePreAuthenticationProvider.