Permalink
Browse files

Merge pull request #13 from oesmith/master

Migrated to cookies
  • Loading branch information...
2 parents e6c2e77 + 3617fea commit eeede3a6d9936c7cb834deb366b3e43be50b1ae4 @jdub committed Mar 4, 2012
Showing with 28 additions and 17 deletions.
  1. +26 −16 lib/twitter.js
  2. +2 −1 package.json
View
@@ -2,7 +2,8 @@ var VERSION = '0.1.17',
http = require('http'),
querystring = require('querystring'),
oauth = require('oauth'),
- cookie = require('cookie'),
+ Cookies = require('cookies'),
+ Keygrip = require('keygrip'),
streamparser = require('./parser');
function merge(defaults, options) {
@@ -37,6 +38,7 @@ function Twitter(options) {
access_token_url: 'https://api.twitter.com/oauth/access_token',
authenticate_url: 'https://api.twitter.com/oauth/authenticate',
authorize_url: 'https://api.twitter.com/oauth/authorize',
+ callback_url: null,
rest_base: 'https://api.twitter.com/1',
search_base: 'https://search.twitter.com',
@@ -51,12 +53,17 @@ function Twitter(options) {
};
this.options = merge(defaults, options);
+ this.keygrip = this.options.cookie_secret === null ? null :
+ new Keygrip([this.options.cookie_secret]);
+
this.oauth = new oauth.OAuth(
this.options.request_token_url,
this.options.access_token_url,
this.options.consumer_key,
this.options.consumer_secret,
- '1.0', null, 'HMAC-SHA1', null,
+ '1.0A',
+ this.options.callback_url,
+ 'HMAC-SHA1', null,
this.options.headers);
}
Twitter.VERSION = VERSION;
@@ -253,12 +260,8 @@ Twitter.prototype.stream = function(method, params, callback) {
*/
Twitter.prototype.cookie = function(req) {
// Fetch the cookie
- try {
- var twauth = JSON.parse(req.getSecureCookie(this.options.cookie));
- } catch (error) {
- var twauth = null;
- }
- return twauth;
+ var cookies = new Cookies(req, null, this.keygrip);
+ return this._readCookie(cookies);
}
Twitter.prototype.login = function(mount, success) {
@@ -271,10 +274,6 @@ Twitter.prototype.login = function(mount, success) {
// Use secure cookie if forced to https and haven't configured otherwise
if ( this.options.secure && !this.options.cookie_options.secure )
this.options.cookie_options.secure = true;
- // Set up the cookie encryption secret if we've been given one
- if ( !cookie.secret && this.options.cookie_secret !== null )
- cookie.secret = this.options.cookie_secret;
- // FIXME: ^ so configs that don't use login() won't work?
return function handle(req, res, next) {
var path = url.parse(req.url, true);
@@ -292,7 +291,8 @@ Twitter.prototype.login = function(mount, success) {
}
// Fetch the cookie
- var twauth = self.cookie(req);
+ var cookies = new Cookies(req, res, self.keygrip);
+ var twauth = self._readCookie(cookies);
// We have a winner, but they're in the wrong place
if ( twauth && twauth.user_id && twauth.access_token_secret ) {
@@ -315,7 +315,7 @@ Twitter.prototype.login = function(mount, success) {
// FIXME: do something more intelligent
return next(500);
} else {
- res.setSecureCookie(self.options.cookie, JSON.stringify({
+ cookies.set(self.options.cookie, JSON.stringify({
user_id: user_id,
screen_name: screen_name,
access_token_key: access_token_key,
@@ -335,7 +335,7 @@ Twitter.prototype.login = function(mount, success) {
// FIXME: do something more intelligent
return next(500);
} else {
- res.setSecureCookie(self.options.cookie, JSON.stringify({
+ cookies.set(self.options.cookie, JSON.stringify({
oauth_token: oauth_token,
oauth_token_secret: oauth_token_secret
}), self.options.cookie_options);
@@ -351,7 +351,7 @@ Twitter.prototype.login = function(mount, success) {
// Broken cookie, clear it and return to originating page
// FIXME: this is dumb
} else {
- res.clearCookie(self.options.cookie);
+ cookies.set(self.options.cookie, null, self.options.cookie_options);
res.writeHead(302, {'Location': mount});
res.end();
return;
@@ -1133,3 +1133,13 @@ Twitter.prototype._getUsingCursor = function(url, params, callback) {
return this;
}
+
+Twitter.prototype._readCookie = function(cookies) {
+ // parse the auth cookie
+ try {
+ var twauth = JSON.parse(cookies.get(this.options.cookie));
+ } catch (error) {
+ var twauth = null;
+ }
+ return twauth;
+}
View
@@ -14,7 +14,8 @@
}
, "dependencies":
{ "oauth": ">=0.8.4"
- , "cookie": ">=0.1.4"
+ , "cookies": ">=0.1.6"
+ , "keygrip": ">=0.1.7"
}
, "engines": ["node >=0.2.0"]
, "main": "./lib/twitter"

0 comments on commit eeede3a

Please sign in to comment.