* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Oauth
*/
class Horde_Oauth_Consumer
{
protected $_config;
/**
* Const'r for consumer.
*
* @param array $config Configuration values:
*
* 'key' - Consumer key
* 'secret' - Consumer secret
* 'requestTokenUrl' - The request token URL
* 'authorizeTokenUrl' - The authorize URL
* 'accessTokenUrl' = To obtain an access token
* 'signatureMethod - Horde_Oauth_SignatureMethod object
*
*
* @return Horde_Oauth_Consumer
*/
public function __construct($config)
{
// Check for required config
if (!is_array($config) || empty($config['key']) || empty($config['secret']) ||
empty($config['requestTokenUrl']) || empty($config['authorizeTokenUrl']) ||
empty($config['signatureMethod'])) {
throw new InvalidArgumentException('Missing a required parameter in Horde_Oauth_Consumer::__construct');
}
$this->_config = $config;
}
public function __get($name)
{
return isset($this->_config[$name]) ? $this->_config[$name] : null;
}
/**
* Obtain an unprivileged request token
*
* @param array $params Parameter array
*
* @return Horde_Oauth_Token The oauth request token
*/
public function getRequestToken($params = array())
{
$params['oauth_consumer_key'] = $this->key;
$params['oauth_callback'] = $this->callbackUrl;
$request = new Horde_Oauth_Request($this->requestTokenUrl, $params);
$request->sign($this->signatureMethod, $this);
$client = new Horde_Http_Client;
try {
$response = $client->post(
$this->requestTokenUrl,
$request->buildHttpQuery()
);
} catch (Horde_Http_Exception $e) {
throw new Horde_Oauth_Exception($e->getMessage());
}
return Horde_Oauth_Token::fromString($response->getBody());
}
/**
* Get the user authorization url used to request user authorization
*
* @param Horde_Oauth_Token $token the oauth request token
*
* @return string The user authorization url string
*/
public function getUserAuthorizationUrl($token)
{
return $this->authorizeTokenUrl . '?oauth_token=' . urlencode($token->key) . '&oauth_callback=' . urlencode($this->callbackUrl);
}
/**
* Obtain an access token from a request token
*
* @param Horde_Oauth_Token $token Open auth token containing the oauth_token
* returned from provider after authorization
* and the token secret returned with the
* original request token.
* @param array $params Any additional parameters for this request
*
* @return unknown_type
*/
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;
try {
$response = $client->post(
$this->accessTokenUrl,
$request->buildHttpQuery()
);
} catch (Horde_Http_Exception $e) {
throw new Horde_Oauth_Exception($e->getMessage());
}
return Horde_Oauth_Token::fromString($response->getBody());
}
}
Horde_Oauth-2.0.4/lib/Horde/Oauth/Exception.php 0000664 0001750 0001750 00000000677 13160157140 017370 0 ustar jan jan
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Oauth
*/
/**
* OAuth exception class
*
* @author Chuck Hagenbuch
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Oauth
*/
class Horde_Oauth_Exception extends Horde_Exception_Wrapped
{
}
Horde_Oauth-2.0.4/lib/Horde/Oauth/Request.php 0000664 0001750 0001750 00000013340 13160157140 017051 0 ustar jan jan
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Oauth
*/
/**
* OAuth request class
*
* @author Chuck Hagenbuch
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Oauth
*/
class Horde_Oauth_Request
{
const VERSION = '1.0';
protected $_params = array();
protected $_url;
protected $_method;
function __construct($url, $params = array(), $method = 'POST')
{
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;
$this->_method = $method;
}
/**
* 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']) && !empty($token)) {
$this->_params['oauth_token'] = $token->key;
}
$this->_params['oauth_signature_method'] = $signatureMethod->getName();
$this->_params['oauth_signature'] = $signatureMethod->sign($this, $consumer, $token);
return $this->_getNormalizedUrl() . '?' . $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(
$this->_getNormalizedHttpMethod(),
$this->_getNormalizedUrl(),
$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);
}
/**
*/
public function buildAuthorizationHeader($realm = '')
{
$header = '';
foreach ($this->_params as $k => $v) {
if (strpos($k, 'oauth_') !== false) {
$header .= Horde_Oauth_Utils::urlencodeRfc3986($k) . '="' . Horde_Oauth_Utils::urlencodeRfc3986($v) . '",';
}
}
$header = substr($header, 0, -1);
if (!empty($realm)) {
$header .= ',realm="' . Horde_Oauth_Utils::urlencodeRfc3986($realm) . '"';
}
return 'OAuth ' . $header;
}
/**
* Generate a nonce.
*/
protected static function _generateNonce()
{
$mt = microtime();
$rand = mt_rand();
return hash('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);
}
/**
* Uppercases the HTTP method
*/
protected function _getNormalizedHttpMethod()
{
return Horde_String::upper($this->_method);
}
/**
* Parse the url and rebuilds it to be scheme://host/path
*/
protected function _getNormalizedUrl()
{
$parts = parse_url($this->_url);
$scheme = $parts['scheme'];
$port = !empty($parts['port'])
? $parts['port']
: $scheme == 'https' ? '443' : '80';
$host = $parts['host'];
$path = !empty($parts['path']) ? $parts['path'] : '';
if (($scheme == 'https' && $port != '443') ||
($scheme == 'http' && $port != '80')) {
$host = "$host:$port";
}
return "$scheme://$host$path";
}
}
Horde_Oauth-2.0.4/lib/Horde/Oauth/SignatureMethod.php 0000664 0001750 0001750 00000001304 13160157140 020520 0 ustar jan jan
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Oauth
*/
/**
* OAuth abstract signature method base class
*
* @author Chuck Hagenbuch
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Oauth
*/
abstract class Horde_Oauth_SignatureMethod
{
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);
}
}
Horde_Oauth-2.0.4/lib/Horde/Oauth/Token.php 0000664 0001750 0001750 00000002263 13160157140 016503 0 ustar jan jan
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Oauth
*/
/**
* OAuth access tokens and request tokens
*
* @author Chuck Hagenbuch
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package 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']);
}
}
Horde_Oauth-2.0.4/lib/Horde/Oauth/Utils.php 0000664 0001750 0001750 00000001146 13160157140 016522 0 ustar jan jan
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Oauth
*/
/**
* OAuth utilities
*
* @author Chuck Hagenbuch
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Oauth
*/
class Horde_Oauth_Utils
{
public static function urlencodeRfc3986($string)
{
return str_replace(array('%7E', '+'),
array('~', '%2B'),
rawurlencode($string));
}
}