2016-12-17 08:06:40 +07:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
run: run
|
|
|
|
}
|
|
|
|
|
|
|
|
var express = require('express');
|
|
|
|
var bodyParser = require('body-parser');
|
|
|
|
var speakeasy = require('speakeasy');
|
2016-12-18 01:36:41 +07:00
|
|
|
var path = require('path');
|
2017-01-21 23:41:06 +07:00
|
|
|
var session = require('express-session');
|
|
|
|
var winston = require('winston');
|
2017-01-22 02:24:35 +07:00
|
|
|
var UserDataStore = require('./user_data_store');
|
2017-01-29 01:59:15 +07:00
|
|
|
var Notifier = require('./notifier');
|
2017-01-28 07:32:25 +07:00
|
|
|
var AuthenticationRegulator = require('./authentication_regulator');
|
2017-01-29 07:33:48 +07:00
|
|
|
var setup_endpoints = require('./setup_endpoints');
|
2016-12-17 08:06:40 +07:00
|
|
|
|
2017-01-27 07:20:03 +07:00
|
|
|
function run(config, ldap_client, deps, fn) {
|
2016-12-18 01:36:41 +07:00
|
|
|
var view_directory = path.resolve(__dirname, '../views');
|
|
|
|
var public_html_directory = path.resolve(__dirname, '../public_html');
|
2017-01-22 02:24:35 +07:00
|
|
|
var datastore_options = {};
|
|
|
|
datastore_options.directory = config.store_directory;
|
2017-01-28 07:32:25 +07:00
|
|
|
if(config.store_in_memory)
|
|
|
|
datastore_options.inMemory = true;
|
2016-12-18 01:36:41 +07:00
|
|
|
|
2016-12-17 08:06:40 +07:00
|
|
|
var app = express();
|
2016-12-18 01:36:41 +07:00
|
|
|
app.use(express.static(public_html_directory));
|
2016-12-17 08:06:40 +07:00
|
|
|
app.use(bodyParser.urlencoded({ extended: false }));
|
2017-01-21 23:41:06 +07:00
|
|
|
app.use(bodyParser.json());
|
|
|
|
app.set('trust proxy', 1); // trust first proxy
|
|
|
|
|
|
|
|
app.use(session({
|
|
|
|
secret: config.session_secret,
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: true,
|
|
|
|
cookie: {
|
|
|
|
secure: false,
|
|
|
|
maxAge: config.session_max_age
|
|
|
|
},
|
|
|
|
}));
|
2016-12-17 08:06:40 +07:00
|
|
|
|
2016-12-18 01:36:41 +07:00
|
|
|
app.set('views', view_directory);
|
2016-12-17 08:06:40 +07:00
|
|
|
app.set('view engine', 'ejs');
|
|
|
|
|
2017-01-29 01:59:15 +07:00
|
|
|
// by default the level of logs is info
|
|
|
|
winston.level = config.logs_level || 'info';
|
2017-01-21 23:41:06 +07:00
|
|
|
|
2017-01-28 07:32:25 +07:00
|
|
|
var five_minutes = 5 * 60;
|
|
|
|
var data_store = new UserDataStore(deps.nedb, datastore_options);
|
|
|
|
var regulator = new AuthenticationRegulator(data_store, five_minutes);
|
2017-01-29 01:59:15 +07:00
|
|
|
var notifier = new Notifier(config.notifier, deps);
|
2017-01-28 07:32:25 +07:00
|
|
|
|
2017-01-21 23:41:06 +07:00
|
|
|
app.set('logger', winston);
|
2017-01-27 07:20:03 +07:00
|
|
|
app.set('ldap', deps.ldap);
|
2016-12-17 08:06:40 +07:00
|
|
|
app.set('ldap client', ldap_client);
|
|
|
|
app.set('totp engine', speakeasy);
|
2017-01-27 07:20:03 +07:00
|
|
|
app.set('u2f', deps.u2f);
|
2017-01-28 07:32:25 +07:00
|
|
|
app.set('user data store', data_store);
|
2017-01-29 01:59:15 +07:00
|
|
|
app.set('notifier', notifier);
|
2017-01-28 07:32:25 +07:00
|
|
|
app.set('authentication regulator', regulator);
|
2016-12-17 08:06:40 +07:00
|
|
|
app.set('config', config);
|
2017-01-27 07:20:03 +07:00
|
|
|
|
2017-01-29 07:33:48 +07:00
|
|
|
setup_endpoints(app);
|
2016-12-17 08:06:40 +07:00
|
|
|
|
2017-01-21 23:41:06 +07:00
|
|
|
return app.listen(config.port, function(err) {
|
2016-12-17 08:06:40 +07:00
|
|
|
console.log('Listening on %d...', config.port);
|
2017-01-21 23:41:06 +07:00
|
|
|
if(fn) fn();
|
2016-12-17 08:06:40 +07:00
|
|
|
});
|
|
|
|
}
|