Commit: 218ceec17f1ff751aac578857a856f00802cf5ea
Author: gwoo | Date: 2010-06-12 10:09:38 -0700
diff --git a/extensions/service/Oauth.php b/extensions/service/Oauth.php
index 4fc0a3b..b658e00 100644
--- a/extensions/service/Oauth.php
+++ b/extensions/service/Oauth.php
@@ -92,7 +92,7 @@ class Oauth extends \lithium\net\http\Service {
* Send request with the given options and data. The token should be part of the options.
*
* @param string $method
- * @param string $url
+ * @param string $path
* @param array $data encoded for the request
* @param array $options oauth parameters
* - headers : send parameters in the header. (default: true)
@@ -100,26 +100,20 @@ class Oauth extends \lithium\net\http\Service {
* @return void
*/
public function send($method, $path = null, $data = array(), array $options = array()) {
- $defaults = array(
- 'headers' => true,
- 'realm' => basename(LITHIUM_APP_PATH),
- 'method' => $method,
- 'port' => $this->_config['port']
- );
+ $defaults = array('headers' => true, 'realm' => basename(LITHIUM_APP_PATH));
$options += $defaults;
+
$url = $this->config($path);
- $oauth = $this->sign($options + compact('data', 'url'));
+ $oauth = $this->sign($options + compact('data', 'url', 'method'));
if ($options['headers']) {
- $auth = 'OAuth realm="' . $options['realm'] . '",';
+ $header = 'OAuth realm="' . $options['realm'] . '",';
foreach ($oauth as $key => $val) {
- $auth .= $key . '="' . rawurlencode($val) . '",';
+ $header .= $key . '="' . rawurlencode($val) . '",';
}
- $options['headers'] = array('Authorization' => $auth);
- $oauth = array();
- }
+ $options['headers'] = array('Authorization' => $header);
+ }
$data += $oauth;
-
$response = parent::send($method, $url, $data, $options);
if (strpos($response, 'oauth_token=') !== false) {
@@ -132,16 +126,22 @@ class Oauth extends \lithium\net\http\Service {
* A utility method to return a authorize or authenticate url for redirect
*
* @param string $url
+ * @param array $options
+ * - `token`: (array) adds the oauth_token to the query params
+ * - `usePort`: (boolean) use the port in the signature base string
* @return void
*/
- public function url($url = null, $token = array()) {
+ public function url($url = null, array $options = array()) {
+ $defaults = array('token' => array('oauth_token' => false), 'usePort' => false);
+ $options += $defaults;
$url = $url ? $this->config($url) : null;
- if (!empty($token['oauth_token'])) {
- $url = "{$url}?oauth_token={$token['oauth_token']}";
+ if (!empty($options['token']['oauth_token'])) {
+ $url = "{$url}?oauth_token={$options['token']['oauth_token']}";
}
-
- return "http://" . str_replace('//', '/', "{$this->_config['host']}:{$this->_config['port']}/{$url}");
+ $base = $this->_config['host'];
+ $base .= ($options['usePort']) ? ":{$this->_config['port']}" : null;
+ return "http://" . str_replace('//', '/', "{$base}/{$url}");
}
/**
@@ -167,7 +167,8 @@ class Oauth extends \lithium\net\http\Service {
);
$options += $defaults;
$params = $this->_params((array) $options['params'] + (array) $options['token']);
- $base = $this->_base($options['method'], $options['url'], ($params + (array) $options['data']));
+ $params += (array) $options['data'];
+ $base = $this->_base($options['method'], $options['url'], $params, $options);
$key = join("&", array(
rawurlencode($options['oauth_consumer_secret']),
@@ -190,17 +191,21 @@ class Oauth extends \lithium\net\http\Service {
*
* @param string $method
* @param string $url
- * @param string $params
+ * @param array $params
+ * @param array $options
* @return void
*/
- protected function _base($method, $url, $params) {
+ protected function _base($method, $url, $params, $options) {
uksort($params, 'strcmp');
$query = array();
array_walk($params, function ($value, $key) use (&$query){
$query[] = $key . '=' . rawurlencode($value);
});
- $path = $this->url($url);
- return join("&", array(strtoupper($method), rawurlencode($path), rawurlencode(join('&', $query))));
+ unset($options['token']);
+ $path = $this->url($url, $options);
+ return join("&", array(
+ strtoupper($method), rawurlencode($path), rawurlencode(join('&', $query))
+ ));
}
/**
@@ -238,9 +243,9 @@ class Oauth extends \lithium\net\http\Service {
}
/**
- * Decodes the response.
+ * Decodes the response body.
*
- * @param string $path
+ * @param string $query
* @return void
*/
protected function _decode($query = null) {
@@ -255,4 +260,5 @@ class Oauth extends \lithium\net\http\Service {
return $token;
}
}
+
?>
\ No newline at end of file
diff --git a/models/Consumer.php b/models/Consumer.php
index deb58d4..1a6d698 100644
--- a/models/Consumer.php
+++ b/models/Consumer.php
@@ -62,7 +62,10 @@ class Consumer extends \lithium\core\StaticObject {
/**
* Signs and Sends a post request to the request token endpoint with optional params
*
+ * @param string $type the type of token to get. request|access
* @param array $options optional params for the request
+ * - `method`: POST
+ * - `oauth_signature_method`: HMAC-SHA1
* @return string
*/
public static function token($type, $options = array()) {
@@ -74,21 +77,21 @@ class Consumer extends \lithium\core\StaticObject {
/**
* get url from remote authorization endpoint along with request params
*
- * @param mixed $token
+ * @param array $token
* @return string
*/
- public static function authorize($token) {
- return static::$_service->url('authorize', $token);
+ public static function authorize(array $token = array()) {
+ return static::$_service->url('authorize', compact('token'));
}
/**
* get url from remote authenticated endpoint along with token
*
- * @param mixed $token
+ * @param array $token
* @return string
*/
- public static function authenticate($token) {
- return static::$_service->url('authenticate', $token);
+ public static function authenticate(array $token = array()) {
+ return static::$_service->url('authenticate', compact('token'));
}
/**
diff --git a/tests/cases/extensions/service/OauthTest.php b/tests/cases/extensions/service/OauthTest.php
index efb1d3a..03a0cfc 100644
--- a/tests/cases/extensions/service/OauthTest.php
+++ b/tests/cases/extensions/service/OauthTest.php
@@ -85,15 +85,15 @@ class OauthTest extends \lithium\test\Unit {
public function testConfigUrl() {
$oauth = new MockOauth($this->_testConfig);
$expected = 'http://localhost:80/';
- $result = $oauth->url();
+ $result = $oauth->url(null, array('usePort' => true));
$this->assertEqual($expected, $result);
- $expected = 'http://localhost:80/oauth/request_token';
+ $expected = 'http://localhost/oauth/request_token';
$result = $oauth->url('request');
$this->assertEqual($expected, $result);
- $expected = 'http://localhost:80/oauth/access_token';
+ $expected = 'http://localhost/oauth/access_token';
$result = $oauth->url('access');
$this->assertEqual($expected, $result);
@@ -102,6 +102,7 @@ class OauthTest extends \lithium\test\Unit {
public function testSign() {
$oauth = new MockOauth($this->_testConfig);
$params = array(
+ 'method' => 'POST',
'oauth_signature_method' => 'HMAC-SHA1',
'params' => array(
'oauth_consumer_key' => 'key',
@@ -111,10 +112,10 @@ class OauthTest extends \lithium\test\Unit {
);
$params = $oauth->sign($params);
- $expected = 'jpQW675nV7uzAbW2jukVr/kfqDA=';
+ $expected = '/dSMA1m+uXGoWB0lV/ncn1S+hBw=';
$result = $params['oauth_signature'];
$this->assertEqual($expected, $result);
-
+
$params = array(
'method' => 'GET',
'oauth_signature_method' => 'HMAC-SHA1',
@@ -126,10 +127,31 @@ class OauthTest extends \lithium\test\Unit {
);
$params = $oauth->sign($params);
- $expected = 'LbeBxtQ9vXOxK6eZgKXBFqIWN7A=';
+ $expected = 'zR1UlutzIhXqWOnf9drJ+koTzMc=';
$result = $params['oauth_signature'];
$this->assertEqual($expected, $result);
}
+ public function testSignAgain() {
+ $this->_testConfig += array(
+ 'request' => 'libraries/oauth_php/example/request_token.php',
+ );
+ $oauth = new MockOauth($this->_testConfig);
+ $params = array(
+ 'method' => 'POST', 'url' => 'request',
+ 'oauth_signature_method' => 'HMAC-SHA1',
+ 'params' => array(
+ 'oauth_consumer_key' => 'key',
+ 'oauth_nonce' => 'eaa196ab3a032e7b2e55d2b3ea21a13d99f1175e',
+ 'oauth_timestamp' => '1276360894',
+ ),
+ );
+ $params = $oauth->sign($params);
+
+ $expected = 'DkFQvURKybQqwkQsf2cASQeJdtU=';
+ $result = $params['oauth_signature'];
+ $this->assertEqual($expected, $result);
+ }
}
+
?>
\ No newline at end of file