Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
node-gapitoken
==============

Node.js module for Google API service account authorization (Server to Server flow).
Node.js module for Google-compatible API service account authorization (Server to Server flow).

Installation
------------

npm install gapitoken
npm install gapitoken-generic

Usage
-----

var GAPI = require('gapitoken');
var GAPI = require('gapitoken-generic');

var gapi = new GAPI({
iss: 'service account email address from Google API console',
scope: 'space delimited list of requested scopes',
keyFile: 'path to private_key.pem'
}, function(err) {
if (err) { return console.log(err); }

gapi.getToken(function(err, token) {
if (err) { return console.log(err); }
console.log(token);
});
});
});

Another option is to pass the private key as a string
Expand All @@ -45,10 +45,10 @@ Another option is to pass the private key as a string
gapi.getToken(function(err, token) {
if (err) { return console.log(err); }
console.log(token);
});
});
});


* for using node-gapitoken to access Google Cloud Storage see https://github.com/bsphere/node-gcs

Creating a Private key file
Expand All @@ -62,4 +62,4 @@ Creating a Private key file

NOTE: You must set a passphrase for the .pem file

4) Remove the passphrase from the .pem file: `openssl rsa -in key.pem -out key.pem`
4) Remove the passphrase from the .pem file: `openssl rsa -in key.pem -out key.pem`
53 changes: 31 additions & 22 deletions gapitoken.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,35 @@ var GAPI = function(options, callback) {
this.scope = options.scope;
this.sub = options.sub;
this.prn = options.prn;

if (options.keyFile) {
var self = this;
process.nextTick(function() {
fs.readFile(options.keyFile, function(err, res) {
if (err) { return callback(err); }
self.key = res;
callback();
});
});
} else if (options.key) {
this.key = options.key;
process.nextTick(callback);
} else {
callback(new Error("Missing key, key or keyFile option must be provided!"));
}
this.aud = options.aud || 'https://accounts.google.com/o/oauth2/token';
this.host = options.host || 'accounts.google.com';
this.path = options.path || '/o/oauth2/token';
this.port = options.port;
this.grant = options.grant || 'urn:ietf:params:oauth:grant-type:jwt-bearer';

if (options.keyFile) {
var self = this;
process.nextTick(function() {
fs.readFile(options.keyFile, function(err, res) {
if (err) { return callback(err); }
self.key = res;
callback();
});
});
} else if (options.key) {
this.key = options.key;
process.nextTick(callback);
} else {
callback(new Error("Missing key, key or keyFile option must be provided!"));
}
};

GAPI.prototype.getToken = function(callback) {
if (this.token && this.token_expires && (new Date()).getTime() < this.token_expires * 1000) {
callback(null, this.token);
} else {
this.getAccessToken(callback);
}
}
};

GAPI.prototype.getAccessToken = function(callback) {
Expand All @@ -42,7 +47,7 @@ GAPI.prototype.getAccessToken = function(callback) {
var payload = {
iss: this.iss,
scope: this.scope,
aud: 'https://accounts.google.com/o/oauth2/token',
aud: this.aud,
exp: iat + 3600,
iat: iat
};
Expand All @@ -59,16 +64,20 @@ GAPI.prototype.getAccessToken = function(callback) {
secret: this.key
});

var post_data = 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=' + signedJWT;
var post_data = 'grant_type=' + encodeURIComponent(this.grant) + '&assertion=' + signedJWT;
var post_options = {
host: 'accounts.google.com',
path: '/o/oauth2/token',
host: this.host,
path: this.path,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};

if (this.port) {
post_options.port = this.port;
}

var self = this;
var post_req = https.request(post_options, function(res) {
var d = '';
Expand Down Expand Up @@ -100,7 +109,7 @@ GAPI.prototype.getAccessToken = function(callback) {
});

post_req.write(post_data);
post_req.end();
post_req.end();
};

module.exports = GAPI;