CSteipp has submitted this change and it was merged.

Change subject: Cleaned up MWOAuthDataStore object construction
......................................................................


Cleaned up MWOAuthDataStore object construction

* Various doc fixes and line breaks

Change-Id: I5f2b267846a83bbfa4d96feb4803840b8011994f
---
M backend/MWOAuthDataStore.php
M backend/MWOAuthUtils.php
M control/MWOAuthConsumerAcceptanceSubmitControl.php
M frontend/specialpages/SpecialMWOAuth.php
4 files changed, 36 insertions(+), 47 deletions(-)

Approvals:
  CSteipp: Verified; Looks good to me, approved



diff --git a/backend/MWOAuthDataStore.php b/backend/MWOAuthDataStore.php
index ec7ff33..e513805 100644
--- a/backend/MWOAuthDataStore.php
+++ b/backend/MWOAuthDataStore.php
@@ -1,33 +1,33 @@
 <?php
 
 class MWOAuthDataStore extends OAuthDataStore {
-       // ObjectCache for Tokens and Nonces
+       /** @var DBConnRef DB for the consumer/grant registry */
+       protected $centralDB;
+       /** @var BagOStuff Cache for Tokens and Nonces */
        protected $cache;
 
-       // Persistant storage for logging/audit
-       protected $logging;
-
        /**
+        * @param DBConnRef $centralDB Central DB slave
         * @param BagOStuff $cache
-        * @param type $logdb
         */
-       public function __construct( BagOStuff $cache, $logdb ) {
+       public function __construct( DBConnRef $centralDB, BagOStuff $cache ) {
+               $this->centralDB = $centralDB;
                $this->cache = $cache;
-               $this->logging = $logdb;
        }
 
        /**
         * Get an MWOAuthConsuer from the consumer's key
+        *
         * @param String $consumerKey the string value of the Consumer's key
         * @return MWOAuthConsumer
         */
        public function lookup_consumer( $consumerKey ) {
-               $dbr = MWOAuthUtils::getCentralDB( DB_SLAVE );
-               return MWOAuthConsumer::newFromKey( $dbr, $consumerKey );
+               return MWOAuthConsumer::newFromKey( $this->centralDB, 
$consumerKey );
        }
 
        /**
-        * Get either a request or access token from the data store.
+        * Get either a request or access token from the data store
+        *
         * @param OAuthConsumer|MWOAuthConsumer $consumer
         * @param $token_type
         * @param $token String the token
@@ -47,8 +47,7 @@
                                throw new MWOAuthException( 
'mwoauthdatastore-request-token-not-found' );
                        }
                } elseif ( $token_type == 'access' ) {
-                       $dbr = MWOAuthUtils::getCentralDB( DB_SLAVE );
-                       $row = $dbr->selectRow(
+                       $row = $this->centralDB->selectRow(
                                'oauth_accepted_consumer',
                                '*',
                                array( 'oaac_access_token' => $token ),
@@ -79,11 +78,9 @@
         */
        public function lookup_nonce( $consumer, $token, $nonce, $timestamp ) {
                $key = MWOAuthUtils::getCacheKey( 'nonce', $consumer->key, 
$token, $nonce );
-
-               // Do an add for the key associated with this nonce. If the key 
exisits, nonce has
-               // been used. Set timeout 5 minutes in the future of the 
timestamp, to match
-               // OAuthServer. Use the timestamp so the client can also expire 
their nonce records
-               // after 5 mins.
+               // Do an add for the key associated with this nonce to check if 
it was already used.
+               // Set timeout 5 minutes in the future of the timestamp as 
OAuthServer does. Use the
+               // timestamp so the client can also expire their nonce records 
after 5 mins.
                if ( !$this->cache->add( $key, 1, $timestamp + 300 ) ) {
                        wfDebugLog( 'OAuth', "$key exists, so nonce has been 
used by this consumer+token" );
                        return true;
@@ -99,21 +96,22 @@
         */
        public static function newToken() {
                return new MWOAuthToken(
-                       MWCryptRand::generateHex( 32, false), //The key doesn't 
need to be unpredictable
+                       MWCryptRand::generateHex( 32, false), // the key 
doesn't need to be unpredictable
                        MWCryptRand::generateHex( 32, true)
                );
        }
 
        /**
-        * Generate a new token, save it in the cache, and return it
+        * Generate a new token (attached to this consumer), save it in the 
cache, and return it
+        *
         * @param MWOAuthConsumer|OAuthConsumer $consumer
         */
        public function new_request_token( $consumer, $callback = null ) {
-               // return a new token attached to this consumer
                $token = $this->newToken();
                $cacheKey = MWOAuthUtils::getCacheKey( 'token', $consumer->key, 
'request', $token->key );
                $this->cache->add( $cacheKey, $token, 600 ); //10 minutes. 
Kindof arbitray.
-               wfDebugLog( 'OAuth', __METHOD__ . ": New request token 
{$token->key} for {$consumer->key}" );
+               wfDebugLog( 'OAuth', __METHOD__ .
+                       ": New request token {$token->key} for 
{$consumer->key}" );
                return $token;
        }
 
@@ -127,30 +125,33 @@
         * @return MWOAuthToken the access token
         */
        public function new_access_token( $token, $consumer, $verifier = null ) 
{
-               wfDebugLog( 'OAuth', __METHOD__ . ": Getting new access token 
for token {$token->key}, consumer {$consumer->key}" );
+               wfDebugLog( 'OAuth', __METHOD__ .
+                       ": Getting new access token for token {$token->key}, 
consumer {$consumer->key}" );
 
                if ( !$token->getVerifyCode() || !$token->getAccessKey() ) {
                        throw new MWOAuthException( 
'mwoauthdatastore-bad-token' );
-               }
-
-               if ( $token->getVerifyCode() !== $verifier ) {
+               } elseif ( $token->getVerifyCode() !== $verifier ) {
                        throw new MWOAuthException( 
'mwoauthdatastore-bad-verifier' );
                }
 
+               $cacheKey = MWOAuthUtils::getCacheKey( 'token',
+                       $consumer->get( 'consumerKey' ), 'request', $token->key 
);
                $accessToken = $this->lookup_token( $consumer, 'access', 
$token->getAccessKey() );
-               $this->cache->delete( MWOAuthUtils::getCacheKey( 'token', 
$consumer->get( 'consumerKey' ), 'request', $token->key ) );
-               wfDebugLog( 'OAuth', __METHOD__ . ": New access token 
{$accessToken->key} for {$consumer->key}" );
+               $this->cache->delete( $cacheKey );
+               wfDebugLog( 'OAuth', __METHOD__ .
+                       ": New access token {$accessToken->key} for 
{$consumer->key}" );
                return $accessToken;
        }
 
        /**
         * Update a request token. The token probably already exists, but had 
another attribute added.
+        *
         * @param MWOAuthToken $token the token to store
         * @param MWOAuthConsumer|OAuthConsumer $consumer
         */
        public function updateRequestToken( $token, $consumer ) {
                $cacheKey = MWOAuthUtils::getCacheKey( 'token', $consumer->key, 
'request', $token->key );
-               $this->cache->set( $cacheKey, $token, 600 ); //10 more minutes. 
Kindof arbitray.
+               $this->cache->set( $cacheKey, $token, 600 ); // 10 more 
minutes. Kindof arbitray.
        }
 
        /**
@@ -160,8 +161,7 @@
         * @return String|null
         */
        public function getRSAKey( $consumerKey ) {
-               $dbr = MWOAuthUtils::getCentralDB( DB_SLAVE );
-               $cmr = MWOAuthConsumer::newFromKey( $dbr, $consumerKey );
+               $cmr = MWOAuthConsumer::newFromKey( $this->centralDB, 
$consumerKey );
                return $cmr ? $cmr->get( 'rsaKey' ) : null;
        }
 }
diff --git a/backend/MWOAuthUtils.php b/backend/MWOAuthUtils.php
index 6da7337..6cda593 100644
--- a/backend/MWOAuthUtils.php
+++ b/backend/MWOAuthUtils.php
@@ -192,15 +192,18 @@
 
        /**
         * Quickly get a new server with all the default configurations
+        *
         * @return MWOAuthServer with default configurations
         */
        public static function newMWOAuthServer() {
                global $wgMemc;
+
                $dbr = MWOAuthUtils::getCentralDB( DB_SLAVE );
-               $store = new MWOAuthDataStore( $wgMemc, $dbr );
+               $store = new MWOAuthDataStore( $dbr, $wgMemc );
                $server = new MWOAuthServer( $store );
                $server->add_signature_method( new 
OAuthSignatureMethod_HMAC_SHA1() );
                $server->add_signature_method( new 
MWOAuthSignatureMethod_RSA_SHA1( $store ) );
+
                return $server;
        }
 
diff --git a/control/MWOAuthConsumerAcceptanceSubmitControl.php 
b/control/MWOAuthConsumerAcceptanceSubmitControl.php
index 636c169..8a1d01f 100644
--- a/control/MWOAuthConsumerAcceptanceSubmitControl.php
+++ b/control/MWOAuthConsumerAcceptanceSubmitControl.php
@@ -95,11 +95,7 @@
                        }
 
                        // @TODO: handle exceptions
-                       $store = new MWOAuthDataStore( $wgMemc, 
MWOAuthUtils::getCentralDB( DB_MASTER ) );
-                       $oauthServer = new MWOAuthServer( $store );
-                       $oauthServer->add_signature_method( new 
OAuthSignatureMethod_HMAC_SHA1() );
-                       $oauthServer->add_signature_method( new 
MWOAuthSignatureMethod_RSA_SHA1( $store ) );
-
+                       $oauthServer = MWOAuthUtils::newMWOAuthServer();
                        $callback = $oauthServer->authorize(
                                $this->vals['consumerKey'], 
$this->vals['requestToken'], $this->getUser() );
 
diff --git a/frontend/specialpages/SpecialMWOAuth.php 
b/frontend/specialpages/SpecialMWOAuth.php
index 523277b..651df66 100644
--- a/frontend/specialpages/SpecialMWOAuth.php
+++ b/frontend/specialpages/SpecialMWOAuth.php
@@ -14,12 +14,7 @@
                }
 
                try {
-
-                       $store = $this->getStorage();
-                       $oauthServer = new MWOAuthServer( $store );
-                       $oauthServer->add_signature_method( new 
OAuthSignatureMethod_HMAC_SHA1() );
-                       $oauthServer->add_signature_method( new 
MWOAuthSignatureMethod_RSA_SHA1( $store ) );
-
+                       $oauthServer = MWOAuthUtils::newMWOAuthServer();
                        switch ( $subpage ) {
                                case 'initiate':
                                        $OAuthRequest = 
MWOAuthRequest::fromRequest( $request );
@@ -115,11 +110,6 @@
        private function doRedirect( $url ) {
                $output = $this->getOutput();
                $output->redirect( $url );
-       }
-
-       private function getStorage() {
-               global $wgMemc; //TODO instance of config
-               return new MWOAuthDataStore( $wgMemc, wfGetDB( DB_MASTER ) );
        }
 
        private function showAuthorizeForm( $params ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/74577
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I5f2b267846a83bbfa4d96feb4803840b8011994f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/OAuth
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <[email protected]>
Gerrit-Reviewer: CSteipp <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to