Skip to content

Commit

Permalink
Updated HTTP_OAuth library and dependencies:
Browse files Browse the repository at this point in the history
* HTTP_OAuth-0.2.3
* HTTP_Request2-2.0.0
* Net_URL2-2.0.0
  • Loading branch information
cwt137 committed Dec 12, 2011
1 parent 40e5a83 commit 4f0279f
Show file tree
Hide file tree
Showing 21 changed files with 2,114 additions and 446 deletions.
2 changes: 1 addition & 1 deletion PEAR/HTTP/OAuth.php
Expand Up @@ -166,7 +166,7 @@ static public function buildHttpQuery(array $params)
static public function urlencode($item)
{
static $search = array('+', '%7E');
static $replace = array(' ', '~');
static $replace = array('%20', '~');

if (is_array($item)) {
return array_map(array('HTTP_OAuth', 'urlencode'), $item);
Expand Down
68 changes: 49 additions & 19 deletions PEAR/HTTP/OAuth/Consumer.php
Expand Up @@ -33,13 +33,13 @@
*
* <code>
* $consumer = new HTTP_OAuth_Consumer('key', 'secret');
* $consumer->getRequestToken('http://example.com/oauth/request_token, $callback);
* $consumer->getRequestToken('http://example.com/oauth/request_token', $callback);
*
* // Store tokens
* $_SESSION['token'] = $consumer->getToken();
* $_SESSION['token_secret'] = $consumer->getTokenSecret();
*
* $url = $consumer->getAuthorizationUrl('http://example.com/oauth/authorize');
* $url = $consumer->getAuthorizeUrl('http://example.com/oauth/authorize');
* http_redirect($url); // function from pecl_http
*
* // When they come back via the $callback url
Expand Down Expand Up @@ -110,6 +110,20 @@ class HTTP_OAuth_Consumer extends HTTP_OAuth
*/
protected $consumerRequest = null;

/**
* Instance of the last request made
*
* @var HTTP_OAuth_Consumer_Request $lastRequest The last request made
*/
protected $lastRequest = null;

/**
* Instance of the last response received
*
* @var HTTP_OAuth_Consumer_Response
*/
protected $lastResponse =null;

/**
* Construct
*
Expand Down Expand Up @@ -142,8 +156,7 @@ public function __construct($key, $secret, $token = null, $tokenSecret = null)
*/
public function getRequestToken($url, $callback = 'oob',
array $additional = array(), $method = 'POST'
)
{
) {
$this->debug('Getting request token from ' . $url);
$additional['oauth_callback'] = $callback;

Expand Down Expand Up @@ -174,14 +187,15 @@ public function getRequestToken($url, $callback = 'oob',
*/
public function getAccessToken($url, $verifier = '',
array $additional = array(), $method = 'POST'
)
{
) {
if ($this->getToken() === null || $this->getTokenSecret() === null) {
throw new HTTP_OAuth_Exception('No token or token_secret');
}

$this->debug('Getting access token from ' . $url);
$additional['oauth_verifier'] = $verifier;
if ($verifier !== null) {
$additional['oauth_verifier'] = $verifier;
}

$this->debug('verifier: ' . $verifier);
$response = $this->sendRequest($url, $additional, $method);
Expand All @@ -199,10 +213,10 @@ public function getAccessToken($url, $verifier = '',
/**
* Get authorize url
*
* @param string $url Authorization url
* @param string $url Authorize url
* @param array $additional Additional parameters for the auth url
*
* @return string Authorization url
* @return string Authorize url
*/
public function getAuthorizeUrl($url, array $additional = array())
{
Expand Down Expand Up @@ -235,20 +249,15 @@ public function sendRequest($url, array $additional = array(), $method = 'POST')
$params = array_merge($additional, $params);

$req = clone $this->getOAuthConsumerRequest();
if ($method == 'GET' && count($additional)) {
$parts = array();
foreach ($additional as $param => $value) {
$parts[] = urlencode($param) . '=' . urlencode($value);
}

$url .= '?' . implode('&', $parts);
}

$req->setUrl($url);
$req->setMethod($method);
$req->setSecrets($this->getSecrets());
$req->setParameters($params);
return $req->send();
$this->lastResponse = $req->send();
$this->lastRequest = $req;

return $this->lastResponse;
}

/**
Expand Down Expand Up @@ -344,7 +353,7 @@ public function setSignatureMethod($method)
*
* @return array Array possible secrets
*/
protected function getSecrets()
public function getSecrets()
{
return array($this->secret, (string) $this->tokenSecret);
}
Expand Down Expand Up @@ -387,6 +396,27 @@ public function getOAuthConsumerRequest()
}
return $this->consumerRequest;
}

/**
* Gets the last request
*
* @return null|HTTP_OAuth_Consumer_Request Instance of the last request
* @see self::sendRequest()
*/
public function getLastRequest()
{
return $this->lastRequest;
}

/**
* Gets the most recent HTTP_OAuth_Consumer_Response object
*
* @return HTTP_OAuth_Consumer_Response|null
*/
public function getLastResponse()
{
return $this->lastResponse;
}
}

?>
67 changes: 55 additions & 12 deletions PEAR/HTTP/OAuth/Consumer/Request.php
Expand Up @@ -117,8 +117,9 @@ public function accept($object)
default:
if ($object instanceof Log) {
HTTP_OAuth::attachLog($object);
$this->getHTTPRequest2()
->attach(new HTTP_Request2_Observer_Log($object));
$this->getHTTPRequest2()->attach(
new HTTP_Request2_Observer_Log($object)
);
}
break;
}
Expand Down Expand Up @@ -208,6 +209,23 @@ public function getAuthType()
public function send()
{
$this->buildRequest();
$request = $this->getHTTPRequest2();

// Hack for the OAuth's spec + => %20 and HTTP_Request2
// HTTP_Request2 uses http_build_query() which does spaces
// as '+' and not '%20'
$headers = $request->getHeaders();
$contentType = isset($headers['content-type'])
? $headers['content-type'] : '';
if ($this->getMethod() == 'POST'
&& $contentType == 'application/x-www-form-urlencoded'
) {

$body = $this->getHTTPRequest2()->getBody();
$body = str_replace('+', '%20', $body);
$this->getHTTPRequest2()->setBody($body);
}

try {
$response = $this->getHTTPRequest2()->send();
} catch (Exception $e) {
Expand All @@ -233,11 +251,17 @@ protected function buildRequest()
$this->oauth_timestamp = time();
$this->oauth_nonce = md5(microtime(true) . rand(1, 999));
$this->oauth_version = '1.0';
$this->oauth_signature = $sig->build($this->getMethod(),
$this->getUrl()->getURL(),
$this->getParameters(),
$this->secrets[0],
$this->secrets[1]);
$params = array_merge(
$this->getParameters(),
$this->getUrl()->getQueryVariables()
);
$this->oauth_signature = $sig->build(
$this->getMethod(),
$this->getUrl()->getURL(),
$params,
$this->secrets[0],
$this->secrets[1]
);

$params = $this->getOAuthParameters();
switch ($this->getAuthType()) {
Expand All @@ -254,15 +278,30 @@ protected function buildRequest()
break;
}

if ($this->getMethod() == 'POST') {
$this->setHeader('Content-Type', 'application/x-www-form-urlencoded');
switch ($this->getMethod()) {
case 'POST':
foreach ($this->getParameters() as $name => $value) {
if (substr($name, 0, 6) == 'oauth_') {
continue;
}

$this->addPostParameter($name, $value);
}
break;
case 'GET':
$url = $this->getUrl();
foreach ($this->getParameters() as $name => $value) {
if (substr($name, 0, 6) == 'oauth_') {
continue;
}

$url->setQueryVariable($name, $value);
}

$this->setUrl($url);
break;
default:
break;
}
}

Expand Down Expand Up @@ -303,9 +342,13 @@ protected function getAuthForHeader(array $params)
*/
public function __call($method, $args)
{
if (method_exists($this->getHTTPRequest2(), $method)) {
return call_user_func_array(array($this->getHTTPRequest2(), $method),
$args);
$httpRequest2 = $this->getHTTPRequest2();

if (is_callable(array($httpRequest2, $method))) {
return call_user_func_array(
array($httpRequest2, $method),
$args
);
}

throw new BadMethodCallException($method);
Expand Down
8 changes: 6 additions & 2 deletions PEAR/HTTP/OAuth/Message.php
Expand Up @@ -59,7 +59,8 @@ abstract class HTTP_OAuth_Message
'nonce',
'verifier',
'version',
'callback'
'callback',
'session_handle'
);

/**
Expand Down Expand Up @@ -95,7 +96,10 @@ public function getOAuthParameters()
*/
public function getParameters()
{
return $this->parameters;
$params = $this->parameters;
ksort($params);

return $params;
}

/**
Expand Down

0 comments on commit 4f0279f

Please sign in to comment.