-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
app.js
246 lines (203 loc) · 6.11 KB
/
app.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
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
'use strict';
const config = require('wild-config');
const log = require('npmlog');
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const flash = require('connect-flash');
const passport = require('./lib/passport');
const httpSso = require('./lib/http-sso');
const db = require('./lib/db');
const multer = require('multer');
const routesIndex = require('./routes/index');
const routesAccount = require('./routes/account');
const routesWebmail = require('./routes/webmail');
const routesApi = require('./routes/api');
const uploader = multer({ storage: multer.memoryStorage() });
const app = express();
// setup extra hbs tags
require('./lib/hbs-helpers');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// Handle proxies. Needed to resolve client IP
if (config.www.proxy) {
app.set('trust proxy', config.www.proxy);
}
// Do not expose software used
app.disable('x-powered-by');
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(
logger(config.www.log, {
stream: {
write: message => {
message = (message || '').toString();
if (message) {
log.info('HTTP', message.replace('\n', '').trim());
}
}
}
})
);
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(
session({
name: 'webmail',
store: new RedisStore({
client: db.redis.duplicate()
}),
secret: config.www.secret,
saveUninitialized: false,
resave: false,
cookie: {
secure: !!config.www.secure
}
})
);
app.use(flash());
app.use(
bodyParser.urlencoded({
extended: true,
limit: config.www.postsize
})
);
app.use(
bodyParser.text({
limit: config.www.postsize
})
);
app.use(
bodyParser.json({
limit: config.www.postsize
})
);
if (config.service.sso.http.enabled) {
httpSso.setup(app);
} else {
// default, session based auth
passport.setup(app);
}
app.use((req, res, next) => {
// make sure flash messages are available
res.locals.flash = req.flash.bind(req);
// userdata
res.locals.user = req.user;
// recaptcha
if (config.recaptcha.enabled) {
res.locals.recaptcha = config.recaptcha.siteKey;
}
// values needed to show unseen messages counter
res.locals.inboxId = req.user ? req.user.inbox.id : false;
res.locals.inboxUnseen = req.user ? req.user.inbox.unseen : false;
res.locals.allowJoin = config.service.allowJoin;
res.locals.u2fEnabled = config.u2f.enabled;
res.locals.serviceName = config.name;
res.locals.serviceDomain = config.service.domain;
res.locals.generalNotification = config.service.generalNotification;
next();
});
// force 2fa prompt if user is logged in and 2fa is enabled
app.use((req, res, next) => {
if (
req.user &&
req.session.require2fa &&
!['/account/logout', '/account/start-u2f', '/account/check-u2f', '/account/check-totp'].includes(req.url.split('?').shift())
) {
return passport.csrf(req, res, err => {
if (err) {
return next(err);
}
return res.render('account/2fa', {
layout: 'layout-popup',
title: 'Two factor authentication',
csrfToken: req.csrfToken(),
enabled2fa: req.session.require2fa,
enabledTotp: req.session.require2fa ? req.session.require2fa.includes('totp') : false,
enabledU2f: req.session.require2fa && req.query.u2f !== 'false' ? req.session.require2fa.includes('u2f') : false,
disableU2f: req.url + (req.url.indexOf('?') >= 0 ? '&' : '?') + 'u2f=false'
});
});
}
next();
});
// force password change prompt if user password is reset
app.use((req, res, next) => {
if (req.user && req.session.requirePasswordChange && !['/account/logout', '/account/update-password'].includes(req.url.split('?').shift())) {
return passport.csrf(req, res, err => {
if (err) {
return next(err);
}
return res.render('account/update-password', {
layout: 'layout-popup',
title: 'Change password',
csrfToken: req.csrfToken()
});
});
}
next();
});
// setup main routes
app.use('/account', passport.csrf, routesAccount);
app.use(
'/webmail',
(req, res, next) => {
if (req.url === '/send' && req.method === 'POST') {
return uploader.array('attachment')(req, res, next);
}
next();
},
passport.csrf,
passport.checkLogin,
routesWebmail
);
app.use('/api', passport.csrf, passport.checkLogin, routesApi);
app.use('/', passport.csrf, routesIndex);
// catch 404 and forward to error handler
app.use((req, res, next) => {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
app.use((err, req, res, next) => {
if (!err) {
return next();
}
if (!err.resetSession) {
return next(err);
}
req.session.regenerate(() => {
return next(err);
});
});
app.use((err, req, res, next) => {
if (!err) {
return next();
}
let message;
switch (err.restCode) {
case 'InvalidToken':
return res.redirect('/account/login');
case 'AuthFailed':
message = 'Authentication failed';
break;
default:
message = err.message;
break;
}
if (err.code === 'InvalidToken') {
return res.redirect('/account/login');
}
res.status(err.status || 500);
res.render('error', {
message,
error: app.get('env') === 'development' ? err : {}
});
});
module.exports = app;