From 63eeec47081cf7eee44e477e93cefcecbf517900 Mon Sep 17 00:00:00 2001 From: Mike Bannister Date: Thu, 2 Aug 2012 01:02:07 -0400 Subject: [PATCH] moved common oauth 1 and 2 client code to accounts-oauth-helper package --- .../accounts-oauth-helper/oauth_client.js | 62 +++++++++++++++++++ .../accounts-oauth-helper/oauth_common.js | 1 + packages/accounts-oauth-helper/package.js | 17 +++++ .../accounts-oauth1-helper/oauth1_client.js | 56 +---------------- packages/accounts-oauth1-helper/package.js | 2 +- .../accounts-oauth2-helper/oauth2_client.js | 58 +---------------- packages/accounts-oauth2-helper/package.js | 1 + 7 files changed, 86 insertions(+), 111 deletions(-) create mode 100644 packages/accounts-oauth-helper/oauth_client.js create mode 100644 packages/accounts-oauth-helper/oauth_common.js create mode 100644 packages/accounts-oauth-helper/package.js diff --git a/packages/accounts-oauth-helper/oauth_client.js b/packages/accounts-oauth-helper/oauth_client.js new file mode 100644 index 0000000000..7640f1a36d --- /dev/null +++ b/packages/accounts-oauth-helper/oauth_client.js @@ -0,0 +1,62 @@ +(function () { + // Open a popup window pointing to a OAuth handshake page + // + // @param state {String} The OAuth state generated by the client + // @param url {String} url to page + Meteor.accounts.oauth.initiateLogin = function(state, url, version) { + // XXX these dimensions worked well for facebook and google, but + // it's sort of weird to have these here. Maybe an optional + // argument instead? + var popup = openCenteredPopup(url, 650, 331); + + var checkPopupOpen = setInterval(function() { + if (popup.closed) { + clearInterval(checkPopupOpen); + tryLoginAfterPopupClosed(state, version); + } + }, 100); + }; + + // Send an OAuth login method to the server. If the user authorized + // access in the popup this should log the user in, otherwise + // nothing should happen. + var tryLoginAfterPopupClosed = function(state, version) { + Meteor.apply('login', [ + {oauth: {version: version, state: state}} + ], {wait: true}, function(error, result) { + if (error) + throw error; + + if (!result) { + // The user either closed the OAuth popup or didn't authorize + // access. Do nothing. + return; + } else { + Meteor.accounts.makeClientLoggedIn(result.id, result.token); + } + }); + }; + + var openCenteredPopup = function(url, width, height) { + var screenX = typeof window.screenX !== 'undefined' + ? window.screenX : window.screenLeft; + var screenY = typeof window.screenY !== 'undefined' + ? window.screenY : window.screenTop; + var outerWidth = typeof window.outerWidth !== 'undefined' + ? window.outerWidth : document.body.clientWidth; + var outerHeight = typeof window.outerHeight !== 'undefined' + ? window.outerHeight : (document.body.clientHeight - 22); + + // Use `outerWidth - width` and `outerHeight - height` for help in + // positioning the popup centered relative to the current window + var left = screenX + (outerWidth - width) / 2; + var top = screenY + (outerHeight - height) / 2; + var features = ('width=' + width + ',height=' + height + + ',left=' + left + ',top=' + top); + + var newwindow = window.open(url, 'Login', features); + if (newwindow.focus) + newwindow.focus(); + return newwindow; + }; +})(); \ No newline at end of file diff --git a/packages/accounts-oauth-helper/oauth_common.js b/packages/accounts-oauth-helper/oauth_common.js new file mode 100644 index 0000000000..3c6e8b8558 --- /dev/null +++ b/packages/accounts-oauth-helper/oauth_common.js @@ -0,0 +1 @@ +Meteor.accounts.oauth = {}; \ No newline at end of file diff --git a/packages/accounts-oauth-helper/package.js b/packages/accounts-oauth-helper/package.js new file mode 100644 index 0000000000..cacb1c06b9 --- /dev/null +++ b/packages/accounts-oauth-helper/package.js @@ -0,0 +1,17 @@ +Package.describe({ + summary: "Common code for OAuth-based login services", + internal: true +}); + +Package.on_use(function (api) { + api.use('accounts', ['client', 'server']); + + api.add_files('oauth_common.js', ['client', 'server']); + api.add_files('oauth_client.js', 'client'); +}); + +Package.on_test(function (api) { + // XXX Fix these! + // api.use('accounts-oauth-helper', 'server'); + // api.add_files("oauth_tests.js", 'server'); +}); diff --git a/packages/accounts-oauth1-helper/oauth1_client.js b/packages/accounts-oauth1-helper/oauth1_client.js index f859ff69cf..f72deb3779 100644 --- a/packages/accounts-oauth1-helper/oauth1_client.js +++ b/packages/accounts-oauth1-helper/oauth1_client.js @@ -4,59 +4,7 @@ // @param state {String} The OAuth1 state generated by the client // @param url {String} url to page Meteor.accounts.oauth1.initiateLogin = function(state, url) { - // XXX these dimensions worked well for facebook and google, but - // it's sort of weird to have these here. Maybe an optional - // argument instead? - var popup = openCenteredPopup(url, 650, 331); - - var checkPopupOpen = setInterval(function() { - if (popup.closed) { - clearInterval(checkPopupOpen); - tryLoginAfterPopupClosed(state); - } - }, 100); - }; - - // Send an OAuth login method to the server. If the user authorized - // access in the popup this should log the user in, otherwise - // nothing should happen. - var tryLoginAfterPopupClosed = function(state) { - Meteor.apply('login', [ - {oauth: {version: 1, state: state}} - ], {wait: true}, function(error, result) { - if (error) - throw error; - - if (!result) { - // The user either closed the OAuth popup or didn't authorize - // access. Do nothing. - return; - } else { - Meteor.accounts.makeClientLoggedIn(result.id, result.token); - } - }); - }; - - var openCenteredPopup = function(url, width, height) { - var screenX = typeof window.screenX !== 'undefined' - ? window.screenX : window.screenLeft; - var screenY = typeof window.screenY !== 'undefined' - ? window.screenY : window.screenTop; - var outerWidth = typeof window.outerWidth !== 'undefined' - ? window.outerWidth : document.body.clientWidth; - var outerHeight = typeof window.outerHeight !== 'undefined' - ? window.outerHeight : (document.body.clientHeight - 22); - - // Use `outerWidth - width` and `outerHeight - height` for help in - // positioning the popup centered relative to the current window - var left = screenX + (outerWidth - width) / 2; - var top = screenY + (outerHeight - height) / 2; - var features = ('width=' + width + ',height=' + height + - ',left=' + left + ',top=' + top); - - var newwindow = window.open(url, 'Login', features); - if (newwindow.focus) - newwindow.focus(); - return newwindow; + // Include the oauth version as the last parameter + Meteor.accounts.oauth.initiateLogin(state, url, 1); }; })(); \ No newline at end of file diff --git a/packages/accounts-oauth1-helper/package.js b/packages/accounts-oauth1-helper/package.js index 57d67e85d3..d6934c525e 100644 --- a/packages/accounts-oauth1-helper/package.js +++ b/packages/accounts-oauth1-helper/package.js @@ -4,7 +4,7 @@ Package.describe({ }); Package.on_use(function (api) { - api.use('accounts', ['client', 'server']); + api.use('accounts-oauth-helper', 'client'); api.use('oauth1', 'server'); api.add_files('oauth1_common.js', ['client', 'server']); diff --git a/packages/accounts-oauth2-helper/oauth2_client.js b/packages/accounts-oauth2-helper/oauth2_client.js index e6bf2ac4cf..2b0c01ee07 100644 --- a/packages/accounts-oauth2-helper/oauth2_client.js +++ b/packages/accounts-oauth2-helper/oauth2_client.js @@ -4,61 +4,7 @@ // @param state {String} The OAuth state generated by the client // @param url {String} url to page Meteor.accounts.oauth2.initiateLogin = function(state, url) { - // XXX these dimensions worked well for facebook and google, but - // it's sort of weird to have these here. Maybe an optional - // argument instead? - var popup = openCenteredPopup(url, 650, 331); - - var checkPopupOpen = setInterval(function() { - if (popup.closed) { - clearInterval(checkPopupOpen); - tryLoginAfterPopupClosed(state); - } - }, 100); - }; - - // Send an OAuth login method to the server. If the user authorized - // access in the popup this should log the user in, otherwise - // nothing should happen. - var tryLoginAfterPopupClosed = function(state) { - Meteor.apply('login', [ - {oauth: {version: 2, state: state}} - ], {wait: true}, function(error, result) { - // XXX this is the wrong thing to do with the error! It should be - // delivered to the user via a callback. - if (error) - throw error; - - if (!result) { - // The user either closed the OAuth popup or didn't authorize - // access. Do nothing. - return; - } else { - Meteor.accounts.makeClientLoggedIn(result.id, result.token); - } - }); - }; - - var openCenteredPopup = function(url, width, height) { - var screenX = typeof window.screenX !== 'undefined' - ? window.screenX : window.screenLeft; - var screenY = typeof window.screenY !== 'undefined' - ? window.screenY : window.screenTop; - var outerWidth = typeof window.outerWidth !== 'undefined' - ? window.outerWidth : document.body.clientWidth; - var outerHeight = typeof window.outerHeight !== 'undefined' - ? window.outerHeight : (document.body.clientHeight - 22); - - // Use `outerWidth - width` and `outerHeight - height` for help in - // positioning the popup centered relative to the current window - var left = screenX + (outerWidth - width) / 2; - var top = screenY + (outerHeight - height) / 2; - var features = ('width=' + width + ',height=' + height + - ',left=' + left + ',top=' + top); - - var newwindow = window.open(url, 'Login', features); - if (newwindow.focus) - newwindow.focus(); - return newwindow; + // Include the oauth version as the last parameter + Meteor.accounts.oauth.initiateLogin(state, url, 2); }; })(); diff --git a/packages/accounts-oauth2-helper/package.js b/packages/accounts-oauth2-helper/package.js index a76d0bf0f6..59f8ccfb83 100644 --- a/packages/accounts-oauth2-helper/package.js +++ b/packages/accounts-oauth2-helper/package.js @@ -4,6 +4,7 @@ Package.describe({ }); Package.on_use(function (api) { + api.use('accounts-oauth-helper', 'client'); api.use('accounts', ['client', 'server']); api.add_files('oauth2_common.js', ['client', 'server']);