From 7c9b4f9ff3f986b275ab7eaa059f83dc28e62a1d Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Fri, 6 Mar 2015 17:11:07 -0800 Subject: [PATCH 1/4] Use p12-to-pem to allow the .p12 files you get directly from Google to be used for authentication. Fixes #5. --- gapitoken.js | 19 +++++++++++++++++-- package.json | 3 ++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/gapitoken.js b/gapitoken.js index a77938e..031e26a 100644 --- a/gapitoken.js +++ b/gapitoken.js @@ -1,6 +1,7 @@ var https = require('https'); var jws = require('jws'); var fs = require('fs'); +var p12ToPem = require('p12-to-pem'); var GAPI = function(options, callback) { this.token = null; @@ -16,12 +17,12 @@ var GAPI = function(options, callback) { process.nextTick(function() { fs.readFile(options.keyFile, function(err, res) { if (err) { return callback(err); } - self.key = res; + self.key = decodeKey(res); callback(); }); }); } else if (options.key) { - this.key = options.key; + this.key = decodeKey(options.key); process.nextTick(callback); } else { callback(new Error("Missing key, key or keyFile option must be provided!")); @@ -103,4 +104,18 @@ GAPI.prototype.getAccessToken = function(callback) { post_req.end(); }; +// Takes either a raw, unprotected key or a password-protected PKCS12 file +// containing a private key and returns the key. +function decodeKey(key) { + var keyString = key.toString(); + var maybeP12 = keyString.indexOf("PRIVATE KEY-----") === -1; + if (maybeP12) { + // Google's PKCS12 files use the password "notasecret" + return p12ToPem(key, "notasecret"); + } + else { + return keyString; + } +} + module.exports = GAPI; diff --git a/package.json b/package.json index abfd3fd..fd53de4 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "license": "MIT", "readmeFilename": "README.md", "dependencies": { - "jws": "0.0.2" + "jws": "0.0.2", + "p12-to-pem": "^1.0.1" } } From 818ddfbd4a2ea3db9f2ad1c5701ec49d3018f86c Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Fri, 6 Mar 2015 17:17:43 -0800 Subject: [PATCH 2/4] Modify README instructions to show using the p12 from Google directly instead of converting it and removing the passphrase by hand. --- README.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 64c7895..4997b49 100644 --- a/README.md +++ b/README.md @@ -56,10 +56,6 @@ Creating a Private key file 1) Login to Google API Console, and under "API Access" create a "service account" for your project. -2) Download the .p12 private key file +2) Download the .p12 private key file. -3) Convert the .p12 file to .pem: `openssl pkcs12 -in key.p12 -out key.pem -nocerts` - -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 +3) Reference the file using the `keyFile` property as in the example above or pass it in via the `key` property as a base64-encoded string. From 391c65f39ce6bfa213954beb0fc68337114931a3 Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Fri, 6 Mar 2015 17:19:27 -0800 Subject: [PATCH 3/4] [Style] Replace some tabs with spaces for consistent indentation. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4997b49..52a2768 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ Node.js module for Google API service account authorization (Server to Server fl Installation ------------ - npm install gapitoken - + npm install gapitoken + Usage ----- From 25ce6eff5b8c6e622cd26c9bfdf8683803835e33 Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Sun, 5 Apr 2015 13:43:52 -0700 Subject: [PATCH 4/4] Revert "[Style] Replace some tabs with spaces for consistent indentation." Realized this was out-of-scope for this change. This reverts commit 391c65f39ce6bfa213954beb0fc68337114931a3. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 52a2768..4997b49 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ Node.js module for Google API service account authorization (Server to Server fl Installation ------------ - npm install gapitoken - + npm install gapitoken + Usage -----