MediaWiki REL1_31
ConfirmLinkSecondaryAuthenticationProviderTest.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Auth;
4
5use Wikimedia\TestingAccessWrapper;
6
17 public function testGetAuthenticationRequests( $action, $response ) {
19
20 $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
21 }
22
23 public static function provideGetAuthenticationRequests() {
24 return [
30 ];
31 }
32
34 $user = \User::newFromName( 'UTSysop' );
35 $obj = new \stdClass;
36
37 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
38 ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
39 ->getMock();
40 $mock->expects( $this->once() )->method( 'beginLinkAttempt' )
41 ->with( $this->identicalTo( $user ), $this->identicalTo( 'AuthManager::authnState' ) )
42 ->will( $this->returnValue( $obj ) );
43 $mock->expects( $this->never() )->method( 'continueLinkAttempt' );
44
45 $this->assertSame( $obj, $mock->beginSecondaryAuthentication( $user, [] ) );
46 }
47
49 $user = \User::newFromName( 'UTSysop' );
50 $obj = new \stdClass;
51 $reqs = [ new \stdClass ];
52
53 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
54 ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
55 ->getMock();
56 $mock->expects( $this->never() )->method( 'beginLinkAttempt' );
57 $mock->expects( $this->once() )->method( 'continueLinkAttempt' )
58 ->with(
59 $this->identicalTo( $user ),
60 $this->identicalTo( 'AuthManager::authnState' ),
61 $this->identicalTo( $reqs )
62 )
63 ->will( $this->returnValue( $obj ) );
64
65 $this->assertSame( $obj, $mock->continueSecondaryAuthentication( $user, $reqs ) );
66 }
67
69 $user = \User::newFromName( 'UTSysop' );
70 $obj = new \stdClass;
71
72 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
73 ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
74 ->getMock();
75 $mock->expects( $this->once() )->method( 'beginLinkAttempt' )
76 ->with( $this->identicalTo( $user ), $this->identicalTo( 'AuthManager::accountCreationState' ) )
77 ->will( $this->returnValue( $obj ) );
78 $mock->expects( $this->never() )->method( 'continueLinkAttempt' );
79
80 $this->assertSame( $obj, $mock->beginSecondaryAccountCreation( $user, $user, [] ) );
81 }
82
84 $user = \User::newFromName( 'UTSysop' );
85 $obj = new \stdClass;
86 $reqs = [ new \stdClass ];
87
88 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
89 ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
90 ->getMock();
91 $mock->expects( $this->never() )->method( 'beginLinkAttempt' );
92 $mock->expects( $this->once() )->method( 'continueLinkAttempt' )
93 ->with(
94 $this->identicalTo( $user ),
95 $this->identicalTo( 'AuthManager::accountCreationState' ),
96 $this->identicalTo( $reqs )
97 )
98 ->will( $this->returnValue( $obj ) );
99
100 $this->assertSame( $obj, $mock->continueSecondaryAccountCreation( $user, $user, $reqs ) );
101 }
102
107 private function getLinkRequests() {
108 $reqs = [];
109
110 $mb = $this->getMockBuilder( AuthenticationRequest::class )
111 ->setMethods( [ 'getUniqueId' ] );
112 for ( $i = 1; $i <= 3; $i++ ) {
113 $req = $mb->getMockForAbstractClass();
114 $req->expects( $this->any() )->method( 'getUniqueId' )
115 ->will( $this->returnValue( "Request$i" ) );
116 $req->id = $i - 1;
117 $reqs[$req->getUniqueId()] = $req;
118 }
119
120 return $reqs;
121 }
122
123 public function testBeginLinkAttempt() {
124 $badReq = $this->getMockBuilder( AuthenticationRequest::class )
125 ->setMethods( [ 'getUniqueId' ] )
126 ->getMockForAbstractClass();
127 $badReq->expects( $this->any() )->method( 'getUniqueId' )
128 ->will( $this->returnValue( "BadReq" ) );
129
130 $user = \User::newFromName( 'UTSysop' );
131 $provider = TestingAccessWrapper::newFromObject(
133 );
134 $request = new \FauxRequest();
135 $manager = $this->getMockBuilder( AuthManager::class )
136 ->setMethods( [ 'allowsAuthenticationDataChange' ] )
137 ->setConstructorArgs( [ $request, \RequestContext::getMain()->getConfig() ] )
138 ->getMock();
139 $manager->expects( $this->any() )->method( 'allowsAuthenticationDataChange' )
140 ->will( $this->returnCallback( function ( $req ) {
141 return $req->getUniqueId() !== 'BadReq'
142 ? \StatusValue::newGood()
143 : \StatusValue::newFatal( 'no' );
144 } ) );
145 $provider->setManager( $manager );
146
147 $this->assertEquals(
149 $provider->beginLinkAttempt( $user, 'state' )
150 );
151
152 $request->getSession()->setSecret( 'state', [
153 'maybeLink' => [],
154 ] );
155 $this->assertEquals(
157 $provider->beginLinkAttempt( $user, 'state' )
158 );
159
160 $reqs = $this->getLinkRequests();
161 $request->getSession()->setSecret( 'state', [
162 'maybeLink' => $reqs + [ 'BadReq' => $badReq ]
163 ] );
164 $res = $provider->beginLinkAttempt( $user, 'state' );
165 $this->assertInstanceOf( AuthenticationResponse::class, $res );
166 $this->assertSame( AuthenticationResponse::UI, $res->status );
167 $this->assertSame( 'authprovider-confirmlink-message', $res->message->getKey() );
168 $this->assertCount( 1, $res->neededRequests );
169 $req = $res->neededRequests[0];
170 $this->assertInstanceOf( ConfirmLinkAuthenticationRequest::class, $req );
171 $expectReqs = $this->getLinkRequests();
172 foreach ( $expectReqs as $r ) {
173 $r->action = AuthManager::ACTION_CHANGE;
174 $r->username = $user->getName();
175 }
176 $this->assertEquals( $expectReqs, TestingAccessWrapper::newFromObject( $req )->linkRequests );
177 }
178
179 public function testContinueLinkAttempt() {
180 $user = \User::newFromName( 'UTSysop' );
181 $obj = new \stdClass;
182 $reqs = $this->getLinkRequests();
183
184 $done = [ false, false, false ];
185
186 // First, test the pass-through for not containing the ConfirmLinkAuthenticationRequest
187 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
188 ->setMethods( [ 'beginLinkAttempt' ] )
189 ->getMock();
190 $mock->expects( $this->once() )->method( 'beginLinkAttempt' )
191 ->with( $this->identicalTo( $user ), $this->identicalTo( 'state' ) )
192 ->will( $this->returnValue( $obj ) );
193 $this->assertSame(
194 $obj,
195 TestingAccessWrapper::newFromObject( $mock )->continueLinkAttempt( $user, 'state', $reqs )
196 );
197
198 // Now test the actual functioning
199 $provider = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
200 ->setMethods( [
201 'beginLinkAttempt', 'providerAllowsAuthenticationDataChange',
202 'providerChangeAuthenticationData'
203 ] )
204 ->getMock();
205 $provider->expects( $this->never() )->method( 'beginLinkAttempt' );
206 $provider->expects( $this->any() )->method( 'providerAllowsAuthenticationDataChange' )
207 ->will( $this->returnCallback( function ( $req ) use ( $reqs ) {
208 return $req->getUniqueId() === 'Request3'
209 ? \StatusValue::newFatal( 'foo' ) : \StatusValue::newGood();
210 } ) );
211 $provider->expects( $this->any() )->method( 'providerChangeAuthenticationData' )
212 ->will( $this->returnCallback( function ( $req ) use ( &$done ) {
213 $done[$req->id] = true;
214 } ) );
215 $config = new \HashConfig( [
216 'AuthManagerConfig' => [
217 'preauth' => [],
218 'primaryauth' => [],
219 'secondaryauth' => [
220 [ 'factory' => function () use ( $provider ) {
221 return $provider;
222 } ],
223 ],
224 ],
225 ] );
226 $request = new \FauxRequest();
227 $manager = new AuthManager( $request, $config );
228 $provider->setManager( $manager );
229 $provider = TestingAccessWrapper::newFromObject( $provider );
230
232
233 $this->assertEquals(
235 $provider->continueLinkAttempt( $user, 'state', [ $req ] )
236 );
237
238 $request->getSession()->setSecret( 'state', [
239 'maybeLink' => [],
240 ] );
241 $this->assertEquals(
243 $provider->continueLinkAttempt( $user, 'state', [ $req ] )
244 );
245
246 $request->getSession()->setSecret( 'state', [
247 'maybeLink' => $reqs
248 ] );
249 $this->assertEquals(
251 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] )
252 );
253 $this->assertSame( [ false, false, false ], $done );
254
255 $request->getSession()->setSecret( 'state', [
256 'maybeLink' => [ $reqs['Request2'] ],
257 ] );
258 $req->confirmedLinkIDs = [ 'Request1', 'Request2' ];
259 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
260 $this->assertEquals( AuthenticationResponse::newPass(), $res );
261 $this->assertSame( [ false, true, false ], $done );
262 $done = [ false, false, false ];
263
264 $request->getSession()->setSecret( 'state', [
265 'maybeLink' => $reqs,
266 ] );
267 $req->confirmedLinkIDs = [ 'Request1', 'Request2' ];
268 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
269 $this->assertEquals( AuthenticationResponse::newPass(), $res );
270 $this->assertSame( [ true, true, false ], $done );
271 $done = [ false, false, false ];
272
273 $request->getSession()->setSecret( 'state', [
274 'maybeLink' => $reqs,
275 ] );
276 $req->confirmedLinkIDs = [ 'Request1', 'Request3' ];
277 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
278 $this->assertEquals( AuthenticationResponse::UI, $res->status );
279 $this->assertCount( 1, $res->neededRequests );
280 $this->assertInstanceOf( ButtonAuthenticationRequest::class, $res->neededRequests[0] );
281 $this->assertSame( [ true, false, false ], $done );
282 $done = [ false, false, false ];
283
284 $res = $provider->continueLinkAttempt( $user, 'state', [ $res->neededRequests[0] ] );
285 $this->assertEquals( AuthenticationResponse::newPass(), $res );
286 $this->assertSame( [ false, false, false ], $done );
287 }
288
289}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
This serves as the entry point to the authentication system.
const ACTION_CHANGE
Change a user's credentials.
const ACTION_REMOVE
Remove a user's credentials.
const ACTION_LINK
Link an existing user to a third-party account.
const ACTION_LOGIN
Log in with an existing (not necessarily local) user.
const ACTION_CREATE
Create a new user.
const UI
Indicates that the authentication needs further user input of some sort.
AuthManager MediaWiki\Auth\ConfirmLinkSecondaryAuthenticationProvider.
static getMain()
Get the RequestContext object associated with the main request.
$res
Definition database.txt:21
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
this hook is for auditing only $req
Definition hooks.txt:990
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2806
this hook is for auditing only $response
Definition hooks.txt:783
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
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:247
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:37