Merge "Warn if stateful ParserOutput transforms are used"
[lhc/web/wiklou.git] / includes / api / ApiCheckToken.php
1 <?php
2 /**
3 * Copyright © 2015 Wikimedia Foundation and contributors
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 use MediaWiki\Session\Token;
24
25 /**
26 * @since 1.25
27 * @ingroup API
28 */
29 class ApiCheckToken extends ApiBase {
30
31 public function execute() {
32 $params = $this->extractRequestParams();
33 $token = $params['token'];
34 $maxage = $params['maxtokenage'];
35 $salts = ApiQueryTokens::getTokenTypeSalts();
36
37 $res = [];
38
39 $tokenObj = ApiQueryTokens::getToken(
40 $this->getUser(), $this->getRequest()->getSession(), $salts[$params['type']]
41 );
42
43 if ( substr( $token, -strlen( urldecode( Token::SUFFIX ) ) ) === urldecode( Token::SUFFIX ) ) {
44 $this->addWarning( 'apiwarn-checktoken-percentencoding' );
45 }
46
47 if ( $tokenObj->match( $token, $maxage ) ) {
48 $res['result'] = 'valid';
49 } elseif ( $maxage !== null && $tokenObj->match( $token ) ) {
50 $res['result'] = 'expired';
51 } else {
52 $res['result'] = 'invalid';
53 }
54
55 $ts = Token::getTimestamp( $token );
56 if ( $ts !== null ) {
57 $mwts = new MWTimestamp();
58 $mwts->timestamp->setTimestamp( $ts );
59 $res['generated'] = $mwts->getTimestamp( TS_ISO_8601 );
60 }
61
62 $this->getResult()->addValue( null, $this->getModuleName(), $res );
63 }
64
65 public function getAllowedParams() {
66 return [
67 'type' => [
68 ApiBase::PARAM_TYPE => array_keys( ApiQueryTokens::getTokenTypeSalts() ),
69 ApiBase::PARAM_REQUIRED => true,
70 ],
71 'token' => [
72 ApiBase::PARAM_TYPE => 'string',
73 ApiBase::PARAM_REQUIRED => true,
74 ApiBase::PARAM_SENSITIVE => true,
75 ],
76 'maxtokenage' => [
77 ApiBase::PARAM_TYPE => 'integer',
78 ],
79 ];
80 }
81
82 protected function getExamplesMessages() {
83 return [
84 'action=checktoken&type=csrf&token=123ABC'
85 => 'apihelp-checktoken-example-simple',
86 ];
87 }
88 }