MediaWiki  master
ResetPasswordSecondaryAuthenticationProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Auth;
4 
6 
19 
20  $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
21  }
22 
23  public static function provideGetAuthenticationRequests() {
24  return [
30  ];
31  }
32 
33  public function testBasics() {
34  $user = \User::newFromName( 'UTSysop' );
35  $user2 = new \User;
36  $obj = new \stdClass;
37  $reqs = [ new \stdClass ];
38 
39  $mb = $this->getMockBuilder( ResetPasswordSecondaryAuthenticationProvider::class )
40  ->setMethods( [ 'tryReset' ] );
41 
42  $methods = [
43  'beginSecondaryAuthentication' => [ $user, $reqs ],
44  'continueSecondaryAuthentication' => [ $user, $reqs ],
45  'beginSecondaryAccountCreation' => [ $user, $user2, $reqs ],
46  'continueSecondaryAccountCreation' => [ $user, $user2, $reqs ],
47  ];
48  foreach ( $methods as $method => $args ) {
49  $mock = $mb->getMock();
50  $mock->expects( $this->once() )->method( 'tryReset' )
51  ->with( $this->identicalTo( $user ), $this->identicalTo( $reqs ) )
52  ->will( $this->returnValue( $obj ) );
53  $this->assertSame( $obj, call_user_func_array( [ $mock, $method ], $args ) );
54  }
55  }
56 
57  public function testTryReset() {
58  $user = \User::newFromName( 'UTSysop' );
59 
60  $provider = $this->getMockBuilder(
62  )
63  ->setMethods( [
64  'providerAllowsAuthenticationDataChange', 'providerChangeAuthenticationData'
65  ] )
66  ->getMock();
67  $provider->expects( $this->any() )->method( 'providerAllowsAuthenticationDataChange' )
68  ->will( $this->returnCallback( function ( $req ) {
69  $this->assertSame( 'UTSysop', $req->username );
70  return $req->allow;
71  } ) );
72  $provider->expects( $this->any() )->method( 'providerChangeAuthenticationData' )
73  ->will( $this->returnCallback( function ( $req ) {
74  $this->assertSame( 'UTSysop', $req->username );
75  $req->done = true;
76  } ) );
77  $config = new \HashConfig( [
78  'AuthManagerConfig' => [
79  'preauth' => [],
80  'primaryauth' => [],
81  'secondaryauth' => [
82  [ 'factory' => function () use ( $provider ) {
83  return $provider;
84  } ],
85  ],
86  ],
87  ] );
88  $manager = new AuthManager( new \FauxRequest, $config );
89  $provider->setManager( $manager );
90  $provider = TestingAccessWrapper::newFromObject( $provider );
91 
92  $msg = wfMessage( 'foo' );
93  $skipReq = new ButtonAuthenticationRequest(
94  'skipReset',
95  wfMessage( 'authprovider-resetpass-skip-label' ),
96  wfMessage( 'authprovider-resetpass-skip-help' )
97  );
98  $passReq = new PasswordAuthenticationRequest();
99  $passReq->action = AuthManager::ACTION_CHANGE;
100  $passReq->password = 'Foo';
101  $passReq->retype = 'Bar';
102  $passReq->allow = \StatusValue::newGood();
103  $passReq->done = false;
104 
105  $passReq2 = $this->getMockBuilder( PasswordAuthenticationRequest::class )
106  ->enableProxyingToOriginalMethods()
107  ->getMock();
108  $passReq2->action = AuthManager::ACTION_CHANGE;
109  $passReq2->password = 'Foo';
110  $passReq2->retype = 'Foo';
111  $passReq2->allow = \StatusValue::newGood();
112  $passReq2->done = false;
113 
114  $passReq3 = new PasswordAuthenticationRequest();
115  $passReq3->action = AuthManager::ACTION_LOGIN;
116  $passReq3->password = 'Foo';
117  $passReq3->retype = 'Foo';
118  $passReq3->allow = \StatusValue::newGood();
119  $passReq3->done = false;
120 
121  $this->assertEquals(
123  $provider->tryReset( $user, [] )
124  );
125 
126  $manager->setAuthenticationSessionData( 'reset-pass', 'foo' );
127  try {
128  $provider->tryReset( $user, [] );
129  $this->fail( 'Expected exception not thrown' );
130  } catch ( \UnexpectedValueException $ex ) {
131  $this->assertSame( 'reset-pass is not valid', $ex->getMessage() );
132  }
133 
134  $manager->setAuthenticationSessionData( 'reset-pass', (object)[] );
135  try {
136  $provider->tryReset( $user, [] );
137  $this->fail( 'Expected exception not thrown' );
138  } catch ( \UnexpectedValueException $ex ) {
139  $this->assertSame( 'reset-pass msg is missing', $ex->getMessage() );
140  }
141 
142  $manager->setAuthenticationSessionData( 'reset-pass', [
143  'msg' => 'foo',
144  ] );
145  try {
146  $provider->tryReset( $user, [] );
147  $this->fail( 'Expected exception not thrown' );
148  } catch ( \UnexpectedValueException $ex ) {
149  $this->assertSame( 'reset-pass msg is not valid', $ex->getMessage() );
150  }
151 
152  $manager->setAuthenticationSessionData( 'reset-pass', [
153  'msg' => $msg,
154  ] );
155  try {
156  $provider->tryReset( $user, [] );
157  $this->fail( 'Expected exception not thrown' );
158  } catch ( \UnexpectedValueException $ex ) {
159  $this->assertSame( 'reset-pass hard is missing', $ex->getMessage() );
160  }
161 
162  $manager->setAuthenticationSessionData( 'reset-pass', [
163  'msg' => $msg,
164  'hard' => true,
165  'req' => 'foo',
166  ] );
167  try {
168  $provider->tryReset( $user, [] );
169  $this->fail( 'Expected exception not thrown' );
170  } catch ( \UnexpectedValueException $ex ) {
171  $this->assertSame( 'reset-pass req is not valid', $ex->getMessage() );
172  }
173 
174  $manager->setAuthenticationSessionData( 'reset-pass', [
175  'msg' => $msg,
176  'hard' => false,
177  'req' => $passReq3,
178  ] );
179  try {
180  $provider->tryReset( $user, [ $passReq ] );
181  $this->fail( 'Expected exception not thrown' );
182  } catch ( \UnexpectedValueException $ex ) {
183  $this->assertSame( 'reset-pass req is not valid', $ex->getMessage() );
184  }
185 
186  $manager->setAuthenticationSessionData( 'reset-pass', [
187  'msg' => $msg,
188  'hard' => true,
189  ] );
190  $res = $provider->tryReset( $user, [] );
191  $this->assertInstanceOf( AuthenticationResponse::class, $res );
192  $this->assertSame( AuthenticationResponse::UI, $res->status );
193  $this->assertEquals( $msg, $res->message );
194  $this->assertCount( 1, $res->neededRequests );
195  $this->assertInstanceOf(
197  $res->neededRequests[0]
198  );
199  $this->assertNotNull( $manager->getAuthenticationSessionData( 'reset-pass' ) );
200  $this->assertFalse( $passReq->done );
201 
202  $manager->setAuthenticationSessionData( 'reset-pass', [
203  'msg' => $msg,
204  'hard' => false,
205  'req' => $passReq,
206  ] );
207  $res = $provider->tryReset( $user, [] );
208  $this->assertInstanceOf( AuthenticationResponse::class, $res );
209  $this->assertSame( AuthenticationResponse::UI, $res->status );
210  $this->assertEquals( $msg, $res->message );
211  $this->assertCount( 2, $res->neededRequests );
212  $expectedPassReq = clone $passReq;
213  $expectedPassReq->required = AuthenticationRequest::OPTIONAL;
214  $this->assertEquals( $expectedPassReq, $res->neededRequests[0] );
215  $this->assertEquals( $skipReq, $res->neededRequests[1] );
216  $this->assertNotNull( $manager->getAuthenticationSessionData( 'reset-pass' ) );
217  $this->assertFalse( $passReq->done );
218 
219  $passReq->retype = 'Bad';
220  $manager->setAuthenticationSessionData( 'reset-pass', [
221  'msg' => $msg,
222  'hard' => false,
223  'req' => $passReq,
224  ] );
225  $res = $provider->tryReset( $user, [ $skipReq, $passReq ] );
226  $this->assertEquals( AuthenticationResponse::newPass(), $res );
227  $this->assertNull( $manager->getAuthenticationSessionData( 'reset-pass' ) );
228  $this->assertFalse( $passReq->done );
229 
230  $passReq->retype = 'Bad';
231  $manager->setAuthenticationSessionData( 'reset-pass', [
232  'msg' => $msg,
233  'hard' => true,
234  ] );
235  $res = $provider->tryReset( $user, [ $skipReq, $passReq ] );
236  $this->assertSame( AuthenticationResponse::UI, $res->status );
237  $this->assertSame( 'badretype', $res->message->getKey() );
238  $this->assertCount( 1, $res->neededRequests );
239  $this->assertInstanceOf(
241  $res->neededRequests[0]
242  );
243  $this->assertNotNull( $manager->getAuthenticationSessionData( 'reset-pass' ) );
244  $this->assertFalse( $passReq->done );
245 
246  $manager->setAuthenticationSessionData( 'reset-pass', [
247  'msg' => $msg,
248  'hard' => true,
249  ] );
250  $res = $provider->tryReset( $user, [ $skipReq, $passReq3 ] );
251  $this->assertSame( AuthenticationResponse::UI, $res->status );
252  $this->assertEquals( $msg, $res->message );
253  $this->assertCount( 1, $res->neededRequests );
254  $this->assertInstanceOf(
256  $res->neededRequests[0]
257  );
258  $this->assertNotNull( $manager->getAuthenticationSessionData( 'reset-pass' ) );
259  $this->assertFalse( $passReq->done );
260 
261  $passReq->retype = $passReq->password;
262  $passReq->allow = \StatusValue::newFatal( 'arbitrary-fail' );
263  $res = $provider->tryReset( $user, [ $skipReq, $passReq ] );
264  $this->assertSame( AuthenticationResponse::UI, $res->status );
265  $this->assertSame( 'arbitrary-fail', $res->message->getKey() );
266  $this->assertCount( 1, $res->neededRequests );
267  $this->assertInstanceOf(
269  $res->neededRequests[0]
270  );
271  $this->assertNotNull( $manager->getAuthenticationSessionData( 'reset-pass' ) );
272  $this->assertFalse( $passReq->done );
273 
274  $passReq->allow = \StatusValue::newGood();
275  $res = $provider->tryReset( $user, [ $skipReq, $passReq ] );
276  $this->assertEquals( AuthenticationResponse::newPass(), $res );
277  $this->assertNull( $manager->getAuthenticationSessionData( 'reset-pass' ) );
278  $this->assertTrue( $passReq->done );
279 
280  $manager->setAuthenticationSessionData( 'reset-pass', [
281  'msg' => $msg,
282  'hard' => false,
283  'req' => $passReq2,
284  ] );
285  $res = $provider->tryReset( $user, [ $passReq2 ] );
286  $this->assertEquals( AuthenticationResponse::newPass(), $res );
287  $this->assertNull( $manager->getAuthenticationSessionData( 'reset-pass' ) );
288  $this->assertTrue( $passReq2->done );
289 
290  $passReq->done = false;
291  $passReq2->done = false;
292  $manager->setAuthenticationSessionData( 'reset-pass', [
293  'msg' => $msg,
294  'hard' => false,
295  'req' => $passReq2,
296  ] );
297  $res = $provider->tryReset( $user, [ $passReq ] );
298  $this->assertInstanceOf( AuthenticationResponse::class, $res );
299  $this->assertSame( AuthenticationResponse::UI, $res->status );
300  $this->assertEquals( $msg, $res->message );
301  $this->assertCount( 2, $res->neededRequests );
302  $expectedPassReq = clone $passReq2;
303  $expectedPassReq->required = AuthenticationRequest::OPTIONAL;
304  $this->assertEquals( $expectedPassReq, $res->neededRequests[0] );
305  $this->assertEquals( $skipReq, $res->neededRequests[1] );
306  $this->assertNotNull( $manager->getAuthenticationSessionData( 'reset-pass' ) );
307  $this->assertFalse( $passReq->done );
308  $this->assertFalse( $passReq2->done );
309  }
310 }
Config $config
Definition: MediaWiki.php:43
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
string $action
Cache what action this request is.
Definition: MediaWiki.php:48
this hook is for auditing only $response
Definition: hooks.txt:785
if( $line===false) $args
Definition: cdb.php:64
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:68
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
$res
Definition: database.txt:21
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
const ACTION_CHANGE
Change a user&#39;s credentials.
const OPTIONAL
Indicates that the request is not required for authentication to proceed.
This serves as the entry point to the authentication system.
Definition: AuthManager.php:83
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
const ACTION_LINK
Link an existing user to a third-party account.
Definition: AuthManager.php:95
This is a value object for authentication requests with a username and password.
AuthManager \MediaWiki\Auth\ResetPasswordSecondaryAuthenticationProvider.
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:990
Reset the local password, if signalled via $this->manager->setAuthenticationSessionData() ...
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
const ACTION_REMOVE
Remove a user&#39;s credentials.
const ACTION_CREATE
Create a new user.
Definition: AuthManager.php:90
This is an authentication request that just implements a simple button.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:585
const ACTION_LOGIN
Log in with an existing (not necessarily local) user.
Definition: AuthManager.php:85
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1487
const UI
Indicates that the authentication needs further user input of some sort.