From a553164674020b25cc2a66cc853b9b0e30b372a9 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Wed, 27 Aug 2014 15:00:05 -0400 Subject: [PATCH] connection: allow passing a credentials object. fixes #136. --- lib/common/connection.js | 8 +++++++- lib/datastore/dataset.js | 8 ++++++-- lib/pubsub/index.js | 8 ++++++-- lib/storage/index.js | 8 ++++++-- test/common/connection.js | 9 ++++++++- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/common/connection.js b/lib/common/connection.js index 53680871532c..4fdd46e4678e 100644 --- a/lib/common/connection.js +++ b/lib/common/connection.js @@ -76,6 +76,7 @@ module.exports.Token = Token; * * @param {object} opts - Configuration options. * @param {array} opts.scopes - Scopes required for access. + * @param {object} opts.credentials - Credentials object. * * @example * var SCOPES = [ @@ -93,6 +94,11 @@ function Connection(opts) { this.isConnecting = false; this.waitQueue = []; + + if (opts.credentials && opts.credentials.private_key && + opts.credentials.client_email) { + this.credentials = opts.credentials; + } } /** @@ -129,7 +135,7 @@ Connection.prototype.connect = function(callback) { */ Connection.prototype.fetchToken = function(callback) { var that = this; - if (!this.opts.keyFilename) { + if (!this.opts.keyFilename && !this.credentials) { // We should be on GCE, try to retrieve token from the metadata server. req({ method: 'get', diff --git a/lib/datastore/dataset.js b/lib/datastore/dataset.js index b53ea8122e87..475f69263b9d 100644 --- a/lib/datastore/dataset.js +++ b/lib/datastore/dataset.js @@ -76,8 +76,11 @@ var SCOPES = [ * @param {object=} options * @param {string} options.projectId - Dataset ID. This is your project ID from * the Google Developers Console. - * @param {string} options.keyFilename - Full path to the JSON key downloaded - * from the Google Developers Console. + * @param {string=} options.keyFilename - Full path to the JSON key downloaded + * from the Google Developers Console. Alternatively, you may provide a + * `credentials` object. + * @param {object=} options.credentials - Credentials object, used in place of + * a `keyFilename`. * @param {string} options.namespace - Namespace to isolate transactions to. * * @example @@ -90,6 +93,7 @@ function Dataset(options) { options = options || {}; this.connection = new conn.Connection({ + credentials: options.credentials, keyFilename: options.keyFilename, scopes: SCOPES }); diff --git a/lib/pubsub/index.js b/lib/pubsub/index.js index d977b690d7d1..8bbfe7c2cd1a 100644 --- a/lib/pubsub/index.js +++ b/lib/pubsub/index.js @@ -219,8 +219,11 @@ Topic.prototype.del = function(callback) { * Represents connection to Google Cloud Pub/Sub API. * @param {string} opts.projectId Google Developers Console Project ID. * @param {string} opts.email Service account email. - * @param {string} opts.pemFilePath Path to the pem file that contains your - * private key. + * @param {string=} options.keyFilename - Full path to the JSON key downloaded + * from the Google Developers Console. Alternatively, you may provide a + * `credentials` object. + * @param {object=} options.credentials - Credentials object, used in place of + * a `keyFilename`. */ function Connection(opts) { opts = opts || {}; @@ -228,6 +231,7 @@ function Connection(opts) { this.id = id; this.conn = new conn.Connection({ + credentials: opts.credentials, keyFilename: opts.keyFilename, scopes: SCOPES }); diff --git a/lib/storage/index.js b/lib/storage/index.js index 61a866c76f17..9bf06fc4a4dc 100644 --- a/lib/storage/index.js +++ b/lib/storage/index.js @@ -92,8 +92,11 @@ BufferStream.prototype._read = function() { * * @param {object} options - Configuration options. * @param {string} options.bucketName - Name of the bucket. - * @param {string} options.keyFilename - Full path to the JSON key downloaded - * from the Google Developers Console. + * @param {string=} options.keyFilename - Full path to the JSON key downloaded + * from the Google Developers Console. Alternatively, you may provide a + * `credentials` object. + * @param {object=} options.credentials - Credentials object, used in place of + * a `keyFilename`. * * @example * var gcloud = require('gcloud'); @@ -117,6 +120,7 @@ function Bucket(options) { } this.bucketName = options.bucketName; this.conn = new conn.Connection({ + credentials: options.credentials, keyFilename: options.keyFilename, scopes: SCOPES }); diff --git a/test/common/connection.js b/test/common/connection.js index 8087774ce16b..6cf34e0850eb 100644 --- a/test/common/connection.js +++ b/test/common/connection.js @@ -26,6 +26,7 @@ var connection = require('../../lib/common/connection.js'); describe('Connection', function() { var conn; + var privateKeyFileJson = require('../testdata/privateKeyFile.json'); beforeEach(function() { conn = new connection.Connection({ @@ -34,7 +35,6 @@ describe('Connection', function() { }); it('should use a private key json file', function(done) { - var privateKeyFileJson = require('../testdata/privateKeyFile.json'); conn.fetchServiceAccountToken_ = function(callback) { callback(null); }; @@ -45,6 +45,13 @@ describe('Connection', function() { }); }); + it('should accept and assign credentials object', function() { + var credConnection = new connection.Connection({ + credentials: privateKeyFileJson + }); + assert.deepEqual(credConnection.credentials, privateKeyFileJson); + }); + describe('Token', function() { var tokenNeverExpires = new connection.Token('token', new Date(3000, 0, 0)); var tokenExpired = new connection.Token('token', new Date(2011, 0, 0));