Skip to content

Commit 5708f00

Browse files
authored
Merge pull request #44 from addwiki/logout-token
Adds csrf token to action=logout requests
2 parents 204e642 + f712854 commit 5708f00

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

src/MediawikiApi.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,9 @@ private function throwLoginUsageException( $result ) {
489489
*/
490490
public function logout() {
491491
$this->logger->log( LogLevel::DEBUG, 'Logging out' );
492-
$result = $this->postRequest( new SimpleRequest( 'logout' ) );
492+
$result = $this->postRequest( new SimpleRequest( 'logout', [
493+
'token' => $this->getToken()
494+
] ) );
493495
if ( $result === [] ) {
494496
$this->isLoggedIn = false;
495497
$this->clearTokens();

tests/Unit/MediawikiApiTest.php

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,21 +246,59 @@ public function testBadLoginSequence() {
246246

247247
public function testLogout() {
248248
$client = $this->getMockClient();
249-
$client->expects( $this->at( 0 ) )
250-
->method( 'request' )
251-
->with( 'POST', null, $this->getExpectedRequestOpts( [ 'action' => 'logout' ], 'form_params' ) )
252-
->will( $this->returnValue( $this->getMockResponse( [] ) ) );
249+
$client->method( 'request' )
250+
->withConsecutive(
251+
[ 'POST', null, $this->getExpectedRequestOpts( [
252+
'action' => 'query',
253+
'meta' => 'tokens',
254+
'type' => 'csrf',
255+
'continue' => ''
256+
], 'form_params' ) ],
257+
[ 'POST', null, $this->getExpectedRequestOpts( [
258+
'action' => 'logout',
259+
'token' => 'TKN-csrf'
260+
], 'form_params' ) ]
261+
)
262+
->willReturnOnConsecutiveCalls(
263+
$this->returnValue( $this->getMockResponse( [
264+
'query' => [
265+
'tokens' => [
266+
'csrf' => 'TKN-csrf',
267+
]
268+
]
269+
] ) ),
270+
$this->returnValue( $this->getMockResponse( [] ) )
271+
);
253272
$api = new MediawikiApi( '', $client );
254273

255274
$this->assertTrue( $api->logout() );
256275
}
257276

258277
public function testLogoutOnFailure() {
259278
$client = $this->getMockClient();
260-
$client->expects( $this->at( 0 ) )
261-
->method( 'request' )
262-
->with( 'POST', null, $this->getExpectedRequestOpts( [ 'action' => 'logout' ], 'form_params' ) )
263-
->will( $this->returnValue( $this->getMockResponse( null ) ) );
279+
$client->method( 'request' )
280+
->withConsecutive(
281+
[ 'POST', null, $this->getExpectedRequestOpts( [
282+
'action' => 'query',
283+
'meta' => 'tokens',
284+
'type' => 'csrf',
285+
'continue' => ''
286+
], 'form_params' ) ],
287+
[ 'POST', null, $this->getExpectedRequestOpts( [
288+
'action' => 'logout',
289+
'token' => 'TKN-csrf'
290+
], 'form_params' ) ]
291+
)
292+
->willReturnOnConsecutiveCalls(
293+
$this->returnValue( $this->getMockResponse( [
294+
'query' => [
295+
'tokens' => [
296+
'csrf' => 'TKN-csrf',
297+
]
298+
]
299+
] ) ),
300+
$this->returnValue( $this->getMockResponse( null ) )
301+
);
264302
$api = new MediawikiApi( '', $client );
265303

266304
$this->assertFalse( $api->logout() );

0 commit comments

Comments
 (0)