Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Twitter and facebook examples now working

  • Loading branch information...
commit 0b34d675369c88888066dd47465be1c2862a66a0 1 parent bd553ed
@mackstar authored
View
4 README.md
@@ -0,0 +1,4 @@
+li3_oauth
+
+The li3_oauth library is an attempt to offer you client and server tools in order to fulfil your oauth needs.
+
View
83 controllers/FacebookController.php
@@ -0,0 +1,83 @@
+<?php
+
+namespace li3_oauth\controllers;
+
+use \li3_oauth\models\Consumer;
+use \lithium\storage\Session;
+
+class FacebookController extends \lithium\action\Controller {
+
+ protected function _init() {
+ parent::_init();
+ Consumer::config(array(
+ 'service' => 'oauth2',
+ 'scheme' => 'https',
+ 'port' => '443',
+ 'host' => 'graph.facebook.com',
+ 'secondary_host' => 'www.facebook.com',
+ 'client_id' => '164931116940829',
+ 'scope' => 'email,read_stream',
+ 'client_secret' => '3f588ddd4a4918bd3a119795a25fcf01',
+ 'success' => 'http://local.moodpik.com/facebook/success',
+ ));
+ }
+
+ public function index() {
+ $message = null;
+ $token = Session::read('oauth.access');
+
+ if (empty($token) && !empty($this->request->query['oauth_token'])) {
+ $this->redirect('Tweet::access');
+ }
+ if (empty($token)) {
+ $this->redirect('Tweet::authorize');
+ }
+ if (!empty($this->request->data)) {
+ $result = Consumer::post('/statuses/update.json',
+ $this->request->data,
+ compact('token')
+ );
+ $message = json_decode($result);
+ }
+ return compact('message');
+ }
+
+ public function authorize() {
+ $url = Consumer::url('authorize', array('params' => array(
+ 'scope', 'client_id', 'redirect_uri' => Consumer::serviceConfig('success')
+ )));
+ $this->redirect($url);
+
+ }
+
+ public function success() {
+ $code = $this->request->query['code'];
+ $access = Consumer::token('access', compact('code') + array('params' => array(
+ 'redirect_uri' => Consumer::serviceConfig('success')
+ )));
+ Session::delete('oauth.access');
+ Session::write('oauth.access', $access);
+ $this->redirect('Facebook::feed');
+ }
+
+ public function feed() {
+ if (!$token = Session::read('oauth.access')) {
+ return $this->redirect('Facebook::authorize');
+ }
+ $items = Consumer::get('/me/feed', array(), compact('token'));
+ return $items;
+ }
+
+ public function post() {
+ $token = Session::read('oauth.access');
+ $result = Consumer::post('/1/statuses/update.json',
+ array('status' => 'Testing my status'),
+ compact('token')
+ );
+ echo $result;
+ exit;
+ }
+
+}
+
+?>
View
48 controllers/TweetController.php
@@ -10,9 +10,9 @@ class TweetController extends \lithium\action\Controller {
protected function _init() {
parent::_init();
Consumer::config(array(
- 'host' => 'twitter.com',
- 'oauth_consumer_key' => '',
- 'oauth_consumer_secret' => '',
+ 'host' => 'api.twitter.com',
+ 'oauth_consumer_key' => 'Mn62xF7i5kwT0rVKA3s4Q',
+ 'oauth_consumer_secret' => '82jkQ4OCty2tjbohEhmXYiZcjwPz5up72WDXozARo'
));
}
@@ -37,23 +37,53 @@ public function index() {
}
public function authorize() {
- $token = Consumer::token('request');
+ Session::delete('oauth.request');
+ Session::delete('oauth.access');
+ $token = Consumer::token('request', array('params' => array(
+ 'oauth_callback' => 'http://local.moodpik.com/tweet/success'
+ )));
if (is_string($token)) {
return $token;
}
Session::write('oauth.request', $token);
$this->redirect(Consumer::authorize($token));
}
-
- public function access() {
- $token = Session::read('oauth.request');
+
+ public function success() {
+ $token = $this->request->query;
+ unset($token['url']);
+ $token += Session::read('oauth.request');
$access = Consumer::token('access', compact('token'));
Session::write('oauth.access', $access);
- $this->redirect('Tweet::index');
+ $this->redirect('Tweet::feed');
+ }
+
+
+ public function feed() {
+ $token = Session::read('oauth.access');
+ $items = Consumer::get('/1/statuses/home_timeline.json', array(), compact('token'));
+ echo $items;
+ exit;
+ }
+
+ public function post() {
+ $token = Session::read('oauth.access');
+ $result = Consumer::post('/1/statuses/update.json',
+ array('status' => 'Testing my status'),
+ compact('token')
+ );
+ echo $result;
+ exit;
}
+
public function login() {
- $token = Session::read('oauth.request');
+ Session::delete('oauth.request');
+ Session::delete('oauth.access');
+ $token = Consumer::token('request', array('params' => array(
+ 'oauth_callback' => 'http://local.moodpik.com/tweet/success'
+ )));
+ Session::write('oauth.request', $token);
if (empty($token)) {
$this->redirect('Tweet::authorize');
}
View
71 extensions/service/Oauth.php
@@ -8,6 +8,8 @@
namespace li3_oauth\extensions\service;
+use li3_oauth\models\Consumer;
+
/**
* Oauth service class for handling requests/response to consumers and from providers
*
@@ -34,7 +36,6 @@ class Oauth extends \lithium\net\http\Service {
*
* @param array $config
* - host: the oauth domain
- * - oauth_consumer_key: key from oauth service provider
* - oauth_consumer_secret: secret from oauth service provider
* - oauth_consumer_key: key from oauth service provider
* - authorize: path to authorize url
@@ -85,14 +86,13 @@ public function config($key = null) {
* @param array $options oauth parameters
* - headers : send parameters in the header. (default: true)
* - realm : the realm to authenticate. (default: app directory name)
- * @return void
+ * @return mixed the response from api call
*/
public function send($method, $path = null, $data = array(), array $options = array()) {
$defaults = array('headers' => true, 'realm' => basename(LITHIUM_APP_PATH));
$options += $defaults + $this->_config;
$url = $this->config($path);
$oauth = $this->sign($options + compact('data', 'url', 'method'));
-
if ($options['headers']) {
$header = 'OAuth realm="' . $options['realm'] . '",';
foreach ($oauth as $key => $val) {
@@ -102,6 +102,7 @@ public function send($method, $path = null, $data = array(), array $options = ar
$options['headers'] = array('Authorization' => $header);
}
$options['host'] = $options['proxy'] ? $options['proxy'] : $options['host'];
+
$response = parent::send($method, $url, $data + $oauth, $options);
if (strpos($response, 'oauth_token=') !== false) {
@@ -111,13 +112,13 @@ public function send($method, $path = null, $data = array(), array $options = ar
}
/**
- * A utility method to return a authorize or authenticate url for redirect
+ * A utility method to return an authorize or authenticate url for redirect
*
* @param string $url
* @param array $options
* - `token`: (array) adds the oauth_token to the query params
* - `usePort`: (boolean) use the port in the signature base string
- * @return void
+ * @return stringthe full url
*/
public function url($url = null, array $options = array()) {
$defaults = array('token' => array('oauth_token' => false), 'usePort' => false);
@@ -133,6 +134,28 @@ public function url($url = null, array $options = array()) {
}
/**
+ * undocumented function
+ *
+ * @param string $method
+ * @param string $url
+ * @param array $params
+ * @param array $options
+ * @return void
+ */
+ protected function _base($method, $url, $params, $options) {
+ uksort($params, 'strcmp');
+ $query = array();
+ array_walk($params, function ($value, $key) use (&$query){
+ $query[] = $key . '=' . rawurlencode($value);
+ });
+ unset($options['token']);
+ $path = $this->url($url, $options);
+ return join("&", array(
+ strtoupper($method), rawurlencode($path), rawurlencode(join('&', $query))
+ ));
+ }
+
+ /**
* Sign the request
*
* @param string $options
@@ -175,28 +198,6 @@ public function sign($options = array()) {
}
/**
- * undocumented function
- *
- * @param string $method
- * @param string $url
- * @param array $params
- * @param array $options
- * @return void
- */
- protected function _base($method, $url, $params, $options) {
- uksort($params, 'strcmp');
- $query = array();
- array_walk($params, function ($value, $key) use (&$query){
- $query[] = $key . '=' . rawurlencode($value);
- });
- unset($options['token']);
- $path = $this->url($url, $options);
- return join("&", array(
- strtoupper($method), rawurlencode($path), rawurlencode(join('&', $query))
- ));
- }
-
- /**
* Handles Oauth specific parameters to ensure they have correct values and order.
*
* @param string $params
@@ -204,11 +205,13 @@ protected function _base($method, $url, $params, $options) {
*/
protected function _params($params = array()) {
$defaults = array(
+ 'oauth_callback' => '',
'oauth_consumer_key' => 'key',
'oauth_nonce' => sha1(time() . mt_rand()),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_token' => '',
+ 'oauth_verifier' => '',
'oauth_version' => '1.0'
);
$result = array();
@@ -231,10 +234,22 @@ protected function _params($params = array()) {
}
/**
+ * Requests a token of a particular type
+ *
+ * @param string $query
+ * @return array parameters sent from the response body
+ */
+ public function token($type, array $options = array()) {
+ $defaults = array('method' => 'POST', 'oauth_signature_method' => 'HMAC-SHA1');
+ $options += $defaults;
+ return $this->send($options['method'], $type, array(), $options);
+ }
+
+ /**
* Decodes the response body.
*
* @param string $query
- * @return void
+ * @return array parameters sent from the response body
*/
protected function _decode($query = null) {
parse_str($query, $data);
View
210 extensions/service/Oauth2.php
@@ -0,0 +1,210 @@
+<?php
+/**
+ * Lithium: the most rad php framework
+ *
+ * @copyright Copyright 2009, Union of RAD (http://union-of-rad.org)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ */
+
+namespace li3_oauth\extensions\service;
+
+use li3_oauth\models\Consumer;
+
+/**
+ * Oauth service class for handling requests/response to consumers and from providers
+ *
+ *
+ */
+class Oauth2 extends \lithium\net\http\Service {
+
+ protected $_autoConfig = array('classes' => 'merge');
+
+ /**
+ * Fully-namespaced class references
+ *
+ * @var array
+ */
+ protected $_classes = array(
+ 'media' => '\lithium\net\http\Media',
+ 'request' => '\lithium\net\http\Request',
+ 'response' => '\lithium\net\http\Response',
+ 'socket' => '\lithium\net\socket\Context',
+ );
+
+ /**
+ * Constructor
+ *
+ * @param array $config
+ * - host: the oauth domain
+ * - oauth_consumer_secret: secret from oauth service provider
+ * - oauth_consumer_key: key from oauth service provider
+ * - authorize: path to authorize url
+ * - request_token: path to request token url
+ * - access_token: path to access token url
+ */
+ public function __construct($config = array()) {
+ $defaults = array(
+ 'scheme' => 'http',
+ 'scope' => 'email',
+ 'host' => 'localhost',
+ 'access' => '/oauth/access_token',
+ 'secondary_host' => '',
+ 'proxy' => false,
+ 'port' => 80,
+ 'authorize' => '/dialog/oauth',
+ 'authenticate' => '/dialog/oauth',
+ 'request' => '/oauth/request_token',
+ 'logout' => '/oauth/request_token',
+ 'client_id' => 'key',
+ 'client_secret' => 'secret',
+ 'restponse_type' => 'code',
+ 'state' => '',
+ 'grant_type' => '',
+ 'next' => '',
+ 'logout' => '/logout.php'
+ );
+ $config += $defaults;
+
+ parent::__construct($config);
+ }
+
+ /**
+ * If a key is set returns the value of that key
+ * Without a key it will return config array
+ *
+ * @param string $key eg `oauth_consumer_key`
+ * @return void
+ */
+ public function config($key = null) {
+ if (isset($this->_config[$key])) {
+ return $this->_config[$key];
+ }
+ if ($key !== null) {
+ return $key;
+ }
+ return $this->_config;
+ }
+
+ /**
+ * Requests a token of a particular type
+ *
+ * @param string $query
+ * @return array parameters sent from the response body
+ */
+ public function token($type, array $options = array()) {
+ $defaults = array('params' => array(
+ 'client_id', 'client_secret'
+ ));
+ $options = array_merge_recursive($options, $defaults);
+ $this->_parseParams($options);
+ $url = $this->_config[$type];
+ $result = parent::get($url, $options['params']);
+ return $result;
+ }
+
+ /**
+ * Send request with the given options and data. The token should be part of the options.
+ *
+ * @param string $method
+ * @param string $path
+ * @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 mixed the response from api call
+ */
+ public function send($method, $path = null, $data = array(), array $options = array()) {
+
+ self::_parseParams($options);
+
+ $data += $options['params'];
+ $defaults = array('headers' => true, 'realm' => basename(LITHIUM_APP_PATH));
+ $url = $this->config($path);
+
+ $options['host'] = $this->_config['proxy'] ?: $this->_config['host'];
+
+ $response = parent::send($method, $url, $data);
+
+ $hasToken = (strpos($response, 'access_token=') !== false);
+ $isJson = (strpos($response, '"data":') !== false);
+ if ($hasToken && !$isJson) {
+ return $this->_decode($response);
+ }
+ return $response;
+ }
+
+ /**
+ * A utility method to return an authorize or authenticate url for redirect
+ *
+ * @param string $url the url key for the required url
+ * @param array $options
+ * - `token`: (array) adds the access_token to the query params
+ * - `usePort`: (boolean) use the port in the signature base string
+ * - `params`: (array) use these as additional parameters on the url
+ * @return string the full url
+ */
+ public function url($url = null, array $options = array()) {
+ $defaults = array(
+ 'token' => array('access_token' => false),
+ 'usePort' => false,
+ 'params' => array()
+ );
+ $options += $defaults;
+
+ $secondaryHost = array('authorize', 'authenticate', 'logout');
+ $host = $this->_config['host'];
+ if (isset($this->_config['secondary_host']) && in_array($url, $secondaryHost)) {
+ $host = $this->_config['secondary_host'];
+ }
+
+ $url = $url ? $this->config($url) : null;
+
+ self::_parseParams(&$options);
+
+ $params = !empty($options['params'])? '?' . http_build_query($options['params']) : '';
+
+ $base = $host;
+ $base .= ($options['usePort']) ? ":{$this->_config['port']}" : null;
+ return "{$this->_config['scheme']}://" . str_replace('//', '/', "{$base}/{$url}{$params}");
+ }
+
+ /**
+ * A utility method to return an authorize or authenticate url for redirect
+ *
+ * @param array $options contains the 'params' sub-array
+ * @return void
+ */
+ protected function _parseParams(&$options) {
+ $defaults = array('params' => array());
+ $options += $defaults;
+
+ if (isset($options['token']['access_token']) && $options['token']['access_token']) {
+ $options['params']['access_token'] = $options['token']['access_token'];
+ }
+
+ if (isset($options['code']) && $options['code']) {
+ $options['params']['code'] = $options['code'];
+ }
+
+ foreach($options['params'] as $key => $value) {
+
+ if (isset($this->_config[$value]) && $this->_config[$value]) {
+ $options['params'][$value] = $this->_config[$value];
+ unset($options['params'][$key]);
+ }
+ }
+ }
+
+ /**
+ * Decodes the response body.
+ *
+ * @param string $query
+ * @return array parameters sent from the response body
+ */
+ protected function _decode($query = null) {
+ parse_str($query, $data);
+ return $data;
+ }
+}
+
+?>
View
18 models/Consumer.php
@@ -18,7 +18,8 @@ class Consumer extends \lithium\core\StaticObject {
protected static $_service = null;
protected static $_classes = array(
- 'oauth' => '\li3_oauth\extensions\service\Oauth'
+ 'oauth' => '\li3_oauth\extensions\service\Oauth',
+ 'oauth2' => '\li3_oauth\extensions\service\Oauth2'
);
/**
@@ -41,11 +42,16 @@ class Consumer extends \lithium\core\StaticObject {
* - authorize: path to authorize url
* - request_token: path to request token url
* - access_token: path to access token url
+ * - service: service class name oauth/oauth2
*
* @return void
*/
public static function config($config) {
- static::$_service = new static::$_classes['oauth']($config);
+ if (isset($config['service'])) {
+ static::$_service = new static::$_classes[$config['service']]($config);
+ } else {
+ static::$_service = new static::$_classes['oauth']($config);
+ }
}
/**
@@ -69,9 +75,7 @@ public static function __callStatic($method, $params) {
* @return string
*/
public static function token($type, array $options = array()) {
- $defaults = array('method' => 'POST', 'oauth_signature_method' => 'HMAC-SHA1');
- $options += $defaults;
- return static::$_service->send($options['method'], $type, array(), $options);
+ return static::$_service->token($type, $options);
}
/**
@@ -126,6 +130,10 @@ public static function fetch($key) {
public static function delete($key) {
return static::$_service->storage->remove($key);
}
+
+ public static function serviceConfig($key) {
+ return static::$_service->config($key);
+ }
}
?>
View
6 models/Provider.php
@@ -8,14 +8,14 @@
namespace li3_oauth\models;
-use \li3_oauth\extensions\service\Oauth;
+use \li3_oauth\extensions\service\Oauth1;
class Provider extends \lithium\core\StaticObject {
/**
* Holds an instance of the oauth service class
*
- * @see \li3_oauth\extensions\services\Oauth
+ * @see \li3_oauth\extensions\services\implementations\Oauth1
*/
protected static $_service = null;
@@ -43,7 +43,7 @@ class Provider extends \lithium\core\StaticObject {
* @return void
*/
public static function config($config) {
- static::$_service = new Oauth($config);
+ static::$_service = new Oauth1($config);
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.