all files / express-stormpath/lib/middleware/ default-organization-resolver.js

93.33% Statements 42/45
75% Branches 18/24
100% Functions 8/8
93.33% Lines 42/45
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        20× 20× 20× 20× 20×     20×   20×                                             20×   20×                                     12×     20×      
'use strict';
 
var parseDomain = require('psl').parse;
var helpers = require('../helpers/');
 
var organizationNameKeyToHrefMap = new helpers.CachedStore();
 
function defaultOrganizationResolver(req, res, next) {
  var client = req.app.get('stormpathClient');
  var application = req.app.get('stormpathApplication');
  var logger = req.app.get('stormpathLogger');
  var config = req.app.get('stormpathConfig');
  var cacheTtl = config.cacheOptions && config.cacheOptions.ttl !== undefined
    ? config.cacheOptions.ttl
    : config.client.cacheManager.defaultTtl;
  var web = config.web;
 
  var currentHost = parseDomain(helpers.getHost(req, true));
 
  function resolveOrganizationByHref(href, callback) {
    client.getOrganization(href, callback);
  }
 
  function resolveOrganizationByNameKey(nameKey, callback) {
    var organizationHref = organizationNameKeyToHrefMap.getCachedItem(nameKey, cacheTtl);
 
    if (organizationHref) {
      return resolveOrganizationByHref(organizationHref, callback);
    }
 
    client.getOrganizations({ nameKey: nameKey }, function (err, collection) {
      Iif (err) {
        return callback(err);
      }
 
      var organization = collection.items[0];
 
      if (organization) {
        organizationNameKeyToHrefMap.setCachedItem(nameKey, organization.href);
      }
 
      callback(null, organization);
    });
  }
 
  // Once we have an organization, attach it to the request and
  // continue processing the middleware pipeline.
  function continueWithOrganization(organization) {
    req.organization = organization;
    next();
  }
 
  // Strategy which tries to resolve an organization from a sub domain.
  // If this step fails then it falls back to resolving an organization from an access token cookie.
  function continueWithSubDomainStrategy() {
 
    Eif (web.multiTenancy.strategy === 'subdomain') {
 
      if ((web.domainName === currentHost.domain) && currentHost.subdomain) {
        return resolveOrganizationByNameKey(currentHost.subdomain, function (err, organization) {
 
          Iif (err) {
            return next(err);
          }
 
          if (!organization) {
            return next();
          }
 
          helpers.assertOrganizationIsMappedToApplication(organization, application, function (err, isMappedToApp) {
            Iif (err) {
              return next(err);
            }
 
            if (isMappedToApp) {
              return continueWithOrganization(organization);
            }
 
            logger.info('The organization "' + organization.name + '" is not mapped to this application, it will not be used.');
 
            next();
          });
        });
      }
 
    }
 
    next();
  }
 
  continueWithSubDomainStrategy();
}
 
module.exports = defaultOrganizationResolver;