Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

adding client and updating server

  • Loading branch information...
commit 0d45515e3717ebd1bdb68a0a788cb81f51b70a5c 1 parent 1a9e452
GWoo authored
7  config/routes.php
@@ -2,7 +2,12 @@
2 2
 
3 3
 use \lithium\http\Router;
4 4
 
5  
-
  5
+Router::connect('/oauth', array(
  6
+	'plugin' => 'li3_oauth', 'controller' => 'server', 'action' => 'account'
  7
+));
  8
+Router::connect('/oauth/client/{:action}/{:args}', array(
  9
+	'plugin' => 'li3_oauth', 'controller' => 'client', 'action' => 'index'
  10
+));
6 11
 Router::connect('/oauth/{:action}/{:args}', array(
7 12
 	'plugin' => 'li3_oauth', 'controller' => 'server', 'action' => 'index'
8 13
 ));
73  controllers/ClientController.php
... ...
@@ -0,0 +1,73 @@
  1
+<?php
  2
+
  3
+namespace li3_oauth\controllers;
  4
+
  5
+use \li3_oauth\models\Consumer;
  6
+use \lithium\storage\Session;
  7
+
  8
+class ClientController extends \lithium\action\Controller {
  9
+
  10
+	protected function _init() {
  11
+		parent::_init();
  12
+		Consumer::config(array(
  13
+			'host' => $this->request->env('SERVER_NAME'),
  14
+			'oauth_consumer_key' => '59f87a2f8e430bbad5c84b61ed06304fc9204bcb',
  15
+			'oauth_consumer_secret' => '4b498c24588bc56685e68f0d2c52ee6becf96ba3',
  16
+			'request_token' => $this->request->env('base') . '/oauth/request_token',
  17
+			'access_token' => $this->request->env('base') . '/oauth/request_token',
  18
+			'authorize' => $this->request->env('base') . '/oauth/authorize',
  19
+			'port' => 30501
  20
+		));
  21
+	}
  22
+
  23
+	public function index() {
  24
+		$message = null;
  25
+		$token = Session::read('oauth.access');
  26
+
  27
+		if (empty($token) && !empty($this->request->query['oauth_token'])) {
  28
+			$this->redirect(array('controller' => 'client', 'action' => 'access'));
  29
+		}
  30
+
  31
+		if (empty($token)) {
  32
+			$this->redirect(array('controller' => 'client', 'action' => 'authorize'));
  33
+		}
  34
+		if (!empty($this->request->data)) {
  35
+			$url = 'statuses/update.json';
  36
+			$result = Consumer::post($url, $token, $this->request->data);
  37
+			$message = json_decode($result);
  38
+		}
  39
+		return compact('message');
  40
+	}
  41
+
  42
+	public function authorize() {
  43
+		$token = Consumer::request();
  44
+		if (is_array($token) && !empty($token['oauth_token'])) {
  45
+			$token += array(
  46
+				'oauth_callback_url' => 'http://' .
  47
+					$this->request->env('HTTP_HOST') . $this->request->env('base') .
  48
+					'/oauth/client/access'
  49
+			);
  50
+			Session::write('oauth.request', $token);
  51
+			$this->redirect(Consumer::authorize($token));
  52
+		}
  53
+
  54
+		return (string) $token;
  55
+	}
  56
+
  57
+	public function access() {
  58
+		$token = Session::read('oauth.request');
  59
+		$access = Consumer::access((array) $token);
  60
+		Session::write('oauth.access', $access);
  61
+		$this->redirect(array('controller' => 'client', 'action' => 'index'));
  62
+	}
  63
+
  64
+	public function login() {
  65
+		$token = Session::read('oauth.request');
  66
+		if (empty($token)) {
  67
+			$this->redirect(array('controller' => 'client', 'action' => 'authorize'));
  68
+		}
  69
+		$this->redirect(Consumer::authenticate($token));
  70
+	}
  71
+
  72
+}
  73
+?>
28  controllers/ServerController.php
@@ -16,28 +16,36 @@ class ServerController extends \lithium\action\Controller {
16 16
 	protected function _init() {
17 17
 		parent::_init();
18 18
 		Provider::config(array(
19  
-			'host' => 'localhost',
20  
-			'request_token' => 'union-of-rad/rad-dev/plugins/oauth/request_token',
21  
-			'access_token' => 'union-of-rad/rad-dev/plugins/oauth/access_token',
22  
-			'port' => 30500
  19
+			'host' => $this->request->env('SERVER_NAME'),
  20
+			'request_token' => $this->request->env('base') . '/oauth/request_token',
  21
+			'access_token' => $this->request->env('base') . '/oauth/request_token',
  22
+			'authorize' => $this->request->env('base') . '/oauth/authorize',
  23
+			'port' => 30501
23 24
 		));
24 25
 	}
25 26
 
26 27
 	public function request_token() {
27 28
 		if (empty($this->request->data)) {
28  
-			return 'Invalid Request';
  29
+			return $this->render(array('text' => 'Invalid Request', 'status' => 401));
29 30
 		}
  31
+
30 32
 		$consumer = Provider::fetch($this->request->data['oauth_consumer_key']);
31  
-		$request = array(
  33
+		if (!$consumer) {
  34
+			return $this->render(array('text' => 'Invalid Consumer Key', 'status' => 401));
  35
+		}
  36
+
  37
+		$isValid = Provider::verify(array(
32 38
 			'params' => $this->request->data, 'url' => 'request_token',
33  
-		) + (array) $consumer;
34  
-		if (Provider::verify($request)) {
  39
+		) + (array) $consumer);
  40
+
  41
+		if ($isValid) {
35 42
 			$token = Provider::create('token');
36 43
 			$data = (array) $consumer + (array) $token;
37 44
 			Provider::store($consumer->oauth_consumer_key, $data);
38 45
 			Provider::store($token->oauth_token, $data);
39 46
 			return http_build_query((array) $token);
40 47
 		}
  48
+		$this->render(array('text' => 'Invalid Signature', 'status' => 401));
41 49
 	}
42 50
 
43 51
 	public function authorize() {
@@ -47,10 +55,10 @@ public function authorize() {
47 55
 		}
48 56
 
49 57
 		if (!empty($this->request->data['allow'])) {
50  
-			
  58
+
51 59
 		}
52 60
 		if (!empty($this->request->data['deny'])) {
53  
-			
  61
+
54 62
 		}
55 63
 		return compact('token');
56 64
 	}
14  extensions/service/Oauth.php
@@ -42,9 +42,9 @@ class Oauth extends \lithium\core\Object {
42 42
 	public function __construct($config = array()) {
43 43
 		$defaults = array(
44 44
 			'host' => null,
45  
-			'authorize' => 'oauth/authorize',
46  
-			'request_token' => 'oauth/request_token',
47  
-			'access_token' => 'oauth/access_token',
  45
+			'authorize' => '/oauth/authorize',
  46
+			'request_token' => '/oauth/request_token',
  47
+			'access_token' => '/oauth/access_token',
48 48
 			'oauth_consumer_key' => 'key',
49 49
 			'oauth_consumer_secret' => 'secret'
50 50
 		);
@@ -95,7 +95,7 @@ public function send($path = null, $data = null, $options = array()) {
95 95
 		$method = !empty($options['method']) ? $options['method'] : 'post';
96 96
 		$data = $this->sign($data + compact('url'));
97 97
 		$response = $this->service->send($method, $url, $data, $options);
98  
-		if (in_array($path, array('request_token', 'access_token'))) {
  98
+		if (strpos($response, 'oauth_') === 0) {
99 99
 			return $this->_decode($response);
100 100
 		}
101 101
 		return $response;
@@ -109,7 +109,7 @@ public function send($path = null, $data = null, $options = array()) {
109 109
 	 */
110 110
 	public function url($url) {
111 111
 		$url = $this->config($url);
112  
-		return "http://{$this->_config['host']}/{$url}";
  112
+		return "http://{$this->_config['host']}{$url}";
113 113
 	}
114 114
 
115 115
 	/**
@@ -134,6 +134,7 @@ public function sign($options = array()) {
134 134
 		$options += $defaults;
135 135
 		$params = $this->_build($options['params'] + (array)$options['token']) + $options['data'];
136 136
 		$base = $this->_base($options['method'], $options['url'], $params);
  137
+
137 138
 		$key = join("&", array(
138 139
 			rawurlencode($options['oauth_consumer_secret']),
139 140
 			rawurlencode($options['token']['oauth_token_secret'])
@@ -210,7 +211,8 @@ protected function _build($params = array()) {
210 211
 	protected function _decode($query = null) {
211 212
 		$token = array();
212 213
 		$result = array_filter(explode('&', $query), function ($value) use (&$token) {
213  
-			if ($parts = explode("=", $value)) {
  214
+			$parts = explode("=", $value);
  215
+			if (count($parts) > 1) {
214 216
 				$token[rawurldecode($parts[0])] = rawurldecode($parts[1]);
215 217
 			}
216 218
 			return false;
3  extensions/storage/File.php
@@ -58,6 +58,9 @@ public function read($key = null) {
58 58
 		if (isset($data->{$key})) {
59 59
 			return $data->{$key};
60 60
 		}
  61
+		if ($key) {
  62
+			return null;
  63
+		}
61 64
 		return $data;
62 65
 	}
63 66
 	
64  models/Consumer.php
@@ -52,10 +52,10 @@ public static function config($config) {
52 52
 	 * @param array $options optional params for the request
53 53
 	 * @return string
54 54
 	 */
55  
-	public static function request($options = array()) {
56  
-		return static::$_service->send('request_token', $options + array(
  55
+	public static function request($params = array(), $options = array()) {
  56
+		return static::$_service->send('request_token', $params + array(
57 57
 			'hash' => 'HMAC-SHA1', 'method' => 'POST'
58  
-		));
  58
+		), $options);
59 59
 	}
60 60
 
61 61
 	/**
@@ -64,10 +64,10 @@ public static function request($options = array()) {
64 64
 	 * @param array $token return value from `Consumer::request()`
65 65
 	 * @return string
66 66
 	 */
67  
-	public static function access($token, $options = array()) {
68  
-		return static::$_service->send('access_token', $options + array(
  67
+	public static function access($token, $params = array(), $options = array()) {
  68
+		return static::$_service->send('access_token', $params + array(
69 69
 			'hash' => 'HMAC-SHA1', 'method' => 'POST', 'token' => (array) $token,
70  
-		));
  70
+		), $options);
71 71
 	}
72 72
 
73 73
 	/**
@@ -78,10 +78,10 @@ public static function access($token, $options = array()) {
78 78
 	 * @param array $data data to send as the body of the request
79 79
 	 * @return string
80 80
 	 */
81  
-	public static function post($url, $token, $data = array(), $options = array()) {
82  
-		return static::$_service->send($url, $options + array(
  81
+	public static function post($url, $token, $data = array(), $params, $options = array()) {
  82
+		return static::$_service->send($url, $params + array(
83 83
 			'hash' => 'HMAC-SHA1', 'method' => 'POST', 'token' => (array) $token, 'data' => $data
84  
-		));
  84
+		), $options);
85 85
 	}
86 86
 
87 87
 	/**
@@ -91,11 +91,16 @@ public static function post($url, $token, $data = array(), $options = array()) {
91 91
 	 * @return string
92 92
 	 */
93 93
 	public static function authorize($token) {
94  
-		$token = (is_array($token) && isset($token['oauth_token'])) ? $token['oauth_token'] : $token;
95 94
 		$url = static::$_service->url('authorize');
  95
+		if (is_array($token)) {
  96
+			if (empty($token['oauth_token'])) {
  97
+				return $url;
  98
+			}
  99
+			$token = $token['oauth_token'];
  100
+		}
96 101
 		return "{$url}?oauth_token={$token}";
97 102
 	}
98  
-	
  103
+
99 104
 	/**
100 105
 	 * get url from remote authenticated endpoint along with token
101 106
 	 *
@@ -103,11 +108,46 @@ public static function authorize($token) {
103 108
 	 * @return string
104 109
 	 */
105 110
 	public static function authenticate($token) {
106  
-		$token = (is_array($token) && isset($token['oauth_token'])) ? $token['oauth_token'] : $token;
107 111
 		$url = static::$_service->url('authenticate');
  112
+		if (is_array($token)) {
  113
+			if (empty($token['oauth_token'])) {
  114
+				return $url;
  115
+			}
  116
+			$token = $token['oauth_token'];
  117
+		}
108 118
 		return "{$url}?oauth_token={$token}";
109 119
 	}
  120
+	
  121
+	/**
  122
+	 * undocumented function
  123
+	 *
  124
+	 * @param string $key
  125
+	 * @param string $value
  126
+	 * @return void
  127
+	 */
  128
+	public static function store($key, $value) {
  129
+		return static::$_service->storage->write($key, $value);
  130
+	}
110 131
 
  132
+	/**
  133
+	 * undocumented function
  134
+	 *
  135
+	 * @param string $key
  136
+	 * @return void
  137
+	 */
  138
+	public static function fetch($key) {
  139
+		return static::$_service->storage->read($key);
  140
+	}
  141
+	
  142
+	/**
  143
+	 * undocumented function
  144
+	 *
  145
+	 * @param string $key
  146
+	 * @return void
  147
+	 */
  148
+	public static function delete($key) {
  149
+		return static::$_service->storage->remove($key);
  150
+	}
111 151
 }
112 152
 
113 153
 ?>
10  models/Provider.php
@@ -113,6 +113,16 @@ public static function store($key, $value) {
113 113
 	public static function fetch($key) {
114 114
 		return static::$_service->storage->read($key);
115 115
 	}
  116
+	
  117
+	/**
  118
+	 * undocumented function
  119
+	 *
  120
+	 * @param string $key
  121
+	 * @return void
  122
+	 */
  123
+	public static function delete($key) {
  124
+		return static::$_service->storage->remove($key);
  125
+	}
116 126
 }
117 127
 
118 128
 ?>
1  views/server/account.html.php
... ...
@@ -1,4 +1,5 @@
1 1
 <div class="account">
  2
+	<h2>your consumer configuration</h2>
2 3
 	<ul>
3 4
 		<li>Key: <?=$token->oauth_consumer_key;?></li>
4 5
 		<li>Secret: <?=$token->oauth_consumer_secret;?></li>

0 notes on commit 0d45515

Please sign in to comment.
Something went wrong with that request. Please try again.