2014-08-08 16:56:07 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Module to fetch tokens via action=query&meta=tokens
|
|
|
|
|
*
|
|
|
|
|
* Created on August 8, 2014
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2014 Brad Jorsch bjorsch@wikimedia.org
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @since 1.24
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module to fetch tokens via action=query&meta=tokens
|
|
|
|
|
*
|
|
|
|
|
* @ingroup API
|
|
|
|
|
* @since 1.24
|
|
|
|
|
*/
|
|
|
|
|
class ApiQueryTokens extends ApiQueryBase {
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$params = $this->extractRequestParams();
|
2015-01-16 19:00:07 +00:00
|
|
|
$res = array(
|
|
|
|
|
ApiResult::META_TYPE => 'assoc',
|
|
|
|
|
);
|
2014-08-08 16:56:07 +00:00
|
|
|
|
2014-12-17 11:09:04 +00:00
|
|
|
if ( $this->lacksSameOriginSecurity() ) {
|
|
|
|
|
$this->setWarning( 'Tokens may not be obtained when the same-origin policy is not applied' );
|
2014-08-08 16:56:07 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
$user = $this->getUser();
|
|
|
|
|
$session = $this->getRequest()->getSession();
|
2014-08-08 16:56:07 +00:00
|
|
|
$salts = self::getTokenTypeSalts();
|
|
|
|
|
foreach ( $params['type'] as $type ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
$res[$type . 'token'] = self::getToken( $user, $session, $salts[$type] )->toString();
|
2014-08-08 16:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->getResult()->addValue( 'query', $this->getModuleName(), $res );
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
/**
|
|
|
|
|
* Get the salts for known token types
|
|
|
|
|
* @return (string|array)[] Returning a string will use that as the salt
|
|
|
|
|
* for User::getEditTokenObject() to fetch the token, which will give a
|
|
|
|
|
* LoggedOutEditToken (always "+\\") for anonymous users. Returning an
|
|
|
|
|
* array will use it as parameters to MediaWiki\\Session\\Session::getToken(),
|
|
|
|
|
* which will always return a full token even for anonymous users.
|
|
|
|
|
*/
|
2014-08-08 16:56:07 +00:00
|
|
|
public static function getTokenTypeSalts() {
|
|
|
|
|
static $salts = null;
|
|
|
|
|
if ( !$salts ) {
|
|
|
|
|
$salts = array(
|
|
|
|
|
'csrf' => '',
|
|
|
|
|
'watch' => 'watch',
|
|
|
|
|
'patrol' => 'patrol',
|
|
|
|
|
'rollback' => 'rollback',
|
|
|
|
|
'userrights' => 'userrights',
|
2016-02-01 20:44:03 +00:00
|
|
|
'login' => array( '', 'login' ),
|
|
|
|
|
'createaccount' => array( '', 'createaccount' ),
|
2014-08-08 16:56:07 +00:00
|
|
|
);
|
2014-12-09 07:23:30 +00:00
|
|
|
Hooks::run( 'ApiQueryTokensRegisterTypes', array( &$salts ) );
|
2014-08-08 16:56:07 +00:00
|
|
|
ksort( $salts );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $salts;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
/**
|
|
|
|
|
* Get a token from a salt
|
|
|
|
|
* @param User $user
|
|
|
|
|
* @param MediaWiki\\Session\\Session $session
|
|
|
|
|
* @param string|array $salt A string will be used as the salt for
|
|
|
|
|
* User::getEditTokenObject() to fetch the token, which will give a
|
|
|
|
|
* LoggedOutEditToken (always "+\\") for anonymous users. An array will
|
|
|
|
|
* be used as parameters to MediaWiki\\Session\\Session::getToken(), which
|
|
|
|
|
* will always return a full token even for anonymous users. An array will
|
|
|
|
|
* also persist the session.
|
|
|
|
|
* @return MediaWiki\\Session\\Token
|
|
|
|
|
*/
|
|
|
|
|
public static function getToken( User $user, MediaWiki\Session\Session $session, $salt ) {
|
|
|
|
|
if ( is_array( $salt ) ) {
|
|
|
|
|
$session->persist();
|
|
|
|
|
return call_user_func_array( array( $session, 'getToken' ), $salt );
|
|
|
|
|
} else {
|
|
|
|
|
return $user->getEditTokenObject( $salt, $session->getRequest() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-08 16:56:07 +00:00
|
|
|
public function getAllowedParams() {
|
|
|
|
|
return array(
|
|
|
|
|
'type' => array(
|
|
|
|
|
ApiBase::PARAM_DFLT => 'csrf',
|
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
|
ApiBase::PARAM_TYPE => array_keys( self::getTokenTypeSalts() ),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-18 17:38:23 +00:00
|
|
|
protected function getExamplesMessages() {
|
2014-08-08 16:56:07 +00:00
|
|
|
return array(
|
2014-09-18 17:38:23 +00:00
|
|
|
'action=query&meta=tokens'
|
|
|
|
|
=> 'apihelp-query+tokens-example-simple',
|
|
|
|
|
'action=query&meta=tokens&type=watch|patrol'
|
|
|
|
|
=> 'apihelp-query+tokens-example-types',
|
2014-08-08 16:56:07 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
public function isReadMode() {
|
|
|
|
|
// So login tokens can be fetched on private wikis
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-08 16:56:07 +00:00
|
|
|
public function getCacheMode( $params ) {
|
|
|
|
|
return 'private';
|
|
|
|
|
}
|
2015-04-07 09:55:31 +00:00
|
|
|
|
|
|
|
|
public function getHelpUrls() {
|
|
|
|
|
return 'https://www.mediawiki.org/wiki/API:Tokens';
|
|
|
|
|
}
|
2014-08-08 16:56:07 +00:00
|
|
|
}
|