From: Chuck Hagenbuch Date: Sat, 15 Nov 2008 14:54:33 +0000 (-0500) Subject: move Horde_Oauth to git X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=3d71f4a09be28b68dd8af01bdb6d69263eb36424;p=horde.git move Horde_Oauth to git --- diff --git a/framework/Oauth/CVS/Entries b/framework/Oauth/CVS/Entries new file mode 100644 index 000000000..dd51d5102 --- /dev/null +++ b/framework/Oauth/CVS/Entries @@ -0,0 +1,2 @@ +/package.xml/1.3/Tue Jul 29 05:00:09 2008// +D/lib//// diff --git a/framework/Oauth/CVS/Repository b/framework/Oauth/CVS/Repository new file mode 100644 index 000000000..c28bf5298 --- /dev/null +++ b/framework/Oauth/CVS/Repository @@ -0,0 +1 @@ +framework/Oauth diff --git a/framework/Oauth/CVS/Root b/framework/Oauth/CVS/Root new file mode 100644 index 000000000..5d636129a --- /dev/null +++ b/framework/Oauth/CVS/Root @@ -0,0 +1 @@ +chuck@cvs.horde.org:/repository diff --git a/framework/Oauth/CVS/Template b/framework/Oauth/CVS/Template new file mode 100644 index 000000000..3971591f9 --- /dev/null +++ b/framework/Oauth/CVS/Template @@ -0,0 +1,8 @@ + +Bug: +Submitted by: +Merge after: +CVS: ---------------------------------------------------------------------- +CVS: Bug: Fill this in if a listed bug is affected by the change. +CVS: Submitted by: Fill this in if someone else sent in the change. +CVS: Merge after: N [day[s]|week[s]|month[s]] (days assumed by default) diff --git a/framework/Oauth/lib/CVS/Entries b/framework/Oauth/lib/CVS/Entries new file mode 100644 index 000000000..ae2779e30 --- /dev/null +++ b/framework/Oauth/lib/CVS/Entries @@ -0,0 +1 @@ +D/Horde//// diff --git a/framework/Oauth/lib/CVS/Repository b/framework/Oauth/lib/CVS/Repository new file mode 100644 index 000000000..99347cd5e --- /dev/null +++ b/framework/Oauth/lib/CVS/Repository @@ -0,0 +1 @@ +framework/Oauth/lib diff --git a/framework/Oauth/lib/CVS/Root b/framework/Oauth/lib/CVS/Root new file mode 100644 index 000000000..5d636129a --- /dev/null +++ b/framework/Oauth/lib/CVS/Root @@ -0,0 +1 @@ +chuck@cvs.horde.org:/repository diff --git a/framework/Oauth/lib/CVS/Template b/framework/Oauth/lib/CVS/Template new file mode 100644 index 000000000..3971591f9 --- /dev/null +++ b/framework/Oauth/lib/CVS/Template @@ -0,0 +1,8 @@ + +Bug: +Submitted by: +Merge after: +CVS: ---------------------------------------------------------------------- +CVS: Bug: Fill this in if a listed bug is affected by the change. +CVS: Submitted by: Fill this in if someone else sent in the change. +CVS: Merge after: N [day[s]|week[s]|month[s]] (days assumed by default) diff --git a/framework/Oauth/lib/Horde/CVS/Entries b/framework/Oauth/lib/Horde/CVS/Entries new file mode 100644 index 000000000..0a1f4effb --- /dev/null +++ b/framework/Oauth/lib/Horde/CVS/Entries @@ -0,0 +1 @@ +D/Oauth//// diff --git a/framework/Oauth/lib/Horde/CVS/Repository b/framework/Oauth/lib/Horde/CVS/Repository new file mode 100644 index 000000000..ccabea292 --- /dev/null +++ b/framework/Oauth/lib/Horde/CVS/Repository @@ -0,0 +1 @@ +framework/Oauth/lib/Horde diff --git a/framework/Oauth/lib/Horde/CVS/Root b/framework/Oauth/lib/Horde/CVS/Root new file mode 100644 index 000000000..5d636129a --- /dev/null +++ b/framework/Oauth/lib/Horde/CVS/Root @@ -0,0 +1 @@ +chuck@cvs.horde.org:/repository diff --git a/framework/Oauth/lib/Horde/CVS/Template b/framework/Oauth/lib/Horde/CVS/Template new file mode 100644 index 000000000..3971591f9 --- /dev/null +++ b/framework/Oauth/lib/Horde/CVS/Template @@ -0,0 +1,8 @@ + +Bug: +Submitted by: +Merge after: +CVS: ---------------------------------------------------------------------- +CVS: Bug: Fill this in if a listed bug is affected by the change. +CVS: Submitted by: Fill this in if someone else sent in the change. +CVS: Merge after: N [day[s]|week[s]|month[s]] (days assumed by default) diff --git a/framework/Oauth/lib/Horde/Oauth/CVS/Entries b/framework/Oauth/lib/Horde/Oauth/CVS/Entries new file mode 100644 index 000000000..4f2535453 --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/CVS/Entries @@ -0,0 +1,6 @@ +/Consumer.php/1.1/Tue Jul 29 03:25:15 2008// +/Exception.php/1.1/Tue Jul 29 03:25:15 2008// +/Request.php/1.1/Tue Jul 29 03:25:15 2008// +/Token.php/1.1/Tue Jul 29 03:25:15 2008// +/Utils.php/1.1/Tue Jul 29 03:25:15 2008// +D/SignatureMethod//// diff --git a/framework/Oauth/lib/Horde/Oauth/CVS/Repository b/framework/Oauth/lib/Horde/Oauth/CVS/Repository new file mode 100644 index 000000000..716193b21 --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/CVS/Repository @@ -0,0 +1 @@ +framework/Oauth/lib/Horde/Oauth diff --git a/framework/Oauth/lib/Horde/Oauth/CVS/Root b/framework/Oauth/lib/Horde/Oauth/CVS/Root new file mode 100644 index 000000000..5d636129a --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/CVS/Root @@ -0,0 +1 @@ +chuck@cvs.horde.org:/repository diff --git a/framework/Oauth/lib/Horde/Oauth/CVS/Template b/framework/Oauth/lib/Horde/Oauth/CVS/Template new file mode 100644 index 000000000..3971591f9 --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/CVS/Template @@ -0,0 +1,8 @@ + +Bug: +Submitted by: +Merge after: +CVS: ---------------------------------------------------------------------- +CVS: Bug: Fill this in if a listed bug is affected by the change. +CVS: Submitted by: Fill this in if someone else sent in the change. +CVS: Merge after: N [day[s]|week[s]|month[s]] (days assumed by default) diff --git a/framework/Oauth/lib/Horde/Oauth/Consumer.php b/framework/Oauth/lib/Horde/Oauth/Consumer.php new file mode 100644 index 000000000..97cadf2a6 --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/Consumer.php @@ -0,0 +1,69 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ + +/** + * OAuth consumer class + * + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ +class Horde_Oauth_Consumer +{ + protected $_config; + + public function __construct($config) + { + $this->_config = $config; + } + + public function __get($name) + { + return isset($this->_config[$name]) ? $this->_config[$name] : null; + } + + public function getRequestToken($params = array()) + { + $params['oauth_consumer_key'] = $this->key; + + $request = new Horde_Oauth_Request($this->requestTokenUrl, $params); + $request->sign($this->signatureMethod, $this); + + $client = new Horde_Http_Client; + $response = $client->post( + $this->requestTokenUrl, + $request->buildHttpQuery() + ); + return Horde_Oauth_Token::fromString($response->getBody()); + } + + public function getUserAuthorizationUrl($token) + { + return $this->authorizeTokenUrl . '?oauth_token=' . urlencode($token->key) . '&oauth_callback=' . urlencode($this->callbackUrl); + } + + public function getAccessToken($token, $params = array()) + { + $params['oauth_consumer_key'] = $this->key; + $params['oauth_token'] = $token->key; + + $request = new Horde_Oauth_Request($this->accessTokenUrl, $params); + $request->sign($this->signatureMethod, $this, $token); + + $client = new Horde_Http_Client; + $response = $client->post( + $this->accessTokenUrl, + $request->buildHttpQuery() + ); + return Horde_Oauth_Token::fromString($response->getBody()); + } + +} diff --git a/framework/Oauth/lib/Horde/Oauth/Exception.php b/framework/Oauth/lib/Horde/Oauth/Exception.php new file mode 100644 index 000000000..677b60e2f --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/Exception.php @@ -0,0 +1,21 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ + +/** + * OAuth exception class + * + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ +class Horde_Oauth_Exception extends Exception +{ +} diff --git a/framework/Oauth/lib/Horde/Oauth/Request.php b/framework/Oauth/lib/Horde/Oauth/Request.php new file mode 100644 index 000000000..2f53d9a9b --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/Request.php @@ -0,0 +1,137 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ + +/** + * OAuth request class + * + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ +class Horde_Oauth_Request +{ + const VERSION = '1.0'; + + protected $_params = array(); + protected $_url; + + function __construct($url, $params = array()) + { + if (!isset($params['oauth_version'])) { + $params['oauth_version'] = self::VERSION; + } + if (!isset($params['oauth_nonce'])) { + $params['oauth_nonce'] = self::_generateNonce(); + } + if (!isset($params['oauth_timestamp'])) { + $params['oauth_timestamp'] = time(); + } + + $this->_params = $params; + $this->_url = $url; + } + + public function sign($signatureMethod, $consumer, $token = null) + { + $this->_params['oauth_signature_method'] = $signatureMethod->getName(); + $this->_params['oauth_signature'] = $signatureMethod->sign($this, $consumer, $token); + + return $this->_url . '?' . $this->buildHttpQuery(); + } + + /** + * Returns the signable string of this request + * + * The base string is defined as the method, the url and the parameters + * (normalized), each urlencoded and concatenated with &. + */ + public function getSignatureBaseString() + { + $parts = array( + 'POST', + $this->_url, + $this->_getSignableParameters() + ); + + return implode('&', array_map(array('Horde_Oauth_Utils', 'urlencodeRfc3986'), $parts)); + } + + /** + * Get a query string suitable for use in a URL or as POST data. + */ + public function buildHttpQuery() + { + $parts = array(); + foreach ($this->_params as $k => $v) { + $parts[] = Horde_Oauth_Utils::urlencodeRfc3986($k) . '=' . Horde_Oauth_Utils::urlencodeRfc3986($v); + } + return implode('&', $parts); + } + + /** + * Generate a nonce. + */ + protected static function _generateNonce() + { + $mt = microtime(); + $rand = mt_rand(); + + return md5(microtime() . mt_rand()); + } + + /** + * Returns the normalized parameters of the request + * + * This will be all parameters except oauth_signature, sorted first by key, + * and if there are duplicate keys, then by value. + * + * The returned string will be all the key=value pairs concatenated by &. + * + * @return string + */ + protected function _getSignableParameters() + { + // Grab all parameters + $params = $this->_params; + + // Remove oauth_signature if present + if (isset($params['oauth_signature'])) { + unset($params['oauth_signature']); + } + + // Urlencode both keys and values + $keys = array_map(array('Horde_Oauth_Utils', 'urlencodeRfc3986'), array_keys($params)); + $values = array_map(array('Horde_Oauth_Utils', 'urlencodeRfc3986'), array_values($params)); + $params = array_combine($keys, $values); + + // Sort by keys (natsort) + uksort($params, 'strnatcmp'); + + // Generate key=value pairs + $pairs = array(); + foreach ($params as $key => $value) { + if (is_array($value)) { + // If the value is an array, it's because there are multiple values + // with the same key. Sort them, then add all the pairs. + natsort($value); + foreach ($value as $v2) { + $pairs[] = $key . '=' . $v2; + } + } else { + $pairs[] = $key . '=' . $value; + } + } + + // Return the pairs, concatenated with & + return implode('&', $pairs); + } + +} diff --git a/framework/Oauth/lib/Horde/Oauth/SignatureMethod/Base.php b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/Base.php new file mode 100644 index 000000000..b1a4e9614 --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/Base.php @@ -0,0 +1,30 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ + +/** + * OAuth abstract signature method base class + * + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ +abstract class Horde_Oauth_SignatureMethod_Base +{ + abstract public function getName(); + + abstract public function sign($request, $consumer, $token); + + public function verify($signature, $request, $consumer, $token) + { + return $signature == $this->sign($request, $consumer, $token); + } + +} diff --git a/framework/Oauth/lib/Horde/Oauth/SignatureMethod/CVS/Entries b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/CVS/Entries new file mode 100644 index 000000000..d8a0c00be --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/CVS/Entries @@ -0,0 +1,5 @@ +/Base.php/1.1/Tue Jul 29 03:25:16 2008// +/HmacSha1.php/1.1/Tue Jul 29 03:25:16 2008// +/Plaintext.php/1.1/Tue Jul 29 03:25:16 2008// +/RsaSha1.php/1.1/Tue Jul 29 03:25:16 2008// +D diff --git a/framework/Oauth/lib/Horde/Oauth/SignatureMethod/CVS/Repository b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/CVS/Repository new file mode 100644 index 000000000..d04a3a6f4 --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/CVS/Repository @@ -0,0 +1 @@ +framework/Oauth/lib/Horde/Oauth/SignatureMethod diff --git a/framework/Oauth/lib/Horde/Oauth/SignatureMethod/CVS/Root b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/CVS/Root new file mode 100644 index 000000000..5d636129a --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/CVS/Root @@ -0,0 +1 @@ +chuck@cvs.horde.org:/repository diff --git a/framework/Oauth/lib/Horde/Oauth/SignatureMethod/CVS/Template b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/CVS/Template new file mode 100644 index 000000000..3971591f9 --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/CVS/Template @@ -0,0 +1,8 @@ + +Bug: +Submitted by: +Merge after: +CVS: ---------------------------------------------------------------------- +CVS: Bug: Fill this in if a listed bug is affected by the change. +CVS: Submitted by: Fill this in if someone else sent in the change. +CVS: Merge after: N [day[s]|week[s]|month[s]] (days assumed by default) diff --git a/framework/Oauth/lib/Horde/Oauth/SignatureMethod/HmacSha1.php b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/HmacSha1.php new file mode 100644 index 000000000..177be9b41 --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/HmacSha1.php @@ -0,0 +1,41 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ + +/** + * OAuth HMAC-SHA1 signature method + * + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ +class Horde_Oauth_SignatureMethod_HmacSha1 extends Horde_Oauth_SignatureMethod_Base +{ + public function getName() + { + return 'HMAC-SHA1'; + } + + public function sign($request, $consumer, $token) + { + $baseString = $request->getSignatureBaseString(); + + $key_parts = array( + $consumer->secret, + ($token) ? $token->secret : '' + ); + + $key_parts = array_map(array('Horde_Oauth_Utils','urlencodeRfc3986'), $key_parts); + $key = implode('&', $key_parts); + + return base64_encode(hash_hmac('sha1', $baseString, $key, true)); + } + +} diff --git a/framework/Oauth/lib/Horde/Oauth/SignatureMethod/Plaintext.php b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/Plaintext.php new file mode 100644 index 000000000..4f0abc59e --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/Plaintext.php @@ -0,0 +1,41 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ + +/** + * OAuth plaintext signature method + * + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ +class Horde_Oauth_SignatureMethod_Plaintext extends Horde_Oauth_SignatureMethod_Base +{ + public function getName() + { + return 'PLAINTEXT'; + } + + public function sign($request, $consumer, $token) + { + $signature = array( + Horde_Oauth_Utils::urlencodeRfc3986($consumer->secret), + ); + + if ($token) { + array_push($signature, Horde_Oauth_Utils::urlencodeRfc3986($token->secret)); + } else { + array_push($signature, ''); + } + + return Horde_Oauth_Utils::urlencodeRfc3986(implode('&', $signature)); + } + +} diff --git a/framework/Oauth/lib/Horde/Oauth/SignatureMethod/RsaSha1.php b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/RsaSha1.php new file mode 100644 index 000000000..7bcf108d5 --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/SignatureMethod/RsaSha1.php @@ -0,0 +1,55 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ + +/** + * OAuth RSA-SHA1 signature method + * + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ +class Horde_Oauth_SignatureMethod_RsaSha1 extends Horde_Oauth_SignatureMethod_Base +{ + public function __construct($publicKey = null, $privateKey = null) + { + $this->_publicKey = $publicKey; + $this->_privateKey = $privateKey; + } + + public function getName() + { + return 'RSA-SHA1'; + } + + public function sign($request, $consumer, $token) + { + $baseString = $request->getSignatureBaseString(); + + $pkeyid = openssl_pkey_get_private($this->_privateKey); + $ok = openssl_sign($baseString, $signature, $pkeyid); + openssl_free_key($pkeyid); + + return base64_encode($signature); + } + + public function verify($signature, $request, $consumer, $token) + { + $decodedSignature = base64_decode($signature); + $baseString = $request->getSignatureBaseString(); + + $pubkeyid = openssl_pkey_get_public($this->_publicKey); + $result = openssl_verify($baseString, $decodedSignature, $pubkeyid); + openssl_free_key($pubkeyid); + + return $result == 1; + } + +} diff --git a/framework/Oauth/lib/Horde/Oauth/Token.php b/framework/Oauth/lib/Horde/Oauth/Token.php new file mode 100644 index 000000000..e43825fc7 --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/Token.php @@ -0,0 +1,51 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ + +/** + * OAuth access tokens and request tokens + * + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ +class Horde_Oauth_Token +{ + public $key; + public $secret; + + /** + * key = the token + * secret = the token secret + */ + function __construct($key, $secret) + { + $this->key = $key; + $this->secret = $secret; + } + + /** + * Generate the basic string serialization of a token that a server would + * respond to request_token and access_token calls with. + */ + public function __toString() + { + return + 'oauth_token='.Horde_Oauth_Utils::urlencodeRfc3986($this->key). + '&oauth_token_secret='.Horde_Oauth_Utils::urlencodeRfc3986($this->secret); + } + + public static function fromString($string) + { + parse_str($string, $parts); + return new self($parts['oauth_token'], $parts['oauth_token_secret']); + } + +} diff --git a/framework/Oauth/lib/Horde/Oauth/Utils.php b/framework/Oauth/lib/Horde/Oauth/Utils.php new file mode 100644 index 000000000..d0638df8e --- /dev/null +++ b/framework/Oauth/lib/Horde/Oauth/Utils.php @@ -0,0 +1,28 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ + +/** + * OAuth utilities + * + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Oauth + */ +class Horde_Oauth_Utils +{ + public static function urlencodeRfc3986($string) + { + return str_replace(array('%7E', '+'), + array('~', '%20'), + rawurlencode($string)); + } + +} diff --git a/framework/Oauth/package.xml b/framework/Oauth/package.xml new file mode 100644 index 000000000..4cfcfb51c --- /dev/null +++ b/framework/Oauth/package.xml @@ -0,0 +1,78 @@ + + + Oauth + pear.horde.org + Horde OAuth client/server + This package provides an OAuth consumer (http://oauth.net) and OAuth infrastruture, and in the future will provide an OAuth server. + + + Chuck Hagenbuch + chuck + chuck@horde.org + yes + + 2008-07-28 + + 0.1.0 + 0.1.0 + + + beta + beta + + BSD + +* Initial release + + + + + + + + + + + + + + + + + + + + + + + + + + 5.2.0 + + + 1.5.0 + + + Http_Client + pear.horde.org + + + + + + + + + + + + + + + + +