Skip to content

Commit

Permalink
Merge pull request #20 from stephenplusplus/master
Browse files Browse the repository at this point in the history
Switch from GAPI to gtoken + Bug Fix
  • Loading branch information
tbetbetbe committed Mar 26, 2015
2 parents ec10a81 + 54dee6e commit 97cefdd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 38 deletions.
44 changes: 18 additions & 26 deletions lib/auth/jwtclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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',
Expand Down Expand Up @@ -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) {
Expand All @@ -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
});
});
}
Expand Down Expand Up @@ -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,
keyFile: this.keyFile,
key: this.key
});
callback(null, this.gtoken);
}
};

Expand Down
5 changes: 3 additions & 2 deletions lib/auth/refreshclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand Down Expand Up @@ -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);
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
18 changes: 9 additions & 9 deletions test/test.jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.deepEqual(['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');
Expand All @@ -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();
Expand All @@ -125,7 +125,7 @@ describe('JWT auth client', function() {
refresh_token: 'jwt-placeholder'
};

jwt.gapi = {
jwt.gtoken = {
getToken: function(callback) {
callback(null, 'abc123');
}
Expand All @@ -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');
}
Expand Down Expand Up @@ -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');
},
Expand Down

0 comments on commit 97cefdd

Please sign in to comment.