From af941f705cdf2d31e3d01401832ca921d473ac67 Mon Sep 17 00:00:00 2001 From: Damon Oehlman Date: Mon, 26 Sep 2011 15:21:39 +1000 Subject: [PATCH 1/7] Updated express helper to use express routing (connect routing deprecated) --- lib/expressHelper.js | 48 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/expressHelper.js b/lib/expressHelper.js index 06aa7cda..d5e66f5c 100644 --- a/lib/expressHelper.js +++ b/lib/expressHelper.js @@ -1,5 +1,49 @@ -module.exports = function (app) { - var everyauth = this; +function registerReqGettersAndMethods (req, res, next) { + var methods = everyauth._req._methods + , getters = everyauth._req._getters; + for (var name in methods) { + req[name] = methods[name]; + } + for (name in getters) { + Object.defineProperty(req, name, { + get: getters[name] + }); + } + next(); +} + +function fetchUserFromSession (req, res, next) { + var sess = req.session + , auth = sess && sess.auth + , everymodule, findUser; + if (!auth) return next(); + if (!auth.userId) return next(); + everymodule = everyauth.everymodule; + if (!everymodule._findUserById) return next(); + var pause = __pause(req); + everymodule._findUserById(auth.userId, function (err, user) { + if (err) throw err; // TODO Leverage everyauth's error handling + if (user) req.user = user; + else delete sess.auth; + next(); + pause.resume(); + }); +} + +module.exports = function (app, modules) { + var everyauth = this, + _module; + + app.use(registerReqGettersAndMethods); + app.use(fetchUserFromSession); + + // attach the routes for each of the enabled modules + for (var _name in modules) { + _module = modules[_name]; + _module.validateSteps(); + _module.routeApp(app); + } // for + app.dynamicHelpers({ everyauth: function (req, res) { var ea = {} From 30d7da86bf7791eb45848f4701606c6e2d907ce8 Mon Sep 17 00:00:00 2001 From: Damon Oehlman Date: Mon, 26 Sep 2011 15:24:33 +1000 Subject: [PATCH 2/7] Fixes to include everyauth reference in helper --- lib/expressHelper.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/expressHelper.js b/lib/expressHelper.js index d5e66f5c..39d635e8 100644 --- a/lib/expressHelper.js +++ b/lib/expressHelper.js @@ -1,3 +1,5 @@ +var everyauth = require('../'); + function registerReqGettersAndMethods (req, res, next) { var methods = everyauth._req._methods , getters = everyauth._req._getters; @@ -33,7 +35,9 @@ function fetchUserFromSession (req, res, next) { module.exports = function (app, modules) { var everyauth = this, _module; - + + // ensure we have modules + modules = modules || everyauth.enabled; app.use(registerReqGettersAndMethods); app.use(fetchUserFromSession); From 2f09b6f6c6cbcc8c186b980cfe7d8d258d2343de Mon Sep 17 00:00:00 2001 From: Damon Oehlman Date: Mon, 26 Sep 2011 15:26:30 +1000 Subject: [PATCH 3/7] Included reference to connect.utils.pause --- lib/expressHelper.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/expressHelper.js b/lib/expressHelper.js index 39d635e8..89e1c742 100644 --- a/lib/expressHelper.js +++ b/lib/expressHelper.js @@ -1,4 +1,5 @@ -var everyauth = require('../'); +var everyauth = require('../'), + __pause = require('connect').utils.pause; function registerReqGettersAndMethods (req, res, next) { var methods = everyauth._req._methods From 8cee4a5c9c6e238a61ef256adab0b0723b82b402 Mon Sep 17 00:00:00 2001 From: Damon Oehlman Date: Mon, 26 Sep 2011 15:33:49 +1000 Subject: [PATCH 4/7] Some route debugging --- lib/modules/everymodule.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/modules/everymodule.js b/lib/modules/everymodule.js index 0181c055..2c713592 100644 --- a/lib/modules/everymodule.js +++ b/lib/modules/everymodule.js @@ -239,6 +239,8 @@ var everyModule = module.exports = { if (!path) throw new Error('You have not defined a path for the route alias ' + routeAlias + '.'); var seq = routes[method][routeAlias]; + + console.log('setting route: ' + path); // This kicks off a sequence of steps based on a // route From 97ec5920b4ef4d6e1ec109e6e5664b6ee9751383 Mon Sep 17 00:00:00 2001 From: Damon Oehlman Date: Mon, 26 Sep 2011 15:42:44 +1000 Subject: [PATCH 5/7] Experimenting with integrating with express app routes --- lib/modules/everymodule.js | 2 -- lib/modules/oauth.js | 18 ++++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/modules/everymodule.js b/lib/modules/everymodule.js index 2c713592..8979b4d3 100644 --- a/lib/modules/everymodule.js +++ b/lib/modules/everymodule.js @@ -240,8 +240,6 @@ var everyModule = module.exports = { throw new Error('You have not defined a path for the route alias ' + routeAlias + '.'); var seq = routes[method][routeAlias]; - console.log('setting route: ' + path); - // This kicks off a sequence of steps based on a // route // Creates a new chain of promises and exposes the leading promise diff --git a/lib/modules/oauth.js b/lib/modules/oauth.js index dfbc0ff9..70bee560 100644 --- a/lib/modules/oauth.js +++ b/lib/modules/oauth.js @@ -93,7 +93,14 @@ everyModule.submodule('oauth') .promises(null) .getRequestToken( function (req, res) { + + var callbackPath = this.callbackPath; + // check for a route + if (req.app) { + callbackPath = (req.app.route == '/' ? '' : req.app.route) + callbackPath; + } // if + // Automatic hostname detection + assignment if (!this._myHostname || this._alwaysDetectHostname) { this.myHostname(extractHostname(req)); @@ -120,9 +127,16 @@ everyModule.submodule('oauth') // module needs it as a uri query parameter. However, in cases such as twitter, it allows you to over-ride // the callback url settings at dev.twitter.com from one place, your app code, rather than in two places -- i.e., // your app code + dev.twitter.com app settings. - var redirectTo = this._oauthHost + this._authorizePath + '?oauth_token=' + token; + var redirectTo = this._oauthHost + this._authorizePath + '?oauth_token=' + token, + callbackPath = this._callbackPath; + + // check for a route + if (req.app) { + callbackPath = (req.app.route == '/' ? '' : req.app.route) + callbackPath; + } // if + if (this._sendCallbackWithAuthorize) { - redirectTo += '&oauth_callback=' + this._myHostname + this._callbackPath; + redirectTo += '&oauth_callback=' + this._myHostname + callbackPath; } res.writeHead(303, { 'Location': redirectTo }); res.end(); From bb758e98d386397c5e755905436b2c346ee83e04 Mon Sep 17 00:00:00 2001 From: Damon Oehlman Date: Mon, 26 Sep 2011 15:44:43 +1000 Subject: [PATCH 6/7] Adjusts accepts parameters for `redirectToProviderAuth` --- lib/modules/oauth.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/modules/oauth.js b/lib/modules/oauth.js index 70bee560..4522084f 100644 --- a/lib/modules/oauth.js +++ b/lib/modules/oauth.js @@ -41,7 +41,7 @@ everyModule.submodule('oauth') .promises(null) .step('redirectToProviderAuth') .description('sends the user to authorization on the OAuth provider site') - .accepts('res token') + .accepts('req res token') .promises(null) .get('callbackPath', @@ -122,7 +122,7 @@ everyModule.submodule('oauth') _provider.token = token; _provider.tokenSecret = tokenSecret; }) - .redirectToProviderAuth( function (res, token) { + .redirectToProviderAuth( function (req, res, token) { // Note: Not all oauth modules need oauth_callback as a uri query parameter. As far as I know, only readability's // module needs it as a uri query parameter. However, in cases such as twitter, it allows you to over-ride // the callback url settings at dev.twitter.com from one place, your app code, rather than in two places -- i.e., From 5b3d6c8d649c3eefe953d22779efe47f167f1027 Mon Sep 17 00:00:00 2001 From: Damon Oehlman Date: Mon, 26 Sep 2011 15:49:06 +1000 Subject: [PATCH 7/7] Fixes to app route detection --- lib/modules/oauth.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/modules/oauth.js b/lib/modules/oauth.js index 4522084f..586b2447 100644 --- a/lib/modules/oauth.js +++ b/lib/modules/oauth.js @@ -94,7 +94,7 @@ everyModule.submodule('oauth') .getRequestToken( function (req, res) { - var callbackPath = this.callbackPath; + var callbackPath = this._callbackPath; // check for a route if (req.app) { @@ -107,7 +107,7 @@ everyModule.submodule('oauth') } var p = this.Promise(); - this.oauth.getOAuthRequestToken({ oauth_callback: this._myHostname + this._callbackPath }, function (err, token, tokenSecret, params) { + this.oauth.getOAuthRequestToken({ oauth_callback: this._myHostname + callbackPath }, function (err, token, tokenSecret, params) { if (err && !~(err.data.indexOf('Invalid / expired Token'))) { return p.fail(err); }