Commit: 0d45515e3717ebd1bdb68a0a788cb81f51b70a5c

Author: gwoo | Date: 2009-11-30 14:06:40 -0800
adding client and updating server
diff --git a/config/routes.php b/config/routes.php index 4a2117d..d6b3c08 100644 --- a/config/routes.php +++ b/config/routes.php @@ -2,7 +2,12 @@ use \lithium\http\Router; - +Router::connect('/oauth', array( + 'plugin' => 'li3_oauth', 'controller' => 'server', 'action' => 'account' +)); +Router::connect('/oauth/client/{:action}/{:args}', array( + 'plugin' => 'li3_oauth', 'controller' => 'client', 'action' => 'index' +)); Router::connect('/oauth/{:action}/{:args}', array( 'plugin' => 'li3_oauth', 'controller' => 'server', 'action' => 'index' )); diff --git a/controllers/ClientController.php b/controllers/ClientController.php new file mode 100644 index 0000000..d0ed438 --- /dev/null +++ b/controllers/ClientController.php @@ -0,0 +1,73 @@ +<?php + +namespace li3_oauth\controllers; + +use \li3_oauth\models\Consumer; +use \lithium\storage\Session; + +class ClientController extends \lithium\action\Controller { + + protected function _init() { + parent::_init(); + Consumer::config(array( + 'host' => $this->request->env('SERVER_NAME'), + 'oauth_consumer_key' => '59f87a2f8e430bbad5c84b61ed06304fc9204bcb', + 'oauth_consumer_secret' => '4b498c24588bc56685e68f0d2c52ee6becf96ba3', + 'request_token' => $this->request->env('base') . '/oauth/request_token', + 'access_token' => $this->request->env('base') . '/oauth/request_token', + 'authorize' => $this->request->env('base') . '/oauth/authorize', + 'port' => 30501 + )); + } + + public function index() { + $message = null; + $token = Session::read('oauth.access'); + + if (empty($token) && !empty($this->request->query['oauth_token'])) { + $this->redirect(array('controller' => 'client', 'action' => 'access')); + } + + if (empty($token)) { + $this->redirect(array('controller' => 'client', 'action' => 'authorize')); + } + if (!empty($this->request->data)) { + $url = 'statuses/update.json'; + $result = Consumer::post($url, $token, $this->request->data); + $message = json_decode($result); + } + return compact('message'); + } + + public function authorize() { + $token = Consumer::request(); + if (is_array($token) && !empty($token['oauth_token'])) { + $token += array( + 'oauth_callback_url' => 'http://' . + $this->request->env('HTTP_HOST') . $this->request->env('base') . + '/oauth/client/access' + ); + 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); + Session::write('oauth.access', $access); + $this->redirect(array('controller' => 'client', 'action' => 'index')); + } + + public function login() { + $token = Session::read('oauth.request'); + if (empty($token)) { + $this->redirect(array('controller' => 'client', 'action' => 'authorize')); + } + $this->redirect(Consumer::authenticate($token)); + } + +} +?> \ No newline at end of file diff --git a/controllers/ServerController.php b/controllers/ServerController.php index 9ae0bd6..aae8b68 100644 --- a/controllers/ServerController.php +++ b/controllers/ServerController.php @@ -16,28 +16,36 @@ class ServerController extends \lithium\action\Controller { protected function _init() { parent::_init(); Provider::config(array( - 'host' => 'localhost', - 'request_token' => 'union-of-rad/rad-dev/plugins/oauth/request_token', - 'access_token' => 'union-of-rad/rad-dev/plugins/oauth/access_token', - 'port' => 30500 + 'host' => $this->request->env('SERVER_NAME'), + 'request_token' => $this->request->env('base') . '/oauth/request_token', + 'access_token' => $this->request->env('base') . '/oauth/request_token', + 'authorize' => $this->request->env('base') . '/oauth/authorize', + 'port' => 30501 )); } public function request_token() { if (empty($this->request->data)) { - return 'Invalid Request'; + return $this->render(array('text' => 'Invalid Request', 'status' => 401)); } + $consumer = Provider::fetch($this->request->data['oauth_consumer_key']); - $request = array( + if (!$consumer) { + return $this->render(array('text' => 'Invalid Consumer Key', 'status' => 401)); + } + + $isValid = Provider::verify(array( 'params' => $this->request->data, 'url' => 'request_token', - ) + (array) $consumer; - if (Provider::verify($request)) { + ) + (array) $consumer); + + if ($isValid) { $token = Provider::create('token'); $data = (array) $consumer + (array) $token; Provider::store($consumer->oauth_consumer_key, $data); Provider::store($token->oauth_token, $data); return http_build_query((array) $token); } + $this->render(array('text' => 'Invalid Signature', 'status' => 401)); } public function authorize() { @@ -47,10 +55,10 @@ class ServerController extends \lithium\action\Controller { } if (!empty($this->request->data['allow'])) { - + } if (!empty($this->request->data['deny'])) { - + } return compact('token'); } diff --git a/extensions/service/Oauth.php b/extensions/service/Oauth.php index 09508cf..b512882 100644 --- a/extensions/service/Oauth.php +++ b/extensions/service/Oauth.php @@ -42,9 +42,9 @@ class Oauth extends \lithium\core\Object { public function __construct($config = array()) { $defaults = array( 'host' => null, - 'authorize' => 'oauth/authorize', - 'request_token' => 'oauth/request_token', - 'access_token' => 'oauth/access_token', + 'authorize' => '/oauth/authorize', + 'request_token' => '/oauth/request_token', + 'access_token' => '/oauth/access_token', 'oauth_consumer_key' => 'key', 'oauth_consumer_secret' => 'secret' ); @@ -95,7 +95,7 @@ class Oauth extends \lithium\core\Object { $method = !empty($options['method']) ? $options['method'] : 'post'; $data = $this->sign($data + compact('url')); $response = $this->service->send($method, $url, $data, $options); - if (in_array($path, array('request_token', 'access_token'))) { + if (strpos($response, 'oauth_') === 0) { return $this->_decode($response); } return $response; @@ -109,7 +109,7 @@ class Oauth extends \lithium\core\Object { */ public function url($url) { $url = $this->config($url); - return "http://{$this->_config['host']}/{$url}"; + return "http://{$this->_config['host']}{$url}"; } /** @@ -134,6 +134,7 @@ class Oauth extends \lithium\core\Object { $options += $defaults; $params = $this->_build($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']) @@ -210,7 +211,8 @@ class Oauth extends \lithium\core\Object { protected function _decode($query = null) { $token = array(); $result = array_filter(explode('&', $query), function ($value) use (&$token) { - if ($parts = explode("=", $value)) { + $parts = explode("=", $value); + if (count($parts) > 1) { $token[rawurldecode($parts[0])] = rawurldecode($parts[1]); } return false; diff --git a/extensions/storage/File.php b/extensions/storage/File.php index 1afe919..48e1775 100644 --- a/extensions/storage/File.php +++ b/extensions/storage/File.php @@ -58,6 +58,9 @@ class File extends \lithium\core\Object { if (isset($data->{$key})) { return $data->{$key}; } + if ($key) { + return null; + } return $data; } diff --git a/models/Consumer.php b/models/Consumer.php index 113ae19..da57c81 100644 --- a/models/Consumer.php +++ b/models/Consumer.php @@ -52,10 +52,10 @@ class Consumer extends \lithium\core\StaticObject { * @param array $options optional params for the request * @return string */ - public static function request($options = array()) { - return static::$_service->send('request_token', $options + array( + public static function request($params = array(), $options = array()) { + return static::$_service->send('request_token', $params + array( 'hash' => 'HMAC-SHA1', 'method' => 'POST' - )); + ), $options); } /** @@ -64,10 +64,10 @@ class Consumer extends \lithium\core\StaticObject { * @param array $token return value from `Consumer::request()` * @return string */ - public static function access($token, $options = array()) { - return static::$_service->send('access_token', $options + array( + 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); } /** @@ -78,10 +78,10 @@ class Consumer extends \lithium\core\StaticObject { * @param array $data data to send as the body of the request * @return string */ - public static function post($url, $token, $data = array(), $options = array()) { - return static::$_service->send($url, $options + array( + public static function post($url, $token, $data = array(), $params, $options = array()) { + return static::$_service->send($url, $params + array( 'hash' => 'HMAC-SHA1', 'method' => 'POST', 'token' => (array) $token, 'data' => $data - )); + ), $options); } /** @@ -91,11 +91,16 @@ class Consumer extends \lithium\core\StaticObject { * @return string */ public static function authorize($token) { - $token = (is_array($token) && isset($token['oauth_token'])) ? $token['oauth_token'] : $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}"; } - + /** * get url from remote authenticated endpoint along with token * @@ -103,11 +108,46 @@ class Consumer extends \lithium\core\StaticObject { * @return string */ public static function authenticate($token) { - $token = (is_array($token) && isset($token['oauth_token'])) ? $token['oauth_token'] : $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}"; } + + /** + * undocumented function + * + * @param string $key + * @param string $value + * @return void + */ + public static function store($key, $value) { + return static::$_service->storage->write($key, $value); + } + /** + * undocumented function + * + * @param string $key + * @return void + */ + public static function fetch($key) { + return static::$_service->storage->read($key); + } + + /** + * undocumented function + * + * @param string $key + * @return void + */ + public static function delete($key) { + return static::$_service->storage->remove($key); + } } ?> \ No newline at end of file diff --git a/models/Provider.php b/models/Provider.php index 3b7f7c0..7155a7a 100644 --- a/models/Provider.php +++ b/models/Provider.php @@ -113,6 +113,16 @@ class Provider extends \lithium\core\StaticObject { public static function fetch($key) { return static::$_service->storage->read($key); } + + /** + * undocumented function + * + * @param string $key + * @return void + */ + public static function delete($key) { + return static::$_service->storage->remove($key); + } } ?> \ No newline at end of file diff --git a/views/server/account.html.php b/views/server/account.html.php index e313e97..4e7ffb0 100644 --- a/views/server/account.html.php +++ b/views/server/account.html.php @@ -1,4 +1,5 @@ <div class="account"> + <h2>your consumer configuration</h2> <ul> <li>Key: <?=$token->oauth_consumer_key;?></li> <li>Secret: <?=$token->oauth_consumer_secret;?></li>