Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor auth mechanism into plugin structure #251

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ app.use(function (req, res, next){
req.i18n = i18n;
req.app_context = app_context;
req.i18n.setLocaleFromCookie();
req.common = common;
req.config = config;
next();
});

Expand Down
1 change: 1 addition & 0 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"show_featured_in_article": false,
"featured_articles_count": "4",
"password_protect": false,
"auth_provider": "db",
"show_kb_meta": true,
"suggest_allowed": true,
"show_author_email": true,
Expand Down
100 changes: 100 additions & 0 deletions plugins/auth/db/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
exports.route = function(router) {
// setup form is shown when there are no users setup in the DB
router.get('/setup', function (req, res){
var db = req.app.db;
db.users.count({}, function (err, user_count){
// dont allow the user to "re-setup" if a user exists.
// set needs_setup to false as a user exists
req.session.needs_setup = false;
if(user_count === 0){
res.render('setup', {
title: 'Setup',
config: req.config,
message: req.common.clear_session_value(req.session, 'message'),
message_type: req.common.clear_session_value(req.session, 'message_type'),
show_footer: 'show_footer',
helpers: req.handlebars
});
}else{
res.redirect(req.app_context + '/login');
}
});
});

// login the user and check the password
router.post('/login_action', function (req, res){
var db = req.app.db;
var bcrypt = req.bcrypt;
var url = require('url');

db.users.findOne({user_email: req.body.email}, function (err, user){
// check if user exists with that email
if(user === undefined || user === null){
req.session.message = req.i18n.__('A user with that email does not exist.');
req.session.message_type = 'danger';
res.redirect(req.app_context + '/login');
}else{
// we have a user under that email so we compare the password
if(bcrypt.compareSync(req.body.password, user.user_password) === true){
req.session.user = req.body.email;
req.session.users_name = user.users_name;
req.session.user_id = user._id.toString();
req.session.is_admin = user.is_admin;
if(req.body.frm_referring_url === undefined || req.body.frm_referring_url === ''){
res.redirect(req.app_context + '/');
}else{
var url_parts = url.parse(req.body.frm_referring_url, true);
if(url_parts.pathname !== '/setup' && url_parts.pathname !== req.app_context + '/login'){
res.redirect(req.body.frm_referring_url);
}else{
res.redirect(req.app_context + '/');
}
}
}else{
// password is not correct
req.session.message = req.i18n.__('Access denied. Check password and try again.');
req.session.message_type = 'danger';
res.redirect(req.app_context + '/login');
}
}
});
});
}

exports.logout = function(req, res) {
res.redirect(req.app_context + '/');
};

exports.login = function(req, res) {
var db = req.app.db;
// set the template
req.common.setTemplateDir('admin', req);

db.users.count({}, function (err, user_count){
// we check for a user. If one exists, redirect to login form otherwise setup
if(user_count > 0){
// set needs_setup to false as a user exists
req.session.needs_setup = false;

// set the referring url
var referringUrl = req.header('Referer');
if(typeof req.session.refer_url !== 'undefined' && req.session.refer_url !== ''){
referringUrl = req.session.refer_url;
}

res.render('login', {
title: 'Login',
referring_url: referringUrl,
config: req.config,
message: req.common.clear_session_value(req.session, 'message'),
message_type: req.common.clear_session_value(req.session, 'message_type'),
show_footer: 'show_footer',
helpers: req.handlebars
});
}else{
// if there are no users set the "needs_setup" session
req.session.needs_setup = true;
res.redirect(req.app_context + '/setup');
}
});
}
3 changes: 3 additions & 0 deletions plugins/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.load = function(pluginType, pluginName) {
return require('./' + pluginType + '/' + pluginName + '/' + pluginName);
};
105 changes: 10 additions & 95 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var mime = require('mime-types');
var lunr = require('lunr');
var config = common.read_config();

var authProvider = require('../plugins/loader').load('auth', config.settings.auth_provider);
authProvider.route(router);

var appDir = path.dirname(require('require-main-filename')());

// The homepage of the site
Expand Down Expand Up @@ -705,7 +708,8 @@ router.get('/logout', function (req, res){
req.session.pw_validated = null;
req.session.message = null;
req.session.message_type = null;
res.redirect(req.app_context + '/');

authProvider.logout(req, res);
});

// users
Expand All @@ -732,9 +736,11 @@ router.get('/users', common.restrict, function (req, res){
});

// users
router.get('/user/edit/:id', common.restrict, function (req, res){
router.get('/user/edit/:id', common.restrict, function (req, res, next){
var db = req.app.db;
db.users.findOne({_id: common.getId(req.params.id)}, function (err, user){
if(!user) return next('router');

// if the user we want to edit is not the current logged in user and the current user is not
// an admin we render an access denied message
if(user.user_email !== req.session.user && req.session.is_admin === 'false'){
Expand Down Expand Up @@ -958,60 +964,8 @@ router.post('/user_update', common.restrict, function (req, res){
});

// login form
router.get('/login', function (req, res){
var db = req.app.db;
// set the template
common.setTemplateDir('admin', req);

db.users.count({}, function (err, user_count){
// we check for a user. If one exists, redirect to login form otherwise setup
if(user_count > 0){
// set needs_setup to false as a user exists
req.session.needs_setup = false;

// set the referring url
var referringUrl = req.header('Referer');
if(typeof req.session.refer_url !== 'undefined' && req.session.refer_url !== ''){
referringUrl = req.session.refer_url;
}

res.render('login', {
title: 'Login',
referring_url: referringUrl,
config: config,
message: common.clear_session_value(req.session, 'message'),
message_type: common.clear_session_value(req.session, 'message_type'),
show_footer: 'show_footer',
helpers: req.handlebars
});
}else{
// if there are no users set the "needs_setup" session
req.session.needs_setup = true;
res.redirect(req.app_context + '/setup');
}
});
});

// setup form is shown when there are no users setup in the DB
router.get('/setup', function (req, res){
var db = req.app.db;
db.users.count({}, function (err, user_count){
// dont allow the user to "re-setup" if a user exists.
// set needs_setup to false as a user exists
req.session.needs_setup = false;
if(user_count === 0){
res.render('setup', {
title: 'Setup',
config: config,
message: common.clear_session_value(req.session, 'message'),
message_type: common.clear_session_value(req.session, 'message_type'),
show_footer: 'show_footer',
helpers: req.handlebars
});
}else{
res.redirect(req.app_context + '/login');
}
});
router.get('/login', function (req, res) {
authProvider.login(req, res);
});

// Loops files on the disk, checks for their existance in any KB articles and removes non used files.
Expand Down Expand Up @@ -1049,45 +1003,6 @@ router.get('/file_cleanup', common.restrict, function (req, res){
});
});

// login the user and check the password
router.post('/login_action', function (req, res){
var db = req.app.db;
var bcrypt = req.bcrypt;
var url = require('url');

db.users.findOne({user_email: req.body.email}, function (err, user){
// check if user exists with that email
if(user === undefined || user === null){
req.session.message = req.i18n.__('A user with that email does not exist.');
req.session.message_type = 'danger';
res.redirect(req.app_context + '/login');
}else{
// we have a user under that email so we compare the password
if(bcrypt.compareSync(req.body.password, user.user_password) === true){
req.session.user = req.body.email;
req.session.users_name = user.users_name;
req.session.user_id = user._id.toString();
req.session.is_admin = user.is_admin;
if(req.body.frm_referring_url === undefined || req.body.frm_referring_url === ''){
res.redirect(req.app_context + '/');
}else{
var url_parts = url.parse(req.body.frm_referring_url, true);
if(url_parts.pathname !== '/setup' && url_parts.pathname !== req.app_context + '/login'){
res.redirect(req.body.frm_referring_url);
}else{
res.redirect(req.app_context + '/');
}
}
}else{
// password is not correct
req.session.message = req.i18n.__('Access denied. Check password and try again.');
req.session.message_type = 'danger';
res.redirect(req.app_context + '/login');
}
}
});
});

// delete user
router.get('/user/delete/:id', common.restrict, function (req, res){
// only allow admin
Expand Down