MediaWiki  master
ConfirmLinkSecondaryAuthenticationProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Auth;
4 
10  protected function setUp() {
12 
13  parent::setUp();
14  if ( $wgDisableAuthManager ) {
15  $this->markTestSkipped( '$wgDisableAuthManager is set' );
16  }
17  }
18 
26 
27  $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
28  }
29 
30  public static function provideGetAuthenticationRequests() {
31  return [
37  ];
38  }
39 
41  $user = \User::newFromName( 'UTSysop' );
42  $obj = new \stdClass;
43 
44  $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
45  ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
46  ->getMock();
47  $mock->expects( $this->once() )->method( 'beginLinkAttempt' )
48  ->with( $this->identicalTo( $user ), $this->identicalTo( 'AuthManager::authnState' ) )
49  ->will( $this->returnValue( $obj ) );
50  $mock->expects( $this->never() )->method( 'continueLinkAttempt' );
51 
52  $this->assertSame( $obj, $mock->beginSecondaryAuthentication( $user, [] ) );
53  }
54 
56  $user = \User::newFromName( 'UTSysop' );
57  $obj = new \stdClass;
58  $reqs = [ new \stdClass ];
59 
60  $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
61  ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
62  ->getMock();
63  $mock->expects( $this->never() )->method( 'beginLinkAttempt' );
64  $mock->expects( $this->once() )->method( 'continueLinkAttempt' )
65  ->with(
66  $this->identicalTo( $user ),
67  $this->identicalTo( 'AuthManager::authnState' ),
68  $this->identicalTo( $reqs )
69  )
70  ->will( $this->returnValue( $obj ) );
71 
72  $this->assertSame( $obj, $mock->continueSecondaryAuthentication( $user, $reqs ) );
73  }
74 
76  $user = \User::newFromName( 'UTSysop' );
77  $obj = new \stdClass;
78 
79  $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
80  ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
81  ->getMock();
82  $mock->expects( $this->once() )->method( 'beginLinkAttempt' )
83  ->with( $this->identicalTo( $user ), $this->identicalTo( 'AuthManager::accountCreationState' ) )
84  ->will( $this->returnValue( $obj ) );
85  $mock->expects( $this->never() )->method( 'continueLinkAttempt' );
86 
87  $this->assertSame( $obj, $mock->beginSecondaryAccountCreation( $user, $user, [] ) );
88  }
89 
91  $user = \User::newFromName( 'UTSysop' );
92  $obj = new \stdClass;
93  $reqs = [ new \stdClass ];
94 
95  $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
96  ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
97  ->getMock();
98  $mock->expects( $this->never() )->method( 'beginLinkAttempt' );
99  $mock->expects( $this->once() )->method( 'continueLinkAttempt' )
100  ->with(
101  $this->identicalTo( $user ),
102  $this->identicalTo( 'AuthManager::accountCreationState' ),
103  $this->identicalTo( $reqs )
104  )
105  ->will( $this->returnValue( $obj ) );
106 
107  $this->assertSame( $obj, $mock->continueSecondaryAccountCreation( $user, $user, $reqs ) );
108  }
109 
114  private function getLinkRequests() {
115  $reqs = [];
116 
117  $mb = $this->getMockBuilder( AuthenticationRequest::class )
118  ->setMethods( [ 'getUniqueId' ] );
119  for ( $i = 1; $i <= 3; $i++ ) {
120  $req = $mb->getMockForAbstractClass();
121  $req->expects( $this->any() )->method( 'getUniqueId' )
122  ->will( $this->returnValue( "Request$i" ) );
123  $req->id = $i - 1;
124  $reqs[$req->getUniqueId()] = $req;
125  }
126 
127  return $reqs;
128  }
129 
130  public function testBeginLinkAttempt() {
131  $badReq = $this->getMockBuilder( AuthenticationRequest::class )
132  ->setMethods( [ 'getUniqueId' ] )
133  ->getMockForAbstractClass();
134  $badReq->expects( $this->any() )->method( 'getUniqueId' )
135  ->will( $this->returnValue( "BadReq" ) );
136 
137  $user = \User::newFromName( 'UTSysop' );
140  );
141  $request = new \FauxRequest();
142  $manager = $this->getMockBuilder( AuthManager::class )
143  ->setMethods( [ 'allowsAuthenticationDataChange' ] )
144  ->setConstructorArgs( [ $request, \RequestContext::getMain()->getConfig() ] )
145  ->getMock();
146  $manager->expects( $this->any() )->method( 'allowsAuthenticationDataChange' )
147  ->will( $this->returnCallback( function ( $req ) {
148  return $req->getUniqueId() !== 'BadReq'
150  : \StatusValue::newFatal( 'no' );
151  } ) );
152  $provider->setManager( $manager );
153 
154  $this->assertEquals(
156  $provider->beginLinkAttempt( $user, 'state' )
157  );
158 
159  $request->getSession()->setSecret( 'state', [
160  'maybeLink' => [],
161  ] );
162  $this->assertEquals(
164  $provider->beginLinkAttempt( $user, 'state' )
165  );
166 
167  $reqs = $this->getLinkRequests();
168  $request->getSession()->setSecret( 'state', [
169  'maybeLink' => $reqs + [ 'BadReq' => $badReq ]
170  ] );
171  $res = $provider->beginLinkAttempt( $user, 'state' );
172  $this->assertInstanceOf( AuthenticationResponse::class, $res );
173  $this->assertSame( AuthenticationResponse::UI, $res->status );
174  $this->assertSame( 'authprovider-confirmlink-message', $res->message->getKey() );
175  $this->assertCount( 1, $res->neededRequests );
176  $req = $res->neededRequests[0];
177  $this->assertInstanceOf( ConfirmLinkAuthenticationRequest::class, $req );
178  $expectReqs = $this->getLinkRequests();
179  foreach ( $expectReqs as $r ) {
180  $r->action = AuthManager::ACTION_CHANGE;
181  $r->username = $user->getName();
182  }
183  $this->assertEquals( $expectReqs, \TestingAccessWrapper::newFromObject( $req )->linkRequests );
184  }
185 
186  public function testContinueLinkAttempt() {
187  $user = \User::newFromName( 'UTSysop' );
188  $obj = new \stdClass;
189  $reqs = $this->getLinkRequests();
190 
191  $done = [ false, false, false ];
192 
193  // First, test the pass-through for not containing the ConfirmLinkAuthenticationRequest
194  $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
195  ->setMethods( [ 'beginLinkAttempt' ] )
196  ->getMock();
197  $mock->expects( $this->once() )->method( 'beginLinkAttempt' )
198  ->with( $this->identicalTo( $user ), $this->identicalTo( 'state' ) )
199  ->will( $this->returnValue( $obj ) );
200  $this->assertSame(
201  $obj,
202  \TestingAccessWrapper::newFromObject( $mock )->continueLinkAttempt( $user, 'state', $reqs )
203  );
204 
205  // Now test the actual functioning
206  $provider = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
207  ->setMethods( [
208  'beginLinkAttempt', 'providerAllowsAuthenticationDataChange',
209  'providerChangeAuthenticationData'
210  ] )
211  ->getMock();
212  $provider->expects( $this->never() )->method( 'beginLinkAttempt' );
213  $provider->expects( $this->any() )->method( 'providerAllowsAuthenticationDataChange' )
214  ->will( $this->returnCallback( function ( $req ) use ( $reqs ) {
215  return $req->getUniqueId() === 'Request3'
217  } ) );
218  $provider->expects( $this->any() )->method( 'providerChangeAuthenticationData' )
219  ->will( $this->returnCallback( function ( $req ) use ( &$done ) {
220  $done[$req->id] = true;
221  } ) );
222  $config = new \HashConfig( [
223  'AuthManagerConfig' => [
224  'preauth' => [],
225  'primaryauth' => [],
226  'secondaryauth' => [
227  [ 'factory' => function () use ( $provider ) {
228  return $provider;
229  } ],
230  ],
231  ],
232  ] );
233  $request = new \FauxRequest();
234  $manager = new AuthManager( $request, $config );
235  $provider->setManager( $manager );
236  $provider = \TestingAccessWrapper::newFromObject( $provider );
237 
238  $req = new ConfirmLinkAuthenticationRequest( $reqs );
239 
240  $this->assertEquals(
242  $provider->continueLinkAttempt( $user, 'state', [ $req ] )
243  );
244 
245  $request->getSession()->setSecret( 'state', [
246  'maybeLink' => [],
247  ] );
248  $this->assertEquals(
250  $provider->continueLinkAttempt( $user, 'state', [ $req ] )
251  );
252 
253  $request->getSession()->setSecret( 'state', [
254  'maybeLink' => $reqs
255  ] );
256  $this->assertEquals(
258  $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] )
259  );
260  $this->assertSame( [ false, false, false ], $done );
261 
262  $request->getSession()->setSecret( 'state', [
263  'maybeLink' => [ $reqs['Request2'] ],
264  ] );
265  $req->confirmedLinkIDs = [ 'Request1', 'Request2' ];
266  $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
267  $this->assertEquals( AuthenticationResponse::newPass(), $res );
268  $this->assertSame( [ false, true, false ], $done );
269  $done = [ false, false, false ];
270 
271  $request->getSession()->setSecret( 'state', [
272  'maybeLink' => $reqs,
273  ] );
274  $req->confirmedLinkIDs = [ 'Request1', 'Request2' ];
275  $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
276  $this->assertEquals( AuthenticationResponse::newPass(), $res );
277  $this->assertSame( [ true, true, false ], $done );
278  $done = [ false, false, false ];
279 
280  $request->getSession()->setSecret( 'state', [
281  'maybeLink' => $reqs,
282  ] );
283  $req->confirmedLinkIDs = [ 'Request1', 'Request3' ];
284  $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
285  $this->assertEquals( AuthenticationResponse::UI, $res->status );
286  $this->assertCount( 1, $res->neededRequests );
287  $this->assertInstanceOf( ButtonAuthenticationRequest::class, $res->neededRequests[0] );
288  $this->assertSame( [ true, false, false ], $done );
289  $done = [ false, false, false ];
290 
291  $res = $provider->continueLinkAttempt( $user, 'state', [ $res->neededRequests[0] ] );
292  $this->assertEquals( AuthenticationResponse::newPass(), $res );
293  $this->assertSame( [ false, false, false ], $done );
294  }
295 
296 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:522
Config $config
Definition: MediaWiki.php:37
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static newFatal($message)
Factory function for fatal errors.
Definition: StatusValue.php:63
this hook is for auditing only $response
Definition: hooks.txt:776
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static getMain()
Static methods.
$res
Definition: database.txt:21
const ACTION_CHANGE
Change a user's credentials.
Definition: AuthManager.php:60
static newGood($value=null)
Factory function for good results.
Definition: StatusValue.php:76
This serves as the entry point to the authentication system.
Definition: AuthManager.php:43
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:55
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
String $action
Cache what action this request is.
Definition: MediaWiki.php:42
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:981
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2458
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's credentials.
Definition: AuthManager.php:62
$wgDisableAuthManager
Disable AuthManager.
const ACTION_CREATE
Create a new user.
Definition: AuthManager.php:50
static newFromObject($object)
Return the same object, without access restrictions.
const ACTION_LOGIN
Log in with an existing (not necessarily local) user.
Definition: AuthManager.php:45
AuthManager MediaWiki\Auth\ConfirmLinkSecondaryAuthenticationProvider.
const UI
Indicates that the authentication needs further user input of some sort.