From 2259ea6e08dee43001cf7a98d2a28ee06d28886a Mon Sep 17 00:00:00 2001 From: Mike Bannister Date: Thu, 2 Aug 2012 04:18:45 -0400 Subject: [PATCH] move common middleware functionality into accounts-oauth-helper middleware --- .../accounts-oauth-helper/oauth_server.js | 42 +++++++++++++++++++ .../accounts-oauth1-helper/oauth1_server.js | 33 +++------------ .../accounts-oauth2-helper/oauth2_server.js | 32 ++------------ 3 files changed, 51 insertions(+), 56 deletions(-) diff --git a/packages/accounts-oauth-helper/oauth_server.js b/packages/accounts-oauth-helper/oauth_server.js index c91a5af6ea..3e7cd8c915 100644 --- a/packages/accounts-oauth-helper/oauth_server.js +++ b/packages/accounts-oauth-helper/oauth_server.js @@ -1,4 +1,5 @@ (function () { + var connect = __meteor_bootstrap__.require("connect"); // Register a handler for an OAuth service. The handler will be called // when we get an incoming http request on /_oauth/{serviceName}. This @@ -49,4 +50,45 @@ }; + // Handle _oauth paths, gets a bunch of stuff ready for the oauth implementation middleware + Meteor.accounts.oauth._handleRequest = function (req, res, next) { + + // req.url will be "/_oauth/?" + var barePath = req.url.substring(0, req.url.indexOf('?')); + var splitPath = barePath.split('/'); + + // Find service based on url + var serviceName = req._serviceName = splitPath[2]; + + // Any non-oauth request will continue down the default middlewares + // Same goes for service that hasn't been registered + if (splitPath[1] !== '_oauth') { + next(); + return; + } + + // Make sure we're configured + if (!Meteor.accounts[serviceName]._appId || !Meteor.accounts[serviceName]._appUrl) + throw new Meteor.accounts.ConfigError("Need to call Meteor.accounts." + serviceName + ".config first"); + if (!Meteor.accounts[serviceName]._secret) + throw new Meteor.accounts.ConfigError("Need to call Meteor.accounts." + serviceName + ".setSecret first"); + + next(); + }; + + Meteor.accounts.oauth._loadMiddleWare = function(middleware) { + __meteor_bootstrap__.app + .use(connect.query()) + .use(function(req, res, next) { + // Need to create a Fiber since we're using synchronous http + // calls and nothing else is wrapping this in a fiber + // automatically + Fiber(function () { + middleware(req, res, next); + }).run(); + }); + }; + + Meteor.accounts.oauth._loadMiddleWare(Meteor.accounts.oauth._handleRequest); + })(); diff --git a/packages/accounts-oauth1-helper/oauth1_server.js b/packages/accounts-oauth1-helper/oauth1_server.js index 23388327bf..326fe7f3a2 100644 --- a/packages/accounts-oauth1-helper/oauth1_server.js +++ b/packages/accounts-oauth1-helper/oauth1_server.js @@ -8,32 +8,19 @@ // connect middleware Meteor.accounts.oauth1._handleRequest = function (req, res, next) { - // req.url will be "/_oauth/?" - var barePath = req.url.substring(0, req.url.indexOf('?')); - var splitPath = barePath.split('/'); + var service = Meteor.accounts.oauth1._services[req._serviceName]; - // Find service based on url - var serviceName = splitPath[2]; - var service = Meteor.accounts.oauth1._services[serviceName]; - - // Any non-oauth request will continue down the default middlewares - // Same goes for service that hasn't been registered - if (splitPath[1] !== '_oauth' || !service) { + // Skip everything if there's no service set by the oauth middleware + if (!service) { next(); return; } - // Make sure we're configured - if (!Meteor.accounts[serviceName]._appId || !Meteor.accounts[serviceName]._appUrl) - throw new Meteor.accounts.ConfigError("Need to call Meteor.accounts." + serviceName + ".config first"); - if (!Meteor.accounts[serviceName]._secret) - throw new Meteor.accounts.ConfigError("Need to call Meteor.accounts." + serviceName + ".setSecret first"); - // Make sure we prepare the login results before returning. // This way the subsequent call to the `login` method will be // immediate. - var config = Meteor.accounts[serviceName]; + var config = Meteor.accounts[req._serviceName]; var oauth = new OAuth1(config); // If we get here with a callback url we need a request token to @@ -96,16 +83,6 @@ } }; - // Listen on /_oauth/* - __meteor_bootstrap__.app - .use(connect.query()) - .use(function(req, res, next) { - // Need to create a Fiber since we're using synchronous http - // calls and nothing else is wrapping this in a fiber - // automatically - Fiber(function () { - Meteor.accounts.oauth1._handleRequest(req, res, next); - }).run(); - }); + Meteor.accounts.oauth._loadMiddleWare(Meteor.accounts.oauth1._handleRequest); })(); diff --git a/packages/accounts-oauth2-helper/oauth2_server.js b/packages/accounts-oauth2-helper/oauth2_server.js index 183ad442af..923d3efa32 100644 --- a/packages/accounts-oauth2-helper/oauth2_server.js +++ b/packages/accounts-oauth2-helper/oauth2_server.js @@ -8,28 +8,14 @@ // connect middleware Meteor.accounts.oauth2._handleRequest = function (req, res, next) { - // req.url will be "/_oauth/?" - // NOTE: query param is mandatory. - var barePath = req.url.substring(0, req.url.indexOf('?')); - var splitPath = barePath.split('/'); + var service = Meteor.accounts.oauth2._services[req._serviceName]; - // Find service based on url - var serviceName = splitPath[2]; - var service = Meteor.accounts.oauth2._services[serviceName]; - - // Any non-oauth request will continue down the default middlewares - // Same goes for service that hasn't been registered - if (splitPath[1] !== '_oauth' || !service) { + // Skip everything if there's no service set by the oauth middleware + if (!service) { next(); return; } - // Make sure we're configured - if (!Meteor.accounts[serviceName]._appId || !Meteor.accounts[serviceName]._appUrl) - throw new Meteor.accounts.ConfigError("Need to call Meteor.accounts." + serviceName + ".config first"); - if (!Meteor.accounts[serviceName]._secret) - throw new Meteor.accounts.ConfigError("Need to call Meteor.accounts." + serviceName + ".setSecret first"); - if (req.query.error) { // The user didn't authorize access return null; @@ -71,16 +57,6 @@ } }; - // Listen on /_oauth/* - __meteor_bootstrap__.app - .use(connect.query()) - .use(function(req, res, next) { - // Need to create a Fiber since we're using synchronous http - // calls and nothing else is wrapping this in a fiber - // automatically - Fiber(function () { - Meteor.accounts.oauth2._handleRequest(req, res, next); - }).run(); - }); + Meteor.accounts.oauth._loadMiddleWare(Meteor.accounts.oauth2._handleRequest); })();