Merge "Rank aliases in search in order they appear in the messages file."
[lhc/web/wiklou.git] / includes / api / ApiCheckToken.php
1 <?php
2 /**
3 * Created on Jan 29, 2015
4 *
5 * Copyright © 2015 Brad Jorsch bjorsch@wikimedia.org
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 */
24
25 use MediaWiki\Session\Token;
26
27 /**
28 * @since 1.25
29 * @ingroup API
30 */
31 class ApiCheckToken extends ApiBase {
32
33 public function execute() {
34 $params = $this->extractRequestParams();
35 $token = $params['token'];
36 $maxage = $params['maxtokenage'];
37 $salts = ApiQueryTokens::getTokenTypeSalts();
38
39 $res = [];
40
41 $tokenObj = ApiQueryTokens::getToken(
42 $this->getUser(), $this->getRequest()->getSession(), $salts[$params['type']]
43 );
44
45 if ( substr( $token, -strlen( urldecode( Token::SUFFIX ) ) ) === urldecode( Token::SUFFIX ) ) {
46 $this->setWarning(
47 "Check that symbols such as \"+\" in the token are properly percent-encoded in the URL."
48 );
49 }
50
51 if ( $tokenObj->match( $token, $maxage ) ) {
52 $res['result'] = 'valid';
53 } elseif ( $maxage !== null && $tokenObj->match( $token ) ) {
54 $res['result'] = 'expired';
55 } else {
56 $res['result'] = 'invalid';
57 }
58
59 $ts = Token::getTimestamp( $token );
60 if ( $ts !== null ) {
61 $mwts = new MWTimestamp();
62 $mwts->timestamp->setTimestamp( $ts );
63 $res['generated'] = $mwts->getTimestamp( TS_ISO_8601 );
64 }
65
66 $this->getResult()->addValue( null, $this->getModuleName(), $res );
67 }
68
69 public function getAllowedParams() {
70 return [
71 'type' => [
72 ApiBase::PARAM_TYPE => array_keys( ApiQueryTokens::getTokenTypeSalts() ),
73 ApiBase::PARAM_REQUIRED => true,
74 ],
75 'token' => [
76 ApiBase::PARAM_TYPE => 'string',
77 ApiBase::PARAM_REQUIRED => true,
78 ],
79 'maxtokenage' => [
80 ApiBase::PARAM_TYPE => 'integer',
81 ],
82 ];
83 }
84
85 protected function getExamplesMessages() {
86 return [
87 'action=checktoken&type=csrf&token=123ABC'
88 => 'apihelp-checktoken-example-simple',
89 ];
90 }
91 }