mail/src/js/service/oauth.js

102 lines
2.9 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
2014-11-18 14:19:29 -05:00
var ngModule = angular.module('woServices');
ngModule.service('oauth', OAuth);
module.exports = OAuth;
2014-12-11 12:12:37 -05:00
function OAuth(oauthRestDao) {
this._googleApi = oauthRestDao;
2014-11-18 14:19:29 -05:00
}
2014-10-02 16:05:44 -04:00
/**
* Check if chrome.identity api is supported
* @return {Boolean} If is supported
*/
OAuth.prototype.isSupported = function() {
return !!(window.chrome && chrome.identity);
};
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)
*/
2014-12-11 08:46:05 -05:00
OAuth.prototype.getOAuthToken = function(emailAddress) {
2014-12-11 12:12:37 -05:00
return new Promise(function(resolve, reject) {
2014-12-11 08:46:05 -05:00
var idOptions = {
interactive: true
};
2014-12-11 08:46:05 -05:00
// check which runtime the app is running under
chrome.runtime.getPlatformInfo(function(platformInfo) {
if (chrome.runtime.lastError || !platformInfo) {
reject(new Error('Error getting chrome platform info!'));
2014-10-02 16:05:44 -04:00
return;
}
2014-12-11 08:46:05 -05: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;
}
// get OAuth Token from chrome
chrome.identity.getAuthToken(idOptions, function(token) {
if (chrome.runtime.lastError || !token) {
reject(new Error('Error fetching an OAuth token for the user!'));
return;
}
resolve(token);
});
});
2014-10-02 16:05:44 -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)
*/
2014-12-11 08:46:05 -05:00
OAuth.prototype.refreshToken = function(options) {
2014-10-02 16:05:44 -04:00
var self = this;
2014-12-11 12:12:37 -05:00
return new Promise(function(resolve) {
2014-12-11 08:46:05 -05:00
if (!options.oldToken) {
throw new Error('oldToken option not set!');
}
2014-12-11 08:46:05 -05:00
// remove cached token
chrome.identity.removeCachedAuthToken({
token: options.oldToken
}, function() {
// get a new token
self.getOAuthToken(options.emailAddress).then(resolve);
});
2014-10-02 16:05:44 -04:00
});
};
/**
* Get email address from google api
* @param {String} token The oauth token
*/
2014-12-11 08:46:05 -05:00
OAuth.prototype.queryEmailAddress = function(token) {
var self = this;
2014-12-11 12:12:37 -05:00
return new Promise(function(resolve) {
2014-12-11 08:46:05 -05:00
if (!token) {
throw new Error('Invalid OAuth token!');
}
resolve();
}).then(function() {
// fetch gmail user's email address from the Google Authorization Server
return self._googleApi.get({
uri: '/oauth2/v3/userinfo?access_token=' + token
});
2014-12-11 08:46:05 -05:00
}).then(function(info) {
if (!info || !info.email) {
throw new Error('Error looking up email address on google api!');
}
2014-12-11 08:46:05 -05:00
return info.email;
2014-10-02 16:05:44 -04:00
});
2014-11-18 14:19:29 -05:00
};