mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
The default value for the `requestPermissions` option is now `profile`, thus preserving the old behavior in case the option was not explicitly set. Added a note in the History.md file regarding this change.
84 lines
2.9 KiB
JavaScript
84 lines
2.9 KiB
JavaScript
Google = {};
|
|
|
|
// Request Google credentials for the user
|
|
// @param options {optional}
|
|
// @param credentialRequestCompleteCallback {Function} Callback function to call on
|
|
// completion. Takes one argument, credentialToken on success, or Error on
|
|
// error.
|
|
Google.requestCredential = function (options, credentialRequestCompleteCallback) {
|
|
// support both (options, callback) and (callback).
|
|
if (!credentialRequestCompleteCallback && typeof options === 'function') {
|
|
credentialRequestCompleteCallback = options;
|
|
options = {};
|
|
} else if (!options) {
|
|
options = {};
|
|
}
|
|
|
|
var config = ServiceConfiguration.configurations.findOne({service: 'google'});
|
|
if (!config) {
|
|
credentialRequestCompleteCallback && credentialRequestCompleteCallback(
|
|
new ServiceConfiguration.ConfigError());
|
|
return;
|
|
}
|
|
|
|
var credentialToken = Random.secret();
|
|
|
|
// we need the email scope to get user id from google.
|
|
var requiredScope = ['email'];
|
|
var scope = ['profile'];
|
|
if (options.requestPermissions)
|
|
scope = options.requestPermissions;
|
|
scope = _.union(scope, requiredScope);
|
|
|
|
var loginUrlParameters = {};
|
|
if (config.loginUrlParameters){
|
|
_.extend(loginUrlParameters, config.loginUrlParameters)
|
|
}
|
|
if (options.loginUrlParameters){
|
|
_.extend(loginUrlParameters, options.loginUrlParameters)
|
|
}
|
|
var ILLEGAL_PARAMETERS = ['response_type', 'client_id', 'scope', 'redirect_uri', 'state'];
|
|
// validate options keys
|
|
_.each(_.keys(loginUrlParameters), function (key) {
|
|
if (_.contains(ILLEGAL_PARAMETERS, key))
|
|
throw new Error("Google.requestCredential: Invalid loginUrlParameter: " + key);
|
|
});
|
|
|
|
// backwards compatible options
|
|
if (options.requestOfflineToken != null){
|
|
loginUrlParameters.access_type = options.requestOfflineToken ? 'offline' : 'online'
|
|
}
|
|
if (options.prompt != null) {
|
|
loginUrlParameters.prompt = options.prompt;
|
|
} else if (options.forceApprovalPrompt) {
|
|
loginUrlParameters.prompt = 'consent'
|
|
}
|
|
|
|
if (options.loginHint) {
|
|
loginUrlParameters.login_hint = options.loginHint;
|
|
}
|
|
|
|
var loginStyle = OAuth._loginStyle('google', config, options);
|
|
// https://developers.google.com/accounts/docs/OAuth2WebServer#formingtheurl
|
|
_.extend(loginUrlParameters, {
|
|
"response_type": "code",
|
|
"client_id": config.clientId,
|
|
"scope": scope.join(' '), // space delimited
|
|
"redirect_uri": OAuth._redirectUri('google', config),
|
|
"state": OAuth._stateParam(loginStyle, credentialToken, options.redirectUrl)
|
|
});
|
|
var loginUrl = 'https://accounts.google.com/o/oauth2/auth?' +
|
|
_.map(loginUrlParameters, function(value, param){
|
|
return encodeURIComponent(param) + '=' + encodeURIComponent(value);
|
|
}).join("&");
|
|
|
|
OAuth.launchLogin({
|
|
loginService: "google",
|
|
loginStyle: loginStyle,
|
|
loginUrl: loginUrl,
|
|
credentialRequestCompleteCallback: credentialRequestCompleteCallback,
|
|
credentialToken: credentialToken,
|
|
popupOptions: { height: 600 }
|
|
});
|
|
};
|