Skip to content

Commit de4136b

Browse files
connection: allow passing a credentials object. fixes #136.
1 parent 6e04a3e commit de4136b

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

lib/common/connection.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ module.exports.Token = Token;
7575
* Create a connection object.
7676
*
7777
* @param {object} opts - Configuration options.
78-
* @param {array} opts.scopes - Scopes required for access.
78+
* @param {array=} opts.scopes - Scopes required for access.
79+
* @param {string=} opts.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=} opts.credentials - Credentials object.
83+
* @param {string} opts.credentials.client_email
84+
* @param {string} opts.credentials.private_key
7985
*
8086
* @example
8187
* var SCOPES = [
@@ -93,6 +99,15 @@ function Connection(opts) {
9399

94100
this.isConnecting = false;
95101
this.waitQueue = [];
102+
103+
if (opts.credentials) {
104+
if (opts.credentials.client_email && opts.credentials.private_key) {
105+
this.credentials = opts.credentials;
106+
} else {
107+
throw new Error('A credentials object must contain the following keys: ' +
108+
'client_email, private_key');
109+
}
110+
}
96111
}
97112

98113
/**
@@ -129,7 +144,7 @@ Connection.prototype.connect = function(callback) {
129144
*/
130145
Connection.prototype.fetchToken = function(callback) {
131146
var that = this;
132-
if (!this.opts.keyFilename) {
147+
if (!this.opts.keyFilename && !this.credentials) {
133148
// We should be on GCE, try to retrieve token from the metadata server.
134149
req({
135150
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: 19 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,24 @@ describe('Connection', function() {
4545
});
4646
});
4747

48+
describe('credentials object', function() {
49+
it('should accept and assign a complete credentials object', function() {
50+
var credConnection = new connection.Connection({
51+
credentials: privateKeyFileJson
52+
});
53+
assert.deepEqual(credConnection.credentials, privateKeyFileJson);
54+
});
55+
56+
it('should reject an incomplete credentials object', function() {
57+
assert.throws(function() {
58+
new connection.Connection({
59+
credentials: {}
60+
});
61+
}, /must contain/);
62+
});
63+
});
64+
65+
4866
describe('Token', function() {
4967
var tokenNeverExpires = new connection.Token('token', new Date(3000, 0, 0));
5068
var tokenExpired = new connection.Token('token', new Date(2011, 0, 0));

0 commit comments

Comments
 (0)