Skip to content

Commit a553164

Browse files
connection: allow passing a credentials object. fixes #136.
1 parent 72752dd commit a553164

5 files changed

Lines changed: 33 additions & 8 deletions

File tree

lib/common/connection.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ module.exports.Token = Token;
7676
*
7777
* @param {object} opts - Configuration options.
7878
* @param {array} opts.scopes - Scopes required for access.
79+
* @param {object} opts.credentials - Credentials object.
7980
*
8081
* @example
8182
* var SCOPES = [
@@ -93,6 +94,11 @@ function Connection(opts) {
9394

9495
this.isConnecting = false;
9596
this.waitQueue = [];
97+
98+
if (opts.credentials && opts.credentials.private_key &&
99+
opts.credentials.client_email) {
100+
this.credentials = opts.credentials;
101+
}
96102
}
97103

98104
/**
@@ -129,7 +135,7 @@ Connection.prototype.connect = function(callback) {
129135
*/
130136
Connection.prototype.fetchToken = function(callback) {
131137
var that = this;
132-
if (!this.opts.keyFilename) {
138+
if (!this.opts.keyFilename && !this.credentials) {
133139
// We should be on GCE, try to retrieve token from the metadata server.
134140
req({
135141
method: 'get',

lib/datastore/dataset.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ var SCOPES = [
7676
* @param {object=} options
7777
* @param {string} options.projectId - Dataset ID. This is your project ID from
7878
* the Google Developers Console.
79-
* @param {string} options.keyFilename - Full path to the JSON key downloaded
80-
* from the Google Developers Console.
79+
* @param {string=} options.keyFilename - Full path to the JSON key downloaded
80+
* from the Google Developers Console. Alternatively, you may provide a
81+
* `credentials` object.
82+
* @param {object=} options.credentials - Credentials object, used in place of
83+
* a `keyFilename`.
8184
* @param {string} options.namespace - Namespace to isolate transactions to.
8285
*
8386
* @example
@@ -90,6 +93,7 @@ function Dataset(options) {
9093
options = options || {};
9194

9295
this.connection = new conn.Connection({
96+
credentials: options.credentials,
9397
keyFilename: options.keyFilename,
9498
scopes: SCOPES
9599
});

lib/pubsub/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,19 @@ Topic.prototype.del = function(callback) {
219219
* Represents connection to Google Cloud Pub/Sub API.
220220
* @param {string} opts.projectId Google Developers Console Project ID.
221221
* @param {string} opts.email Service account email.
222-
* @param {string} opts.pemFilePath Path to the pem file that contains your
223-
* private key.
222+
* @param {string=} options.keyFilename - Full path to the JSON key downloaded
223+
* from the Google Developers Console. Alternatively, you may provide a
224+
* `credentials` object.
225+
* @param {object=} options.credentials - Credentials object, used in place of
226+
* a `keyFilename`.
224227
*/
225228
function Connection(opts) {
226229
opts = opts || {};
227230
var id = opts.projectId;
228231

229232
this.id = id;
230233
this.conn = new conn.Connection({
234+
credentials: opts.credentials,
231235
keyFilename: opts.keyFilename,
232236
scopes: SCOPES
233237
});

lib/storage/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,11 @@ BufferStream.prototype._read = function() {
9292
*
9393
* @param {object} options - Configuration options.
9494
* @param {string} options.bucketName - Name of the bucket.
95-
* @param {string} options.keyFilename - Full path to the JSON key downloaded
96-
* from the Google Developers Console.
95+
* @param {string=} options.keyFilename - Full path to the JSON key downloaded
96+
* from the Google Developers Console. Alternatively, you may provide a
97+
* `credentials` object.
98+
* @param {object=} options.credentials - Credentials object, used in place of
99+
* a `keyFilename`.
97100
*
98101
* @example
99102
* var gcloud = require('gcloud');
@@ -117,6 +120,7 @@ function Bucket(options) {
117120
}
118121
this.bucketName = options.bucketName;
119122
this.conn = new conn.Connection({
123+
credentials: options.credentials,
120124
keyFilename: options.keyFilename,
121125
scopes: SCOPES
122126
});

test/common/connection.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var connection = require('../../lib/common/connection.js');
2626

2727
describe('Connection', function() {
2828
var conn;
29+
var privateKeyFileJson = require('../testdata/privateKeyFile.json');
2930

3031
beforeEach(function() {
3132
conn = new connection.Connection({
@@ -34,7 +35,6 @@ describe('Connection', function() {
3435
});
3536

3637
it('should use a private key json file', function(done) {
37-
var privateKeyFileJson = require('../testdata/privateKeyFile.json');
3838
conn.fetchServiceAccountToken_ = function(callback) {
3939
callback(null);
4040
};
@@ -45,6 +45,13 @@ describe('Connection', function() {
4545
});
4646
});
4747

48+
it('should accept and assign credentials object', function() {
49+
var credConnection = new connection.Connection({
50+
credentials: privateKeyFileJson
51+
});
52+
assert.deepEqual(credConnection.credentials, privateKeyFileJson);
53+
});
54+
4855
describe('Token', function() {
4956
var tokenNeverExpires = new connection.Token('token', new Date(3000, 0, 0));
5057
var tokenExpired = new connection.Token('token', new Date(2011, 0, 0));

0 commit comments

Comments
 (0)