2018-06-28 11:50:03 +03:00
|
|
|
const fs = require('fs-extra'),
|
2017-10-03 14:40:53 +03:00
|
|
|
session = require('cookie-session'),
|
|
|
|
crypto = require('crypto'),
|
|
|
|
path = require('path'),
|
|
|
|
config = require('../../../config'),
|
2017-12-11 21:14:05 +03:00
|
|
|
urlService = require('../../../services/url'),
|
2017-12-14 16:13:40 +03:00
|
|
|
constants = require('../../../lib/constants'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../../../lib/common'),
|
2017-12-14 05:01:23 +03:00
|
|
|
settingsCache = require('../../../services/settings/cache'),
|
2018-04-17 12:36:05 +03:00
|
|
|
// routeKeywords.private: 'private'
|
2018-06-28 11:50:03 +03:00
|
|
|
privateRoute = '/private/';
|
|
|
|
|
|
|
|
let privateBlogging = null;
|
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
|
|
|
}
|
|
|
|
|
2018-06-28 11:50:03 +03:00
|
|
|
let hasher = crypto.createHash('sha256');
|
2017-10-03 14:40:53 +03:00
|
|
|
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) {
|
2018-06-28 11:50:03 +03:00
|
|
|
let 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({
|
2017-12-14 16:13:40 +03:00
|
|
|
maxAge: constants.ONE_MONTH_MS,
|
2017-10-03 14:40:53 +03:00
|
|
|
signed: false
|
|
|
|
})(req, res, next);
|
2015-07-15 19:01:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
filterPrivateRoutes: function filterPrivateRoutes(req, res, next) {
|
2018-09-15 09:58:53 +03:00
|
|
|
if (!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
|
2018-06-04 19:57:18 +03:00
|
|
|
if (req.path.match(/\/rss(\/?|\/\d+\/?)$/)) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return next(new common.errors.NotFoundError({
|
|
|
|
message: common.i18n.t('errors.errors.pageNotFound')
|
2017-10-05 13:07:32 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
2015-07-15 19:01:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
authenticatePrivateSession: function authenticatePrivateSession(req, res, next) {
|
2018-06-28 11:50:03 +03:00
|
|
|
let hash = req.session.token || '',
|
2015-07-15 19:01:23 +03:00
|
|
|
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 {
|
2017-12-11 21:14:05 +03:00
|
|
|
url = urlService.utils.urlFor({relativeUrl: privateRoute});
|
2017-10-03 14:40:53 +03:00
|
|
|
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) {
|
2017-12-11 21:14:05 +03:00
|
|
|
return res.redirect(urlService.utils.urlFor('home', true));
|
2015-07-15 19:01:23 +03:00
|
|
|
}
|
|
|
|
|
2018-06-28 11:50:03 +03:00
|
|
|
let 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
|
2017-12-11 21:14:05 +03:00
|
|
|
return res.redirect(urlService.utils.urlFor('home', true));
|
2017-10-03 14:40:53 +03:00
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
|
2018-06-28 11:50:03 +03:00
|
|
|
let bodyPass = req.body.password,
|
2017-10-03 14:40:53 +03:00
|
|
|
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;
|
|
|
|
|
2017-12-11 21:14:05 +03:00
|
|
|
return res.redirect(urlService.utils.urlFor({relativeUrl: decodeURIComponent(forward)}));
|
2017-10-03 14:40:53 +03:00
|
|
|
} else {
|
|
|
|
res.error = {
|
2017-12-12 00:47:46 +03:00
|
|
|
message: common.i18n.t('errors.middleware.privateblogging.wrongPassword')
|
2017-10-03 14:40:53 +03:00
|
|
|
};
|
|
|
|
return next();
|
|
|
|
}
|
2015-07-15 19:01:23 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-12 19:54:15 +03:00
|
|
|
module.exports = privateBlogging;
|