Commit: 374eb50113025cfe58ce8a94a6c746119ae7996b

Author: gwoo | Date: 2010-06-05 08:37:07 -0700
finishing up refactor.
diff --git a/controllers/ClientController.php b/controllers/ClientController.php index 51dea7d..e6b8b0c 100644 --- a/controllers/ClientController.php +++ b/controllers/ClientController.php @@ -39,14 +39,14 @@ class ClientController extends \lithium\action\Controller { } if (!empty($this->request->data)) { $url = '/statuses/update.json'; - $result = Consumer::post($url, $token, $this->request->data); + $result = Consumer::post($url, $this->request->data, compact('token')); $message = json_decode($result); } return compact('message'); } public function authorize() { - $token = Consumer::request(); + $token = Consumer::token('request'); if (is_array($token) && !empty($token['oauth_token'])) { $token += array( 'oauth_callback_url' => 'http://' . @@ -56,13 +56,12 @@ class ClientController extends \lithium\action\Controller { Session::write('oauth.request', $token); $this->redirect(Consumer::authorize($token)); } - return (string) $token; } public function access() { $token = Session::read('oauth.request'); - $access = Consumer::access((array) $token); + $access = Consumer::token('access', $token); Session::write('oauth.access', $access); $this->redirect(array('controller' => 'client', 'action' => 'index')); } diff --git a/extensions/service/Oauth.php b/extensions/service/Oauth.php index 41d93d6..435b59b 100644 --- a/extensions/service/Oauth.php +++ b/extensions/service/Oauth.php @@ -88,19 +88,21 @@ class Oauth extends \lithium\net\http\Service { } /** - * Send request + * Send request with the given options and data. The token should be part of the options. * * @param string $method * @param string $url - * @param string $data - * @param string $options + * @param array $data encoded for the request + * @param array $options oauth parameters + * - headers : send parameters in the header. (default: true) + * - realm : the realm to authenticate. (default: app directory name) * @return void */ - public function send($method, $path = null, $data = null, array $options = array()) { + public function send($method, $path = null, $data = array(), array $options = array()) { $defaults = array('headers' => true, 'realm' => basename(LITHIUM_APP_PATH)); $options += $defaults; $url = $this->config($path); - $data = $this->sign($data + $options + compact('url')); + $data = $this->sign($options + compact('data', 'url')); if ($options['headers']) { $auth = 'OAuth realm="' . $options['realm'] . '",'; @@ -156,7 +158,8 @@ class Oauth extends \lithium\net\http\Service { 'token' => array('oauth_token' => null, 'oauth_token_secret' => null), ); $options += $defaults; - $params = $this->_params($options['params'] + (array) $options['token']) + $options['data']; + $params = $this->_params((array) $options['params'] + (array) $options['token']) + + (array) $options['data']; $base = $this->_base($options['method'], $options['url'], $params); $key = join("&", array( @@ -208,6 +211,7 @@ class Oauth extends \lithium\net\http\Service { 'oauth_version' => '1.0' ); $result = array(); + foreach ($defaults as $key => $value) { if (isset($params[$key])) { $result[$key] = $params[$key]; diff --git a/models/Consumer.php b/models/Consumer.php index 1373723..deb58d4 100644 --- a/models/Consumer.php +++ b/models/Consumer.php @@ -48,6 +48,13 @@ class Consumer extends \lithium\core\StaticObject { static::$_service = new static::$_classes['oauth']($config); } + /** + * Magic method to pass through HTTP methods. i.e.`Consumer::post()` + * + * @param string $method + * @param string $params + * @return mixed + */ public static function __callStatic($method, $params) { return static::$_service->invokeMethod($method, $params); } @@ -58,10 +65,10 @@ class Consumer extends \lithium\core\StaticObject { * @param array $options optional params for the request * @return string */ - public static function token($type, $params = array(), $options = array()) { + public static function token($type, $options = array()) { $defaults = array('method' => 'POST', 'oauth_signature_method' => 'HMAC-SHA1'); $options += $defaults; - return static::$_service->send($options['method'], $type, $params, $options); + return static::$_service->send($options['method'], $type, array(), $options); } /** diff --git a/tests/cases/extensions/service/OauthTest.php b/tests/cases/extensions/service/OauthTest.php index 219ad32..0e3ac83 100644 --- a/tests/cases/extensions/service/OauthTest.php +++ b/tests/cases/extensions/service/OauthTest.php @@ -36,8 +36,9 @@ class OauthTest extends \lithium\test\Unit { } public function testCustomConfig() { - $this->_testConfig['request'] = 'request_token.php'; - $oauth = new MockOauth($this->_testConfig); + $config = $this->_testConfig; + $config['request'] = 'request_token.php'; + $oauth = new MockOauth($config); $config = $oauth->config(); $expected = 'request_token.php'; @@ -71,7 +72,7 @@ class OauthTest extends \lithium\test\Unit { 'oauth_token' => 'accesskey', 'oauth_token_secret' => 'accesssecret' ); - $result = $oauth->post('access', array( + $result = $oauth->post('access', array('message' => 'hello'), array( 'params' => array(), 'token' => array( 'oauth_token' => 'requestkey', @@ -83,9 +84,19 @@ class OauthTest extends \lithium\test\Unit { public function testConfigUrl() { $oauth = new MockOauth($this->_testConfig); - $expected = 'http://localhost'; + $expected = 'http://localhost/'; $result = $oauth->url(); $this->assertEqual($expected, $result); + + $expected = 'http://localhost/oauth/request_token'; + $result = $oauth->url('request'); + $this->assertEqual($expected, $result); + + + $expected = 'http://localhost/oauth/access_token'; + $result = $oauth->url('access'); + $this->assertEqual($expected, $result); + } public function testSign() { @@ -100,7 +111,7 @@ class OauthTest extends \lithium\test\Unit { ); $params = $oauth->sign($params); - $expected = 'FRBjLiJQEDyekFLxtK2EaoSDFOU='; + $expected = '/dSMA1m+uXGoWB0lV/ncn1S+hBw='; $result = $params['oauth_signature']; $this->assertEqual($expected, $result); } diff --git a/tests/mocks/extensions/service/MockStorage.php b/tests/mocks/extensions/service/MockStorage.php index 6ee6583..ff2525a 100644 --- a/tests/mocks/extensions/service/MockStorage.php +++ b/tests/mocks/extensions/service/MockStorage.php @@ -15,9 +15,9 @@ class MockStorage extends \lithium\net\http\Service { public function read() { return $this->_data; } - + public function write($data, $options = array()) { - $this->_data - $data; + $this->_data = $data; } } \ No newline at end of file