move common middleware functionality into accounts-oauth-helper middleware

This commit is contained in:
Mike Bannister
2012-08-02 04:18:45 -04:00
committed by Nick Martin
parent 380910d72f
commit 2259ea6e08
3 changed files with 51 additions and 56 deletions

View File

@@ -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/<service name>?<action>"
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);
})();

View File

@@ -8,32 +8,19 @@
// connect middleware
Meteor.accounts.oauth1._handleRequest = function (req, res, next) {
// req.url will be "/_oauth/<service name>?<action>"
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);
})();

View File

@@ -8,28 +8,14 @@
// connect middleware
Meteor.accounts.oauth2._handleRequest = function (req, res, next) {
// req.url will be "/_oauth/<service name>?<action>"
// 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);
})();