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);
|
ngModule.service('oauth', OAuth);
|
||||||
module.exports = OAuth;
|
module.exports = OAuth;
|
||||||
|
|
||||||
function OAuth(oauthRestDao) {
|
function OAuth($q, oauthRestDao) {
|
||||||
|
this._q = $q;
|
||||||
this._googleApi = oauthRestDao;
|
this._googleApi = oauthRestDao;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,7 +21,8 @@ OAuth.prototype.isSupported = function() {
|
|||||||
* Request an OAuth token from chrome for gmail users
|
* Request an OAuth token from chrome for gmail users
|
||||||
* @param {String} emailAddress The user's email address (optional)
|
* @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 = {
|
var idOptions = {
|
||||||
interactive: true
|
interactive: true
|
||||||
};
|
};
|
||||||
@ -28,7 +30,7 @@ OAuth.prototype.getOAuthToken = function(emailAddress, callback) {
|
|||||||
// check which runtime the app is running under
|
// check which runtime the app is running under
|
||||||
chrome.runtime.getPlatformInfo(function(platformInfo) {
|
chrome.runtime.getPlatformInfo(function(platformInfo) {
|
||||||
if (chrome.runtime.lastError || !platformInfo) {
|
if (chrome.runtime.lastError || !platformInfo) {
|
||||||
callback(new Error('Error getting chrome platform info!'));
|
reject(new Error('Error getting chrome platform info!'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,13 +42,12 @@ OAuth.prototype.getOAuthToken = function(emailAddress, callback) {
|
|||||||
// get OAuth Token from chrome
|
// get OAuth Token from chrome
|
||||||
chrome.identity.getAuthToken(idOptions, function(token) {
|
chrome.identity.getAuthToken(idOptions, function(token) {
|
||||||
if (chrome.runtime.lastError || !token) {
|
if (chrome.runtime.lastError || !token) {
|
||||||
callback({
|
reject(new Error('Error fetching an OAuth token for the user!'));
|
||||||
errMsg: 'Error fetching an OAuth token for the user!'
|
|
||||||
});
|
|
||||||
return;
|
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.oldToken The old token to be removed
|
||||||
* @param {String} options.emailAddress The user's email address (optional)
|
* @param {String} options.emailAddress The user's email address (optional)
|
||||||
*/
|
*/
|
||||||
OAuth.prototype.refreshToken = function(options, callback) {
|
OAuth.prototype.refreshToken = function(options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
return self._q(function(resolve) {
|
||||||
if (!options.oldToken) {
|
if (!options.oldToken) {
|
||||||
callback(new Error('oldToken option not set!'));
|
throw new Error('oldToken option not set!');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove cached token
|
// remove cached token
|
||||||
@ -69,7 +69,8 @@ OAuth.prototype.refreshToken = function(options, callback) {
|
|||||||
token: options.oldToken
|
token: options.oldToken
|
||||||
}, function() {
|
}, function() {
|
||||||
// get a new token
|
// 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
|
* Get email address from google api
|
||||||
* @param {String} token The oauth token
|
* @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) {
|
if (!token) {
|
||||||
callback({
|
throw new Error('Invalid OAuth token!');
|
||||||
errMsg: 'Invalid OAuth token!'
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resolve();
|
||||||
|
|
||||||
|
}).then(function() {
|
||||||
// fetch gmail user's email address from the Google Authorization Server
|
// 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
|
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() {
|
beforeEach(function() {
|
||||||
googleApiStub = sinon.createStubInstance(RestDAO);
|
googleApiStub = sinon.createStubInstance(RestDAO);
|
||||||
oauth = new OAuth(googleApiStub);
|
oauth = new OAuth(qMock, googleApiStub);
|
||||||
|
|
||||||
window.chrome = window.chrome || {};
|
window.chrome = window.chrome || {};
|
||||||
|
|
||||||
@ -56,15 +56,14 @@ describe('OAuth unit tests', function() {
|
|||||||
it('should work', function() {
|
it('should work', function() {
|
||||||
removeCachedStub.withArgs({
|
removeCachedStub.withArgs({
|
||||||
token: 'oldToken'
|
token: 'oldToken'
|
||||||
}).yields();
|
}).returns(resolves());
|
||||||
|
|
||||||
getOAuthTokenStub.withArgs(testEmail).yields();
|
getOAuthTokenStub.withArgs(testEmail).returns(resolves());
|
||||||
|
|
||||||
oauth.refreshToken({
|
oauth.refreshToken({
|
||||||
oldToken: 'oldToken',
|
oldToken: 'oldToken',
|
||||||
emailAddress: testEmail
|
emailAddress: testEmail
|
||||||
}, function(err) {
|
}).then(function() {
|
||||||
expect(err).to.not.exist;
|
|
||||||
expect(removeCachedStub.calledOnce).to.be.true;
|
expect(removeCachedStub.calledOnce).to.be.true;
|
||||||
expect(getOAuthTokenStub.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() {
|
it('should work without email', function() {
|
||||||
removeCachedStub.withArgs({
|
removeCachedStub.withArgs({
|
||||||
token: 'oldToken'
|
token: 'oldToken'
|
||||||
}).yields();
|
}).returns(resolves());
|
||||||
|
|
||||||
getOAuthTokenStub.withArgs(undefined).yields();
|
getOAuthTokenStub.withArgs(undefined).returns(resolves());
|
||||||
|
|
||||||
oauth.refreshToken({
|
oauth.refreshToken({
|
||||||
oldToken: 'oldToken',
|
oldToken: 'oldToken',
|
||||||
}, function(err) {
|
}).then(function() {
|
||||||
expect(err).to.not.exist;
|
|
||||||
expect(removeCachedStub.calledOnce).to.be.true;
|
expect(removeCachedStub.calledOnce).to.be.true;
|
||||||
expect(getOAuthTokenStub.calledOnce).to.be.true;
|
expect(getOAuthTokenStub.calledOnce).to.be.true;
|
||||||
expect(getOAuthTokenStub.calledWith(undefined)).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() {
|
it('should fail without all options', function() {
|
||||||
oauth.refreshToken({
|
oauth.refreshToken({
|
||||||
emailAddress: testEmail
|
emailAddress: testEmail
|
||||||
}, function(err) {
|
}).catch(function(err) {
|
||||||
expect(err).to.exist;
|
expect(err).to.exist;
|
||||||
expect(removeCachedStub.called).to.be.false;
|
expect(removeCachedStub.called).to.be.false;
|
||||||
expect(getOAuthTokenStub.called).to.be.false;
|
expect(getOAuthTokenStub.called).to.be.false;
|
||||||
@ -107,8 +105,7 @@ describe('OAuth unit tests', function() {
|
|||||||
interactive: true
|
interactive: true
|
||||||
}).yields('token');
|
}).yields('token');
|
||||||
|
|
||||||
oauth.getOAuthToken(undefined, function(err, token) {
|
oauth.getOAuthToken(undefined).then(function(token) {
|
||||||
expect(err).to.not.exist;
|
|
||||||
expect(token).to.equal('token');
|
expect(token).to.equal('token');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@ -123,8 +120,7 @@ describe('OAuth unit tests', function() {
|
|||||||
accountHint: testEmail
|
accountHint: testEmail
|
||||||
}).yields('token');
|
}).yields('token');
|
||||||
|
|
||||||
oauth.getOAuthToken(testEmail, function(err, token) {
|
oauth.getOAuthToken(testEmail).then(function(token) {
|
||||||
expect(err).to.not.exist;
|
|
||||||
expect(token).to.equal('token');
|
expect(token).to.equal('token');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@ -138,8 +134,7 @@ describe('OAuth unit tests', function() {
|
|||||||
interactive: true
|
interactive: true
|
||||||
}).yields('token');
|
}).yields('token');
|
||||||
|
|
||||||
oauth.getOAuthToken(testEmail, function(err, token) {
|
oauth.getOAuthToken(testEmail).then(function(token) {
|
||||||
expect(err).to.not.exist;
|
|
||||||
expect(token).to.equal('token');
|
expect(token).to.equal('token');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@ -151,9 +146,8 @@ describe('OAuth unit tests', function() {
|
|||||||
});
|
});
|
||||||
identityStub.yields();
|
identityStub.yields();
|
||||||
|
|
||||||
oauth.getOAuthToken(testEmail, function(err, token) {
|
oauth.getOAuthToken(testEmail).catch(function(err) {
|
||||||
expect(err).to.exist;
|
expect(err).to.exist;
|
||||||
expect(token).to.not.exist;
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -163,21 +157,19 @@ describe('OAuth unit tests', function() {
|
|||||||
it('should work', function(done) {
|
it('should work', function(done) {
|
||||||
googleApiStub.get.withArgs({
|
googleApiStub.get.withArgs({
|
||||||
uri: '/oauth2/v3/userinfo?access_token=token'
|
uri: '/oauth2/v3/userinfo?access_token=token'
|
||||||
}).yields(null, {
|
}).returns(resolves({
|
||||||
email: 'asdf@example.com'
|
email: 'asdf@example.com'
|
||||||
});
|
}));
|
||||||
|
|
||||||
oauth.queryEmailAddress('token', function(err, emailAddress) {
|
oauth.queryEmailAddress('token').then(function(emailAddress) {
|
||||||
expect(err).to.not.exist;
|
|
||||||
expect(emailAddress).to.equal('asdf@example.com');
|
expect(emailAddress).to.equal('asdf@example.com');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail due to invalid token', function(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(err).to.exist;
|
||||||
expect(emailAddress).to.not.exist;
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -187,9 +179,8 @@ describe('OAuth unit tests', function() {
|
|||||||
uri: '/oauth2/v3/userinfo?access_token=token'
|
uri: '/oauth2/v3/userinfo?access_token=token'
|
||||||
}).yields(new Error());
|
}).yields(new Error());
|
||||||
|
|
||||||
oauth.queryEmailAddress('token', function(err, emailAddress) {
|
oauth.queryEmailAddress('token').catch(function(err) {
|
||||||
expect(err).to.exist;
|
expect(err).to.exist;
|
||||||
expect(emailAddress).to.not.exist;
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user