From 4bb3072361f0deddcf2668ada5b9511ca861f765 Mon Sep 17 00:00:00 2001 From: Mike Bannister Date: Tue, 21 Aug 2012 03:35:43 -0400 Subject: [PATCH] Handle oauth 1 & 2 errors in common spot --- .../accounts-oauth-helper/oauth_server.js | 28 +++++++++++---- .../accounts-oauth1-helper/oauth1_server.js | 3 +- .../accounts-oauth2-helper/oauth2_server.js | 35 ++++++------------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/packages/accounts-oauth-helper/oauth_server.js b/packages/accounts-oauth-helper/oauth_server.js index a4e1a6d38a..956112f5df 100644 --- a/packages/accounts-oauth-helper/oauth_server.js +++ b/packages/accounts-oauth-helper/oauth_server.js @@ -76,7 +76,6 @@ // Skip everything if there's no service set by the oauth middleware // XXX should we instead throw an error? - // XXX we should catch all exceptions here as we do in oauth2_server.js if (!service) { next(); return; @@ -85,12 +84,27 @@ // Make sure we're configured ensureConfigured(serviceName); - if (service.version === 1) - Meteor.accounts.oauth1._handleRequest(service, req.query, res); - else if (service.version === 2) - Meteor.accounts.oauth2._handleRequest(service, req.query, res); - else - throw new Error("Unexpected OAuth version " + service.version); + try { + if (service.version === 1) + Meteor.accounts.oauth1._handleRequest(service, req.query, res); + else if (service.version === 2) + Meteor.accounts.oauth2._handleRequest(service, req.query, res); + else + throw new Error("Unexpected OAuth version " + service.version); + } catch (err) { + // if we got thrown an error, save it off, it will get passed to + // the approporiate login call (if any) and reported there. + // + // The other option would be to display it in the popup tab that + // is still open at this point, ignoring the 'close' or 'redirect' + // we were passed. But then the developer wouldn't be able to + // style the error or react to it in any way. + if (req.query.state && err instanceof Error) + Meteor.accounts.oauth._loginResultForState[req.query.state] = err; + + // also log to the server console, so the developer sees it. + Meteor._debug("Exception in oauth" + service.version + " handler", err); + } }; // Handle _oauth paths, gets a bunch of stuff ready for the oauth implementation middleware diff --git a/packages/accounts-oauth1-helper/oauth1_server.js b/packages/accounts-oauth1-helper/oauth1_server.js index 3225d85eff..5b6f71bb77 100644 --- a/packages/accounts-oauth1-helper/oauth1_server.js +++ b/packages/accounts-oauth1-helper/oauth1_server.js @@ -41,7 +41,8 @@ // Get or create user id var oauthResult = service.handleOauthRequest(oauth); - var userId = Meteor.accounts.updateOrCreateUser(oauthResult.options, oauthResult.extra); + var userId = Meteor.accounts.updateOrCreateUser( + oauthResult.options, oauthResult.extra); // Generate and store a login token for reconnect // XXX this could go in accounts_server.js instead diff --git a/packages/accounts-oauth2-helper/oauth2_server.js b/packages/accounts-oauth2-helper/oauth2_server.js index ce45da67dd..017a2540fa 100644 --- a/packages/accounts-oauth2-helper/oauth2_server.js +++ b/packages/accounts-oauth2-helper/oauth2_server.js @@ -12,34 +12,19 @@ // This way the subsequent call to the `login` method will be // immediate. - try { - // Get or create user id - var oauthResult = service.handleOauthRequest(query); + // Get or create user id + var oauthResult = service.handleOauthRequest(query); - var userId = Meteor.accounts.updateOrCreateUser( - oauthResult.options, oauthResult.extra); + var userId = Meteor.accounts.updateOrCreateUser( + oauthResult.options, oauthResult.extra); - // Generate and store a login token for reconnect - // XXX this could go in accounts_server.js instead - var loginToken = Meteor.accounts._loginTokens.insert({userId: userId}); + // Generate and store a login token for reconnect + // XXX this could go in accounts_server.js instead + var loginToken = Meteor.accounts._loginTokens.insert({userId: userId}); - // Store results to subsequent call to `login` - Meteor.accounts.oauth._loginResultForState[query.state] = - {token: loginToken, id: userId}; - } catch (err) { - // if we got thrown an error, save it off, it will get passed to - // the approporiate login call (if any) and reported there. - // - // The other option would be to display it in the popup tab that - // is still open at this point, ignoring the 'close' or 'redirect' - // we were passed. But then the developer wouldn't be able to - // style the error or react to it in any way. - if (query.state && err instanceof Error) - Meteor.accounts.oauth._loginResultForState[query.state] = err; - - // also log to the server console, so the developer sees it. - Meteor._debug("Exception in oauth2 handler", err); - } + // Store results to subsequent call to `login` + Meteor.accounts.oauth._loginResultForState[query.state] = + {token: loginToken, id: userId}; // XXX push down to oauth_server.js?