Commit: c4ba03fecc32664b84cb70f62893e2c7568297d9

Author: mhueneburg | Date: 2010-06-07 23:56:20 +0200
Fixed issue where request method would be ignored in base generation. Fixed issue where request data would be sent inside Authoriziation header. Added request port to url. Updated test cases.
diff --git a/extensions/service/Oauth.php b/extensions/service/Oauth.php index 435b59b..4fc0a3b 100644 --- a/extensions/service/Oauth.php +++ b/extensions/service/Oauth.php @@ -44,6 +44,7 @@ class Oauth extends \lithium\net\http\Service { public function __construct($config = array()) { $defaults = array( 'host' => 'localhost', + 'port' => 80, 'authorize' => '/oauth/authorize', 'authenticate' => '/oauth/authenticate', 'request' => '/oauth/request_token', @@ -99,19 +100,26 @@ 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)); + $defaults = array( + 'headers' => true, + 'realm' => basename(LITHIUM_APP_PATH), + 'method' => $method, + 'port' => $this->_config['port'] + ); $options += $defaults; $url = $this->config($path); - $data = $this->sign($options + compact('data', 'url')); + $oauth = $this->sign($options + compact('data', 'url')); if ($options['headers']) { $auth = 'OAuth realm="' . $options['realm'] . '",'; - foreach ($data as $key => $val) { + foreach ($oauth as $key => $val) { $auth .= $key . '="' . rawurlencode($val) . '",'; } $options['headers'] = array('Authorization' => $auth); - $data = array(); - } + $oauth = array(); + } + $data += $oauth; + $response = parent::send($method, $url, $data, $options); if (strpos($response, 'oauth_token=') !== false) { @@ -133,7 +141,7 @@ class Oauth extends \lithium\net\http\Service { $url = "{$url}?oauth_token={$token['oauth_token']}"; } - return "http://" . str_replace('//', '/', "{$this->_config['host']}/{$url}"); + return "http://" . str_replace('//', '/', "{$this->_config['host']}:{$this->_config['port']}/{$url}"); } /** @@ -158,9 +166,8 @@ class Oauth extends \lithium\net\http\Service { 'token' => array('oauth_token' => null, 'oauth_token_secret' => null), ); $options += $defaults; - $params = $this->_params((array) $options['params'] + (array) $options['token']) - + (array) $options['data']; - $base = $this->_base($options['method'], $options['url'], $params); + $params = $this->_params((array) $options['params'] + (array) $options['token']); + $base = $this->_base($options['method'], $options['url'], ($params + (array) $options['data'])); $key = join("&", array( rawurlencode($options['oauth_consumer_secret']), @@ -187,12 +194,13 @@ class Oauth extends \lithium\net\http\Service { * @return void */ protected function _base($method, $url, $params) { + uksort($params, 'strcmp'); $query = array(); array_walk($params, function ($value, $key) use (&$query){ $query[] = $key . '=' . rawurlencode($value); }); $path = $this->url($url); - return join("&", array($method, rawurlencode($path), rawurlencode(join('&', $query)))); + return join("&", array(strtoupper($method), rawurlencode($path), rawurlencode(join('&', $query)))); } /** diff --git a/tests/cases/extensions/service/OauthTest.php b/tests/cases/extensions/service/OauthTest.php index 0e3ac83..efb1d3a 100644 --- a/tests/cases/extensions/service/OauthTest.php +++ b/tests/cases/extensions/service/OauthTest.php @@ -84,16 +84,16 @@ class OauthTest extends \lithium\test\Unit { public function testConfigUrl() { $oauth = new MockOauth($this->_testConfig); - $expected = 'http://localhost/'; + $expected = 'http://localhost:80/'; $result = $oauth->url(); $this->assertEqual($expected, $result); - $expected = 'http://localhost/oauth/request_token'; + $expected = 'http://localhost:80/oauth/request_token'; $result = $oauth->url('request'); $this->assertEqual($expected, $result); - $expected = 'http://localhost/oauth/access_token'; + $expected = 'http://localhost:80/oauth/access_token'; $result = $oauth->url('access'); $this->assertEqual($expected, $result); @@ -111,9 +111,25 @@ class OauthTest extends \lithium\test\Unit { ); $params = $oauth->sign($params); - $expected = '/dSMA1m+uXGoWB0lV/ncn1S+hBw='; + $expected = 'jpQW675nV7uzAbW2jukVr/kfqDA='; + $result = $params['oauth_signature']; + $this->assertEqual($expected, $result); + + $params = array( + 'method' => 'GET', + 'oauth_signature_method' => 'HMAC-SHA1', + 'params' => array( + 'oauth_consumer_key' => 'key', + 'oauth_nonce' => '4d31073c8ce205ecd3145d6cc0a3a4f6', + 'oauth_timestamp' => '1259606608', + ), + ); + $params = $oauth->sign($params); + + $expected = 'LbeBxtQ9vXOxK6eZgKXBFqIWN7A='; $result = $params['oauth_signature']; $this->assertEqual($expected, $result); } + } ?> \ No newline at end of file