[SECURITY] [API BREAKING CHANGE] Require logout token.
[lhc/web/wiklou.git] / includes / api / ApiLogout.php
1 <?php
2 /**
3 * Copyright © 2008 Yuri Astrakhan "<Firstname><Lastname>@gmail.com",
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use MediaWiki\Session\BotPasswordSessionProvider;
24
25 /**
26 * API module to allow users to log out of the wiki. API equivalent of
27 * Special:Userlogout.
28 *
29 * @ingroup API
30 */
31 class ApiLogout extends ApiBase {
32
33 public function execute() {
34 $session = MediaWiki\Session\SessionManager::getGlobalSession();
35
36 // Handle bot password logout specially
37 if ( $session->getProvider() instanceof BotPasswordSessionProvider ) {
38 $session->unpersist();
39 return;
40 }
41
42 // Make sure it's possible to log out
43 if ( !$session->canSetUser() ) {
44 $this->dieWithError(
45 [
46 'cannotlogoutnow-text',
47 $session->getProvider()->describe( $this->getErrorFormatter()->getLanguage() )
48 ],
49 'cannotlogout'
50 );
51 }
52
53 $user = $this->getUser();
54 $oldName = $user->getName();
55 $user->logout();
56
57 // Give extensions to do something after user logout
58 $injected_html = '';
59 Hooks::run( 'UserLogoutComplete', [ &$user, &$injected_html, $oldName ] );
60 }
61
62 public function mustBePosted() {
63 return true;
64 }
65
66 public function needsToken() {
67 return 'csrf';
68 }
69
70 public function isReadMode() {
71 return false;
72 }
73
74 protected function getExamplesMessages() {
75 return [
76 'action=logout&token=123ABC'
77 => 'apihelp-logout-example-logout',
78 ];
79 }
80
81 public function getHelpUrls() {
82 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Logout';
83 }
84 }