MediaWiki  1.27.2
ConfirmLinkSecondaryAuthenticationProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Auth;
4 
17 
18  $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
19  }
20 
21  public static function provideGetAuthenticationRequests() {
22  return [
28  ];
29  }
30 
32  $user = \User::newFromName( 'UTSysop' );
33  $obj = new \stdClass;
34 
35  $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
36  ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
37  ->getMock();
38  $mock->expects( $this->once() )->method( 'beginLinkAttempt' )
39  ->with( $this->identicalTo( $user ), $this->identicalTo( 'AuthManager::authnState' ) )
40  ->will( $this->returnValue( $obj ) );
41  $mock->expects( $this->never() )->method( 'continueLinkAttempt' );
42 
43  $this->assertSame( $obj, $mock->beginSecondaryAuthentication( $user, [] ) );
44  }
45 
47  $user = \User::newFromName( 'UTSysop' );
48  $obj = new \stdClass;
49  $reqs = [ new \stdClass ];
50 
51  $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
52  ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
53  ->getMock();
54  $mock->expects( $this->never() )->method( 'beginLinkAttempt' );
55  $mock->expects( $this->once() )->method( 'continueLinkAttempt' )
56  ->with(
57  $this->identicalTo( $user ),
58  $this->identicalTo( 'AuthManager::authnState' ),
59  $this->identicalTo( $reqs )
60  )
61  ->will( $this->returnValue( $obj ) );
62 
63  $this->assertSame( $obj, $mock->continueSecondaryAuthentication( $user, $reqs ) );
64  }
65 
67  $user = \User::newFromName( 'UTSysop' );
68  $obj = new \stdClass;
69 
70  $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
71  ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
72  ->getMock();
73  $mock->expects( $this->once() )->method( 'beginLinkAttempt' )
74  ->with( $this->identicalTo( $user ), $this->identicalTo( 'AuthManager::accountCreationState' ) )
75  ->will( $this->returnValue( $obj ) );
76  $mock->expects( $this->never() )->method( 'continueLinkAttempt' );
77 
78  $this->assertSame( $obj, $mock->beginSecondaryAccountCreation( $user, $user, [] ) );
79  }
80 
82  $user = \User::newFromName( 'UTSysop' );
83  $obj = new \stdClass;
84  $reqs = [ new \stdClass ];
85 
86  $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
87  ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
88  ->getMock();
89  $mock->expects( $this->never() )->method( 'beginLinkAttempt' );
90  $mock->expects( $this->once() )->method( 'continueLinkAttempt' )
91  ->with(
92  $this->identicalTo( $user ),
93  $this->identicalTo( 'AuthManager::accountCreationState' ),
94  $this->identicalTo( $reqs )
95  )
96  ->will( $this->returnValue( $obj ) );
97 
98  $this->assertSame( $obj, $mock->continueSecondaryAccountCreation( $user, $user, $reqs ) );
99  }
100 
105  private function getLinkRequests() {
106  $reqs = [];
107 
108  $mb = $this->getMockBuilder( AuthenticationRequest::class )
109  ->setMethods( [ 'getUniqueId' ] );
110  for ( $i = 1; $i <= 3; $i++ ) {
111  $req = $mb->getMockForAbstractClass();
112  $req->expects( $this->any() )->method( 'getUniqueId' )
113  ->will( $this->returnValue( "Request$i" ) );
114  $req->id = $i - 1;
115  $reqs[$req->getUniqueId()] = $req;
116  }
117 
118  return $reqs;
119  }
120 
121  public function testBeginLinkAttempt() {
122  $badReq = $this->getMockBuilder( AuthenticationRequest::class )
123  ->setMethods( [ 'getUniqueId' ] )
124  ->getMockForAbstractClass();
125  $badReq->expects( $this->any() )->method( 'getUniqueId' )
126  ->will( $this->returnValue( "BadReq" ) );
127 
128  $user = \User::newFromName( 'UTSysop' );
131  );
132  $request = new \FauxRequest();
133  $manager = $this->getMockBuilder( AuthManager::class )
134  ->setMethods( [ 'allowsAuthenticationDataChange' ] )
135  ->setConstructorArgs( [ $request, \RequestContext::getMain()->getConfig() ] )
136  ->getMock();
137  $manager->expects( $this->any() )->method( 'allowsAuthenticationDataChange' )
138  ->will( $this->returnCallback( function ( $req ) {
139  return $req->getUniqueId() !== 'BadReq'
141  : \StatusValue::newFatal( 'no' );
142  } ) );
143  $provider->setManager( $manager );
144 
145  $this->assertEquals(
147  $provider->beginLinkAttempt( $user, 'state' )
148  );
149 
150  $request->getSession()->setSecret( 'state', [
151  'maybeLink' => [],
152  ] );
153  $this->assertEquals(
155  $provider->beginLinkAttempt( $user, 'state' )
156  );
157 
158  $reqs = $this->getLinkRequests();
159  $request->getSession()->setSecret( 'state', [
160  'maybeLink' => $reqs + [ 'BadReq' => $badReq ]
161  ] );
162  $res = $provider->beginLinkAttempt( $user, 'state' );
163  $this->assertInstanceOf( AuthenticationResponse::class, $res );
164  $this->assertSame( AuthenticationResponse::UI, $res->status );
165  $this->assertSame( 'authprovider-confirmlink-message', $res->message->getKey() );
166  $this->assertCount( 1, $res->neededRequests );
167  $req = $res->neededRequests[0];
168  $this->assertInstanceOf( ConfirmLinkAuthenticationRequest::class, $req );
169  $expectReqs = $this->getLinkRequests();
170  foreach ( $expectReqs as $r ) {
171  $r->action = AuthManager::ACTION_CHANGE;
172  $r->username = $user->getName();
173  }
174  $this->assertEquals( $expectReqs, \TestingAccessWrapper::newFromObject( $req )->linkRequests );
175  }
176 
177  public function testContinueLinkAttempt() {
178  $user = \User::newFromName( 'UTSysop' );
179  $obj = new \stdClass;
180  $reqs = $this->getLinkRequests();
181 
182  $done = [ false, false, false ];
183 
184  // First, test the pass-through for not containing the ConfirmLinkAuthenticationRequest
185  $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
186  ->setMethods( [ 'beginLinkAttempt' ] )
187  ->getMock();
188  $mock->expects( $this->once() )->method( 'beginLinkAttempt' )
189  ->with( $this->identicalTo( $user ), $this->identicalTo( 'state' ) )
190  ->will( $this->returnValue( $obj ) );
191  $this->assertSame(
192  $obj,
193  \TestingAccessWrapper::newFromObject( $mock )->continueLinkAttempt( $user, 'state', $reqs )
194  );
195 
196  // Now test the actual functioning
197  $provider = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
198  ->setMethods( [
199  'beginLinkAttempt', 'providerAllowsAuthenticationDataChange',
200  'providerChangeAuthenticationData'
201  ] )
202  ->getMock();
203  $provider->expects( $this->never() )->method( 'beginLinkAttempt' );
204  $provider->expects( $this->any() )->method( 'providerAllowsAuthenticationDataChange' )
205  ->will( $this->returnCallback( function ( $req ) use ( $reqs ) {
206  return $req->getUniqueId() === 'Request3'
208  } ) );
209  $provider->expects( $this->any() )->method( 'providerChangeAuthenticationData' )
210  ->will( $this->returnCallback( function ( $req ) use ( &$done ) {
211  $done[$req->id] = true;
212  } ) );
213  $config = new \HashConfig( [
214  'AuthManagerConfig' => [
215  'preauth' => [],
216  'primaryauth' => [],
217  'secondaryauth' => [
218  [ 'factory' => function () use ( $provider ) {
219  return $provider;
220  } ],
221  ],
222  ],
223  ] );
224  $request = new \FauxRequest();
225  $manager = new AuthManager( $request, $config );
226  $provider->setManager( $manager );
227  $provider = \TestingAccessWrapper::newFromObject( $provider );
228 
229  $req = new ConfirmLinkAuthenticationRequest( $reqs );
230 
231  $this->assertEquals(
233  $provider->continueLinkAttempt( $user, 'state', [ $req ] )
234  );
235 
236  $request->getSession()->setSecret( 'state', [
237  'maybeLink' => [],
238  ] );
239  $this->assertEquals(
241  $provider->continueLinkAttempt( $user, 'state', [ $req ] )
242  );
243 
244  $request->getSession()->setSecret( 'state', [
245  'maybeLink' => $reqs
246  ] );
247  $this->assertEquals(
249  $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] )
250  );
251  $this->assertSame( [ false, false, false ], $done );
252 
253  $request->getSession()->setSecret( 'state', [
254  'maybeLink' => [ $reqs['Request2'] ],
255  ] );
256  $req->confirmedLinkIDs = [ 'Request1', 'Request2' ];
257  $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
258  $this->assertEquals( AuthenticationResponse::newPass(), $res );
259  $this->assertSame( [ false, true, false ], $done );
260  $done = [ false, false, false ];
261 
262  $request->getSession()->setSecret( 'state', [
263  'maybeLink' => $reqs,
264  ] );
265  $req->confirmedLinkIDs = [ 'Request1', 'Request2' ];
266  $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
267  $this->assertEquals( AuthenticationResponse::newPass(), $res );
268  $this->assertSame( [ true, true, false ], $done );
269  $done = [ false, false, false ];
270 
271  $request->getSession()->setSecret( 'state', [
272  'maybeLink' => $reqs,
273  ] );
274  $req->confirmedLinkIDs = [ 'Request1', 'Request3' ];
275  $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
276  $this->assertEquals( AuthenticationResponse::UI, $res->status );
277  $this->assertCount( 1, $res->neededRequests );
278  $this->assertInstanceOf( ButtonAuthenticationRequest::class, $res->neededRequests[0] );
279  $this->assertSame( [ true, false, false ], $done );
280  $done = [ false, false, false ];
281 
282  $res = $provider->continueLinkAttempt( $user, 'state', [ $res->neededRequests[0] ] );
283  $this->assertEquals( AuthenticationResponse::newPass(), $res );
284  $this->assertSame( [ false, false, false ], $done );
285  }
286 
287 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:568
Config $config
Definition: MediaWiki.php:37
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
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
this hook is for auditing only $response
Definition: hooks.txt:762
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:965
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2418
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
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.