MediaWiki  1.28.3
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' => [ [
16  'count' => 123,
17  'seconds' => 86400,
18  ] ],
19  'PasswordAttemptThrottle' => [ [
20  'count' => 5,
21  'seconds' => 300,
22  ] ],
23  ] );
24  $provider->setConfig( $config );
25  $this->assertSame( [
26  'accountCreationThrottle' => [ [ 'count' => 123, 'seconds' => 86400 ] ],
27  'passwordAttemptThrottle' => [ [ 'count' => 5, 'seconds' => 300 ] ]
28  ], $providerPriv->throttleSettings );
29  $accountCreationThrottle = \TestingAccessWrapper::newFromObject(
30  $providerPriv->accountCreationThrottle );
31  $this->assertSame( [ [ 'count' => 123, 'seconds' => 86400 ] ],
32  $accountCreationThrottle->conditions );
33  $passwordAttemptThrottle = \TestingAccessWrapper::newFromObject(
34  $providerPriv->passwordAttemptThrottle );
35  $this->assertSame( [ [ 'count' => 5, 'seconds' => 300 ] ],
36  $passwordAttemptThrottle->conditions );
37 
38  $provider = new ThrottlePreAuthenticationProvider( [
39  'accountCreationThrottle' => [ [ 'count' => 43, 'seconds' => 10000 ] ],
40  'passwordAttemptThrottle' => [ [ 'count' => 11, 'seconds' => 100 ] ],
41  ] );
42  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
43  $config = new \HashConfig( [
44  'AccountCreationThrottle' => [ [
45  'count' => 123,
46  'seconds' => 86400,
47  ] ],
48  'PasswordAttemptThrottle' => [ [
49  'count' => 5,
50  'seconds' => 300,
51  ] ],
52  ] );
53  $provider->setConfig( $config );
54  $this->assertSame( [
55  'accountCreationThrottle' => [ [ 'count' => 43, 'seconds' => 10000 ] ],
56  'passwordAttemptThrottle' => [ [ 'count' => 11, 'seconds' => 100 ] ],
57  ], $providerPriv->throttleSettings );
58 
59  $cache = new \HashBagOStuff();
60  $provider = new ThrottlePreAuthenticationProvider( [ 'cache' => $cache ] );
61  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
62  $provider->setConfig( new \HashConfig( [
63  'AccountCreationThrottle' => [ [ 'count' => 1, 'seconds' => 1 ] ],
64  'PasswordAttemptThrottle' => [ [ 'count' => 1, 'seconds' => 1 ] ],
65  ] ) );
66  $accountCreationThrottle = \TestingAccessWrapper::newFromObject(
67  $providerPriv->accountCreationThrottle );
68  $this->assertSame( $cache, $accountCreationThrottle->cache );
69  $passwordAttemptThrottle = \TestingAccessWrapper::newFromObject(
70  $providerPriv->passwordAttemptThrottle );
71  $this->assertSame( $cache, $passwordAttemptThrottle->cache );
72  }
73 
74  public function testDisabled() {
75  $provider = new ThrottlePreAuthenticationProvider( [
76  'accountCreationThrottle' => [],
77  'passwordAttemptThrottle' => [],
78  'cache' => new \HashBagOStuff(),
79  ] );
80  $provider->setLogger( new \Psr\Log\NullLogger() );
81  $provider->setConfig( new \HashConfig( [
82  'AccountCreationThrottle' => null,
83  'PasswordAttemptThrottle' => null,
84  ] ) );
85  $provider->setManager( AuthManager::singleton() );
86 
87  $this->assertEquals(
89  $provider->testForAccountCreation(
90  \User::newFromName( 'Created' ),
91  \User::newFromName( 'Creator' ),
92  []
93  )
94  );
95  $this->assertEquals(
97  $provider->testForAuthentication( [] )
98  );
99  }
100 
107  public function testTestForAccountCreation( $creatorname, $succeed, $hook ) {
108  $provider = new ThrottlePreAuthenticationProvider( [
109  'accountCreationThrottle' => [ [ 'count' => 2, 'seconds' => 86400 ] ],
110  'cache' => new \HashBagOStuff(),
111  ] );
112  $provider->setLogger( new \Psr\Log\NullLogger() );
113  $provider->setConfig( new \HashConfig( [
114  'AccountCreationThrottle' => null,
115  'PasswordAttemptThrottle' => null,
116  ] ) );
117  $provider->setManager( AuthManager::singleton() );
118 
119  $user = \User::newFromName( 'RandomUser' );
120  $creator = \User::newFromName( $creatorname );
121  if ( $hook ) {
122  $mock = $this->getMock( 'stdClass', [ 'onExemptFromAccountCreationThrottle' ] );
123  $mock->expects( $this->any() )->method( 'onExemptFromAccountCreationThrottle' )
124  ->will( $this->returnValue( false ) );
125  $this->mergeMwGlobalArrayValue( 'wgHooks', [
126  'ExemptFromAccountCreationThrottle' => [ $mock ],
127  ] );
128  }
129 
130  $this->assertEquals(
131  true,
132  $provider->testForAccountCreation( $user, $creator, [] )->isOK(),
133  'attempt #1'
134  );
135  $this->assertEquals(
136  true,
137  $provider->testForAccountCreation( $user, $creator, [] )->isOK(),
138  'attempt #2'
139  );
140  $this->assertEquals(
141  $succeed ? true : false,
142  $provider->testForAccountCreation( $user, $creator, [] )->isOK(),
143  'attempt #3'
144  );
145  }
146 
147  public static function provideTestForAccountCreation() {
148  return [
149  'Normal user' => [ 'NormalUser', false, false ],
150  'Sysop' => [ 'UTSysop', true, false ],
151  'Normal user with hook' => [ 'NormalUser', true, true ],
152  ];
153  }
154 
155  public function testTestForAuthentication() {
156  $provider = new ThrottlePreAuthenticationProvider( [
157  'passwordAttemptThrottle' => [ [ 'count' => 2, 'seconds' => 86400 ] ],
158  'cache' => new \HashBagOStuff(),
159  ] );
160  $provider->setLogger( new \Psr\Log\NullLogger() );
161  $provider->setConfig( new \HashConfig( [
162  'AccountCreationThrottle' => null,
163  'PasswordAttemptThrottle' => null,
164  ] ) );
165  $provider->setManager( AuthManager::singleton() );
166 
168  $req->username = 'SomeUser';
169  for ( $i = 1; $i <= 3; $i++ ) {
170  $status = $provider->testForAuthentication( [ $req ] );
171  $this->assertEquals( $i < 3, $status->isGood(), "attempt #$i" );
172  }
173  $this->assertCount( 1, $status->getErrors() );
174  $msg = new \Message( $status->getErrors()[0]['message'], $status->getErrors()[0]['params'] );
175  $this->assertEquals( 'login-throttled', $msg->getKey() );
176 
177  $provider->postAuthentication( \User::newFromName( 'SomeUser' ),
179  $this->assertFalse( $provider->testForAuthentication( [ $req ] )->isGood(), 'after FAIL' );
180 
181  $provider->postAuthentication( \User::newFromName( 'SomeUser' ),
183  $this->assertTrue( $provider->testForAuthentication( [ $req ] )->isGood(), 'after PASS' );
184 
185  $req1 = new UsernameAuthenticationRequest;
186  $req1->username = 'foo';
187  $req2 = new UsernameAuthenticationRequest;
188  $req2->username = 'bar';
189  $this->assertTrue( $provider->testForAuthentication( [ $req1, $req2 ] )->isGood() );
190 
192  $req->username = 'Some user';
193  $provider->testForAuthentication( [ $req ] );
194  $req->username = 'Some_user';
195  $provider->testForAuthentication( [ $req ] );
196  $req->username = 'some user';
197  $status = $provider->testForAuthentication( [ $req ] );
198  $this->assertFalse( $status->isGood(), 'denormalized usernames are normalized' );
199  }
200 
201  public function testPostAuthentication() {
202  $provider = new ThrottlePreAuthenticationProvider( [
203  'passwordAttemptThrottle' => [],
204  'cache' => new \HashBagOStuff(),
205  ] );
206  $provider->setLogger( new \TestLogger );
207  $provider->setConfig( new \HashConfig( [
208  'AccountCreationThrottle' => null,
209  'PasswordAttemptThrottle' => null,
210  ] ) );
211  $provider->setManager( AuthManager::singleton() );
212  $provider->postAuthentication( \User::newFromName( 'SomeUser' ),
214 
215  $provider = new ThrottlePreAuthenticationProvider( [
216  'passwordAttemptThrottle' => [ [ 'count' => 2, 'seconds' => 86400 ] ],
217  'cache' => new \HashBagOStuff(),
218  ] );
219  $logger = new \TestLogger( true );
220  $provider->setLogger( $logger );
221  $provider->setConfig( new \HashConfig( [
222  'AccountCreationThrottle' => null,
223  'PasswordAttemptThrottle' => null,
224  ] ) );
225  $provider->setManager( AuthManager::singleton() );
226  $provider->postAuthentication( \User::newFromName( 'SomeUser' ),
228  $this->assertSame( [
229  [ \Psr\Log\LogLevel::ERROR, 'throttler data not found for {user}' ],
230  ], $logger->getBuffer() );
231  }
232 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:525
Config $config
Definition: MediaWiki.php:38
mergeMwGlobalArrayValue($name, $values)
Merges the given values into a MW global array variable.
testTestForAccountCreation($creatorname, $succeed, $hook)
provideTestForAccountCreation
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:1940
A logger that may be configured to either buffer logs or to print them to the output where PHPUnit wi...
Definition: TestLogger.php:34
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 unsetoffset-wrap String Wrap the message in html(usually something like"&lt
static singleton()
Get the global AuthManager.
$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:246
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:1011
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:1050
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.