From c39f2a9dd3c9d2e62daaca7f19b2c10b2f8c579d Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Wed, 25 Mar 2015 16:11:20 -0400 Subject: [PATCH 1/4] update credentials object after refresh --- lib/auth/refreshclient.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/auth/refreshclient.js b/lib/auth/refreshclient.js index 844d2b88..d6bea66f 100644 --- a/lib/auth/refreshclient.js +++ b/lib/auth/refreshclient.js @@ -49,8 +49,8 @@ function callback(c, err, res) { * @private */ UserRefreshClient.prototype.refreshToken_ = function(ignored_, opt_callback) { - UserRefreshClient.super_.prototype.refereshToken_.call(this._refreshToken, - opt_callback); + UserRefreshClient.super_.prototype.refreshToken_.call( + this, this._refreshToken, opt_callback); }; /** @@ -88,6 +88,7 @@ UserRefreshClient.prototype.fromJSON = function(json, opt_callback) { that.clientId_ = json.client_id; that.clientSecret_ = json.client_secret; that._refreshToken = json.refresh_token; + that.credentials.refresh_token = json.refresh_token; callback(opt_callback); }; From 845f63d030e4c40bccf23be2e0b1efca3f994502 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Wed, 25 Mar 2015 16:11:35 -0400 Subject: [PATCH 2/4] switch to gtoken --- lib/auth/jwtclient.js | 44 ++++++++++++++++++------------------------- package.json | 2 +- test/test.jwt.js | 16 ++++++++-------- 3 files changed, 27 insertions(+), 35 deletions(-) diff --git a/lib/auth/jwtclient.js b/lib/auth/jwtclient.js index b3409373..1739ef98 100644 --- a/lib/auth/jwtclient.js +++ b/lib/auth/jwtclient.js @@ -18,12 +18,12 @@ var Auth2Client = require('./oauth2client.js'); var util = require('util'); -var GAPI = require('gapitoken'); +var gToken = require('gtoken'); /** * JWT service account credentials. * - * Retrieve access token using gapitoken. + * Retrieve access token using gtoken. * * @param {string=} email service account email address. * @param {string=} keyFile path to private key file. @@ -39,7 +39,7 @@ function JWT(email, keyFile, key, scopes, subject) { this.key = key; this.scopes = scopes; this.subject = subject; - this.GAPI = GAPI; + this.gToken = gToken; this.credentials = { refresh_token: 'jwt-placeholder', @@ -91,7 +91,7 @@ JWT.prototype.createScopedRequired = function() { }; /** - * Get the initial access token using gapitoken. + * Get the initial access token using gToken. * @param {function=} opt_callback Optional callback. */ JWT.prototype.authorize = function(opt_callback) { @@ -113,17 +113,15 @@ JWT.prototype.authorize = function(opt_callback) { * @private */ JWT.prototype.refreshToken_ = function(ignored_, opt_callback) { - var that = this; - - this._createGAPI(function(err, gapi) { + this._createGToken(function(err, gToken) { if (err) { callback(opt_callback, err); } else { - gapi.getToken(function (err, token) { + gToken.getToken(function (err, token) { callback(opt_callback, err, { access_token: token, token_type: 'Bearer', - expiry_date: that.gapi.token_expires * 1000 + expiry_date: gToken.token_expires * 1000 }); }); } @@ -189,28 +187,22 @@ JWT.prototype.fromStream = function(stream, opt_callback) { }; /** - * Creates the GAPI instance if it has not been created already. + * Creates the gToken instance if it has not been created already. * @param {function=} callback Callback. * @private */ -JWT.prototype._createGAPI = function(callback) { - var that = this; - - if (that.gapi) { - callback(null, that.gapi); +JWT.prototype._createGToken = function(callback) { + if (this.gtoken) { + callback(null, this.gtoken); } else { - that.gapi = new that.GAPI({ - iss: that.email, - sub: that.subject, - scope: that.scopes instanceof Array ? that.scopes.join(' ') : that.scopes, - keyFile: that.keyFile, - key: that.key - }, function (err) { - if (err) { - that.gapi = null; - } - callback(err, that.gapi); + this.gtoken = this.gToken({ + iss: this.email, + sub: this.subject, + scope: this.scopes instanceof Array ? this.scopes.join(' ') : this.scopes, + keyFile: this.keyFile, + key: this.key }); + callback(null, this.gtoken); } }; diff --git a/package.json b/package.json index 36ba8cde..3af483c2 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ ], "dependencies": { "async": "~0.9.0", - "gapitoken": "~0.1.2", + "gtoken": "^1.1.0", "request": "~2.51.0", "string-template": "~0.2.0" }, diff --git a/test/test.jwt.js b/test/test.jwt.js index a57b9d5b..0059b787 100644 --- a/test/test.jwt.js +++ b/test/test.jwt.js @@ -74,14 +74,11 @@ describe('JWT auth client', function() { null, ['http://bar', 'http://foo'], 'bar@subjectaccount.com'); - jwt.GAPI = function(opts, callback) { + jwt.gToken = function(opts) { assert.equal('foo@serviceaccount.com', opts.iss); assert.equal('/path/to/key.pem', opts.keyFile); assert.equal('http://bar http://foo', opts.scope); assert.equal('bar@subjectaccount.com', opts.sub); - setTimeout(function() { - callback(null); - }, 0); return { getToken: function(opt_callback) { opt_callback(null, 'initial-access-token'); @@ -104,9 +101,12 @@ describe('JWT auth client', function() { 'http://foo', 'bar@subjectaccount.com'); - jwt.GAPI = function(opts) { + jwt.gToken = function(opts) { assert.equal('http://foo', opts.scope); done(); + return { + getToken: function() {} + } }; jwt.authorize(); @@ -125,7 +125,7 @@ describe('JWT auth client', function() { refresh_token: 'jwt-placeholder' }; - jwt.gapi = { + jwt.gtoken = { getToken: function(callback) { callback(null, 'abc123'); } @@ -152,7 +152,7 @@ describe('JWT auth client', function() { expiry_date: (new Date()).getTime() - 1000 }; - jwt.gapi = { + jwt.gtoken = { getToken: function(callback) { callback(null, 'abc123'); } @@ -234,7 +234,7 @@ describe('JWT auth client', function() { var dateInSeconds = (new Date()).getTime() / 1000; - jwt.gapi = { + jwt.gtoken = { getToken: function(callback) { callback(null, 'token'); }, From ad6e6bfe225938e8d771144ac1ce78a47701b6b2 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Wed, 25 Mar 2015 16:41:31 -0400 Subject: [PATCH 3/4] send raw scopes message to gtoken https://github.com/google/google-auth-library-nodejs/pull/20\#discussion_r27163383 --- lib/auth/jwtclient.js | 2 +- test/test.jwt.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/auth/jwtclient.js b/lib/auth/jwtclient.js index 1739ef98..6cfd38d3 100644 --- a/lib/auth/jwtclient.js +++ b/lib/auth/jwtclient.js @@ -198,7 +198,7 @@ JWT.prototype._createGToken = function(callback) { this.gtoken = this.gToken({ iss: this.email, sub: this.subject, - scope: this.scopes instanceof Array ? this.scopes.join(' ') : this.scopes, + scope: this.scopes, keyFile: this.keyFile, key: this.key }); diff --git a/test/test.jwt.js b/test/test.jwt.js index 0059b787..1078337d 100644 --- a/test/test.jwt.js +++ b/test/test.jwt.js @@ -77,7 +77,7 @@ describe('JWT auth client', function() { jwt.gToken = function(opts) { assert.equal('foo@serviceaccount.com', opts.iss); assert.equal('/path/to/key.pem', opts.keyFile); - assert.equal('http://bar http://foo', opts.scope); + assert.deepEqual(['http://bar', 'http://foo'], opts.scope); assert.equal('bar@subjectaccount.com', opts.sub); return { getToken: function(opt_callback) { From 54dee6e57739fafa85d0ec3aec9265275a3bea16 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Wed, 25 Mar 2015 16:54:49 -0400 Subject: [PATCH 4/4] fix failing test https://github.com/google/google-auth-library-nodejs/pull/20\#discussion_r27164584 --- test/test.jwt.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.jwt.js b/test/test.jwt.js index 1078337d..628f7e70 100644 --- a/test/test.jwt.js +++ b/test/test.jwt.js @@ -106,7 +106,7 @@ describe('JWT auth client', function() { done(); return { getToken: function() {} - } + }; }; jwt.authorize();