Skip to content

Commit

Permalink
Merge pull request technoweenie#13 from oesmith/master
Browse files Browse the repository at this point in the history
Migrated to cookies
  • Loading branch information
jdub committed Mar 4, 2012
2 parents e6c2e77 + 3617fea commit eeede3a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
42 changes: 26 additions & 16 deletions lib/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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',
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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 ) {
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit eeede3a

Please sign in to comment.