Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const fs = require('fs')
const path = require('path')
const auth = require('http-auth')
const config = require('../config')
const basic = auth.basic({
realm: 'Http Auth Realm',
}, (username, password, cb) => {
cb(username === config.HTTP_AUTH.USERNAME && password === config.HTTP_AUTH.PASSWORD)
})
module.exports = (req, res, next) => {
// 1. intercept request on favicon.ico
if (req.path === '/favicon.ico') {
return res.status(204).end()
}
// 2. apply http auth to log accessing
if (config.HTTP_AUTH.ITEMS_REG.test(req.path)) {
return basic.check(req, res, (request, response, err) => {
if (err) {
return next(err)
(function() {
var auth = require('http-auth'), // @see https://github.com/gevorg/http-auth
express = require('express'),
app = express(),
scribe = require('../scribe')(),
console = process.console;
app.set('port', (process.env.PORT || 5000));
/**
* User : test
* Pwd : tes
*/
var basicAuth = auth.basic({ //basic auth config
realm: "ScribeJS WebPanel",
file: __dirname + "/users.htpasswd" // test:test
});
app.get('/', function(req, res) {
res.send('Hello world, see you at /logs');
});
app.use('/logs', auth.connect(basicAuth), scribe.webPanel());
//Make some logs
console.addLogger('log');
console.addLogger('debug', 'inverse');
console.addLogger('fun', 'rainbow');
console.time().fun('hello world');
var forceSsl = function(req, res, next) {
if (req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
return next();
};
app.use(forceSsl);
}
// Logging
logger.express.access(app);
logger.express.error(app);
// Authentication
if (config.get('auth:enabled')) {
var basic = auth.basic({
realm: config.get('auth:realm')
}, function(username, password, callback) {
// Custom authentication method.
callback(username === config.get('auth:username') &&
password === config.get('auth:password'));
});
app.use(auth.connect(basic));
}
// Asset handling
var assets = {};
try {
assets = require('../assets.json');
} catch (e) {}
const auth = require('http-auth');
const bodyParser = require('body-parser');
const config = require('config');
const elasticsearch = require('elasticsearch');
const Express = require('express');
const passport = require('passport');
const path = require('path');
const Router = Express.Router;
const _ = require('lodash');
const { makeElasticsearchOptions } = require('../util/elasticOptions.js');
const context = new Router();
const basicAuth = auth.basic({
file: config.ElasticsearchAPI.htpasswd
});
passport.use(auth.passport(basicAuth));
const app = Express();
app.use(bodyParser.json());
app.use(context);
// redirect http to https
if (process.env.NODE_ENV === "production") {
app.enable('trust proxy');
app.use(function(req, res, next) {
if (req.headers['x-forwarded-proto'] && req.headers['x-forwarded-proto'].toLowerCase() === 'http') {
return res.redirect('https://' + req.headers.host + req.url);
}
return next();
const config = require('config');
const moment = require('moment');
const Express = require('express');
const path = require('path');
const Promise = require('bluebird');
const SenecaWeb = require('seneca-web');
const _ = require('lodash');
const bodyParser = require('body-parser');
const auth = require('http-auth');
const passport = require('passport');
const Router = Express.Router;
const context = new Router();
const basicAuth = auth.basic({
file: config.Dashboard.htpasswd
});
passport.use(auth.passport(basicAuth));
const senecaWebConfig = {
context,
adapter: require('seneca-web-adapter-express'), // eslint-disable-line
options: { parseBody: false },
};
const app = Express();
app.use(bodyParser.json());
app.use(context);
app.use(Express.static(path.resolve(`${__dirname}/../public`)) );
app.set('view engine', 'pug');
app.set('views', path.resolve(`${__dirname}/../views`));
let auth = require('http-auth');
let fs = require('fs');
let config = require('../util').config;
let basic = auth.basic({
realm: "Http Auth Realm"
}, (username, password, cb) => {
cb(username == config.HTTP_AUTH.username && password == config.HTTP_AUTH.password);
}
);
module.exports = (req, res, next) => {
basic.check(req, res, (req, res, err) => {
if (err) {
return next(err);
}
//apply http auth to log accessing
if (/\.log$/.test(req.url)) {
res.end(fs.readFileSync(config.API_LOG_PATH + req.url.substr(1)));
}
console.log('[FFM] https configuration found');
} else {
console.log('[FFM] no https configuration found');
}
var server;
if (config.basic_auth === undefined) {
console.log('[FFM] no basic auth configuration found');
if (httpsOptions !== undefined) {
server = https.createServer(httpsOptions, requestListener);
} else {
server = http.createServer(requestListener);
}
} else {
console.log('[FFM] using basic auth, passwd file: ' + config.basic_auth.file);
var basic = auth.basic(config.basic_auth);
if (httpsOptions !== undefined) {
server = https.createServer(basic, httpsOptions, requestListener);
} else {
server = http.createServer(basic, requestListener);
}
}
if (config.hostname === undefined) {
server.listen(config.port);
console.log('[FFM] listening ' + config.port);
} else {
server.listen(config.port, config.hostname);
console.log('[FFM] listening ' + config.port + ", " + config.hostname);
}
http.createServer(requestListener).listen(config.local_port, '127.0.0.1');
app.use('/subscribe', subscribeapi);
app.use('/', queryapi);
if (!Config.server_port) {
let server_port = process.env.PORT;
}
if (Config.enable_https) {
console.log("Https Server is enabled. Now loading credentials...");
let privateKey = fs.readFileSync(Config.private_key_path, 'utf8');
let certificate = fs.readFileSync(Config.certificate_path, 'utf8');
let credentials = {key: privateKey, cert: certificate};
if (!Config.auth) {
let basicAuth = auth.basic(Config.auth);
let httpsServer = Https.createServer(basicAuth, credentials, app);
console.log("Http authorization is enabled.");
} else {
let httpsServer = Https.createServer(credentials, app);
}
httpsServer.listen(Config.server_port, () => {
let host = httpsServer.address().address;
let port = httpsServer.address().port;
console.log("Https Server running at https://%s:%s", host, port);
});
} else {
if (!Config.auth) {
let basicAuth = auth.basic(Config.auth);
let httpServer = Http.createServer(basicAuth, app);
router.use((req, res, next) => {
if ( req.url !== '/elb-healthcheck' ) {
let basic = httpAuth.basic({
realm : "Reactium.",
file : basicAuthFile
});
httpAuth.connect(basic)(req, res, next);
} else {
next();
}
})
}
createLogsAuthForExpress: function() {
var basicAuth = auth.basic({
realm: 'achievibit ScribeJS WebPanel'
}, function (username, password, callback) {
var logsUsername = nconf.get('logsUsername') ?
nconf.get('logsUsername') + '' : '';
var logsPassword = nconf.get('logsPassword') ?
nconf.get('logsPassword') + '' : '';
callback(username === logsUsername && password === logsPassword);
});
return auth.connect(basicAuth);
}
};