Merge "Convert article delete to use OOUI"
[lhc/web/wiklou.git] / includes / api / ApiCheckToken.php
index dfcbaf8..e1be8ef 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * Created on Jan 29, 2015
  *
- * Copyright © 2015 Brad Jorsch bjorsch@wikimedia.org
+ * Copyright © 2015 Wikimedia Foundation and contributors
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -22,6 +22,8 @@
  * @file
  */
 
+use MediaWiki\Session\Token;
+
 /**
  * @since 1.25
  * @ingroup API
@@ -34,11 +36,16 @@ class ApiCheckToken extends ApiBase {
                $maxage = $params['maxtokenage'];
                $salts = ApiQueryTokens::getTokenTypeSalts();
 
-               $res = array();
+               $res = [];
 
                $tokenObj = ApiQueryTokens::getToken(
                        $this->getUser(), $this->getRequest()->getSession(), $salts[$params['type']]
                );
+
+               if ( substr( $token, -strlen( urldecode( Token::SUFFIX ) ) ) === urldecode( Token::SUFFIX ) ) {
+                       $this->addWarning( 'apiwarn-checktoken-percentencoding' );
+               }
+
                if ( $tokenObj->match( $token, $maxage ) ) {
                        $res['result'] = 'valid';
                } elseif ( $maxage !== null && $tokenObj->match( $token ) ) {
@@ -47,7 +54,7 @@ class ApiCheckToken extends ApiBase {
                        $res['result'] = 'invalid';
                }
 
-               $ts = MediaWiki\Session\Token::getTimestamp( $token );
+               $ts = Token::getTimestamp( $token );
                if ( $ts !== null ) {
                        $mwts = new MWTimestamp();
                        $mwts->timestamp->setTimestamp( $ts );
@@ -58,25 +65,26 @@ class ApiCheckToken extends ApiBase {
        }
 
        public function getAllowedParams() {
-               return array(
-                       'type' => array(
+               return [
+                       'type' => [
                                ApiBase::PARAM_TYPE => array_keys( ApiQueryTokens::getTokenTypeSalts() ),
                                ApiBase::PARAM_REQUIRED => true,
-                       ),
-                       'token' => array(
+                       ],
+                       'token' => [
                                ApiBase::PARAM_TYPE => 'string',
                                ApiBase::PARAM_REQUIRED => true,
-                       ),
-                       'maxtokenage' => array(
+                               ApiBase::PARAM_SENSITIVE => true,
+                       ],
+                       'maxtokenage' => [
                                ApiBase::PARAM_TYPE => 'integer',
-                       ),
-               );
+                       ],
+               ];
        }
 
        protected function getExamplesMessages() {
-               return array(
+               return [
                        'action=checktoken&type=csrf&token=123ABC'
                                => 'apihelp-checktoken-example-simple',
-               );
+               ];
        }
 }