diff --git a/README.md b/README.md index 64c7895..87b3700 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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` \ No newline at end of file +4) Remove the passphrase from the .pem file: `openssl rsa -in key.pem -out key.pem` diff --git a/gapitoken.js b/gapitoken.js index 25259b9..f7e9592 100644 --- a/gapitoken.js +++ b/gapitoken.js @@ -10,22 +10,27 @@ 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) { @@ -33,7 +38,7 @@ GAPI.prototype.getToken = function(callback) { callback(null, this.token); } else { this.getAccessToken(callback); - } + } }; GAPI.prototype.getAccessToken = function(callback) { @@ -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 }; @@ -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 = ''; @@ -100,7 +109,7 @@ GAPI.prototype.getAccessToken = function(callback) { }); post_req.write(post_data); - post_req.end(); + post_req.end(); }; module.exports = GAPI;