mirror of
https://github.com/moparisthebest/mail
synced 2024-11-25 18:32:20 -05:00
Refactor oauth
This commit is contained in:
parent
88a48ec540
commit
7bb69c76b6
@ -4,7 +4,8 @@ var ngModule = angular.module('woServices');
|
||||
ngModule.service('oauth', OAuth);
|
||||
module.exports = OAuth;
|
||||
|
||||
function OAuth(oauthRestDao) {
|
||||
function OAuth($q, oauthRestDao) {
|
||||
this._q = $q;
|
||||
this._googleApi = oauthRestDao;
|
||||
}
|
||||
|
||||
@ -20,7 +21,8 @@ OAuth.prototype.isSupported = function() {
|
||||
* Request an OAuth token from chrome for gmail users
|
||||
* @param {String} emailAddress The user's email address (optional)
|
||||
*/
|
||||
OAuth.prototype.getOAuthToken = function(emailAddress, callback) {
|
||||
OAuth.prototype.getOAuthToken = function(emailAddress) {
|
||||
return this._q(function(resolve, reject) {
|
||||
var idOptions = {
|
||||
interactive: true
|
||||
};
|
||||
@ -28,7 +30,7 @@ OAuth.prototype.getOAuthToken = function(emailAddress, callback) {
|
||||
// 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!'));
|
||||
reject(new Error('Error getting chrome platform info!'));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -40,13 +42,12 @@ OAuth.prototype.getOAuthToken = function(emailAddress, callback) {
|
||||
// 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!'
|
||||
});
|
||||
reject(new Error('Error fetching an OAuth token for the user!'));
|
||||
return;
|
||||
}
|
||||
|
||||
callback(null, token);
|
||||
resolve(token);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
@ -56,12 +57,11 @@ OAuth.prototype.getOAuthToken = function(emailAddress, callback) {
|
||||
* @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) {
|
||||
OAuth.prototype.refreshToken = function(options) {
|
||||
var self = this;
|
||||
|
||||
return self._q(function(resolve) {
|
||||
if (!options.oldToken) {
|
||||
callback(new Error('oldToken option not set!'));
|
||||
return;
|
||||
throw new Error('oldToken option not set!');
|
||||
}
|
||||
|
||||
// remove cached token
|
||||
@ -69,7 +69,8 @@ OAuth.prototype.refreshToken = function(options, callback) {
|
||||
token: options.oldToken
|
||||
}, function() {
|
||||
// get a new token
|
||||
self.getOAuthToken(options.emailAddress, callback);
|
||||
self.getOAuthToken(options.emailAddress).then(resolve);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@ -77,25 +78,26 @@ OAuth.prototype.refreshToken = function(options, callback) {
|
||||
* Get email address from google api
|
||||
* @param {String} token The oauth token
|
||||
*/
|
||||
OAuth.prototype.queryEmailAddress = function(token, callback) {
|
||||
OAuth.prototype.queryEmailAddress = function(token) {
|
||||
var self = this;
|
||||
return self._q(function(resolve) {
|
||||
if (!token) {
|
||||
callback({
|
||||
errMsg: 'Invalid OAuth token!'
|
||||
});
|
||||
return;
|
||||
throw new Error('Invalid OAuth token!');
|
||||
}
|
||||
|
||||
resolve();
|
||||
|
||||
}).then(function() {
|
||||
// fetch gmail user's email address from the Google Authorization Server
|
||||
this._googleApi.get({
|
||||
return self._googleApi.get({
|
||||
uri: '/oauth2/v3/userinfo?access_token=' + token
|
||||
}, function(err, info) {
|
||||
if (err || !info || !info.email) {
|
||||
callback({
|
||||
errMsg: 'Error looking up email address on google api!'
|
||||
});
|
||||
return;
|
||||
|
||||
}).then(function(info) {
|
||||
if (!info || !info.email) {
|
||||
throw new Error('Error looking up email address on google api!');
|
||||
}
|
||||
|
||||
callback(null, info.email);
|
||||
return info.email;
|
||||
});
|
||||
};
|
@ -9,7 +9,7 @@ describe('OAuth unit tests', function() {
|
||||
|
||||
beforeEach(function() {
|
||||
googleApiStub = sinon.createStubInstance(RestDAO);
|
||||
oauth = new OAuth(googleApiStub);
|
||||
oauth = new OAuth(qMock, googleApiStub);
|
||||
|
||||
window.chrome = window.chrome || {};
|
||||
|
||||
@ -56,15 +56,14 @@ describe('OAuth unit tests', function() {
|
||||
it('should work', function() {
|
||||
removeCachedStub.withArgs({
|
||||
token: 'oldToken'
|
||||
}).yields();
|
||||
}).returns(resolves());
|
||||
|
||||
getOAuthTokenStub.withArgs(testEmail).yields();
|
||||
getOAuthTokenStub.withArgs(testEmail).returns(resolves());
|
||||
|
||||
oauth.refreshToken({
|
||||
oldToken: 'oldToken',
|
||||
emailAddress: testEmail
|
||||
}, function(err) {
|
||||
expect(err).to.not.exist;
|
||||
}).then(function() {
|
||||
expect(removeCachedStub.calledOnce).to.be.true;
|
||||
expect(getOAuthTokenStub.calledOnce).to.be.true;
|
||||
});
|
||||
@ -73,14 +72,13 @@ describe('OAuth unit tests', function() {
|
||||
it('should work without email', function() {
|
||||
removeCachedStub.withArgs({
|
||||
token: 'oldToken'
|
||||
}).yields();
|
||||
}).returns(resolves());
|
||||
|
||||
getOAuthTokenStub.withArgs(undefined).yields();
|
||||
getOAuthTokenStub.withArgs(undefined).returns(resolves());
|
||||
|
||||
oauth.refreshToken({
|
||||
oldToken: 'oldToken',
|
||||
}, function(err) {
|
||||
expect(err).to.not.exist;
|
||||
}).then(function() {
|
||||
expect(removeCachedStub.calledOnce).to.be.true;
|
||||
expect(getOAuthTokenStub.calledOnce).to.be.true;
|
||||
expect(getOAuthTokenStub.calledWith(undefined)).to.be.true;
|
||||
@ -90,7 +88,7 @@ describe('OAuth unit tests', function() {
|
||||
it('should fail without all options', function() {
|
||||
oauth.refreshToken({
|
||||
emailAddress: testEmail
|
||||
}, function(err) {
|
||||
}).catch(function(err) {
|
||||
expect(err).to.exist;
|
||||
expect(removeCachedStub.called).to.be.false;
|
||||
expect(getOAuthTokenStub.called).to.be.false;
|
||||
@ -107,8 +105,7 @@ describe('OAuth unit tests', function() {
|
||||
interactive: true
|
||||
}).yields('token');
|
||||
|
||||
oauth.getOAuthToken(undefined, function(err, token) {
|
||||
expect(err).to.not.exist;
|
||||
oauth.getOAuthToken(undefined).then(function(token) {
|
||||
expect(token).to.equal('token');
|
||||
done();
|
||||
});
|
||||
@ -123,8 +120,7 @@ describe('OAuth unit tests', function() {
|
||||
accountHint: testEmail
|
||||
}).yields('token');
|
||||
|
||||
oauth.getOAuthToken(testEmail, function(err, token) {
|
||||
expect(err).to.not.exist;
|
||||
oauth.getOAuthToken(testEmail).then(function(token) {
|
||||
expect(token).to.equal('token');
|
||||
done();
|
||||
});
|
||||
@ -138,8 +134,7 @@ describe('OAuth unit tests', function() {
|
||||
interactive: true
|
||||
}).yields('token');
|
||||
|
||||
oauth.getOAuthToken(testEmail, function(err, token) {
|
||||
expect(err).to.not.exist;
|
||||
oauth.getOAuthToken(testEmail).then(function(token) {
|
||||
expect(token).to.equal('token');
|
||||
done();
|
||||
});
|
||||
@ -151,9 +146,8 @@ describe('OAuth unit tests', function() {
|
||||
});
|
||||
identityStub.yields();
|
||||
|
||||
oauth.getOAuthToken(testEmail, function(err, token) {
|
||||
oauth.getOAuthToken(testEmail).catch(function(err) {
|
||||
expect(err).to.exist;
|
||||
expect(token).to.not.exist;
|
||||
done();
|
||||
});
|
||||
});
|
||||
@ -163,21 +157,19 @@ describe('OAuth unit tests', function() {
|
||||
it('should work', function(done) {
|
||||
googleApiStub.get.withArgs({
|
||||
uri: '/oauth2/v3/userinfo?access_token=token'
|
||||
}).yields(null, {
|
||||
}).returns(resolves({
|
||||
email: 'asdf@example.com'
|
||||
});
|
||||
}));
|
||||
|
||||
oauth.queryEmailAddress('token', function(err, emailAddress) {
|
||||
expect(err).to.not.exist;
|
||||
oauth.queryEmailAddress('token').then(function(emailAddress) {
|
||||
expect(emailAddress).to.equal('asdf@example.com');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail due to invalid token', function(done) {
|
||||
oauth.queryEmailAddress('', function(err, emailAddress) {
|
||||
oauth.queryEmailAddress('').catch(function(err) {
|
||||
expect(err).to.exist;
|
||||
expect(emailAddress).to.not.exist;
|
||||
done();
|
||||
});
|
||||
});
|
||||
@ -187,9 +179,8 @@ describe('OAuth unit tests', function() {
|
||||
uri: '/oauth2/v3/userinfo?access_token=token'
|
||||
}).yields(new Error());
|
||||
|
||||
oauth.queryEmailAddress('token', function(err, emailAddress) {
|
||||
oauth.queryEmailAddress('token').catch(function(err) {
|
||||
expect(err).to.exist;
|
||||
expect(emailAddress).to.not.exist;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user