Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Adds csrf token to action=logout requests
  • Loading branch information
Tpt authored and addshore committed Nov 27, 2019
commit f712854aa6350714ee1c877f38f99ef7efa6d8af
4 changes: 3 additions & 1 deletion src/MediawikiApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,9 @@ private function throwLoginUsageException( $result ) {
*/
public function logout() {
$this->logger->log( LogLevel::DEBUG, 'Logging out' );
$result = $this->postRequest( new SimpleRequest( 'logout' ) );
$result = $this->postRequest( new SimpleRequest( 'logout', [
'token' => $this->getToken()
] ) );
if ( $result === [] ) {
$this->isLoggedIn = false;
$this->clearTokens();
Expand Down
54 changes: 46 additions & 8 deletions tests/Unit/MediawikiApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,59 @@ public function testBadLoginSequence() {

public function testLogout() {
$client = $this->getMockClient();
$client->expects( $this->at( 0 ) )
->method( 'request' )
->with( 'POST', null, $this->getExpectedRequestOpts( [ 'action' => 'logout' ], 'form_params' ) )
->will( $this->returnValue( $this->getMockResponse( [] ) ) );
$client->method( 'request' )
->withConsecutive(
[ 'POST', null, $this->getExpectedRequestOpts( [
'action' => 'query',
'meta' => 'tokens',
'type' => 'csrf',
'continue' => ''
], 'form_params' ) ],
[ 'POST', null, $this->getExpectedRequestOpts( [
'action' => 'logout',
'token' => 'TKN-csrf'
], 'form_params' ) ]
)
->willReturnOnConsecutiveCalls(
$this->returnValue( $this->getMockResponse( [
'query' => [
'tokens' => [
'csrf' => 'TKN-csrf',
]
]
] ) ),
$this->returnValue( $this->getMockResponse( [] ) )
);
$api = new MediawikiApi( '', $client );

$this->assertTrue( $api->logout() );
}

public function testLogoutOnFailure() {
$client = $this->getMockClient();
$client->expects( $this->at( 0 ) )
->method( 'request' )
->with( 'POST', null, $this->getExpectedRequestOpts( [ 'action' => 'logout' ], 'form_params' ) )
->will( $this->returnValue( $this->getMockResponse( null ) ) );
$client->method( 'request' )
->withConsecutive(
[ 'POST', null, $this->getExpectedRequestOpts( [
'action' => 'query',
'meta' => 'tokens',
'type' => 'csrf',
'continue' => ''
], 'form_params' ) ],
[ 'POST', null, $this->getExpectedRequestOpts( [
'action' => 'logout',
'token' => 'TKN-csrf'
], 'form_params' ) ]
)
->willReturnOnConsecutiveCalls(
$this->returnValue( $this->getMockResponse( [
'query' => [
'tokens' => [
'csrf' => 'TKN-csrf',
]
]
] ) ),
$this->returnValue( $this->getMockResponse( null ) )
);
$api = new MediawikiApi( '', $client );

$this->assertFalse( $api->logout() );
Expand Down