Commit: 8df3032cf9c6ccc3b322233041350fe8550038bb

Author: gwoo | Date: 2010-06-02 07:49:05 -0700
refactored pretty much everything. all green.
diff --git a/extensions/service/Oauth.php b/extensions/service/Oauth.php index 56c89ea..41d93d6 100644 --- a/extensions/service/Oauth.php +++ b/extensions/service/Oauth.php @@ -46,8 +46,8 @@ class Oauth extends \lithium\net\http\Service { 'host' => 'localhost', 'authorize' => '/oauth/authorize', 'authenticate' => '/oauth/authenticate', - 'request_token' => '/oauth/request_token', - 'access_token' => '/oauth/access_token', + 'request' => '/oauth/request_token', + 'access' => '/oauth/access_token', 'oauth_consumer_key' => 'key', 'oauth_consumer_secret' => 'secret' ); @@ -97,16 +97,15 @@ class Oauth extends \lithium\net\http\Service { * @return void */ public function send($method, $path = null, $data = null, array $options = array()) { - $defaults = array('via' => 'headers'); + $defaults = array('headers' => true, 'realm' => basename(LITHIUM_APP_PATH)); $options += $defaults; $url = $this->config($path); - $method = !empty($data['method']) ? $data['method'] : 'post'; - $data = $this->sign($data + compact('url')); + $data = $this->sign($data + $options + compact('url')); - if ($options['via'] == 'headers') { - $auth = 'OAuth realm="auth",'; + if ($options['headers']) { + $auth = 'OAuth realm="' . $options['realm'] . '",'; foreach ($data as $key => $val) { - $auth .= $key . '="'.rawurlencode($val).'",'; + $auth .= $key . '="' . rawurlencode($val) . '",'; } $options['headers'] = array('Authorization' => $auth); $data = array(); @@ -120,31 +119,38 @@ class Oauth extends \lithium\net\http\Service { } /** - * undocumented function + * A utility method to return a authorize or authenticate url for redirect * * @param string $url * @return void */ - public function url($url) { - $url = $this->config($url); - return "http://{$this->_config['host']}{$url}"; + public function url($url = null, $token = array()) { + $url = $url ? $this->config($url) : null; + + if (!empty($token['oauth_token'])) { + $url = "{$url}?oauth_token={$token['oauth_token']}"; + } + + return "http://" . str_replace('//', '/', "{$this->_config['host']}/{$url}"); } /** - * undocumented function + * Sign the request * * @param string $options - * - hash: HMAC-SHA1 + * - method: POST + * - url: url of request + * - oauth_signature_method: HMAC-SHA1 * - secret: config['oauth_consumer_secret'] * - params: extra params for to sign request - * - url: url of request * - data: post data for request * - token: array with keys oauth_token, oauth_token_secret * @return void */ public function sign($options = array()) { $defaults = array( - 'url' => '', 'method' => 'POST', 'hash' => 'HMAC-SHA1', + 'url' => '', 'method' => 'POST', + 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_consumer_secret' => $this->_config['oauth_consumer_secret'], 'params' => array(), 'data' => array(), 'token' => array('oauth_token' => null, 'oauth_token_secret' => null), @@ -152,11 +158,12 @@ class Oauth extends \lithium\net\http\Service { $options += $defaults; $params = $this->_params($options['params'] + (array) $options['token']) + $options['data']; $base = $this->_base($options['method'], $options['url'], $params); + $key = join("&", array( rawurlencode($options['oauth_consumer_secret']), rawurlencode($options['token']['oauth_token_secret']) )); - switch ($options['hash']) { + switch ($options['oauth_signature_method']) { case 'HMAC-SHA1': $signature = base64_encode(hash_hmac('sha1', $base, $key, true)); break; @@ -186,10 +193,10 @@ class Oauth extends \lithium\net\http\Service { } /** - * undocumented function + * Handles Oauth specific parameters to ensure they have correct values and order. * * @param string $params - * @return void + * @return array $params */ protected function _params($params = array()) { $defaults = array( @@ -219,7 +226,7 @@ class Oauth extends \lithium\net\http\Service { } /** - * undocumented function + * Decodes the response. * * @param string $path * @return void diff --git a/models/Consumer.php b/models/Consumer.php index 06e7083..1373723 100644 --- a/models/Consumer.php +++ b/models/Consumer.php @@ -59,35 +59,9 @@ class Consumer extends \lithium\core\StaticObject { * @return string */ public static function token($type, $params = array(), $options = array()) { - return static::$_service->send('request_token', $params + array( - 'hash' => 'HMAC-SHA1', 'method' => 'POST' - ), $options); - } - - /** - * Signs and Sends request to access token endpoint with the token returned from request method - * - * @param array $token return value from `Consumer::request()` - * @return string - */ - public static function access($token, $params = array(), $options = array()) { - return static::$_service->send('access_token', $params + array( - 'hash' => 'HMAC-SHA1', 'method' => 'POST', 'token' => (array) $token, - ), $options); - } - - /** - * Signs and Sends a post request to the given url - * - * @param string $url request path that follows host: eg `/statues/update.json` - * @param array $token the token from a request - * @param array $data data to send as the body of the request - * @return string - */ - public static function post($url, $token, $data = array(), $params = array(), $options = array()) { - return static::$_service->send($url, $params + array( - 'hash' => 'HMAC-SHA1', 'method' => 'POST', 'token' => (array) $token, 'data' => $data - ), $options); + $defaults = array('method' => 'POST', 'oauth_signature_method' => 'HMAC-SHA1'); + $options += $defaults; + return static::$_service->send($options['method'], $type, $params, $options); } /** @@ -97,14 +71,7 @@ class Consumer extends \lithium\core\StaticObject { * @return string */ public static function authorize($token) { - $url = static::$_service->url('authorize'); - if (is_array($token)) { - if (empty($token['oauth_token'])) { - return $url; - } - $token = $token['oauth_token']; - } - return "{$url}?oauth_token={$token}"; + return static::$_service->url('authorize', $token); } /** @@ -114,14 +81,7 @@ class Consumer extends \lithium\core\StaticObject { * @return string */ public static function authenticate($token) { - $url = static::$_service->url('authenticate'); - if (is_array($token)) { - if (empty($token['oauth_token'])) { - return $url; - } - $token = $token['oauth_token']; - } - return "{$url}?oauth_token={$token}"; + return static::$_service->url('authenticate', $token); } /** diff --git a/tests/cases/extensions/service/OauthTest.php b/tests/cases/extensions/service/OauthTest.php index 1fb2d77..219ad32 100644 --- a/tests/cases/extensions/service/OauthTest.php +++ b/tests/cases/extensions/service/OauthTest.php @@ -22,7 +22,8 @@ class OauthTest extends \lithium\test\Unit { 'login' => 'root', 'password' => '', 'port' => 80, - 'timeout' => 1 + 'timeout' => 1, + 'oauth_consumer_secret' => 'secret' ); public function testDefaultConfig() { @@ -30,17 +31,17 @@ class OauthTest extends \lithium\test\Unit { $config = $oauth->config(); $expected = '/oauth/request_token'; - $result = $config['request_token']; + $result = $config['request']; $this->assertEqual($expected, $result); } public function testCustomConfig() { - $this->_testConfig['request_token'] = 'request_token.php'; + $this->_testConfig['request'] = 'request_token.php'; $oauth = new MockOauth($this->_testConfig); $config = $oauth->config(); $expected = 'request_token.php'; - $result = $config['request_token']; + $result = $config['request']; $this->assertEqual($expected, $result); } @@ -59,9 +60,7 @@ class OauthTest extends \lithium\test\Unit { 'oauth_token' => 'requestkey', 'oauth_token_secret' => 'requestsecret' ); - $result = $oauth->post('request_token', array( - 'hash' => 'HMAC-SHA1', 'params' => array() - )); + $result = $oauth->post('request'); $this->assertEqual($expected, $result); } @@ -72,8 +71,8 @@ class OauthTest extends \lithium\test\Unit { 'oauth_token' => 'accesskey', 'oauth_token_secret' => 'accesssecret' ); - $result = $oauth->post('access_token', array( - 'hash' => 'HMAC-SHA1', 'params' => array(), + $result = $oauth->post('access', array( + 'params' => array(), 'token' => array( 'oauth_token' => 'requestkey', 'oauth_token_secret' => 'requestsecret' @@ -85,9 +84,25 @@ class OauthTest extends \lithium\test\Unit { public function testConfigUrl() { $oauth = new MockOauth($this->_testConfig); $expected = 'http://localhost'; - $result = $oauth->url(''); + $result = $oauth->url(); $this->assertEqual($expected, $result); + } + + public function testSign() { + $oauth = new MockOauth($this->_testConfig); + $params = array( + 'oauth_signature_method' => 'HMAC-SHA1', + 'params' => array( + 'oauth_consumer_key' => 'key', + 'oauth_nonce' => '4d31073c8ce205ecd3145d6cc0a3a4f6', + 'oauth_timestamp' => '1259606608', + ), + ); + $params = $oauth->sign($params); + $expected = 'FRBjLiJQEDyekFLxtK2EaoSDFOU='; + $result = $params['oauth_signature']; + $this->assertEqual($expected, $result); } } ?> \ No newline at end of file diff --git a/tests/cases/models/ConsumerTest.php b/tests/cases/models/ConsumerTest.php index 7a58d1e..991d57b 100644 --- a/tests/cases/models/ConsumerTest.php +++ b/tests/cases/models/ConsumerTest.php @@ -17,8 +17,8 @@ class ConsumerTest extends \lithium\test\Unit { 'host' => 'localhost', 'oauth_consumer_key' => 'key', 'oauth_consumer_secret' => 'secret', - 'request_token' => 'libraries/oauth_php/example/request_token.php', - 'access_token' => 'libraries/oauth_php/example/access_token.php', + 'request' => 'libraries/oauth_php/example/request_token.php', + 'access' => 'libraries/oauth_php/example/access_token.php', 'port' => 30500 )); } @@ -31,40 +31,51 @@ class ConsumerTest extends \lithium\test\Unit { )); $this->assertEqual($expected, $result); } - - public function testRequestToken() { - $expected = 'http://localhost/oauth/authorize?oauth_token=requestkey'; - $result = Consumer::authorize(array( + + public function testAuthenticate() { + $expected = 'http://localhost/oauth/authenticate?oauth_token=requestkey'; + $result = Consumer::authenticate(array( 'oauth_token' => 'requestkey', 'oauth_token_secret' => 'requestsecret' )); $this->assertEqual($expected, $result); } - - public function testAccessToken() { - $expected = 'http://localhost/oauth/authorize?oauth_token=requestkey'; - $result = Consumer::authorize(array( + + public function testRequestToken() { + $expected = array( 'oauth_token' => 'requestkey', 'oauth_token_secret' => 'requestsecret' + ); + $result = Consumer::token('request', array( + 'oauth_token' => 'key', + 'oauth_token_secret' => 'secret' )); $this->assertEqual($expected, $result); } - - public function testPost() { - $expected = 'http://localhost/oauth/authorize?oauth_token=requestkey'; - $result = Consumer::post(array( + + public function testAccessToken() { + $expected = array( + 'oauth_token' => 'accesskey', + 'oauth_token_secret' => 'accesssecret' + ); + $token = array( 'oauth_token' => 'requestkey', 'oauth_token_secret' => 'requestsecret' - )); + ); + $result = Consumer::token('access', compact('token')); $this->assertEqual($expected, $result); } - - public function testGet() { - $expected = 'http://localhost/oauth/authorize?oauth_token=requestkey'; - $result = Consumer::authorize(array( + + public function testPost() { + $expected = '{"test":"cool"}'; + $token = array( 'oauth_token' => 'requestkey', 'oauth_token_secret' => 'requestsecret' - )); + ); + Consumer::config(array('classes' => array( + 'socket' => '\li3_oauth\tests\mocks\extensions\service\MockSocket', + ))); + $result = Consumer::post('search', array(), compact('token')); $this->assertEqual($expected, $result); } } diff --git a/tests/cases/models/ProviderTest.php b/tests/cases/models/ProviderTest.php index 27ddc35..3360f52 100644 --- a/tests/cases/models/ProviderTest.php +++ b/tests/cases/models/ProviderTest.php @@ -58,7 +58,7 @@ class ProviderTest extends \lithium\test\Unit { public function testVerify() { $request = array( - 'url' => 'request_token', + 'url' => 'request', 'params' => array( 'oauth_consumer_key' => 'key', 'oauth_nonce' => '4d31073c8ce205ecd3145d6cc0a3a4f6', @@ -73,7 +73,7 @@ class ProviderTest extends \lithium\test\Unit { public function testVerifyWithToken() { $request = array( - 'url' => 'request_token', + 'url' => 'request', 'params' => array( 'oauth_consumer_key' => 'key', 'oauth_nonce' => '4d31073c8ce205ecd3145d6cc0a3a4f6', diff --git a/tests/mocks/extensions/service/MockSocket.php b/tests/mocks/extensions/service/MockSocket.php index 4025e92..ca4f535 100644 --- a/tests/mocks/extensions/service/MockSocket.php +++ b/tests/mocks/extensions/service/MockSocket.php @@ -55,6 +55,9 @@ class MockSocket extends \lithium\net\Socket { if (strpos($message->path, 'access_token') !== false) { $body = 'oauth_token=accesskey&oauth_token_secret=accesssecret'; } + if (strpos($message->path, 'search') !== false) { + $body = json_encode(array('test' => 'cool')); + } $message = $this->read($body); return new $options['classes']['response'](compact('message')); }