2015-01-29 20:14:40 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created on Jan 29, 2015
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2015 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
|
|
|
|
|
*/
|
|
|
|
|
|
2016-11-28 20:22:56 +00:00
|
|
|
use MediaWiki\Session\Token;
|
|
|
|
|
|
2015-01-29 20:14:40 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.25
|
|
|
|
|
* @ingroup API
|
|
|
|
|
*/
|
|
|
|
|
class ApiCheckToken extends ApiBase {
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
$token = $params['token'];
|
|
|
|
|
$maxage = $params['maxtokenage'];
|
|
|
|
|
$salts = ApiQueryTokens::getTokenTypeSalts();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$res = [];
|
2015-01-29 20:14:40 +00:00
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
$tokenObj = ApiQueryTokens::getToken(
|
|
|
|
|
$this->getUser(), $this->getRequest()->getSession(), $salts[$params['type']]
|
|
|
|
|
);
|
2016-11-28 20:22:56 +00:00
|
|
|
|
|
|
|
|
if ( substr( $token, -strlen( urldecode( Token::SUFFIX ) ) ) === urldecode( Token::SUFFIX ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->addWarning( 'apiwarn-checktoken-percentencoding' );
|
2016-11-28 20:22:56 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( $tokenObj->match( $token, $maxage ) ) {
|
2015-01-29 20:14:40 +00:00
|
|
|
$res['result'] = 'valid';
|
2016-02-01 20:44:03 +00:00
|
|
|
} elseif ( $maxage !== null && $tokenObj->match( $token ) ) {
|
2015-01-29 20:14:40 +00:00
|
|
|
$res['result'] = 'expired';
|
|
|
|
|
} else {
|
|
|
|
|
$res['result'] = 'invalid';
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-28 20:22:56 +00:00
|
|
|
$ts = Token::getTimestamp( $token );
|
2015-01-29 20:14:40 +00:00
|
|
|
if ( $ts !== null ) {
|
|
|
|
|
$mwts = new MWTimestamp();
|
|
|
|
|
$mwts->timestamp->setTimestamp( $ts );
|
|
|
|
|
$res['generated'] = $mwts->getTimestamp( TS_ISO_8601 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $res );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
'type' => [
|
2015-01-29 20:14:40 +00:00
|
|
|
ApiBase::PARAM_TYPE => array_keys( ApiQueryTokens::getTokenTypeSalts() ),
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'token' => [
|
2015-01-29 20:14:40 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2016-08-18 17:37:05 +00:00
|
|
|
ApiBase::PARAM_SENSITIVE => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'maxtokenage' => [
|
2015-01-29 20:14:40 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2015-01-29 20:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getExamplesMessages() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2015-01-29 20:14:40 +00:00
|
|
|
'action=checktoken&type=csrf&token=123ABC'
|
|
|
|
|
=> 'apihelp-checktoken-example-simple',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2015-01-29 20:14:40 +00:00
|
|
|
}
|
|
|
|
|
}
|