2017-10-03 14:40:53 +03:00
|
|
|
var fs = require('fs'),
|
|
|
|
session = require('cookie-session'),
|
|
|
|
crypto = require('crypto'),
|
|
|
|
path = require('path'),
|
|
|
|
config = require('../../../config'),
|
|
|
|
utils = require('../../../utils'),
|
2017-10-05 13:07:32 +03:00
|
|
|
errors = require('../../../errors'),
|
2017-10-03 14:40:53 +03:00
|
|
|
i18n = require('../../../i18n'),
|
|
|
|
settingsCache = require('../../../settings/cache'),
|
2016-09-13 18:41:14 +03:00
|
|
|
privateRoute = '/' + config.get('routeKeywords').private + '/',
|
2015-10-12 19:54:15 +03:00
|
|
|
privateBlogging;
|
2015-07-15 19:01:23 +03:00
|
|
|
|
|
|
|
function verifySessionHash(salt, hash) {
|
|
|
|
if (!salt || !hash) {
|
2017-10-03 14:40:53 +03:00
|
|
|
return false;
|
2015-07-15 19:01:23 +03:00
|
|
|
}
|
|
|
|
|
2017-10-03 14:40:53 +03:00
|
|
|
var hasher = crypto.createHash('sha256');
|
|
|
|
hasher.update(settingsCache.get('password') + salt, 'utf8');
|
|
|
|
return hasher.digest('hex') === hash;
|
2015-07-15 19:01:23 +03:00
|
|
|
}
|
|
|
|
|
2015-10-12 19:54:15 +03:00
|
|
|
privateBlogging = {
|
2015-07-15 19:01:23 +03:00
|
|
|
checkIsPrivate: function checkIsPrivate(req, res, next) {
|
2017-10-03 14:40:53 +03:00
|
|
|
var isPrivateBlog = settingsCache.get('is_private');
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2017-10-03 14:40:53 +03:00
|
|
|
if (!isPrivateBlog) {
|
|
|
|
res.isPrivateBlog = false;
|
|
|
|
return next();
|
|
|
|
}
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2017-10-03 14:40:53 +03:00
|
|
|
res.isPrivateBlog = true;
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2017-10-03 14:40:53 +03:00
|
|
|
return session({
|
|
|
|
maxAge: utils.ONE_MONTH_MS,
|
|
|
|
signed: false
|
|
|
|
})(req, res, next);
|
2015-07-15 19:01:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
filterPrivateRoutes: function filterPrivateRoutes(req, res, next) {
|
2016-04-11 16:58:41 +03:00
|
|
|
if (res.isAdmin || !res.isPrivateBlog || req.url.lastIndexOf(privateRoute, 0) === 0) {
|
2015-07-15 19:01:23 +03:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2017-09-11 16:09:19 +03:00
|
|
|
if (req.url.lastIndexOf('/robots.txt', 0) === 0) {
|
2017-10-03 14:40:53 +03:00
|
|
|
return fs.readFile(path.resolve(__dirname, '../', 'robots.txt'), function readFile(err, buf) {
|
2015-07-15 19:01:23 +03:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2017-05-29 23:10:32 +03:00
|
|
|
|
2015-07-15 19:01:23 +03:00
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'text/plain',
|
|
|
|
'Content-Length': buf.length,
|
2017-05-29 23:10:32 +03:00
|
|
|
'Cache-Control': 'public, max-age=' + config.get('caching:robotstxt:maxAge')
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
2017-05-29 23:10:32 +03:00
|
|
|
|
2015-07-15 19:01:23 +03:00
|
|
|
res.end(buf);
|
|
|
|
});
|
|
|
|
}
|
2017-10-03 14:40:53 +03:00
|
|
|
|
2017-10-05 13:07:32 +03:00
|
|
|
// CASE: Allow private RSS feed urls.
|
|
|
|
// Any url which contains the hash and the postfix /rss is allowed to access a private rss feed without
|
|
|
|
// a session. As soon as a path matches, we rewrite the url. Even Express uses rewriting when using `app.use()`.
|
|
|
|
if (req.url.indexOf(settingsCache.get('public_hash') + '/rss') !== -1) {
|
|
|
|
req.url = req.url.replace(settingsCache.get('public_hash') + '/', '');
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Redirect to /private if the session does not exist.
|
|
|
|
privateBlogging.authenticatePrivateSession(req, res, function onSessionVerified() {
|
|
|
|
// CASE: RSS is disabled for private blogging e.g. they create overhead
|
|
|
|
if (req.path.lastIndexOf('/rss/', 0) === 0 || req.path.lastIndexOf('/rss/') === req.url.length - 5) {
|
|
|
|
return next(new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.errors.pageNotFound')
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
2015-07-15 19:01:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
authenticatePrivateSession: function authenticatePrivateSession(req, res, next) {
|
|
|
|
var hash = req.session.token || '',
|
|
|
|
salt = req.session.salt || '',
|
2017-10-03 14:40:53 +03:00
|
|
|
isVerified = verifySessionHash(salt, hash),
|
2015-07-15 19:01:23 +03:00
|
|
|
url;
|
|
|
|
|
2017-10-03 14:40:53 +03:00
|
|
|
if (isVerified) {
|
|
|
|
return next();
|
|
|
|
} else {
|
|
|
|
url = utils.url.urlFor({relativeUrl: privateRoute});
|
|
|
|
url += req.url === '/' ? '' : '?r=' + encodeURIComponent(req.url);
|
|
|
|
return res.redirect(url);
|
|
|
|
}
|
2015-07-15 19:01:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// This is here so a call to /private/ after a session is verified will redirect to home;
|
|
|
|
isPrivateSessionAuth: function isPrivateSessionAuth(req, res, next) {
|
|
|
|
if (!res.isPrivateBlog) {
|
2016-09-09 13:23:47 +03:00
|
|
|
return res.redirect(utils.url.urlFor('home', true));
|
2015-07-15 19:01:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var hash = req.session.token || '',
|
2017-10-03 14:40:53 +03:00
|
|
|
salt = req.session.salt || '',
|
|
|
|
isVerified = verifySessionHash(salt, hash);
|
|
|
|
|
|
|
|
if (isVerified) {
|
|
|
|
// redirect to home if user is already authenticated
|
|
|
|
return res.redirect(utils.url.urlFor('home', true));
|
|
|
|
} else {
|
|
|
|
return next();
|
|
|
|
}
|
2015-07-15 19:01:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
authenticateProtection: function authenticateProtection(req, res, next) {
|
|
|
|
// if errors have been generated from the previous call
|
|
|
|
if (res.error) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2017-10-03 14:40:53 +03:00
|
|
|
var bodyPass = req.body.password,
|
|
|
|
pass = settingsCache.get('password'),
|
|
|
|
hasher = crypto.createHash('sha256'),
|
|
|
|
salt = Date.now().toString(),
|
|
|
|
forward = req.query && req.query.r ? req.query.r : '/';
|
|
|
|
|
|
|
|
if (pass === bodyPass) {
|
|
|
|
hasher.update(bodyPass + salt, 'utf8');
|
|
|
|
req.session.token = hasher.digest('hex');
|
|
|
|
req.session.salt = salt;
|
|
|
|
|
|
|
|
return res.redirect(utils.url.urlFor({relativeUrl: decodeURIComponent(forward)}));
|
|
|
|
} else {
|
|
|
|
res.error = {
|
|
|
|
message: i18n.t('errors.middleware.privateblogging.wrongPassword')
|
|
|
|
};
|
|
|
|
return next();
|
|
|
|
}
|
2015-07-15 19:01:23 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-12 19:54:15 +03:00
|
|
|
module.exports = privateBlogging;
|