Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from GAPI to gtoken + Bug Fix #20

Merged
merged 4 commits into from
Mar 26, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 instanceof Array ? this.scopes.join(' ') : this.scopes,

This comment was marked as spam.

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);

This comment was marked as spam.

};

/**
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;

This comment was marked as spam.

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
16 changes: 8 additions & 8 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'],
'[email protected]');
jwt.GAPI = function(opts, callback) {
jwt.gToken = function(opts) {
assert.equal('[email protected]', opts.iss);
assert.equal('/path/to/key.pem', opts.keyFile);
assert.equal('http://bar http://foo', opts.scope);
assert.equal('[email protected]', 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',
'[email protected]');

jwt.GAPI = function(opts) {
jwt.gToken = function(opts) {
assert.equal('http://foo', opts.scope);
done();
return {
getToken: function() {}
}

This comment was marked as spam.

};

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