Permalink
Browse files

Switch to cookies library (from cookie-node).

Switch to oauth 1.0A, add option to specify callback URL.
  • Loading branch information...
1 parent 641ebf3 commit 292980fd8062c762b879a5404cebbfdd8d959197 @oesmith oesmith committed May 6, 2011
Showing with 20 additions and 15 deletions.
  1. +18 −14 lib/twitter.js
  2. +2 −1 package.json
View
@@ -2,7 +2,8 @@ var VERSION = '0.1.16',
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: 'http://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;
@@ -248,12 +255,11 @@ Twitter.prototype.stream = function(method, params, callback) {
/*
* TWITTER "O"AUTHENTICATION UTILITIES, INCLUDING THE GREAT
* CONNECT/STACK STYLE TWITTER "O"AUTHENTICATION MIDDLEWARE
- * and helpful utilities to retrieve the twauth cookie etc.
*/
-Twitter.prototype.cookie = function(req) {
+Twitter.prototype.cookie = function(cookies) {
// Fetch the cookie
try {
- var twauth = JSON.parse(req.getSecureCookie(this.options.cookie));
+ var twauth = JSON.parse(cookies.get(this.options.cookie));
} catch (error) {
var twauth = null;
}
@@ -270,10 +276,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);
@@ -291,7 +293,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.cookie(cookies);
// We have a winner, but they're in the wrong place
if ( twauth && twauth.user_id && twauth.access_token_secret ) {
@@ -314,7 +317,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,
@@ -334,7 +337,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);
@@ -350,7 +353,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;
@@ -363,7 +366,8 @@ Twitter.prototype.gatekeeper = function(failure) {
mount = this.options.login_mount || '/twauth';
return function(req, res, next) {
- var twauth = self.cookie(req);
+ var cookies = new Cookies(req, res, self.keygrip);
+ var twauth = self.cookie(cookies);
// We have a winner
if ( twauth && twauth.user_id && twauth.access_token_secret )
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 292980f

Please sign in to comment.