Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / api / ApiCheckToken.php
blob3cc7a8a058dffac69bcc6f607800c79d1c4dff48
1 <?php
2 /**
3 * Created on Jan 29, 2015
5 * Copyright © 2015 Brad Jorsch bjorsch@wikimedia.org
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
22 * @file
25 use MediaWiki\Session\Token;
27 /**
28 * @since 1.25
29 * @ingroup API
31 class ApiCheckToken extends ApiBase {
33 public function execute() {
34 $params = $this->extractRequestParams();
35 $token = $params['token'];
36 $maxage = $params['maxtokenage'];
37 $salts = ApiQueryTokens::getTokenTypeSalts();
39 $res = [];
41 $tokenObj = ApiQueryTokens::getToken(
42 $this->getUser(), $this->getRequest()->getSession(), $salts[$params['type']]
45 if ( substr( $token, -strlen( urldecode( Token::SUFFIX ) ) ) === urldecode( Token::SUFFIX ) ) {
46 $this->addWarning( 'apiwarn-checktoken-percentencoding' );
49 if ( $tokenObj->match( $token, $maxage ) ) {
50 $res['result'] = 'valid';
51 } elseif ( $maxage !== null && $tokenObj->match( $token ) ) {
52 $res['result'] = 'expired';
53 } else {
54 $res['result'] = 'invalid';
57 $ts = Token::getTimestamp( $token );
58 if ( $ts !== null ) {
59 $mwts = new MWTimestamp();
60 $mwts->timestamp->setTimestamp( $ts );
61 $res['generated'] = $mwts->getTimestamp( TS_ISO_8601 );
64 $this->getResult()->addValue( null, $this->getModuleName(), $res );
67 public function getAllowedParams() {
68 return [
69 'type' => [
70 ApiBase::PARAM_TYPE => array_keys( ApiQueryTokens::getTokenTypeSalts() ),
71 ApiBase::PARAM_REQUIRED => true,
73 'token' => [
74 ApiBase::PARAM_TYPE => 'string',
75 ApiBase::PARAM_REQUIRED => true,
77 'maxtokenage' => [
78 ApiBase::PARAM_TYPE => 'integer',
83 protected function getExamplesMessages() {
84 return [
85 'action=checktoken&type=csrf&token=123ABC'
86 => 'apihelp-checktoken-example-simple',