summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorZach Copley <zach@controlyourself.ca>2009-08-01 08:20:44 +0000
committerEvan Prodromou <evan@controlyourself.ca>2009-08-24 11:52:15 -0400
commite9edaab3588b18677c2b37a3115f447307e64689 (patch)
tree96476f46d4896f4f3b81c5c3c7e637a10dfcef4d /lib
parent5d5b9f7022ad144cc747a33bcabea773d2f92b28 (diff)
Twitter OAuth server dance working
Diffstat (limited to 'lib')
-rw-r--r--lib/common.php3
-rw-r--r--lib/router.php4
-rw-r--r--lib/twitteroauthclient.php109
3 files changed, 116 insertions, 0 deletions
diff --git a/lib/common.php b/lib/common.php
index 8cd3ae2fc..5d6956d9b 100644
--- a/lib/common.php
+++ b/lib/common.php
@@ -188,6 +188,9 @@ $config =
'integration' =>
array('source' => 'Laconica', # source attribute for Twitter
'taguri' => $_server.',2009'), # base for tag URIs
+ 'twitter' =>
+ array('consumer_key' => null,
+ 'consumer_secret' => null),
'memcached' =>
array('enabled' => false,
'server' => 'localhost',
diff --git a/lib/router.php b/lib/router.php
index 19839b997..6651773c0 100644
--- a/lib/router.php
+++ b/lib/router.php
@@ -88,6 +88,10 @@ class Router
$m->connect('doc/:title', array('action' => 'doc'));
+ // Twitter
+
+ $m->connect('twitter/authorization', array('action' => 'twitterauthorization'));
+
// facebook
$m->connect('facebook', array('action' => 'facebookhome'));
diff --git a/lib/twitteroauthclient.php b/lib/twitteroauthclient.php
new file mode 100644
index 000000000..616fbc213
--- /dev/null
+++ b/lib/twitteroauthclient.php
@@ -0,0 +1,109 @@
+<?php
+
+require_once('OAuth.php');
+
+class TwitterOAuthClient
+{
+ public static $requestTokenURL = 'https://twitter.com/oauth/request_token';
+ public static $authorizeURL = 'https://twitter.com/oauth/authorize';
+ public static $accessTokenURL = 'https://twitter.com/oauth/access_token';
+
+ function __construct($oauth_token = null, $oauth_token_secret = null)
+ {
+ $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
+ $consumer_key = common_config('twitter', 'consumer_key');
+ $consumer_secret = common_config('twitter', 'consumer_secret');
+ $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
+ $this->token = null;
+
+ if (isset($oauth_token) && isset($oauth_token_secret)) {
+ $this->token = new OAuthToken($oauth_token, $oauth_token_secret);
+ }
+ }
+
+ function getRequestToken()
+ {
+ $response = $this->oAuthGet(TwitterOAuthClient::$requestTokenURL);
+ parse_str($response);
+ $token = new OAuthToken($oauth_token, $oauth_token_secret);
+ return $token;
+ }
+
+ function getAuthorizeLink($request_token)
+ {
+ // Not sure Twitter actually looks at oauth_callback
+
+ return TwitterOAuthClient::$authorizeURL .
+ '?oauth_token=' . $request_token->key . '&oauth_callback=' .
+ urlencode(common_local_url('twitterauthorization'));
+ }
+
+ function getAccessToken()
+ {
+ $response = $this->oAuthPost(TwitterOAuthClient::$accessTokenURL);
+ parse_str($response);
+ $token = new OAuthToken($oauth_token, $oauth_token_secret);
+ return $token;
+ }
+
+ function verify_credentials()
+ {
+ $url = 'https://twitter.com/account/verify_credentials.json';
+ $response = $this->oAuthGet($url);
+ $twitter_user = json_decode($response);
+ return $twitter_user;
+ }
+
+ function oAuthGet($url)
+ {
+ $request = OAuthRequest::from_consumer_and_token($this->consumer,
+ $this->token, 'GET', $url, null);
+ $request->sign_request($this->sha1_method,
+ $this->consumer, $this->token);
+
+ return $this->httpRequest($request->to_url());
+ }
+
+ function oAuthPost($url, $params = null)
+ {
+ $request = OAuthRequest::from_consumer_and_token($this->consumer,
+ $this->token, 'POST', $url, $params);
+ $request->sign_request($this->sha1_method,
+ $this->consumer, $this->token);
+
+ return $this->httpRequest($request->get_normalized_http_url(),
+ $request->to_postdata());
+ }
+
+ function httpRequest($url, $params = null)
+ {
+ $options = array(
+ CURLOPT_RETURNTRANSFER => true,
+ CURLOPT_FAILONERROR => true,
+ CURLOPT_HEADER => false,
+ CURLOPT_FOLLOWLOCATION => true,
+ CURLOPT_USERAGENT => 'Laconica',
+ CURLOPT_CONNECTTIMEOUT => 120,
+ CURLOPT_TIMEOUT => 120,
+ CURLOPT_HTTPAUTH => CURLAUTH_ANY,
+ CURLOPT_SSL_VERIFYPEER => false,
+
+ // Twitter is strict about accepting invalid "Expect" headers
+
+ CURLOPT_HTTPHEADER => array('Expect:')
+ );
+
+ if (isset($params)) {
+ $options[CURLOPT_POST] = true;
+ $options[CURLOPT_POSTFIELDS] = $params;
+ }
+
+ $ch = curl_init($url);
+ curl_setopt_array($ch, $options);
+ $response = curl_exec($ch);
+ curl_close($ch);
+
+ return $response;
+ }
+
+}