Permalink
Browse files

adding client and updating server

  • Loading branch information...
gwoo committed Nov 30, 2009
1 parent 1a9e452 commit 0d45515e3717ebd1bdb68a0a788cb81f51b70a5c
View
@@ -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'
));
@@ -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));
+ }
+
+}
+?>
@@ -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 @@ public function authorize() {
}
if (!empty($this->request->data['allow'])) {
-
+
}
if (!empty($this->request->data['deny'])) {
-
+
}
return compact('token');
}
@@ -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 @@ public function send($path = null, $data = null, $options = array()) {
$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 @@ public function send($path = null, $data = null, $options = array()) {
*/
public function url($url) {
$url = $this->config($url);
- return "http://{$this->_config['host']}/{$url}";
+ return "http://{$this->_config['host']}{$url}";
}
/**
@@ -134,6 +134,7 @@ public function sign($options = array()) {
$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 @@ protected function _build($params = array()) {
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;
@@ -58,6 +58,9 @@ public function read($key = null) {
if (isset($data->{$key})) {
return $data->{$key};
}
+ if ($key) {
+ return null;
+ }
return $data;
}
View
@@ -52,10 +52,10 @@ public static function config($config) {
* @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 @@ public static function request($options = array()) {
* @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 @@ public static function access($token, $options = array()) {
* @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,23 +91,63 @@ public static function post($url, $token, $data = array(), $options = array()) {
* @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
*
* @param mixed $token
* @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);
+ }
}
?>
View
@@ -113,6 +113,16 @@ public static function store($key, $value) {
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);
+ }
}
?>
@@ -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>

0 comments on commit 0d45515

Please sign in to comment.