]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryTokens.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.git] / includes / api / ApiQueryTokens.php
1 <?php
2 /**
3  * Module to fetch tokens via action=query&meta=tokens
4  *
5  * Created on August 8, 2014
6  *
7  * Copyright © 2014 Wikimedia Foundation and contributors
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  * http://www.gnu.org/copyleft/gpl.html
23  *
24  * @file
25  * @since 1.24
26  */
27
28 /**
29  * Module to fetch tokens via action=query&meta=tokens
30  *
31  * @ingroup API
32  * @since 1.24
33  */
34 class ApiQueryTokens extends ApiQueryBase {
35
36         public function execute() {
37                 $params = $this->extractRequestParams();
38                 $res = [
39                         ApiResult::META_TYPE => 'assoc',
40                 ];
41
42                 if ( $this->lacksSameOriginSecurity() ) {
43                         $this->addWarning( [ 'apiwarn-tokens-origin' ] );
44                         return;
45                 }
46
47                 $user = $this->getUser();
48                 $session = $this->getRequest()->getSession();
49                 $salts = self::getTokenTypeSalts();
50                 foreach ( $params['type'] as $type ) {
51                         $res[$type . 'token'] = self::getToken( $user, $session, $salts[$type] )->toString();
52                 }
53
54                 $this->getResult()->addValue( 'query', $this->getModuleName(), $res );
55         }
56
57         /**
58          * Get the salts for known token types
59          * @return (string|array)[] Returning a string will use that as the salt
60          *  for User::getEditTokenObject() to fetch the token, which will give a
61          *  LoggedOutEditToken (always "+\\") for anonymous users. Returning an
62          *  array will use it as parameters to MediaWiki\Session\Session::getToken(),
63          *  which will always return a full token even for anonymous users.
64          */
65         public static function getTokenTypeSalts() {
66                 static $salts = null;
67                 if ( !$salts ) {
68                         $salts = [
69                                 'csrf' => '',
70                                 'watch' => 'watch',
71                                 'patrol' => 'patrol',
72                                 'rollback' => 'rollback',
73                                 'userrights' => 'userrights',
74                                 'login' => [ '', 'login' ],
75                                 'createaccount' => [ '', 'createaccount' ],
76                         ];
77                         Hooks::run( 'ApiQueryTokensRegisterTypes', [ &$salts ] );
78                         ksort( $salts );
79                 }
80
81                 return $salts;
82         }
83
84         /**
85          * Get a token from a salt
86          * @param User $user
87          * @param MediaWiki\Session\Session $session
88          * @param string|array $salt A string will be used as the salt for
89          *  User::getEditTokenObject() to fetch the token, which will give a
90          *  LoggedOutEditToken (always "+\\") for anonymous users. An array will
91          *  be used as parameters to MediaWiki\Session\Session::getToken(), which
92          *  will always return a full token even for anonymous users. An array will
93          *  also persist the session.
94          * @return MediaWiki\Session\Token
95          */
96         public static function getToken( User $user, MediaWiki\Session\Session $session, $salt ) {
97                 if ( is_array( $salt ) ) {
98                         $session->persist();
99                         return call_user_func_array( [ $session, 'getToken' ], $salt );
100                 } else {
101                         return $user->getEditTokenObject( $salt, $session->getRequest() );
102                 }
103         }
104
105         public function getAllowedParams() {
106                 return [
107                         'type' => [
108                                 ApiBase::PARAM_DFLT => 'csrf',
109                                 ApiBase::PARAM_ISMULTI => true,
110                                 ApiBase::PARAM_TYPE => array_keys( self::getTokenTypeSalts() ),
111                         ],
112                 ];
113         }
114
115         protected function getExamplesMessages() {
116                 return [
117                         'action=query&meta=tokens'
118                                 => 'apihelp-query+tokens-example-simple',
119                         'action=query&meta=tokens&type=watch|patrol'
120                                 => 'apihelp-query+tokens-example-types',
121                 ];
122         }
123
124         public function isReadMode() {
125                 // So login tokens can be fetched on private wikis
126                 return false;
127         }
128
129         public function getCacheMode( $params ) {
130                 return 'private';
131         }
132
133         public function getHelpUrls() {
134                 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tokens';
135         }
136 }