Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call fromJSON with a JSON key so the project ID is read by google-auth-library #7

Closed
stephenplusplus opened this issue Nov 14, 2016 · 0 comments

Comments

@stephenplusplus
Copy link
Owner

stephenplusplus commented Nov 14, 2016

Auth.prototype.getAuthClient = function (callback) {
  var self = this;
  var config = self.config;

  if (this.authClient) {
    setImmediate(function () {
      callback(null, self.authClient);
    });

    return;
  }

  var googleAuth = new GoogleAuth();
  var keyFile = config.keyFilename || config.keyFile;

  if (config.credentials || path.extname(keyFile) === '.json') {
    googleAuth.fromJSON(config.credentials || require(keyFile), addScope);
  } else if (keyFile) {
    var authClient = new googleAuth.JWT();
    authClient.keyFile = keyFile;
    authClient.email = config.email;
    addScope(null, authClient);
  } else {
    googleAuth.getApplicationDefault(addScope);
  }

  function addScope(err, authClient, projectId) {
    if (err) {
      callback(err);
      return;
    }

    if (authClient.createScopedRequired()) {
      if (!config.scopes) {
        var scopeError = new Error('Scopes are required for this request.');
        scopeError.code = 'MISSING_SCOPE';
        callback(scopeError);
        return;
      }

      authClient.scopes = config.scopes;
    }

    self.authClient = authClient;
    self.projectId = projectId || authClient.projectId;

    callback(null, authClient);
  }
};
 var assign = require('object-assign');
 var GoogleAuth = require('google-auth-library');
+var path = require('path');

 function Auth(config) {
   if (!(this instanceof Auth)) {
@@ -40,15 +41,15 @@ Auth.prototype.getAuthClient = function (callback) {
   }

   var googleAuth = new GoogleAuth();
+  var keyFile = config.keyFilename || config.keyFile;

-  if (config.keyFilename || config.keyFile) {
+  if (config.credentials || path.extname(keyFile) === '.json') {
+    googleAuth.fromJSON(config.credentials || require(keyFile), addScope);
+  } else if (keyFile) {
     var authClient = new googleAuth.JWT();
-    authClient.keyFile = config.keyFilename || config.keyFile;
+    authClient.keyFile = keyFile;
     authClient.email = config.email;
-    authClient.scopes = config.scopes;
     addScope(null, authClient);
-  } else if (config.credentials) {
-    googleAuth.fromJSON(config.credentials, addScope);
   } else {
     googleAuth.getApplicationDefault(addScope);
   }
@@ -59,7 +60,7 @@ Auth.prototype.getAuthClient = function (callback) {
       return;
     }

-    if (authClient.createScopedRequired && authClient.createScopedRequired()) {
+    if (authClient.createScopedRequired()) {
       if (!config.scopes) {
         var scopeError = new Error('Scopes are required for this request.');
         scopeError.code = 'MISSING_SCOPE';
@@ -67,11 +68,11 @@ Auth.prototype.getAuthClient = function (callback) {
         return;
       }

-      authClient = authClient.createScoped(config.scopes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant