UNPKG

oidc-lib

Version:

A library for creating OIDC Service Providers

72 lines (62 loc) 2.59 kB
module.exports = { build: build } function build(pk, feature_config){ var possibleError = 'error in build'; try{ // var issuerDirectory = process.cwd(); // load the sts config possible_error = 'unexpected error reading sts_config'; var stsConfigPath = __dirname + '/data/sts_config.json'; var stsConfigString = pk.fs.readFileSync(stsConfigPath, {encoding: 'utf8'}); var config = JSON.parse(stsConfigString); config.logging = pk.util.logging_integer(config.logging); // read the issuerTemplate file that adds default config to the issuer configurations possible_error = 'unexpected error reading issuer_config_template'; var issuerTemplatePath = __dirname + '/data/issuer_config_template.json'; var issuerTemplateString = pk.fs.readFileSync(issuerTemplatePath, {encoding: 'utf8'}); // read the startup directory and check subdirectories for oidc_config files var featureConfigs = {}; for (var client in feature_config){ var feature = feature_config[client]; var additionalCollections; // read the configuration and separate out the collections, deleting it // from the configuration if (feature.collections){ additionalCollections = feature.collections; delete feature.collections; } possible_error = "Unable to set issuer using config.client in config_builder"; const regExp = new RegExp(/\{\{issuer\}\}/g); // Throws SyntaxError var moduleIssuerTemplateString = issuerTemplateString.replace(regExp, client); // add all the elements in the moduleIssuerTemplate that aren't explicityly // defined in the module config file possible_error = 'Conbfiguration Error - bad JSON processing issuerTemplateString (' + moduleIssuerTemplateString + ')'; var moduleIssuerTemplate = JSON.parse(moduleIssuerTemplateString); for (var key in moduleIssuerTemplate){ if (!feature[key]){ feature[key] = moduleIssuerTemplate[key]; } } // reinsert the collections if (additionalCollections){ var collections = feature.db.collections; for (var key in additionalCollections){ if (collections[key]){ pk.util.log_error('config_builder', 'collection present so cannot be added: ' + key); continue; } collections[key] = additionalCollections[key]; } } featureConfigs[client] = feature; } config.content_modules = featureConfigs; } catch(err){ pk.util.log_error(possible_error, err); pk.util.log_error('Fatal error - terminating') process.exit(-1); } return config; }