stephenplusplus / google-auto-auth

Making it as easy as possible to authenticate a Google API request
MIT License
34 stars 9 forks source link

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

Closed stephenplusplus closed 7 years ago

stephenplusplus commented 7 years ago
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);