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 72752dd commit a553164
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
8 changes: 7 additions & 1 deletion lib/common/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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;
}
}

/**
Expand Down Expand Up @@ -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',
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
9 changes: 8 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,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));
Expand Down

0 comments on commit a553164

Please sign in to comment.