Comparing version 1.0.0 to 1.1.0
@@ -22,2 +22,4 @@ var gp12pem = require('google-p12-pem'); | ||
GoogleToken.prototype._mime = mime; | ||
/** | ||
@@ -52,6 +54,9 @@ * Returns whether the token has expired. | ||
} else if (!this.key && this.keyFile) { | ||
var mimeType = mime.lookup(this.keyFile); | ||
var mimeType = this._mime.lookup(this.keyFile); | ||
if (mimeType === 'application/x-pkcs12') { | ||
// detect .p12 file and convert to .pem on the fly | ||
gp12pem(this.keyFile, handleKey); | ||
} else if (mimeType === 'application/json') { | ||
// json file | ||
fs.readFile(this.keyFile, handleJSONKey); | ||
} else { | ||
@@ -66,2 +71,19 @@ // assume .pem key otherwise | ||
function handleJSONKey(err, key) { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
try { | ||
var body = JSON.parse(key); | ||
self.key = body.private_key; | ||
self.iss = body.client_email; | ||
} catch (e) { | ||
callback(e); | ||
return; | ||
} | ||
self._requestToken(callback); | ||
} | ||
function handleKey(err, key) { | ||
@@ -81,3 +103,2 @@ if (err) { | ||
* @param {Function} callback The callback function. | ||
* @return {[type]} [description] | ||
*/ | ||
@@ -119,3 +140,3 @@ GoogleToken.prototype.revokeToken = function(callback) { | ||
if(options.sub) { | ||
if (options.sub) { | ||
this.sub = options.sub; | ||
@@ -207,3 +228,3 @@ } | ||
callback(null, signedJWT); | ||
} catch(err) { | ||
} catch (err) { | ||
callback(err, null); | ||
@@ -210,0 +231,0 @@ } |
{ | ||
"name": "gtoken", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Node.js Google Authentication Service Account Tokens", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -5,3 +5,3 @@ # node-gtoken | ||
[![Build Status](https://travis-ci.org/ryanseys/node-gtoken.svg?branch=travis)](https://travis-ci.org/ryanseys/node-gtoken) | ||
[![Build Status](https://travis-ci.org/ryanseys/node-gtoken.svg?branch=master)](https://travis-ci.org/ryanseys/node-gtoken) | ||
@@ -16,7 +16,10 @@ ## Installation | ||
### Use with a `.pem` or `.p12` key file: | ||
``` js | ||
var gtoken = require('gtoken')({ | ||
var GoogleToken = require('gtoken'); | ||
var gtoken = GoogleToken({ | ||
keyFile: 'path/to/key.pem', // or path to .p12 key file | ||
email: 'my_service_account_email@developer.gserviceaccount.com', | ||
scope: ['https://scope1', 'https://scope2'], // or space-delimited string of scopes | ||
keyFile: 'path/to/key.pem' // or path to .p12 key file | ||
scope: ['https://scope1', 'https://scope2'] // or space-delimited string of scopes | ||
}); | ||
@@ -33,7 +36,26 @@ | ||
Another option is to pass the private key as a string: | ||
### Use with a service account `.json` key file: | ||
``` js | ||
var GoogleToken = require('gtoken'); | ||
var gtoken = GoogleToken({ | ||
keyFile: 'path/to/key.json', | ||
scope: ['https://scope1', 'https://scope2'] // or space-delimited string of scopes | ||
}); | ||
gtoken.getToken(function(err, token) { | ||
if (err) { | ||
console.log(err); | ||
return; | ||
} | ||
console.log(token); | ||
}); | ||
``` | ||
### Pass the private key as a string directly: | ||
``` js | ||
var key = '-----BEGIN RSA PRIVATE KEY-----\nXXXXXXXXXXX...'; | ||
var gtoken = require('gtoken')({ | ||
var GoogleToken = require('gtoken'); | ||
var gtoken = GoogleToken({ | ||
email: 'my_service_account_email@developer.gserviceaccount.com', | ||
@@ -52,3 +74,3 @@ scope: ['https://scope1', 'https://scope2'], // or space-delimited string of scopes | ||
- `options.sub`: The email address of the user requesting delegated access. | ||
- `options.keyFile`: The filename of `.pem` key or `.p12` key. | ||
- `options.keyFile`: The filename of `.json` key, `.pem` key or `.p12` key. | ||
- `options.key`: The raw RSA private key value, in place of using `options.keyFile`. | ||
@@ -55,0 +77,0 @@ |
@@ -7,6 +7,8 @@ var assert = require('assert'); | ||
var P12FILE = './test/assets/key.p12'; | ||
var KEYFILEJSON = './test/assets/key.json'; | ||
var KEYCONTENTS = fs.readFileSync(KEYFILE); | ||
var KEYJSONCONTENTS = fs.readFileSync(KEYFILEJSON); | ||
var SCOPE1 = 'https://www.googleapis.com/auth/urlshortener'; | ||
var SCOPE2 = 'https://www.googleapis.com/auth/drive'; | ||
var SCOPES = [ SCOPE1, SCOPE2 ]; | ||
var SCOPES = [SCOPE1, SCOPE2]; | ||
var sandbox = require('sandboxed-module'); | ||
@@ -29,2 +31,7 @@ | ||
var TESTDATA_KEYFILEJSON = { | ||
scope: 'scope123', // or space-delimited string of scopes | ||
keyFile: KEYFILEJSON | ||
}; | ||
var TESTDATA_P12 = { | ||
@@ -38,4 +45,6 @@ email: 'email@developer.gserviceaccount.com', | ||
lookup: function(filename) { | ||
if(filename === P12FILE) { | ||
if (filename === P12FILE) { | ||
return 'application/x-pkcs12'; | ||
} else if (filename === KEYFILEJSON) { | ||
return 'application/json'; | ||
} else { | ||
@@ -47,3 +56,3 @@ return ''; | ||
var noop = function(){}; | ||
var noop = function() {}; | ||
@@ -56,3 +65,3 @@ describe('gtoken', function() { | ||
it('should work without new or options', function() { | ||
var gtoken = require('../lib/index.js')(); | ||
var gtoken = GoogleToken(); | ||
assert(gtoken); | ||
@@ -63,3 +72,3 @@ }); | ||
it('should be set from email option', function() { | ||
var gtoken = require('../lib/index.js')({ | ||
var gtoken = GoogleToken({ | ||
email: EMAIL | ||
@@ -72,3 +81,3 @@ }); | ||
it('should be set from iss option', function() { | ||
var gtoken = require('../lib/index.js')({ | ||
var gtoken = GoogleToken({ | ||
iss: EMAIL | ||
@@ -80,3 +89,3 @@ }); | ||
it('should be set from email option over iss option', function() { | ||
var gtoken = require('../lib/index.js')({ | ||
var gtoken = GoogleToken({ | ||
iss: EMAIL, | ||
@@ -91,3 +100,3 @@ email: 'another' + EMAIL | ||
it('should accept strings', function() { | ||
var gtoken = require('../lib/index.js')({ | ||
var gtoken = GoogleToken({ | ||
scope: 'hello world' | ||
@@ -99,4 +108,4 @@ }); | ||
it('should accept array of strings', function() { | ||
var gtoken = require('../lib/index.js')({ | ||
scope: [ 'hello', 'world' ] | ||
var gtoken = GoogleToken({ | ||
scope: ['hello', 'world'] | ||
}); | ||
@@ -109,3 +118,3 @@ assert.equal(gtoken.scope, 'hello world'); | ||
it('should exist', function() { | ||
var gtoken = require('../lib/index.js')(); | ||
var gtoken = GoogleToken(); | ||
assert.equal(typeof gtoken.hasExpired, 'function'); | ||
@@ -115,3 +124,3 @@ }); | ||
it('should detect expired tokens', function() { | ||
var gtoken = require('../lib/index.js')(); | ||
var gtoken = GoogleToken(); | ||
assert(gtoken.hasExpired(), 'should be expired without token'); | ||
@@ -132,3 +141,3 @@ gtoken.token = 'hello'; | ||
it('should exist', function() { | ||
var gtoken = require('../lib/index.js')(); | ||
var gtoken = GoogleToken(); | ||
assert.equal(typeof gtoken.revokeToken, 'function'); | ||
@@ -139,6 +148,6 @@ }); | ||
it('should run ._configure()', function(done) { | ||
var gtoken = require('../lib/index.js')(); | ||
var gtoken = GoogleToken(); | ||
gtoken.token = 'woot'; | ||
gtoken._request = function(opts, cb) { | ||
assert.equal(opts, GOOGLE_REVOKE_TOKEN_URL+'woot'); | ||
assert.equal(opts, GOOGLE_REVOKE_TOKEN_URL + 'woot'); | ||
cb(); | ||
@@ -153,3 +162,3 @@ }; | ||
it('should return error when no token set', function(done) { | ||
var gtoken = require('../lib/index.js')(); | ||
var gtoken = GoogleToken(); | ||
gtoken.token = null; | ||
@@ -189,2 +198,3 @@ gtoken.revokeToken(function(err) { | ||
var gtoken = GoogleToken(TESTDATA_KEYFILE); | ||
gtoken._mime = MIME; | ||
@@ -205,2 +215,22 @@ gtoken._signJWT = function(opts, cb) { | ||
it('should read .json key from file', function(done) { | ||
var gtoken = GoogleToken(TESTDATA_KEYFILEJSON); | ||
gtoken._mime = MIME; | ||
gtoken._signJWT = function(opts, cb) { | ||
cb(); | ||
}; | ||
gtoken._request = function(opts, cb) { | ||
cb(); | ||
}; | ||
gtoken.getToken(function(err, token) { | ||
var parsed = JSON.parse(KEYJSONCONTENTS); | ||
assert.deepEqual(gtoken.key, parsed.private_key); | ||
assert.deepEqual(gtoken.iss, parsed.client_email); | ||
done(); | ||
}); | ||
}); | ||
it('should return cached token if not expired', function(done) { | ||
@@ -217,3 +247,5 @@ var gtoken = GoogleToken(TESTDATA); | ||
it('should run mime.lookup if keyFile given', function(done) { | ||
var mime = { | ||
var gtoken = GoogleToken(TESTDATA_KEYFILE); | ||
gtoken._mime = { | ||
lookup: function(filename) { | ||
@@ -225,11 +257,6 @@ assert.equal(filename, KEYFILE); | ||
var gtoken = sandbox.require('../lib/index.js', { | ||
requires: { | ||
'mime': mime | ||
} | ||
})(TESTDATA_KEYFILE); | ||
gtoken._request = function(opts, callback) { | ||
callback(); | ||
}; | ||
gtoken.getToken(noop); | ||
@@ -241,3 +268,2 @@ }); | ||
requires: { | ||
'mime': MIME, | ||
'google-p12-pem': function(filename, callback) { | ||
@@ -249,2 +275,4 @@ assert.equal(filename, P12FILE); | ||
})(TESTDATA_P12); | ||
gtoken._mime = MIME; | ||
gtoken.getToken(noop); | ||
@@ -251,0 +279,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24018
10
503
141