3 * Copyright © 2015 Wikimedia Foundation and contributors
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.
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.
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
23 use MediaWiki\Session\Token
;
29 class ApiCheckToken
extends ApiBase
{
31 public function execute() {
32 $params = $this->extractRequestParams();
33 $token = $params['token'];
34 $maxage = $params['maxtokenage'];
35 $salts = ApiQueryTokens
::getTokenTypeSalts();
39 $tokenObj = ApiQueryTokens
::getToken(
40 $this->getUser(), $this->getRequest()->getSession(), $salts[$params['type']]
43 if ( substr( $token, -strlen( urldecode( Token
::SUFFIX
) ) ) === urldecode( Token
::SUFFIX
) ) {
44 $this->addWarning( 'apiwarn-checktoken-percentencoding' );
47 if ( $tokenObj->match( $token, $maxage ) ) {
48 $res['result'] = 'valid';
49 } elseif ( $maxage !== null && $tokenObj->match( $token ) ) {
50 $res['result'] = 'expired';
52 $res['result'] = 'invalid';
55 $ts = Token
::getTimestamp( $token );
57 $mwts = new MWTimestamp();
58 $mwts->timestamp
->setTimestamp( $ts );
59 $res['generated'] = $mwts->getTimestamp( TS_ISO_8601
);
62 $this->getResult()->addValue( null, $this->getModuleName(), $res );
65 public function getAllowedParams() {
68 ApiBase
::PARAM_TYPE
=> array_keys( ApiQueryTokens
::getTokenTypeSalts() ),
69 ApiBase
::PARAM_REQUIRED
=> true,
72 ApiBase
::PARAM_TYPE
=> 'string',
73 ApiBase
::PARAM_REQUIRED
=> true,
74 ApiBase
::PARAM_SENSITIVE
=> true,
77 ApiBase
::PARAM_TYPE
=> 'integer',
82 protected function getExamplesMessages() {
84 'action=checktoken&type=csrf&token=123ABC'
85 => 'apihelp-checktoken-example-simple',