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 namespace MediaWiki\Api
;
25 use MediaWiki\Session\Token
;
26 use MediaWiki\Utils\MWTimestamp
;
27 use Wikimedia\ParamValidator\ParamValidator
;
33 class ApiCheckToken
extends ApiBase
{
35 public function execute() {
36 $params = $this->extractRequestParams();
37 $token = $params['token'];
38 $maxage = $params['maxtokenage'];
39 $salts = ApiQueryTokens
::getTokenTypeSalts();
43 $tokenObj = ApiQueryTokens
::getToken(
44 $this->getUser(), $this->getRequest()->getSession(), $salts[$params['type']]
47 if ( str_ends_with( $token, urldecode( Token
::SUFFIX
) ) ) {
48 $this->addWarning( 'apiwarn-checktoken-percentencoding' );
51 if ( $tokenObj->match( $token, $maxage ) ) {
52 $res['result'] = 'valid';
53 } elseif ( $maxage !== null && $tokenObj->match( $token ) ) {
54 $res['result'] = 'expired';
56 $res['result'] = 'invalid';
59 $ts = Token
::getTimestamp( $token );
61 $mwts = new MWTimestamp();
62 $mwts->timestamp
->setTimestamp( $ts );
63 $res['generated'] = $mwts->getTimestamp( TS_ISO_8601
);
66 $this->getResult()->addValue( null, $this->getModuleName(), $res );
69 public function getAllowedParams() {
72 ParamValidator
::PARAM_TYPE
=> array_keys( ApiQueryTokens
::getTokenTypeSalts() ),
73 ParamValidator
::PARAM_REQUIRED
=> true,
76 ParamValidator
::PARAM_TYPE
=> 'string',
77 ParamValidator
::PARAM_REQUIRED
=> true,
78 ParamValidator
::PARAM_SENSITIVE
=> true,
81 ParamValidator
::PARAM_TYPE
=> 'integer',
86 protected function getExamplesMessages() {
88 'action=checktoken&type=csrf&token=123ABC'
89 => 'apihelp-checktoken-example-simple',
93 public function getHelpUrls() {
94 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Checktoken';
98 /** @deprecated class alias since 1.43 */
99 class_alias( ApiCheckToken
::class, 'ApiCheckToken' );