authelia/src/lib/server.js

72 lines
2.1 KiB
JavaScript
Raw Normal View History

module.exports = {
run: run
}
var express = require('express');
var bodyParser = require('body-parser');
var speakeasy = require('speakeasy');
var path = require('path');
2017-01-21 23:41:06 +07:00
var winston = require('winston');
var UserDataStore = require('./user_data_store');
var Notifier = require('./notifier');
2017-01-28 07:32:25 +07:00
var AuthenticationRegulator = require('./authentication_regulator');
var setup_endpoints = require('./setup_endpoints');
var config_adapter = require('./config_adapter');
function run(yaml_config, ldap_client, deps, fn) {
var config = config_adapter(yaml_config);
var view_directory = path.resolve(__dirname, '../views');
var public_html_directory = path.resolve(__dirname, '../public_html');
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;
var app = express();
app.use(express.static(public_html_directory));
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
2017-03-16 05:07:57 +07:00
app.use(deps.session({
2017-01-21 23:41:06 +07:00
secret: config.session_secret,
resave: false,
saveUninitialized: true,
cookie: {
secure: false,
2017-03-16 05:07:57 +07:00
maxAge: config.session_max_age,
domain: config.session_domain
2017-01-21 23:41:06 +07:00
},
}));
app.set('views', view_directory);
app.set('view engine', 'ejs');
// 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);
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);
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);
app.set('notifier', notifier);
2017-01-28 07:32:25 +07:00
app.set('authentication regulator', regulator);
app.set('config', config);
2017-01-27 07:20:03 +07:00
setup_endpoints(app);
2017-01-21 23:41:06 +07:00
return app.listen(config.port, function(err) {
console.log('Listening on %d...', config.port);
2017-01-21 23:41:06 +07:00
if(fn) fn();
});
}