moved common oauth 1 and 2 client code to accounts-oauth-helper package

This commit is contained in:
Mike Bannister
2012-08-02 01:02:07 -04:00
committed by Nick Martin
parent 98eaf61bef
commit 63eeec4708
7 changed files with 86 additions and 111 deletions

View File

@@ -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;
};
})();

View File

@@ -0,0 +1 @@
Meteor.accounts.oauth = {};

View File

@@ -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');
});

View File

@@ -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);
};
})();

View File

@@ -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']);

View File

@@ -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);
};
})();

View File

@@ -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']);