forked from mozilla/CSOL-site
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.js
112 lines (93 loc) · 2.71 KB
/
middleware.js
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
var errors = require('./lib/errors');
var express = require('express');
var _ = require('underscore');
var clientSessions = require('client-sessions');
var COOKIE_KEY = 'session';
if ('COOKIE_SECRET' in process.env) {
var COOKIE_SECRET = process.env.COOKIE_SECRET;
} else {
// TODO - switch this behaviour to be more like that in the backpack,
// where a token is generated automatically and stored for future use
// See: https://github.com/mozilla/openbadges/blob/development/middleware.js#L14
throw new Error('COOKIE_SECRET not set in environment');
}
exports.session = function session () {
return clientSessions({
cookieName: COOKIE_KEY,
secret: COOKIE_SECRET,
maxAge: (7 * 24 * 60 * 60 * 1000), //one week
cookie: {
httpOnly: true
}
});
};
function isUserType (type) {
return function (req, res, next) {
exports.loggedIn(req, res, function() {
if (req.session.user.type !== type)
return res.redirect(req.session.user.home);
next();
});
}
}
exports.isLearner = isUserType('learner');
exports.isGuardian = isUserType('guardian');
exports.isUnderageLearner = function (req, res, next) {
exports.isLearner(req, res, function() {
if (req.session.user.underage)
return next();
return next('route');
});
}
exports.loggedIn = function loggedIn(req, res, next) {
var user = req.session.user;
if (!user) {
req.session.afterLogin = req.originalUrl;
return res.redirect('/login');
}
return next();
};
exports.csrf = function (options) {
options = options || {};
var value = options.value || defaultCsrfValue;
var list = options.whitelist;
return function (req, res, next) {
if (whitelisted(list, req.url))
return next();
var token = req.session._csrf || (req.session._csrf = uid(24));
if ('GET' === req.method || 'HEAD' === req.method)
return next();
var val = value(req);
if (val != token) {
// logger.debug("CSRF token failure");
return next(errors.Forbidden());
}
next();
};
};
function defaultCsrfValue (req) {
return (req.body && req.body._csrf)
|| (req.query && req.query._csrf)
|| (req.headers['x-csrf-token']);
}
function whitelisted (list, input) {
var pattern;
for (var i = list.length; i--;) {
pattern = list[i];
if (RegExp('^' + list[i] + '$').test(input))
return true;
}
return false;
}
function uid (len) {
var buf = [];
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charlen = chars.length;
for (var i = 0; i < len; ++i) {
buf.push(chars[getRandomInt(0, charlen - 1)]);
}
return buf.join('');
};
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}