authelia/src/lib/server.js

88 lines
2.9 KiB
JavaScript
Raw Normal View History

module.exports = {
run: run
}
var routes = require('./routes');
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 session = require('express-session');
var winston = require('winston');
var UserDataStore = require('./user_data_store');
var EmailSender = require('./email_sender');
2017-01-27 07:20:03 +07:00
var identity_check = require('./identity_check');
2017-01-27 07:20:03 +07:00
function run(config, ldap_client, deps, fn) {
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;
var email_options = {};
email_options.gmail = config.gmail;
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
app.use(session({
secret: config.session_secret,
resave: false,
saveUninitialized: true,
cookie: {
secure: false,
maxAge: config.session_max_age
},
}));
app.set('views', view_directory);
app.set('view engine', 'ejs');
winston.level = config.debug_level || 'info';
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);
app.set('user data store', new UserDataStore(deps.nedb, datastore_options));
app.set('email sender', new EmailSender(deps.nodemailer, email_options));
app.set('config', config);
2017-01-27 07:20:03 +07:00
var base_endpoint = '/authentication';
// web pages
2017-01-27 07:20:03 +07:00
app.get (base_endpoint + '/login', routes.login);
app.get (base_endpoint + '/logout', routes.logout);
identity_check(app, base_endpoint + '/u2f-register', routes.u2f_register.icheck_interface);
identity_check(app, base_endpoint + '/reset-password', routes.reset_password.icheck_interface);
app.get (base_endpoint + '/reset-password-form', function(req, res) { res.render('reset-password-form'); });
2017-01-27 07:20:03 +07:00
// Reset the password
app.post (base_endpoint + '/new-password', routes.reset_password.post);
// verify authentication
2017-01-27 07:20:03 +07:00
app.get (base_endpoint + '/verify', routes.verify);
2017-01-21 23:41:06 +07:00
// Authentication process
2017-01-27 07:20:03 +07:00
app.post (base_endpoint + '/1stfactor', routes.first_factor);
app.post (base_endpoint + '/2ndfactor/totp', routes.second_factor.totp);
2017-01-27 07:20:03 +07:00
app.get (base_endpoint + '/2ndfactor/u2f/register_request', routes.second_factor.u2f.register_request);
app.post (base_endpoint + '/2ndfactor/u2f/register', routes.second_factor.u2f.register);
2017-01-27 07:20:03 +07:00
app.get (base_endpoint + '/2ndfactor/u2f/sign_request', routes.second_factor.u2f.sign_request);
app.post (base_endpoint + '/2ndfactor/u2f/sign', routes.second_factor.u2f.sign);
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();
});
}