Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// read in all the files with secrets, keys, certs
//
var files = {};
switch (config['auth-mode']) {
case 'oauth2':
// look for an oauth secret -- crash if not there
files.oauthSecret = fs.readFileSync(config['oauth-secret'], 'utf8').
replace(/(\n|\r)/gm,''); // newlines can mismatch secret
try { // ok if missing, we will generate
files.sessionSecret = fs.readFileSync(config['session-secret'], 'utf8');
} catch(err) {
console.error('error reading session secret: %s', JSON.stringify(err));
} finally { // just ignore if the file is not there
if (files.sessionSecret == null) {
console.error('generating session secret (will not work with scaled service)');
files.sessionSecret = require('base64url')(require('crypto').randomBytes(256)).substring(0, 256);
}
};
// don't break, do both.
case 'bearer': // and oauth2 as well:
// ensure we validate connections to master w/ master CA.
// technically this might not be required, but passport fails
// silently if it *is* needed and is not present.
var cas = https.globalAgent.options.ca || [];
cas.push(fs.readFileSync(config['master-ca'], 'utf8'));
https.globalAgent.options.ca = cas;
break;
case 'mutual_tls':
try {
files.mutualTlsCa = fs.readFileSync(config['mutual-tls-ca'], 'utf8');
} catch(err) {
throw 'No CA read for mutual TLS. Looked in: ' + config['mutual-tls-ca'];