Refactor requiresToken to getTokenSalt - Returns salt if exists, null if no salt...
authorSam Reed <reedy@users.mediawiki.org>
Mon, 15 Feb 2010 23:53:43 +0000 (23:53 +0000)
committerSam Reed <reedy@users.mediawiki.org>
Mon, 15 Feb 2010 23:53:43 +0000 (23:53 +0000)
Move sessionfailure (token validation checking) up a couple of levels

Part of bug 21991

Followup to r62482 and r62504

15 files changed:
includes/api/ApiBase.php
includes/api/ApiBlock.php
includes/api/ApiDelete.php
includes/api/ApiEditPage.php
includes/api/ApiEmailUser.php
includes/api/ApiImport.php
includes/api/ApiMain.php
includes/api/ApiMove.php
includes/api/ApiPatrol.php
includes/api/ApiProtect.php
includes/api/ApiRollback.php
includes/api/ApiUnblock.php
includes/api/ApiUndelete.php
includes/api/ApiUpload.php
includes/api/ApiUserrights.php

index a3033fb..e62e9f9 100644 (file)
@@ -970,10 +970,10 @@ abstract class ApiBase {
        }
        
        /**
-       * Indicates whether this module needs a token to preform the request
+       * Returns the token salt if there is one, null if the module doesn't require a salt, else false if the module doesn't need a token
        * @returns bool
        */
-       public function requiresToken() {
+       public function getTokenSalt() {
                return false;
        }
 
@@ -997,7 +997,7 @@ abstract class ApiBase {
                        $ret[] = array ( 'writedisabled' );
                }
                
-               if ( $this->requiresToken() ) {
+               if ( $this->getTokenSalt() != false ) {
                        $ret[] = array( 'missingparam', 'token' );
                }
 
index f2b41fe..27895aa 100644 (file)
@@ -61,8 +61,6 @@ class ApiBlock extends ApiBase {
 
                if ( is_null( $params['user'] ) )
                        $this->dieUsageMsg( array( 'missingparam', 'user' ) );
-               if ( !$wgUser->matchEditToken( $params['token'] ) )
-                       $this->dieUsageMsg( array( 'sessionfailure' ) );
                if ( !$wgUser->isAllowed( 'block' ) )
                        $this->dieUsageMsg( array( 'cantblock' ) );
                if ( $params['hidename'] && !$wgUser->isAllowed( 'hideuser' ) )
@@ -161,15 +159,14 @@ class ApiBlock extends ApiBase {
        public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
                        array( 'missingparam', 'user' ),
-                       array( 'sessionfailure' ),
                        array( 'cantblock' ),
                        array( 'canthide' ),
                        array( 'cantblock-email' ),
         ) );
        }
        
-       public function requiresToken() {
-               return true;
+       public function getTokenSalt() {
+               return null;
        }
 
        protected function getExamples() {
index e4686cf..89ee831 100644 (file)
@@ -47,7 +47,8 @@ class ApiDelete extends ApiBase {
         * result object.
         */
        public function execute() {
-               global $wgUser;
+       global $wgUser;
+       
                $params = $this->extractRequestParams();
 
                $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
@@ -78,7 +79,7 @@ class ApiDelete extends ApiBase {
                        
                        if ( count( $retval ) )
                                $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
-
+                               
                        if ( $params['watch'] || $wgUser->getOption( 'watchdeletion' ) )
                                $articleObj->doWatch();
                        else if ( $params['unwatch'] )
@@ -95,10 +96,7 @@ class ApiDelete extends ApiBase {
                // Check permissions
                $errors = $title->getUserPermissionsErrors( 'delete', $wgUser );
                if ( count( $errors ) > 0 ) return $errors;
-               
-               // Check token
-               if ( !$wgUser->matchEditToken( $token ) )
-                       return array( array( 'sessionfailure' ) );
+
                return array();
        }
 
@@ -219,8 +217,8 @@ class ApiDelete extends ApiBase {
                ) );
        }
        
-       public function requiresToken() {
-               return true;
+       public function getTokenSalt() {
+               return null;
        }
 
        protected function getExamples() {
index 3eb4141..5c434d3 100644 (file)
@@ -53,9 +53,6 @@ class ApiEditPage extends ApiBase {
                                $params['undo'] == 0 )
                        $this->dieUsageMsg( array( 'missingtext' ) );
 
-               if ( !$wgUser->matchEditToken( $params['token'] ) )
-                       $this->dieUsageMsg( array( 'sessionfailure' ) );
-
                $titleObj = Title::newFromText( $params['title'] );
                if ( !$titleObj || $titleObj->isExternal() )
                        $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
@@ -347,7 +344,6 @@ class ApiEditPage extends ApiBase {
                return array_merge( parent::getPossibleErrors(), array(
                        array( 'missingparam', 'title' ),
                        array( 'missingtext' ),
-                       array( 'sessionfailure' ),
                        array( 'invalidtitle', 'title' ),
                        array( 'createonly-exists' ),
                        array( 'nocreate-missing' ),
@@ -463,8 +459,8 @@ class ApiEditPage extends ApiBase {
                );
        }
        
-       public function requiresToken() {
-               return true;
+       public function getTokenSalt() {
+               return null;
        }
 
        protected function getExamples() {
index b93b77f..1a735b7 100644 (file)
@@ -112,8 +112,8 @@ class ApiEmailUser extends ApiBase {
         ) );
        }
        
-       public function requiresToken() {
-               return true;
+       public function getTokenSalt() {
+               return null;
        }
 
        protected function getExamples() {
index a68a103..163f2eb 100644 (file)
@@ -44,8 +44,6 @@ class ApiImport extends ApiBase {
                if ( !$wgUser->isAllowed( 'import' ) )
                        $this->dieUsageMsg( array( 'cantimport' ) );
                $params = $this->extractRequestParams();
-               if ( !$wgUser->matchEditToken( $params['token'] ) )
-                       $this->dieUsageMsg( array( 'sessionfailure' ) );
 
                $source = null;
                $isUpload = false;
@@ -144,7 +142,6 @@ class ApiImport extends ApiBase {
        public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
                        array( 'cantimport' ),
-                       array( 'sessionfailure' ),
                        array( 'missingparam', 'interwikipage' ),
                        array( 'cantimport-upload' ),
                        array( 'import-unknownerror', 'source' ),
@@ -152,8 +149,8 @@ class ApiImport extends ApiBase {
                ) );
        }
        
-       public function requiresToken() {
-               return true;
+       public function getTokenSalt() {
+               return null;
        }
 
        protected function getExamples() {
index 3a2602e..0686fe5 100644 (file)
@@ -400,7 +400,7 @@ class ApiMain extends ApiBase {
                        $this->getResult()->addValue( null, 'requestid', $requestid );
 
                $params = $this->extractRequestParams();
-
+                       
                $this->mShowVersions = $params['version'];
                $this->mAction = $params['action'];
 
@@ -412,9 +412,22 @@ class ApiMain extends ApiBase {
                $module = new $this->mModules[$this->mAction] ( $this, $this->mAction );
                $this->mModule = $module;
 
+               $moduleParams = $module->extractRequestParams();
+               
                //Die if token required, but not provided (unless there is a gettoken parameter)
-               if ( $module->requiresToken() && !isset( $params['token'] ) && isset( $params['gettoken'] ) )
-                       $this->dieUsageMsg( array( 'missingparam', 'token' ) );
+               $salt = $module->getTokenSalt();
+               if ( $salt != false )
+               {
+                       if ( !isset( $moduleParams['token'] ) && !isset( $moduleParams['gettoken'] ) ) {
+                               $this->dieUsageMsg( array( 'missingparam', 'token' ) );
+                       } else {
+                               global $wgUser;
+                               if ( ( $salt != null /*&& !$wgUser->matchEditToken( $moduleParams['token'], $salt )*/ )
+                                       /*|| !$wgUser->matchEditToken( $moduleParams['token'] )*/ ) {
+                                       $this->dieUsageMsg( array( 'sessionfailure' ) );
+                               }
+                       }
+               }
 
                if ( $module->shouldCheckMaxlag() && isset( $params['maxlag'] ) ) {
                        // Check for maxlag
index 0f1e31c..66bf71b 100644 (file)
@@ -46,8 +46,6 @@ class ApiMove extends ApiBase {
                $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
                if ( !isset( $params['to'] ) )
                        $this->dieUsageMsg( array( 'missingparam', 'to' ) );
-               if ( !$wgUser->matchEditToken( $params['token'] ) )
-                       $this->dieUsageMsg( array( 'sessionfailure' ) );
 
                if ( isset( $params['from'] ) )
                {
@@ -213,7 +211,6 @@ class ApiMove extends ApiBase {
        public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
                        array( 'missingparam', 'to' ),
-                       array( 'sessionfailure' ),
                        array( 'invalidtitle', 'from' ),
                        array( 'nosuchpageid', 'fromid' ),
                        array( 'notanarticle' ),
@@ -222,8 +219,8 @@ class ApiMove extends ApiBase {
                ) );
        }
        
-       public function requiresToken() {
-               return true;
+       public function getTokenSalt() {
+               return null;
        }
 
        protected function getExamples() {
index 101bed1..f42523b 100644 (file)
@@ -41,13 +41,10 @@ class ApiPatrol extends ApiBase {
         * Patrols the article or provides the reason the patrol failed.
         */
        public function execute() {
-               global $wgUser;
                $params = $this->extractRequestParams();
                
                if ( !isset( $params['rcid'] ) )
                        $this->dieUsageMsg( array( 'missingparam', 'rcid' ) );
-               if ( !$wgUser->matchEditToken( $params['token'] ) )
-                       $this->dieUsageMsg( array( 'sessionfailure' ) );
 
                $rc = RecentChange::newFromID( $params['rcid'] );
                if ( !$rc instanceof RecentChange )
@@ -91,13 +88,12 @@ class ApiPatrol extends ApiBase {
     public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
                        array( 'missingparam', 'rcid' ),
-                       array( 'sessionfailure' ),
                        array( 'nosuchrcid', 'rcid' ),
         ) );
        }
        
-       public function requiresToken() {
-               return true;
+       public function getTokenSalt() {
+               return null;
        }
 
        protected function getExamples() {
index a417dac..a1aff82 100644 (file)
@@ -46,9 +46,6 @@ class ApiProtect extends ApiBase {
                if ( empty( $params['protections'] ) )
                        $this->dieUsageMsg( array( 'missingparam', 'protections' ) );
 
-               if ( !$wgUser->matchEditToken( $params['token'] ) )
-                       $this->dieUsageMsg( array( 'sessionfailure' ) );
-
                $titleObj = Title::newFromText( $params['title'] );
                if ( !$titleObj )
                        $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
@@ -176,7 +173,6 @@ class ApiProtect extends ApiBase {
                return array_merge( parent::getPossibleErrors(), array(
                        array( 'missingparam', 'title' ),
                        array( 'missingparam', 'protections' ),
-                       array( 'sessionfailure' ),
                        array( 'invalidtitle', 'title' ),
                        array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
                        array( 'create-titleexists' ),
@@ -188,8 +184,8 @@ class ApiProtect extends ApiBase {
                ) );
        }
        
-       public function requiresToken() {
-               return true;
+       public function getTokenSalt() {
+               return null;
        }
 
        protected function getExamples() {
index 4245289..d09800c 100644 (file)
@@ -122,8 +122,8 @@ class ApiRollback extends ApiBase {
                ) );
        }
        
-       public function requiresToken() {
-               return true;
+       public function getTokenSalt() {
+               return null;
        }
 
        protected function getExamples() {
index 595dcc7..9fe7ac9 100644 (file)
@@ -57,8 +57,7 @@ class ApiUnblock extends ApiBase {
                        $this->dieUsageMsg( array( 'unblock-notarget' ) );
                if ( !is_null( $params['id'] ) && !is_null( $params['user'] ) )
                        $this->dieUsageMsg( array( 'unblock-idanduser' ) );
-               if ( !$wgUser->matchEditToken( $params['token'] ) )
-                       $this->dieUsageMsg( array( 'sessionfailure' ) );
+
                if ( !$wgUser->isAllowed( 'block' ) )
                        $this->dieUsageMsg( array( 'cantunblock' ) );
 
@@ -113,13 +112,12 @@ class ApiUnblock extends ApiBase {
                return array_merge( parent::getPossibleErrors(), array(
                        array( 'unblock-notarget' ),
                        array( 'unblock-idanduser' ),
-                       array( 'sessionfailure' ),
                        array( 'cantunblock' ),
         ) );
        }
        
-       public function requiresToken() {
-               return true;
+       public function getTokenSalt() {
+               return null;
        }
 
        protected function getExamples() {
index 828d60b..41ee00d 100644 (file)
@@ -50,9 +50,6 @@ class ApiUndelete extends ApiBase {
                if ( $wgUser->isBlocked() )
                        $this->dieUsageMsg( array( 'blockedtext' ) );
 
-               if ( !$wgUser->matchEditToken( $params['token'] ) )
-                       $this->dieUsageMsg( array( 'sessionfailure' ) );
-
                $titleObj = Title::newFromText( $params['title'] );
                if ( !$titleObj )
                        $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
@@ -123,14 +120,13 @@ class ApiUndelete extends ApiBase {
                        array( 'missingparam', 'title' ),
                        array( 'permdenied-undelete' ),
                        array( 'blockedtext' ),
-                       array( 'sessionfailure' ),
                        array( 'invalidtitle', 'title' ),
                        array( 'cannotundelete' ),
                ) );
        }
        
-       public function requiresToken() {
-               return true;
+       public function getTokenSalt() {
+               return null;
        }
 
        protected function getExamples() {
index 41742a9..0995326 100644 (file)
@@ -47,10 +47,6 @@ class ApiUpload extends ApiBase {
                $this->mParams = $this->extractRequestParams();
                $request = $this->getMain()->getRequest();
 
-               // Do token checks:
-               if ( !$wgUser->matchEditToken( $this->mParams['token'] ) )
-                       $this->dieUsageMsg( array( 'sessionfailure' ) );
-
                // Add the uploaded file to the params array
                $this->mParams['file'] = $request->getFileName( 'file' );
 
@@ -328,7 +324,6 @@ class ApiUpload extends ApiBase {
     public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
                        array( 'uploaddisabled' ),
-                       array( 'sessionfailure' ),
                        array( 'invalid-session-key' ),
                        array( 'uploaddisabled' ),
                        array( 'badaccess-groups' ),
@@ -347,8 +342,8 @@ class ApiUpload extends ApiBase {
         ) );
        }
        
-       public function requiresToken() {
-               return true;
+       public function getTokenSalt() {
+               return null;
        }
 
        protected function getExamples() {
index 6117c8c..bf44976 100644 (file)
@@ -37,19 +37,11 @@ class ApiUserrights extends ApiBase {
        }
 
        public function execute() {
-               global $wgUser;
                $params = $this->extractRequestParams();
-               if ( is_null( $params['user'] ) )
-                       $this->dieUsageMsg( array( 'missingparam', 'user' ) );
-
+               
+               //User already validated in call to getTokenSalt from Main
                $form = new UserrightsPage;
                $user = $form->fetchUser( $params['user'] );
-               if ( $user instanceof WikiErrorMsg )
-                       $this->dieUsageMsg( array_merge(
-                               (array)$user->getMessageKey(), $user->getMessageArgs() ) );
-
-               if ( !$wgUser->matchEditToken( $params['token'], $user->getName() ) )
-                       $this->dieUsageMsg( array( 'sessionfailure' ) );
                
                $r['user'] = $user->getName();
                list( $r['added'], $r['removed'] ) =
@@ -107,12 +99,21 @@ class ApiUserrights extends ApiBase {
     public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
                        array( 'missingparam', 'user' ),
-                       array( 'sessionfailure' ),
         ) );
        }
        
-       public function requiresToken() {
-               return true;
+       public function getTokenSalt() {
+               $params = $this->extractRequestParams();
+               if ( is_null( $params['user'] ) )
+                       $this->dieUsageMsg( array( 'missingparam', 'user' ) );
+
+               $form = new UserrightsPage;
+               $user = $form->fetchUser( $params['user'] );
+               if ( $user instanceof WikiErrorMsg )
+                       $this->dieUsageMsg( array_merge(
+                               (array)$user->getMessageKey(), $user->getMessageArgs() ) );
+
+               return $user->getName();
        }
 
        protected function getExamples() {