From 31ebe2b8501f8c74bad4762e9c35a02815d39249 Mon Sep 17 00:00:00 2001 From: Ryan Seys Date: Wed, 30 Jul 2014 16:03:29 -0700 Subject: [PATCH] Remove expires_in and simplify shouldRefresh logic --- lib/auth/computeclient.js | 1 + lib/auth/jwtclient.js | 1 - lib/auth/oauth2client.js | 15 ++++++--------- test/test.oauth2.js | 4 +--- 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/auth/computeclient.js b/lib/auth/computeclient.js index 3fc19a72f6..e2389aad1f 100644 --- a/lib/auth/computeclient.js +++ b/lib/auth/computeclient.js @@ -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); }); diff --git a/lib/auth/jwtclient.js b/lib/auth/jwtclient.js index ec6bd40098..977a9c9840 100644 --- a/lib/auth/jwtclient.js +++ b/lib/auth/jwtclient.js @@ -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)) }); }); diff --git a/lib/auth/oauth2client.js b/lib/auth/oauth2client.js index 6138971f34..75d3407780 100644 --- a/lib/auth/oauth2client.js +++ b/lib/auth/oauth2client.js @@ -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); }); @@ -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); }); @@ -230,15 +232,12 @@ 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; @@ -246,13 +245,11 @@ OAuth2Client.prototype.request = function(opts, callback, opt_forceDontRefresh) 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); diff --git a/test/test.oauth2.js b/test/test.oauth2.js index e08094f2f1..fb1a0bc25f 100644 --- a/test/test.oauth2.js +++ b/test/test.oauth2.js @@ -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(); @@ -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(); @@ -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')