2013-11-26 00:31:18 +04:00
|
|
|
// # Custom Middleware
|
|
|
|
// The following custom middleware functions are all unit testable, and have accompanying unit tests in
|
|
|
|
// middleware_spec.js
|
2013-10-05 14:17:08 +04:00
|
|
|
|
2014-02-05 12:40:30 +04:00
|
|
|
var _ = require('lodash'),
|
2014-04-12 07:46:15 +04:00
|
|
|
csrf = require('csurf'),
|
2013-11-05 09:41:36 +04:00
|
|
|
express = require('express'),
|
2013-12-04 06:47:39 +04:00
|
|
|
busboy = require('./ghost-busboy'),
|
2013-11-20 17:58:52 +04:00
|
|
|
config = require('../config'),
|
2013-11-05 09:41:36 +04:00
|
|
|
path = require('path'),
|
2013-12-06 12:51:35 +04:00
|
|
|
api = require('../api'),
|
2013-12-31 03:13:25 +04:00
|
|
|
|
|
|
|
expressServer,
|
2014-05-22 07:56:25 +04:00
|
|
|
ONE_HOUR_MS = 60 * 60 * 1000,
|
|
|
|
ONE_YEAR_MS = 365 * 24 * ONE_HOUR_MS;
|
2013-10-05 14:17:08 +04:00
|
|
|
|
|
|
|
function isBlackListedFileType(file) {
|
2013-10-25 02:55:51 +04:00
|
|
|
var blackListedFileTypes = ['.hbs', '.md', '.json'],
|
2013-10-05 14:17:08 +04:00
|
|
|
ext = path.extname(file);
|
|
|
|
return _.contains(blackListedFileTypes, ext);
|
|
|
|
}
|
|
|
|
|
2013-12-06 18:13:15 +04:00
|
|
|
function cacheServer(server) {
|
|
|
|
expressServer = server;
|
|
|
|
}
|
|
|
|
|
2013-10-05 14:17:08 +04:00
|
|
|
var middleware = {
|
|
|
|
|
2014-02-14 14:00:11 +04:00
|
|
|
// ### Authenticate Middleware
|
2014-02-25 14:20:32 +04:00
|
|
|
// authentication has to be done for /ghost/* routes with
|
2014-02-14 14:00:11 +04:00
|
|
|
// exceptions for signin, signout, signup, forgotten, reset only
|
|
|
|
// api and frontend use different authentication mechanisms atm
|
|
|
|
authenticate: function (req, res, next) {
|
2014-02-25 14:20:32 +04:00
|
|
|
var noAuthNeeded = [
|
2014-02-14 14:00:11 +04:00
|
|
|
'/ghost/signin/', '/ghost/signout/', '/ghost/signup/',
|
2014-05-09 09:00:10 +04:00
|
|
|
'/ghost/forgotten/', '/ghost/reset/', '/ghost/ember/',
|
|
|
|
'/ghost/ember/signin'
|
2014-02-25 14:20:32 +04:00
|
|
|
],
|
|
|
|
subPath;
|
|
|
|
|
|
|
|
// SubPath is the url path starting after any default subdirectories
|
|
|
|
// it is stripped of anything after the two levels `/ghost/.*?/` as the reset link has an argument
|
|
|
|
subPath = req.path.substring(config().paths.subdir.length);
|
|
|
|
/*jslint regexp:true, unparam:true*/
|
|
|
|
subPath = subPath.replace(/^(\/.*?\/.*?\/)(.*)?/, function (match, a) {
|
|
|
|
return a;
|
|
|
|
});
|
|
|
|
|
2014-02-20 02:53:40 +04:00
|
|
|
if (res.isAdmin) {
|
|
|
|
if (subPath.indexOf('/ghost/api/') === 0) {
|
|
|
|
return middleware.authAPI(req, res, next);
|
|
|
|
}
|
2014-02-14 14:00:11 +04:00
|
|
|
|
2014-02-20 02:53:40 +04:00
|
|
|
if (noAuthNeeded.indexOf(subPath) < 0) {
|
2014-02-14 14:00:11 +04:00
|
|
|
return middleware.auth(req, res, next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
|
2013-11-05 09:41:36 +04:00
|
|
|
// ### Auth Middleware
|
|
|
|
// Authenticate a request by redirecting to login if not logged in.
|
|
|
|
// We strip /ghost/ out of the redirect parameter for neatness
|
|
|
|
auth: function (req, res, next) {
|
|
|
|
if (!req.session.user) {
|
2014-02-20 02:53:40 +04:00
|
|
|
var subPath = req.path.substring(config().paths.subdir.length),
|
|
|
|
reqPath = subPath.replace(/^\/ghost\/?/gi, ''),
|
2013-11-05 09:41:36 +04:00
|
|
|
redirect = '',
|
|
|
|
msg;
|
|
|
|
|
2013-12-06 12:51:35 +04:00
|
|
|
return api.notifications.browse().then(function (notifications) {
|
|
|
|
if (reqPath !== '') {
|
|
|
|
msg = {
|
|
|
|
type: 'error',
|
|
|
|
message: 'Please Sign In',
|
2014-04-29 00:58:18 +04:00
|
|
|
status: 'passive'
|
2013-12-06 12:51:35 +04:00
|
|
|
};
|
|
|
|
// let's only add the notification once
|
|
|
|
if (!_.contains(_.pluck(notifications, 'id'), 'failedauth')) {
|
2014-06-16 09:24:19 +04:00
|
|
|
api.notifications.add({ notifications: [msg] });
|
2013-12-06 12:51:35 +04:00
|
|
|
}
|
|
|
|
redirect = '?r=' + encodeURIComponent(reqPath);
|
2013-11-05 09:41:36 +04:00
|
|
|
}
|
2014-05-15 05:24:49 +04:00
|
|
|
|
|
|
|
if (subPath.indexOf('/ember') > -1) {
|
|
|
|
return res.redirect(config().paths.subdir + '/ghost/ember/signin');
|
|
|
|
}
|
|
|
|
|
2014-01-05 10:40:53 +04:00
|
|
|
return res.redirect(config().paths.subdir + '/ghost/signin/' + redirect);
|
2013-12-06 12:51:35 +04:00
|
|
|
});
|
2013-11-05 09:41:36 +04:00
|
|
|
}
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
|
|
|
|
// ## AuthApi Middleware
|
|
|
|
// Authenticate a request to the API by responding with a 401 and json error details
|
|
|
|
authAPI: function (req, res, next) {
|
|
|
|
if (!req.session.user) {
|
|
|
|
res.json(401, { error: 'Please sign in' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Check if we're logged in, and if so, redirect people back to dashboard
|
|
|
|
// Login and signup forms in particular
|
|
|
|
redirectToDashboard: function (req, res, next) {
|
2014-04-28 03:28:50 +04:00
|
|
|
if (req.session && req.session.user) {
|
2014-01-05 10:40:53 +04:00
|
|
|
return res.redirect(config().paths.subdir + '/ghost/');
|
2013-11-05 09:41:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
|
|
|
|
// While we're here, let's clean up on aisle 5
|
|
|
|
// That being ghost.notifications, and let's remove the passives from there
|
|
|
|
// plus the local messages, as they have already been added at this point
|
|
|
|
// otherwise they'd appear one too many times
|
2014-04-29 00:58:18 +04:00
|
|
|
// ToDo: Remove once ember handles passive notifications.
|
2013-11-05 09:41:36 +04:00
|
|
|
cleanNotifications: function (req, res, next) {
|
|
|
|
/*jslint unparam:true*/
|
2013-12-06 12:51:35 +04:00
|
|
|
api.notifications.browse().then(function (notifications) {
|
2014-04-29 00:58:18 +04:00
|
|
|
_.each(notifications.notifications, function (notification) {
|
2013-12-06 12:51:35 +04:00
|
|
|
if (notification.status === 'passive') {
|
|
|
|
api.notifications.destroy(notification);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
next();
|
2013-11-05 09:41:36 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-12-31 03:13:25 +04:00
|
|
|
// ### CacheControl Middleware
|
|
|
|
// provide sensible cache control headers
|
|
|
|
cacheControl: function (options) {
|
2013-11-05 09:41:36 +04:00
|
|
|
/*jslint unparam:true*/
|
2013-12-31 03:13:25 +04:00
|
|
|
var profiles = {
|
|
|
|
'public': 'public, max-age=0',
|
|
|
|
'private': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
|
|
|
|
},
|
|
|
|
output;
|
|
|
|
|
|
|
|
if (_.isString(options) && profiles.hasOwnProperty(options)) {
|
|
|
|
output = profiles[options];
|
|
|
|
}
|
2013-11-05 09:41:36 +04:00
|
|
|
|
2013-12-31 03:13:25 +04:00
|
|
|
return function cacheControlHeaders(req, res, next) {
|
|
|
|
if (output) {
|
|
|
|
res.set({'Cache-Control': output});
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
};
|
2013-11-05 09:41:36 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// ### whenEnabled Middleware
|
|
|
|
// Selectively use middleware
|
|
|
|
// From https://github.com/senchalabs/connect/issues/676#issuecomment-9569658
|
|
|
|
whenEnabled: function (setting, fn) {
|
|
|
|
return function settingEnabled(req, res, next) {
|
2013-12-06 18:13:15 +04:00
|
|
|
// Set from server/middleware/index.js for now
|
|
|
|
if (expressServer.enabled(setting)) {
|
2013-11-05 09:41:36 +04:00
|
|
|
fn(req, res, next);
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2013-11-20 17:58:52 +04:00
|
|
|
staticTheme: function () {
|
2013-10-05 14:17:08 +04:00
|
|
|
return function blackListStatic(req, res, next) {
|
|
|
|
if (isBlackListedFileType(req.url)) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2013-11-20 17:58:52 +04:00
|
|
|
return middleware.forwardToExpressStatic(req, res, next);
|
2013-10-05 14:17:08 +04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
// to allow unit testing
|
2013-11-20 17:58:52 +04:00
|
|
|
forwardToExpressStatic: function (req, res, next) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
api.settings.read({context: {internal: true}, key: 'activeTheme'}).then(function (response) {
|
2014-04-28 03:28:50 +04:00
|
|
|
var activeTheme = response.settings[0];
|
2014-05-22 07:56:25 +04:00
|
|
|
|
|
|
|
express['static'](path.join(config().paths.themePath, activeTheme.value), {maxAge: ONE_YEAR_MS})(req, res, next);
|
2013-12-06 12:51:35 +04:00
|
|
|
});
|
2013-11-26 13:38:54 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
conditionalCSRF: function (req, res, next) {
|
|
|
|
// CSRF is needed for admin only
|
|
|
|
if (res.isAdmin) {
|
2014-04-12 07:46:15 +04:00
|
|
|
csrf()(req, res, next);
|
2013-11-26 13:38:54 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
next();
|
2013-12-04 06:47:39 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
busboy: busboy
|
2013-10-05 14:17:08 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = middleware;
|
2014-05-22 07:56:25 +04:00
|
|
|
module.exports.cacheServer = cacheServer;
|