UNPKG

oidc-lib

Version:

A library for creating OIDC Service Providers

187 lines (161 loc) 7.08 kB
const fs = require('fs'); const path = require('path'); const util = require('./util'); const nodeInput = require('./nodeInput'); const defaultHostName = 'virtual.itsourweb.org'; const defaultDomain = 'itsourweb.org'; const defaultCertificateFilename = 'itsourweb.org'; const interactive = false; var updateIssuerDir = false; if (interactive){ console.log(); console.log(); console.log('/////////////////////////////////////////////////////////////////'); console.log(' INPUT REQUIRED'); console.log('/////////////////////////////////////////////////////////////////'); console.log(); } begin(); async function begin(){ try{ var libDir = process.cwd(); var issuerDir = path.join(libDir, '../..'); var libParent = path.join(libDir, '..'); if (!libParent.endsWith('node_modules')){ updateIssuerDir = false; } var possibleError = ''; // var parameters = await setInstallParameters(); var parameters = useDefaultInstallParameters(); if (parameters){ var httpsServerUrl = 'https://' + parameters.hostname + ':3000'; var keysPath = path.join(libDir, 'src/claimer_sts/keys', parameters.certificateFilename); keysPath = keysPath.replace(/\\/g, '\\\\'); var certificateFilename = keysPath; } else{ var httpsServerUrl = 'https://<your_url><:your_https_port_if_necessary>'; var certificateFilename = '<name_of_your_PEM_certificate_file>'; } var systemRoot = process.env.SystemRoot; if (systemRoot !== undefined){ var hostsPath = systemRoot.replace('\\', '/') + '/system32/drivers/etc/hosts'; possibleError = 'reading hosts file'; var hostsContent = fs.readFileSync(hostsPath, {encoding: 'utf8'}); if (hostsContent.indexOf(parameters.hostname) < 0){ console.warn(); console.warn('************* WARNING: If your DNS is not set to ' + parameters.hostname + ', you must manually add'); console.warn('************* the following line to ' + hostsPath + ' as administrator:'); console.warn('************* 127.0.0.1 ' + parameters.hostname); console.warn(); } } possibleError = 'reading oidc-lib package.json'; var oidc_lib_package = JSON.parse(fs.readFileSync(path.join(libDir, 'package.json'), {encoding: 'utf-8'})); var version = oidc_lib_package.version; var templatePath = path.join(libDir, 'src/claimer_sts/data/sts_config_template.json'); possibleError = 'reading claimer_config_template' var stringEncoding = fs.readFileSync(templatePath, {encoding: 'utf8'}); stringEncoding = stringEncoding.replace(/\{\{httpsServerUrl\}\}/g, httpsServerUrl); stringEncoding = stringEncoding.replace(/\{\{certificateFilename\}\}/g, certificateFilename); var stsConfigFilePath = path.join(libDir, 'src/claimer_sts/data/sts_config.json'); possibleError = 'writing ' + stsConfigFilePath; fs.writeFileSync(stsConfigFilePath, stringEncoding, 'utf8'); if (updateIssuerDir){ // write the index file into the issuer directory but only if it // doesn't exist since it may have been edited and expanded var issuerIndex = path.join(issuerDir, 'index.js'); if (!fs.existsSync(issuerIndex)){ var indexContent = fs.readFileSync('./src/install/process_cwd_index.js', 'utf-8'); fs.writeFileSync(issuerIndex, indexContent, { encoding: 'utf-8'}); } var packageFilePath = path.join(issuerDir, 'package.json'); possibleError = 'no package.json in startup directory'; if (!fs.existsSync(packageFilePath)){ throw ('Cannot start up'); } possibleError = 'issuer package.json is missing'; var issuerPackageString = fs.readFileSync(packageFilePath, {encoding: 'utf8'}); possibleError = 'issuer package.json is corrupt'; var issuerPackage = JSON.parse(issuerPackageString); // update contents of issuer package file if needed var package_update_required = false; // make sure startup directory's issuerPackage.json contains oidc scripts var oidc_scripts = { list: 'node node_modules/oidc-lib/src/install/list_module_config.js', import: 'node node_modules/oidc-lib/src/install/import_module_config.js', export: 'node node_modules/oidc-lib/src/install/export_module_config.js', delete: 'node node_modules/oidc-lib/src/install/delete_module_config.js' } if (issuerPackage.scripts === undefined){ package_update_required = true; issuerPackage.scripts = {}; } for (var script in oidc_scripts){ if (oidc_scripts[script] !== issuerPackage.scripts[script]){ package_update_required = true; issuerPackage.scripts[script] = oidc_scripts[script]; } } if (package_update_required){ packageString = JSON.stringify(issuerPackage, null, ' '); fs.writeFileSync(packageFilePath, packageString, {encoding: 'utf8'}); } // ensure wallet code is present var appWeb = path.join(issuerDir, 'web'); util.copyDirectory('src/web', appWeb); } console.log('All automated claimer install tasks complete.'); console.log(); process.exit(); } catch(err){ console.error(possibleError, err); } } function useDefaultInstallParameters(){ var parameters = {}; parameters.hostname = defaultHostName; parameters.extension = defaultHostName.substring(0, defaultHostName.indexOf('.')); parameters.certificateFilename = defaultCertificateFilename; return parameters; } async function setInstallParameters(){ var parameters = {}; var ni = new nodeInput(); var useDefault = await ni.question( 'Use the default hostname of ' + defaultHostName + '?', 'trueFalse'); if (useDefault){ parameters.hostname = defaultHostName; parameters.extension = defaultHostName.substring(0, defaultHostName.indexOf('.')); parameters.certificateFilename = defaultCertificateFilename; return parameters; } var useHostname = await ni.question( 'Use a test hostname within ' + defaultDomain + '?', 'trueFalse'); if (useHostname){ var hostname = await ni.question( 'Enter subdomain'); hostname = hostname.trim(' '); if (hostname.indexOf('.') < 0){ hostname += '.' + defaultDomain; } parameters.hostname = hostname; parameters.extension = hostname.substring(0, hostname.indexOf('.')); parameters.certificateFilename = defaultCertificateFilename; return parameters; } var useCustom = await ni.question( 'Use a hostname in your own domain?', 'trueFalse'); if (useCustom){ var custom = await ni.question( 'Enter hostname including domain (e.g. somehost.somewhere.com)'); parameters.hostname = custom; parameters.extension = custom.substring(0, custom.indexOf('.')); parameters.certificateFilename = '<name_of_your_PEM_certificate_file>'; console.log('You will need to set the certificate filename in claimer_config.js yourself.') return parameters; } console.log('You will need to set the server url, issuer url and certificate filename in claimer_config.js yourself.') return(null); }