1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-29 04:12:18 -05:00

Refactor oauth

This commit is contained in:
Tankred Hase 2014-12-11 14:46:05 +01:00
parent 88a48ec540
commit 7bb69c76b6
2 changed files with 73 additions and 80 deletions

View File

@ -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,33 +21,33 @@ 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) {
var idOptions = {
interactive: true
};
OAuth.prototype.getOAuthToken = function(emailAddress) {
return this._q(function(resolve, reject) {
var idOptions = {
interactive: true
};
// 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;
}
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) {
callback({
errMsg: 'Error fetching an OAuth token for the user!'
});
// 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!'));
return;
}
callback(null, token);
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);
});
});
});
};
@ -56,20 +57,20 @@ 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) {
throw new Error('oldToken option not set!');
}
if (!options.oldToken) {
callback(new Error('oldToken option not set!'));
return;
}
// remove cached token
chrome.identity.removeCachedAuthToken({
token: options.oldToken
}, function() {
// get a new token
self.getOAuthToken(options.emailAddress, callback);
// remove cached token
chrome.identity.removeCachedAuthToken({
token: options.oldToken
}, function() {
// get a new token
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) {
if (!token) {
callback({
errMsg: 'Invalid OAuth token!'
});
return;
}
// 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) {
callback({
errMsg: 'Error looking up email address on google api!'
});
return;
OAuth.prototype.queryEmailAddress = function(token) {
var self = this;
return self._q(function(resolve) {
if (!token) {
throw new Error('Invalid OAuth token!');
}
callback(null, info.email);
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
});
}).then(function(info) {
if (!info || !info.email) {
throw new Error('Error looking up email address on google api!');
}
return info.email;
});
};

View File

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