Skip to content

Commit

Permalink
Remove expires_in and simplify shouldRefresh logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanseys committed Jul 30, 2014
1 parent 10303fd commit 31ebe2b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/auth/computeclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Compute.prototype.refreshToken_ = function(ignored_, opt_callback) {
}, function(err, tokens) {
if (!err && tokens && tokens.expires_in) {
tokens.expiry_date = ((new Date()).getTime() + (tokens.expires_in * 1000));
delete tokens.expires_in;
}
opt_callback && opt_callback(err, tokens);
});
Expand Down
1 change: 0 additions & 1 deletion lib/auth/jwtclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ JWT.prototype.refreshToken_ = function(ignored_, opt_callback) {
opt_callback && opt_callback(err, {
access_token: token,
token_type: 'Bearer',
expires_in: that.gapi.token_expires,
expiry_date: ((new Date()).getTime() + (that.gapi.token_expires * 1000))
});
});
Expand Down
15 changes: 6 additions & 9 deletions lib/auth/oauth2client.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ OAuth2Client.prototype.getToken = function(code, opt_callback) {
}, function(err, tokens) {
if (!err && tokens && tokens.expires_in) {
tokens.expiry_date = ((new Date()).getTime() + (tokens.expires_in * 1000));
delete tokens.expires_in;
}
opt_callback && opt_callback(err, tokens);
});
Expand Down Expand Up @@ -167,6 +168,7 @@ OAuth2Client.prototype.refreshToken_ = function(refresh_token, opt_callback) {
}, function(err, tokens) {
if (!err && tokens && tokens.expires_in) {
tokens.expiry_date = ((new Date()).getTime() + (tokens.expires_in * 1000));
delete tokens.expires_in;
}
opt_callback && opt_callback(err, tokens);
});
Expand Down Expand Up @@ -230,29 +232,24 @@ OAuth2Client.prototype.revokeCredentials = function(callback) {
* access token and replays the unsuccessful request.
* @param {object} opts Request options.
* @param {function} callback callback.
* @param {boolean=} opt_forceDontRefresh If set, don't ask for a new token
* with refresh_token.
* @return {Request} Request object
*/
OAuth2Client.prototype.request = function(opts, callback, opt_forceDontRefresh) {
OAuth2Client.prototype.request = function(opts, callback) {

var that = this;
var credentials = this.credentials;
var shouldRefresh = !opt_forceDontRefresh;
var expiryDate = credentials.expiry_date;
// if no expiry time, assume it's not expired
var isTokenExpired = expiryDate ? expiryDate <= (new Date()).getTime() : false;

if (!credentials.access_token && !credentials.refresh_token) {
callback(new Error('No access or refresh token is set.'), null);
return;
} else if (!credentials.refresh_token) {
shouldRefresh = false;
} else if (credentials.access_token && !isTokenExpired) {
shouldRefresh = false;
}

if (shouldRefresh) {
var shouldRefresh = !credentials.access_token || isTokenExpired;

if (shouldRefresh && credentials.refresh_token) {
this.refreshAccessToken(function(err, tokens) {
if (err) {
callback(err, null);
Expand Down
4 changes: 1 addition & 3 deletions test/test.oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,6 @@ describe('OAuth2 client', function() {
assert(expiry_date < now + 5000);
assert.equal(oauth2client.credentials.refresh_token, 'abc');
assert.equal(oauth2client.credentials.access_token, 'abc123');
assert.equal(oauth2client.credentials.expires_in, 1);
assert.equal(oauth2client.credentials.token_type, 'Bearer');
scope.done();
done();
Expand Down Expand Up @@ -877,7 +876,6 @@ describe('OAuth2 client', function() {
assert(expiry_date < now + 4000);
assert.equal(oauth2client.credentials.refresh_token, 'abc');
assert.equal(oauth2client.credentials.access_token, 'abc123');
assert.equal(oauth2client.credentials.expires_in, 1);
assert.equal(oauth2client.credentials.token_type, 'Bearer');
scope.done();
done();
Expand Down Expand Up @@ -915,7 +913,7 @@ describe('OAuth2 client', function() {
});

describe('getToken()', function() {
it('should return expiry_date object with expires_in', function(done) {
it('should return expiry_date', function(done) {
var now = (new Date()).getTime();
var scope = nock('https://accounts.google.com')
.post('/o/oauth2/token')
Expand Down

0 comments on commit 31ebe2b

Please sign in to comment.