Build authorization header, check for missing oauth_token and oauth_consumer_key
authorMichael J. Rubinsky <mrubinsk@horde.org>
Sat, 18 Jul 2009 22:07:30 +0000 (18:07 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Sat, 18 Jul 2009 22:07:30 +0000 (18:07 -0400)
Add method to Horde_Oauth_Request that builds an OAuth Authorization header
(http://oauth.net/core/1.0#auth_header)

Allow H_O_Request to obtain the *required* oauth_consumer_key and the oauth_token
values from the Consumer and Token objects respectively so that client code doesn't
have to awkardly pass them in both the Horde_Oauth_Consumer const'r and in the
Horde_Oauth_Request const'r

Add method to build an OAuth Authorization Header

framework/Oauth/lib/Horde/Oauth/Request.php

index 2676e58..97991e5 100644 (file)
@@ -39,8 +39,24 @@ class Horde_Oauth_Request
         $this->_url = $url;
     }
 
+    /**
+     * Sign this request in accordance with OAuth
+     *
+     * @param $signatureMethod
+     * @param $consumer
+     * @param $token
+     * @return unknown_type
+     */
     public function sign($signatureMethod, $consumer, $token = null)
     {
+        if (empty($this->_params['oauth_consumer_key'])) {
+            $this->_params['oauth_consumer_key'] = $consumer->key;
+        }
+
+        if (empty($this->_params['oauth_token']) && !is_null($token)) {
+            $this->_params['oauth_token'] = $token->key;
+        }
+
         $this->_params['oauth_signature_method'] = $signatureMethod->getName();
         $this->_params['oauth_signature'] = $signatureMethod->sign($this, $consumer, $token);
 
@@ -76,6 +92,19 @@ class Horde_Oauth_Request
         return implode('&', $parts);
     }
 
+    public function buildAuthorizationHeader()
+    {
+        $header = '';
+        var_dump($this->_params);
+        foreach ($this->_params as $k => $v) {
+            if (strpos($k, 'oauth_') !== false) {
+                $header .= Horde_Oauth_Utils::urlencodeRfc3986($k) . '="' . Horde_Oauth_Utils::urlencodeRfc3986($v) . '",';
+            }
+        }
+        $header .= 'realm="' . Horde_Oauth_Utils::urlencodeRfc3986('twitter.com') . '"';
+        return 'OAuth ' . $header;
+    }
+
     /**
      * Generate a nonce.
      */