UNPKG

oidc-lib

Version:

A library for creating OIDC Service Providers

187 lines (158 loc) 5.53 kB
var fs = require('fs'); var path = require('path'); var base64url = require('base64url'); var node_input = require('./nodeInput'); const lib_data = 'oidc_lib_data'; const moniker_signature = /(?:['|"])(https:\/\/[0-9A-Za-z-\.]+:*\d*)['|"|\/]/g; const monikers = { "https://virtual.itsourweb.org:3000": "<--ISSUER_HOST-->", "https://virtual.itsourweb.org:3001": "<--CLIENT_API-->", "https://virtual.itsourweb.org:3002": "<--APP_LAUNCHER-->", "https://virtual.itsourweb.org:3003": "<--SMART_CREDENTIAL_HOST-->", "https://virtual.itsourweb.org:3004": "<--GATEWAY-->", "https://virtual.itsourweb.org:3005": "<--UHN-->", "https://virtual.itsourweb.org:3006": "<--DENTIST-->", "https://virtual.itsourweb.org:3007": "<--WALLET-->", } startup(); async function startup(){ try{ var output = {}; var possibleError; var ni; console.log('usage: node export_module_config [ targetDirectory feature ]'); var targetDirectory = process.cwd(); var feature; var proceed = false; if (process.argv.length === 4){ targetDirectory = process.argv[2]; feature = process.argv[3]; proceed = true; } console.log('Target directory:' + targetDirectory); if (!feature){ ni = new node_input(); feature = await ni.question( 'What feature do you want to export?'); feature = feature.trim(' '); } console.log('Feature:' + feature); var filename = path.join(process.cwd(), feature + '.json'); console.log('The feature will be exported to: ' + filename); var includeJsCss = false; /* var includeJsCss = await ni.question( 'Press "Y" to include js and css directories, or "N" skip them.', 'trueFalse'); */ if (!proceed){ proceed = await ni.question( 'Press "Y" to proceed, "N" to exit.', 'trueFalse'); if (!proceed){ process.exit(1); } } output.issuer = feature; possibleError = "Feature not found: " + feature; var configPath = path.join(targetDirectory, feature, 'oidc_config.json'); var configString = fs.readFileSync(configPath, {encoding: 'utf8'}); possibleError = "Feture configuration corrupt: " + feature; var moduleConfig = JSON.parse(configString); output.description = moduleConfig.config.credential_description; var packageDef = fs.readFileSync(path.join(targetDirectory, 'package.json'), 'utf-8'); possibleError = 'package.json is corrupted'; var packageObj = JSON.parse(packageDef); output.oidc_lib_version = packageObj.dependencies['oidc-lib']; var replacementInfo = { signature: moniker_signature, monikers: monikers, stretch: 1 } var codePath = path.join(targetDirectory, feature); output.code = grabDirectory(codePath, replacementInfo); var webPath = path.join(targetDirectory, 'web', feature); output['web/' + feature] = grabDirectory(webPath, replacementInfo); if (includeJsCss){ webPath = path.join(targetDirectory, 'web', 'js'); output['web/js'] = grabDirectory(webPath, replacementInfo); webPath = path.join(targetDirectory, 'web', 'css'); output['web/css'] = grabDirectory(webPath, replacementInfo); } webPath = path.join(targetDirectory, 'views'); output.views = grabDirectory(webPath, replacementInfo); for (var field in output){ if (output[field] === undefined){ continue; } var str = JSON.stringify(output[field]); console.log('length of ' + field + ': ' + str.length); } fs.writeFileSync(path.join(filename), JSON.stringify(output), {encoding: 'utf8'}); console.log('Done'); process.exit(0); } catch(err){ console.log(possibleError); process.exit(-1); } } function grabDirectory(directoryName, replaceInfo){ var output = {}; var dirents; try { dirents = fs.readdirSync(directoryName, {withFileTypes: true}); } catch(err){ return output; } var dirents = fs.readdirSync(directoryName, {withFileTypes: true}); for (var i=0; i<dirents.length; i++){ var dirent = dirents[i]; if (dirent.isFile()){ var name = dirent.name; var encoding = encodingFromExtension(name); var srcPath = path.join(directoryName, name); var bufferOrString = fs.readFileSync(srcPath, encoding); if (replaceInfo && encoding){ bufferOrString = processReplace(bufferOrString, replaceInfo) } output[name] = base64url.encode(bufferOrString); } } for (var i=0; i<dirents.length; i++){ var dirent = dirents[i]; if (dirent.isDirectory()){ output[dirent.name] = grabDirectory(path.join(directoryName, dirent.name), replaceInfo); } } return output; } function processReplace(content, replaceInfo){ var groups; var new_content = ''; var last_index = 0; while (groups = replaceInfo.signature.exec(content)){ var value = groups[1]; if (value && replaceInfo.monikers[value]){ new_content += content.substr(last_index, groups.index - last_index + replaceInfo.stretch) + replaceInfo.monikers[value]; last_index = replaceInfo.signature.lastIndex - replaceInfo.stretch; } } if (last_index){ new_content += content.substr(last_index); content = new_content; } return content; } function encodingFromExtension(name){ var encoding; var extension = name.substr(name.lastIndexOf('.') + 1); var binaries = [ 'jpg', 'png', 'jpeg', 'gif', 'bmp', 'eot', 'ttf', 'svg', 'woff', 'woff2' ]; if (binaries.includes(extension)){ encoding = undefined; } else{ encoding = 'utf8'; } return encoding; }