2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
var OAuth = function(googleApi) {
|
|
|
|
this._googleApi = googleApi;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if chrome.identity api is supported
|
|
|
|
* @return {Boolean} If is supported
|
|
|
|
*/
|
|
|
|
OAuth.prototype.isSupported = function() {
|
|
|
|
return !!(window.chrome && chrome.identity);
|
|
|
|
};
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
|
|
|
* Request an OAuth token from chrome for gmail users
|
|
|
|
* @param {String} emailAddress The user's email address (optional)
|
|
|
|
*/
|
|
|
|
OAuth.prototype.getOAuthToken = function(emailAddress, callback) {
|
|
|
|
var idOptions = {
|
|
|
|
interactive: true
|
2014-04-01 07:16:39 -04:00
|
|
|
};
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// check which runtime the app is running under
|
|
|
|
chrome.runtime.getPlatformInfo(function(platformInfo) {
|
|
|
|
if (chrome.runtime.lastError || !platformInfo) {
|
|
|
|
callback(new Error('Error getting chrome platform info!'));
|
|
|
|
return;
|
|
|
|
}
|
2014-05-21 07:01:41 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
if (emailAddress && platformInfo.os.indexOf('android') !== -1) {
|
|
|
|
// set accountHint so that native Android account picker does not show up each time
|
|
|
|
idOptions.accountHint = emailAddress;
|
|
|
|
}
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// get OAuth Token from chrome
|
|
|
|
chrome.identity.getAuthToken(idOptions, function(token) {
|
|
|
|
if (chrome.runtime.lastError || !token) {
|
|
|
|
callback({
|
|
|
|
errMsg: 'Error fetching an OAuth token for the user!'
|
|
|
|
});
|
|
|
|
return;
|
2014-05-21 07:01:41 -04:00
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
callback(null, token);
|
2014-04-01 07:16:39 -04:00
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
};
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
/**
|
|
|
|
* Remove an old OAuth token and get a new one.
|
|
|
|
* @param {String} options.oldToken The old token to be removed
|
|
|
|
* @param {String} options.emailAddress The user's email address (optional)
|
|
|
|
*/
|
|
|
|
OAuth.prototype.refreshToken = function(options, callback) {
|
|
|
|
var self = this;
|
2014-08-05 10:44:39 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
if (!options.oldToken) {
|
|
|
|
callback(new Error('oldToken option not set!'));
|
|
|
|
return;
|
|
|
|
}
|
2014-08-05 10:44:39 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// remove cached token
|
|
|
|
chrome.identity.removeCachedAuthToken({
|
|
|
|
token: options.oldToken
|
|
|
|
}, function() {
|
|
|
|
// get a new token
|
|
|
|
self.getOAuthToken(options.emailAddress, callback);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get email address from google api
|
|
|
|
* @param {String} token The oauth token
|
|
|
|
*/
|
|
|
|
OAuth.prototype.queryEmailAddress = function(token, callback) {
|
|
|
|
if (!token) {
|
|
|
|
callback({
|
|
|
|
errMsg: 'Invalid OAuth token!'
|
2014-08-05 10:44:39 -04:00
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-08-05 10:44:39 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// fetch gmail user's email address from the Google Authorization Server
|
|
|
|
this._googleApi.get({
|
|
|
|
uri: '/oauth2/v3/userinfo?access_token=' + token
|
|
|
|
}, function(err, info) {
|
|
|
|
if (err || !info || !info.email) {
|
2014-04-01 07:16:39 -04:00
|
|
|
callback({
|
2014-10-02 16:05:44 -04:00
|
|
|
errMsg: 'Error looking up email address on google api!'
|
2014-04-01 07:16:39 -04:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
callback(null, info.email);
|
|
|
|
});
|
|
|
|
};
|
2014-04-01 07:16:39 -04:00
|
|
|
|
2014-10-08 06:34:34 -04:00
|
|
|
module.exports = OAuth;
|