Skip to content

Commit

Permalink
connection: allow passing a credentials object. fixes googleapis#136.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Sawchuk committed Aug 27, 2014
1 parent 610e478 commit 4f1313c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
19 changes: 17 additions & 2 deletions lib/common/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ module.exports.Token = Token;
* Create a connection object.
*
* @param {object} opts - Configuration options.
* @param {array} opts.scopes - Scopes required for access.
* @param {array=} opts.scopes - Scopes required for access.
* @param {string=} opts.keyFilename - Full path to the JSON key downloaded
* from the Google Developers Console. Alternatively, you may provide a
* `credentials` object.
* @param {object=} opts.credentials - Credentials object.
* @param {string} opts.credentials.client_email
* @param {string} opts.credentials.private_key
*
* @example
* var SCOPES = [
Expand All @@ -93,6 +99,15 @@ function Connection(opts) {

this.isConnecting = false;
this.waitQueue = [];

if (opts.credentials) {
if (opts.credentials.client_email && opts.credentials.private_key) {
this.credentials = opts.credentials;
} else {
throw new Error('A credentials object must contain the following keys: ' +
'client_email, private_key');
}
}
}

/**
Expand Down Expand Up @@ -129,7 +144,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',
Expand Down
8 changes: 6 additions & 2 deletions lib/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -90,6 +93,7 @@ function Dataset(options) {
options = options || {};

this.connection = new conn.Connection({
credentials: options.credentials,
keyFilename: options.keyFilename,
scopes: SCOPES
});
Expand Down
8 changes: 6 additions & 2 deletions lib/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,19 @@ 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 || {};
var id = opts.projectId;

this.id = id;
this.conn = new conn.Connection({
credentials: opts.credentials,
keyFilename: opts.keyFilename,
scopes: SCOPES
});
Expand Down
8 changes: 6 additions & 2 deletions lib/storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -117,6 +120,7 @@ function Bucket(options) {
}
this.bucketName = options.bucketName;
this.conn = new conn.Connection({
credentials: options.credentials,
keyFilename: options.keyFilename,
scopes: SCOPES
});
Expand Down
20 changes: 19 additions & 1 deletion test/common/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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);
};
Expand All @@ -45,6 +45,24 @@ describe('Connection', function() {
});
});

describe('credentials object', function() {
it('should accept and assign a complete credentials object', function() {
var credConnection = new connection.Connection({
credentials: privateKeyFileJson
});
assert.deepEqual(credConnection.credentials, privateKeyFileJson);
});

it('should reject an incomplete credentials object', function() {
assert.throws(function() {
new connection.Connection({
credentials: {}
});
}, /must contain/);
});
});


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));
Expand Down

0 comments on commit 4f1313c

Please sign in to comment.