Abstract out both the Auth class and the Request class, add Account class
authorMichael J. Rubinsky <mrubinsk@horde.org>
Sun, 19 Jul 2009 17:01:10 +0000 (13:01 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Sun, 19 Jul 2009 17:01:10 +0000 (13:01 -0400)
Abstract out Auth and Request so we can support both OAuth and Http Basic
authentication mechanisms.

Add Account class to handle the REST API's account/* methods

framework/Service_Twitter/lib/Horde/Service/Twitter.php
framework/Service_Twitter/lib/Horde/Service/Twitter/Account.php
framework/Service_Twitter/lib/Horde/Service/Twitter/Auth.php
framework/Service_Twitter/lib/Horde/Service/Twitter/Auth/Oauth.php [new file with mode: 0644]
framework/Service_Twitter/lib/Horde/Service/Twitter/Request.php [new file with mode: 0644]
framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php [new file with mode: 0644]
framework/Service_Twitter/lib/Horde/Service/Twitter/Statuses.php
framework/Service_Twitter/package.xml

index 0a86214..d3c224d 100644 (file)
@@ -20,20 +20,65 @@ class Horde_Service_Twitter
      */
     protected $_objCache = array();
 
+    /**
+     * Configuration values
+     *
+     * @var array
+     */
     protected $_config;
 
     /**
+     * Type of authentication (Oauth, Basic)
+     *
+     * @var string
+     */
+    protected $_authType;
+
+    /**
+     * Can't lazy load the auth or request class since we need to know early if
+     *  we are OAuth or Basic
+     *
+     * @var Horde_Service_Twitter_Auth
+     */
+    protected $_auth;
+
+    /**
+     *
+     * @var Horde_Service_Twitter_Request
+     */
+    protected $_request;
+
+    /**
      * Const'r
      *
      * @param array $config  Configuration parameters:
      *   <pre>
-     *     'oauth'  - Horde_Oauth object
+     *     'oauth'    - Horde_Oauth object if using Oauth
+     *     'username' - if using Basic auth
+     *     'password' - if using Basic auth
+     *   </pre>
      */
     public function __construct($config)
     {
         // TODO: Check for req'd config
         $this->_config = $config;
 
+        // Need to determine the type of authentication we will be using early..
+        if (!empty($config['oauth'])) {
+            // OAuth
+            $this->_authType = 'Oauth';
+            $params = array('oauth' => $config['oauth']);
+        } elseif (!empty($config['username']) && !empty($config['password'])) {
+            // Http_Basic
+            $this->_authType = 'Basic';
+            $params = array();
+        }
+
+        $aclass = 'Horde_Service_Twitter_Auth_' . $this->_authType;
+        $rclass = 'Horde_Service_Twitter_Request_' . $this->_authType;
+
+        $this->_auth = new $aclass($this, $params);
+        $this->_request = new $rclass($this);
     }
 
     /**
@@ -48,9 +93,10 @@ class Horde_Service_Twitter
     {
         // First, see if it's an allowed protected value.
         switch ($value) {
-        case 'oauth':
-            return $this->_config['oauth'];
-
+        case 'auth':
+            return $this->_auth;
+        case 'request':
+            return $this->_request;
         }
 
         // If not, assume it's a method/action class...
@@ -64,37 +110,8 @@ class Horde_Service_Twitter
         }
 
 
-        $this->_objCache[$class] = new $class($this, $this->oauth);
+        $this->_objCache[$class] = new $class($this);
         return $this->_objCache[$class];
     }
 
-    /**
-     * Send a request to the Twitter api
-     *
-     * @param $url
-     * @param $params
-     * @return unknown_type
-     */
-    public function getRequest($url, $params = array())
-    {
-        $request = new Horde_Oauth_Request($url, $params);
-        $request->sign($this->oauth->signatureMethod, $this->oauth, $this->auth->getAccessToken());
-
-        $client = new Horde_Http_Client();
-        $response = $client->get($url, array('Authorization' => $request->buildAuthorizationHeader()));
-
-        return $response->getBody();
-    }
-
-    public function postRequest($url, $params = array())
-    {
-        $request = new Horde_Oauth_Request($url, $params);
-        $request->sign($this->oauth->signatureMethod, $this->oauth, $this->auth->getAccessToken());
-
-        $client = new Horde_Http_Client();
-        $response = $client->post($url, $params, array('Authorization' => $request->buildAuthorizationHeader()));
-
-        return $response->getBody();
-    }
-
 }
index b3d9bbc..3c8dc0e 100644 (file)
@@ -1 +1,28 @@
 <?php
+/**
+ * Horde_Service_Twitter_Account class for calling account methods
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org)
+ *
+ * @author Michael J. Rubinsky <mrubinsk@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package Horde_Service_Twitter
+ */
+class Horde_Service_Twitter_Account
+{
+    protected $_endpoint = 'http://twitter.com/account/';
+    protected $_format = 'json';
+
+    public function __construct($twitter)
+    {
+        $this->_twitter = $twitter;
+    }
+
+    public function verifyCredentials()
+    {
+        $url = $this->_endpoint . 'verify_credentials.' . $this->_format;
+        return $this->_twitter->request->get($url);
+    }
+
+}
index bcf2f86..32673ca 100644 (file)
@@ -1,9 +1,7 @@
 <?php
 /**
- * Horde_Service_Twitter_Auth class to abstract all auth related tasks
- *
- * Basically implements Horde_Oauth_Client and passes the calls along to the
- * protected oauth object.
+ * Horde_Service_Twitter_Auth_* classes to abstract all auth related tasks for
+ * various auth mechanisms.
  *
  * Copyright 2009 The Horde Project (http://www.horde.org)
  *
@@ -12,8 +10,8 @@
  * @category Horde
  * @package Horde_Service_Twitter
  */
-class Horde_Service_Twitter_Auth {
-
+abstract class Horde_Service_Twitter_Auth
+{
     /**
      *
      * @var Horde_Service_Twitter
@@ -21,74 +19,30 @@ class Horde_Service_Twitter_Auth {
     protected $_twitter;
 
     /**
+     * Configuration parameters
      *
+     * @param array
      */
-    protected $_token;
+    protected $_config;
 
     /**
      * Const'r
      *
      * @return Horde_Service_Twitter_Auth
      */
-    public function __construct($twitter, $oauth)
+    public function __construct($twitter, $config)
     {
         $this->_twitter = $twitter;
+        $this->_config = $config;
     }
 
-    /**
-     * Obtain the URL used to get an authorization token.
-     *
-     * @param Horde_Oauth_Token $requestToken The request token
-     *
-     * @return string  The Url
-     */
-    public function getUserAuthorizationUrl($requestToken)
-    {
-        return $this->_twitter->oauth->getUserAuthorizationUrl($requestToken);
-    }
-
-    /**
-     * Set the access token
-     *
-     * @param $token
-     * @return unknown_type
-     */
-    public function setToken($token)
-    {
-        // @TODO: sanity check this
-        $this->_token = $token;
-    }
-
-    /**
-     * Obtain the access token. This is the token that should be persisted to
-     * storage.
-     *
-     * @param Horde_Controller_Request_Http     Http request object
-     * @param Horde_Oauth_Token $requestSecret  The token secret returned by
-     *                                          Twitter after the user authorizes
-     *                                          the application.
-     * @return Horde_Oauth_Token
-     */
-    public function getAccessToken($request = null, $requestSecret = null)
+    public function __get($value)
     {
-        if (!empty($this->_token)) {
-            return $this->_token;
+        if (!empty($this->_config[$value])) {
+            return $this->_config[$value];
         }
 
-        //@TODO: Verify the existence of requestSecret...
-
-        $params = $request->getGetParams();
-        if (empty($params['oauth_token'])) {
-            return false;
-        }
-        $token = new Horde_Oauth_Token($params['oauth_token'], $requestSecret);
-
-        return $this->_twitter->oauth->getAccessToken($token);
-    }
-
-    public function getRequestToken($params = array())
-    {
-        return $this->_twitter->oauth->getRequestToken($params);
+        throw new Horde_Service_Twitter_Exception(sprintf("The property %s does not exist", $value));
     }
 
 }
diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth/Oauth.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Auth/Oauth.php
new file mode 100644 (file)
index 0000000..113936a
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+/**
+ * Horde_Service_Twitter_Auth class to abstract all auth related tasks
+ *
+ * Basically implements Horde_Oauth_Client and passes the calls along to the
+ * protected oauth object.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org)
+ *
+ * @author Michael J. Rubinsky <mrubinsk@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package Horde_Service_Twitter
+ */
+class Horde_Service_Twitter_Auth_Oauth extends Horde_Service_Twitter_Auth
+{
+    /**
+     *
+     */
+    protected $_token;
+
+    /**
+     * Obtain the URL used to get an authorization token.
+     *
+     * @param Horde_Oauth_Token $requestToken The request token
+     *
+     * @return string  The Url
+     */
+    public function getUserAuthorizationUrl($requestToken)
+    {
+        return $this->oauth->getUserAuthorizationUrl($requestToken);
+    }
+
+    /**
+     * Set the access token
+     *
+     * @param $token
+     * @return unknown_type
+     */
+    public function setToken($token)
+    {
+        // @TODO: sanity check this
+        $this->_token = $token;
+    }
+
+    /**
+     * Obtain the access token. This is the token that should be persisted to
+     * storage.
+     *
+     * @param Horde_Controller_Request_Http     Http request object
+     * @param Horde_Oauth_Token $requestSecret  The token secret returned by
+     *                                          Twitter after the user authorizes
+     *                                          the application.
+     * @return Horde_Oauth_Token
+     */
+    public function getAccessToken($request = null, $requestSecret = null)
+    {
+        if (!empty($this->_token)) {
+            return $this->_token;
+        }
+
+        //@TODO: Verify the existence of requestSecret...
+
+        $params = $request->getGetParams();
+        if (empty($params['oauth_token'])) {
+            return false;
+        }
+        $token = new Horde_Oauth_Token($params['oauth_token'], $requestSecret);
+
+        return $this->oauth->getAccessToken($token);
+    }
+
+    public function getRequestToken($params = array())
+    {
+        return $this->oauth->getRequestToken($params);
+    }
+
+}
diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request.php
new file mode 100644 (file)
index 0000000..28d3ac8
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+/**
+ * Horde_Service_Twitter_Request_* classes wrap sending requests to Twitter's
+ * REST API using various authentication mechanisms.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org)
+ *
+ * @author Michael J. Rubinsky <mrubinsk@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package Horde_Service_Twitter
+ */
+abstract class Horde_Service_Twitter_Request
+{
+   protected $_twitter;
+
+   public function __construct($twitter)
+   {
+       $this->_twitter = $twitter;
+   }
+
+   abstract public function get($url, $params = array());
+   abstract public function post($url, $params = array());
+
+}
diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php
new file mode 100644 (file)
index 0000000..0adecbc
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+/**
+ * Horde_Service_Twitter_Request_Oauth class wraps sending requests to Twitter's
+ * REST API using OAuth authentication.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org)
+ *
+ * @author Michael J. Rubinsky <mrubinsk@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package Horde_Service_Twitter
+ */
+class Horde_Service_Twitter_Request_Oauth extends Horde_Service_Twitter_Request
+{
+
+    protected $_twitter;
+
+    public function __construct($twitter)
+    {
+        $this->_twitter = $twitter;
+    }
+
+    public function get($url, $params = array())
+    {
+        $request = new Horde_Oauth_Request($url, $params);
+        $request->sign($this->_twitter->auth->oauth->signatureMethod,
+                       $this->_twitter->auth->oauth,
+                       $this->_twitter->auth->getAccessToken());
+        $client = new Horde_Http_Client();
+        $response = $client->get($url, array('Authorization' => $request->buildAuthorizationHeader('Twitter API')));
+
+        return $response->getBody();
+    }
+
+    public function post($url, $params = array())
+    {
+        $request = new Horde_Oauth_Request($url, $params);
+        $request->sign($this->_twitter->auth->oauth->signatureMethod,
+                       $this->_twitter->auth->oauth,
+                       $this->_twitter->auth->getAccessToken());
+
+        $client = new Horde_Http_Client();
+        $response = $client->post($url, $params, array('Authorization' => $request->buildAuthorizationHeader('Twitter API')));
+
+        return $response->getBody();
+    }
+
+}
index 234b45a..0b17717 100644 (file)
@@ -12,7 +12,7 @@
 class Horde_Service_Twitter_Statuses
 {
 
-    public function __construct($twitter, $oauth)
+    public function __construct($twitter)
     {
         $this->_twitter = $twitter;
     }
@@ -30,6 +30,6 @@ class Horde_Service_Twitter_Statuses
     public function update($status)
     {
         $url = 'http://twitter.com/statuses/update.json';
-        return $this->_twitter->postRequest($url, array('status' => $status));
+        return $this->_twitter->request->post($url, array('status' => $status));
     }
 }
index aff37cc..910be2f 100644 (file)
@@ -33,9 +33,19 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <dir name="Horde">
      <dir name="Service">
       <dir name="Twitter">
+       <dir name="Auth">
+        <file name="Oauth.php" role="php" />
+        <file name="Basic.php" role="php" />
+       </dir> <!-- /lib/Horde/Service/Twitter/Auth -->
+       <dir name="Request">
+        <file name="Oauth.php" role="php" />
+        <file name="Basic.php" role="php" />
+       </dir> <!-- /lib/Horde/Service/Twitter/Request -->
        <file name="Auth.php" role="php" />
+       <file name="Request.php" role="php" />
        <file name="Exception.php" role="php" />
        <file name="Statuses.php" role="php" />
+       <file name="Account.php" role="php" />
       </dir> <!-- /lib/Horde/Service/Twitter-->
       <file name="Twitter.php" role="php" />
      </dir> <!-- /lib/Horde/Service -->
@@ -60,8 +70,14 @@ http://pear.php.net/dtd/package-2.0.xsd">
  <phprelease>
   <filelist>
    <install name="lib/Horde/Service/Twitter/Auth.php" as="Horde/Service/Twitter/Auth.php" />
+   <install name="lib/Horde/Service/Twitter/Auth/Oauth.php" as="Horde/Service/Twitter/Auth/Oauth.php" />
+   <install name="lib/Horde/Service/Twitter/Auth/Basic.php" as="Horde/Service/Twitter/Auth/Basic.php" />
    <install name="lib/Horde/Service/Twitter/Exception.php" as="Horde/Service/Twitter/Exception.php" />
    <install name="lib/Horde/Service/Twitter/Statuses.php" as="Horde/Service/Twitter/Statuses.php" />
+   <install name="lib/Horde/Service/Twitter/Account.php" as="Horde/Service/Twitter/Account.php" />
+   <install name="lib/Horde/Service/Twitter/Request.php" as="Horde/Service/Twitter/Request.php" />
+   <install name="lib/Horde/Service/Twitter/Request/Oauth.php" as="Horde/Service/Twitter/Request/Oauth.php" />
+   <install name="lib/Horde/Service/Twitter/Request/Basic.php" as="Horde/Service/Twitter/Request/Basic.php" />
    <install name="lib/Horde/Service/Twitter.php" as="Horde/Service/Twitter.php" />
   </filelist>
  </phprelease>