all files / lib/strategy/ EnrichClientFromRemoteConfigStrategy.js

97.83% Statements 45/46
95.45% Branches 21/22
100% Functions 13/13
97.83% Lines 45/46
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                                                                                                                         
'use strict';
 
var strings = require('../strings');
 
/**
 * Retrieves Stormpath settings from the API service, and ensures the local
 * configuration object properly reflects these settings.
 *
 * @class
*/
function EnrichClientFromRemoteConfigStrategy (clientFactory) {
  this.clientFactory = clientFactory;
}
 
// Finds and returns an Application object given an Application HREF.  Will
// return an error if no Application is found.
EnrichClientFromRemoteConfigStrategy.prototype._resolveApplicationByHref = function (client, config, href, cb) {
  client.getApplication(href, function (err, app) {
    if (err) {
      if(err.status === 404){
        cb(new Error(
          strings.APP_HREF_NOT_FOUND.replace('%href%', href)
        ));
      }else{
        cb(err);
      }
    } else {
      config.application = app;
      cb(null, app);
    }
  });
};
 
// Finds and returns an Application object given an Application name.  Will
// return an error if no Application is found.
EnrichClientFromRemoteConfigStrategy.prototype._resolveApplicationByName = function (client, config, name, cb) {
  client.getApplications({ name: name }, function (err, applications) {
    if (err) {
      return cb(err);
    }
 
    applications.detect(function (app, cb) {
      cb(name === app.name);
    }, function (app) {
      if (!app) {
        cb(new Error(
          strings.APP_NAME_NOT_FOUND.replace('%name%', name)
        ));
      } else {
        config.application = app;
        cb(null, app);
      }
    });
  });
};
 
// If there are only two applications and one of them is
// the Stormpath application, then use the other one as default.
EnrichClientFromRemoteConfigStrategy.prototype._resolveDefaultApplication = function (client, config, cb) {
  client.getApplications(function (err, applications) {
    if (err) {
      return cb(err);
    }
 
    var userApplications = applications.items.filter(function (app) {
      return app.name !== 'Stormpath';
    });
 
    if (userApplications.length === 1) {
      config.application = userApplications[0];
      cb(null, userApplications[0]);
    } else {
      cb(new Error(
        strings.UNABLE_TO_AUTO_RESOLVE_APP
      ));
    }
  });
};
 
EnrichClientFromRemoteConfigStrategy.prototype.process = function (config, callback) {
  Iif (config.skipRemoteConfig) {
    return callback(null, config);
  }
 
  var resolver = null;
  var application = config.application || {};
  var client = this.clientFactory(config);
 
  // Resolve the application either explicitly by HREF or implicitly by name.
  if (application.href) {
    resolver = this._resolveApplicationByHref.bind(this, client, config, application.href);
  } else if (application.name) {
    resolver = this._resolveApplicationByName.bind(this, client, config, application.name);
  } else {
    resolver = this._resolveDefaultApplication.bind(this, client, config);
  }
 
  client.on('ready', function () {
    resolver(function (err) {
      callback(err, err ? null : config);
    });
  });
};
 
module.exports = EnrichClientFromRemoteConfigStrategy;