-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmiddleware.js
More file actions
296 lines (258 loc) · 8.69 KB
/
middleware.js
File metadata and controls
296 lines (258 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
var path = require('path');
var async = require('async');
var crypto = require('crypto');
var debug = function() {};
if (process.env.NODE_DEBUG && /middleware/.test(process.env.NODE_DEBUG)) {
var $ = require('./colors').$;
debug = function(x) {
$.puts($('HATCH MIDDLEWARE: ').grey + x);
};
}
exports.rewrite = function(compound) {
return function rewriteMiddleware(req, res, next) {
debug('start rewriteMiddleware');
var url = req.url.split('?')[0];
var urlParts = url.split('/do/');
if (urlParts.length > 1) {
req.pagePath = urlParts[0].replace(/\/$/, '');
req.moduleName = urlParts[1].split('/')[0];
loadGroup(compound, req, function (err) {
res.locals.group = req.group;
req.url = '/do/' + urlParts[1];
end(err);
});
} else {
req.pagePath = url.replace(/\/$/, '');
end();
}
function end(err) {
debug('end rewriteMiddleware');
next(err);
}
}
};
exports.hatch = function(compound) {
var Group, User;
var hatch = compound.hatch;
return function hatchMiddleware(req, res, next) {
User = compound.models.User;
Group = compound.models.Group;
// load user, page and group in parallel to take advantage of MULTI
async.parallel([
function (done) {
authenticate(compound, req, done);
},
function (done) {
loadPage(compound, req, done);
},
function (done) {
loadGroup(compound, req, done);
}
], function (err) {
if (err) {
return next(err);
}
req.member = req.group && req.user && req.user.memberships &&
req.user.memberships.find(req.group.id, 'groupId');
if (req.user && req.group) {
req.user.hasPermission(req.group, 'edit', function (err, result) {
req.user.canEdit = result;
next();
});
} else {
next();
}
});
}
};
function authenticate(compound, req, next) {
if (req.user) {
return next();
}
var AccessToken = compound.models.AccessToken;
var User = compound.models.User;
// authenticate via access token
var token = req.headers.token || req.body.token || req.query.token;
// make sure to exclude invalid (null) tokens from iOS
if (token && token !== '(null)') {
AccessToken.loadUser(token, function(err, user) {
if (err) {
return next(err);
}
req.user = user;
next();
});
} else if (req.session && req.session.userId) {
User.find(req.session.userId, function(err, user) {
if (err) {
return next(err);
}
req.user = user;
next();
});
} else {
next();
}
}
function loadPage(compound, req, next) {
debug('loading page');
var url = req.headers.host + req.pagePath;
compound.models.Page.findOne({ where: { url: url }}, function (err, page) {
req.page = page;
debug('loading page done, ' +
(err ? 'with err ' + err + ' ' : '') +
(page ? 'found' : 'not found'));
next();
});
}
function loadGroup(compound, req, next) {
debug('loading group');
if (req.group) {
debug('already got group');
return next();
}
var Group = compound.models.Group;
var Content = compound.models.Content;
var groupId = req.query.groupId;
if (groupId) {
debug('loading group by id');
Group.find(groupId, gotGroup);
} else {
var url = path.join(req.headers.host || '', req.path || '');
url = url.split('/do/')[0].split('?')[0];
url = url.replace(/\/$/, "");
debug('loading group by url: ' + url);
Group.findByUrl(url, function (err, group, post) {
if (group) {
debug('found group: ' + group.id);
return gotGroup(err, group);
} else if (post) {
req.post = post;
debug('found content: ' + post.id);
Group.find(post.groupId, function (err, group) {
debug('found group: ' + group.id);
// adjust the request url to match the content type
req.url = '/' + path.join(group.homepage.url.replace(req.headers.host, ''), post.type);
return gotGroup(err, group);
});
} else {
Group.all({limit: 1}, function (err, groups) {
// if there are no groups, allow fall through to the setup middleware
if (groups.length === 0) {
return next();
} else {
var error = new compound.hatch.errors.NotFound(req, 'Group ('+url+') not found');
error.showStack = false;
return next(error);
}
});
}
});
}
function gotGroup(err, group) {
if(err) {
return next(err);
}
req.group = group;
return next();
}
}
exports.errorHandler = function(compound) {
return errorHandler;
function errorHandler(err, req, res, next) {
debug('start errorHandler');
var code = err.code || 500;
if(err.showStack) {
compound.log(err.stack);
} else {
// compound.log(err.stack);
}
res.status(code);
if (compound.app.get('env') === 'production') {
compound.hatch.errorReporter.notify(err, console.log);
}
if (req.params && req.params.format === 'json') {
res.send({error: err});
return;
}
var found = req.group && req.group.pagesCache.find(code.toString(), 'type');
if (found) {
compound.models.Page.find(found.id, function (e, p) {
req.page = p;
compound.controllerBridge.callControllerAction(
'page',
'render', req, res, next
);
});
} else {
var view = compound.structure.views['common/errors/' + code];
if (!view) {
view = compound.structure.views['common/errors/500'];
}
if (compound.app.get('show errors')) {
res.locals.err = err;
} else {
res.locals.err = null;
}
res.render(view);
}
}
};
exports.timeLogger = function(compound) {
return timeLogger;
function timeLogger(req, res, next) {
req.startedAt = Date.now();
next();
}
};
exports.csrf = function(compound) {
return csrfProtection;
function csrfProtection(req, res, next) {
if (!compound.app.get('csrf enabled')) return next();
debug('start csrfProtection');
if (!req.session) {
return next();
}
if (!req.session.csrfToken) {
req.session.csrfToken = Math.random();
req.csrfParam = 'authenticity_token';
req.csrfToken = sign(req.session.csrfToken);
return next();
}
// publish secure credentials
req.csrfParam = 'authenticity_token';
req.csrfToken = sign(req.session.csrfToken);
if (['HEAD', 'GET', 'OPTIONS'].indexOf(req.originalMethod) === -1) {
var token = req.param(req.csrfParam) || req.header('X-CSRF-Token');
if (!token || token !== req.csrfToken) {
compound.log('Incorrect authenticity token');
debug('end csrfProtection: failed');
res.send(403);
} else {
debug('end csrfProtection: passed');
next();
}
} else {
debug('end csrfProtection: not required');
next();
}
}
function sign(n) {
var res = crypto.createHash('sha1').update(n.toString()).update(compound.app.get('csrf secret').toString()).digest('hex');
return res;
}
};
exports.pjax = function (compound) {
return function (req, res, next) {
var send = res.send;
res.send = function (data) {
if (req.query._pjax) {
data = data.substring(data.indexOf('pjax-body'));
data = data.substring(data.indexOf('>') +1);
data = data.substring(0, data.indexOf('<!-- END PJAX-BODY -->'));
}
send.apply(this, [data]);
};
next();
};
};