-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
533 lines (502 loc) · 16.8 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
var express = require('express');
var path = require('path');
var request = require('request');
var shortid = require('shortid');
var logger = require('morgan');
var compression = require('compression');
var session = require('express-session');
var CouchConnect = require('connect-couchdb')(session);
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var auth = require('passport-local-authenticate');
var flash = require('connect-flash');
var nodemailer = require("nodemailer");
var helmet = require('helmet');
var expressSanitized = require('express-sanitize-escape');
var config;
if (process.env.NODE_ENV === 'development') {
config = require('./config/dev.secret.json');
} else config = require('./config/secret.json');
// Wiki editions @github.com
var EMPTY_URL = require('./config/editions').emptyUrl;
// require routes
var index = require('./routes/index');
var signup = require('./routes/signup');
var login = require('./routes/login');
var account = require('./routes/account');
var recovery = require('./routes/recovery');
var about = require('./routes/about');
var changeFeed = require('./routes/changefeed');
var offline = require('./routes/offline');
var explore = require('./routes/explore');
// prepare database drive
var nano = require('nano')(config.database);
var db = nano.db.use('maarfapad');
// For logging errors
function logError (err) {
console.error('Error', err);
};
var app = express();
app.use(helmet());
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use('/edition_index.json', express.static(__dirname + '/public/edition_index.json',{setHeaders: function (res, path, stat) {
res.set('Content-Type', 'application/json');
}}));
app.use('/manifest.json', express.static(__dirname + '/public/manifest.json',{setHeaders: function (res, path, stat) {
res.set('Content-Type', 'application/json');
}}));
app.use('/wiki/favicon.ico', express.static(__dirname + '/public' + '/favicon.ico'));
app.use('/favicon.ico', express.static(__dirname + '/public' + '/favicon.ico'));
app.use('/sw.js', express.static(__dirname + '/public' + '/javascripts/sw.js',{setHeaders: function (res, path, stat) {
res.set('Content-Type', 'application/javascript');
}}));
app.use('/images', express.static(__dirname + '/public' + '/images'));
app.use('/javascript', express.static(__dirname + '/public' + '/javascripts'));
app.use('/stylesheets', express.static(__dirname + '/public' + '/stylesheets'));
if (process.env.NODE_ENV === 'development') {
app.use(logger('dev'));
} else {
app.use(logger('combined',{
skip: function (req,res) { return res.statusCode < 400 }
}));
}
app.use(compression());
app.use(bodyParser.raw({ type: 'text/html', limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.use(cookieParser());
app.use(expressSanitized.middleware());
expressSanitized.sanitizeParams(app, ['name', 'rev', 'wikiType', 'wikiName']);
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
store: new CouchConnect({
name: config.dbname,
username: config.username,
password: config.pass,
host: config.host
}),
name: 'm|pad',
secret: config.secret,
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 4.32e+8
}
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
function paramCheck (param = '') {
// remove url chars that could mess with the viewing of the named wiki
var regex = new RegExp(/[/.&?=]/g);
return param.replace(regex,'');
};
// configure local strategy for passport authentication
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
function (req, username, password, done) {
db.view('user', 'verify', {
'key': username
}, function (err, body) {
if (err) {
return done(err, null);
} else {
if (body.rows.length === 0) {
return done(null, false, req.flash('login-info', 'No account is associated with provided email'));
}
body.rows.forEach(function (user) {
auth.verify(password, user.value, function (err, verified) {
if (err) {
logError(err);
return done(err,null);
}
if (verified === false) {
return done(null, false, req.flash('login-info', 'Incorrect password'));
}
if (verified === true) {
return done(null, user);
}
});
});
}
});
}
));
// serialise to session
passport.serializeUser(function (user, done) {
done(null, user.key);
});
// deserialise user
passport.deserializeUser(function (id, done) {
db.view('user', 'verify', { 'key': id }, function (err, body) {
if (err) {
logError(err);
done(err,null);
return;
}
body.rows.forEach(function (user) {
done(err, user);
});
});
});
/**
* Node mailer settings
*
*/
let smtpTransport = nodemailer.createTransport({
host: "mail.gandi.net", // hostname
secureConnection: true, // use SSL
port: 465, // port for secure SMTP
auth: {
user: '[email protected]',
pass: config.mailerPass
}
});
/**
* Routes for the express app go here
*/
app.use('/', index);
app.use('/signup', signup);
app.use('/login', login);
app.use('/account', account);
app.use('/recovery', recovery);
app.use('/about', about);
app.use('/offline', offline);
app.use('/changefeed',changeFeed);
app.use('/explore', explore);
// create a user account then redirect to index page
app.post('/create_user', function (req, res, next) {
nano.uuids().then(function (result) {
var id = result.uuids[0];
// check for account duplicate
db.view('user', 'verify', {
'key': req.body.email
}, function (err, body) {
if (!err) {
if (body.rows.length > 0) {
req.flash('signup-info', 'An account with that email already exists.');
res.redirect('/signup');
} else {
const mailOptions = {
from: 'Maarfapad project <[email protected]>',
to: req.body.email,
subject: 'Maarfapad sign up',
html: `<p>You are all set!</p><p>Get the latest updates via <a href='https://abesamma.github.io/maarfapad-blog/index.html'>Maarfapad's blog.</a></p> <a href='http://maarfapad.com'>Click here</a> to login</p>`
};
// hash and salt pass
auth.hash(req.body.password, function (err, hashed) {
req.body.password = hashed; // replace plain pass with hashed pass
request(EMPTY_URL, function (error, resp, data) {
if (resp.statusMessage === 'OK') {
if (!error) {
db.multipart.insert(
req.body,
[{ name: 'home', data: data, content_type: 'text/html' }],
id,
function (err, body) {
if (!err) {
// email to confirm successful action
smtpTransport.sendMail(mailOptions, function (err, response) {
if (!err) {
// redirect to login page if successful
req.flash('login-info', 'Account created successfuly! Please login.');
res.redirect('/login');
} else {
logError(err);
req.flash('signup-info', 'Something went wrong with the email address you supplied. Please try signing up again with another email address.');
res.redirect('/signup');
}
});
}
});
} else {
logError(error);
req.flash('signup-info', 'An error occured. Please try again later');
res.redirect('/signup');
}
} else {
logError('Failed to retrieve template Wiki from CDN');
req.flash('signup-info', 'Failed to create account. Try again later');
res.redirect('/signup');
}
});
});
}
}
});
}).catch(err => {
logError(err);
req.flash('signup-info', 'An error occured. Please try again later');
res.redirect('/signup');
})
});
// change email
app.post('/change_email', function (req, res) {
if (req.isAuthenticated()) {
var id = req.user.id
var data = req.body.newemail;
db.view('user', 'verify', { 'key': data }, function (err, body) {
if (err) {
logError(err);
return res.send(`<p id='flash'>Something went wrong. Please try again later</p>`);
}
if (body.rows.length > 0) {
return res.send(`<p id='flash'>That email is already in use by another account</p><br><a href='/account'>Go back</a>`);
} else {
db.atomic('user', 'updateEmail', id, { value: data }, function (err, body) {
if (!err) {
req.session.destroy(function (err) {
return res.redirect('/login');
});
} else {
logError(err);
return res.send(`<p id='flash'>Something went wrong. Please try again later</p>`);
}
});
}
});
} else res.sendStatus(401);
});
// change password
app.post('/change_password', function (req, res) {
if (req.isAuthenticated()) {
var id = req.user.id
var verify = req.body.verifypass
var _new = req.body.newpass
if (_new === verify) {
auth.hash(_new, function (err, hashed) {
db.atomic('user', 'updatePass', id, { value: hashed }, function (err, body) {
if (!err) {
res.send(`<p>Password change successful</p><br><a href='/'>Go back home</a>`);
} else {
logError(err);
res.send(`<p id='flash'>Something went wrong. Please try again later</p><br><a href='/'>Go back home</a>`);
}
});
});
} else {
res.send(`<p id='flash'>Passwords do not match</p><br><a href='/account'>Try again</a>`);
}
} else res.sendStatus(401);
});
// reset password
app.post('/reset', function (req, res) {
var resetPass = shortid.generate();
db.view('user', 'verify', {
'key': req.body.email
}, function (err, body) {
if (!err) {
if (body.rows.length === 1) {
var userID = body.rows[0].id;
// prepare email to send
var mail = {
from: '[email protected]',
to: req.body.email,
subject: 'Maarfapad password reset',
html: `<p>Here's your reset password: ${resetPass}</p><p><a href='http://maarfapad.com'>Click here</a> to login</p>`
};
auth.hash(resetPass, function (err, hashed) {
db.atomic('user', 'updatePass', userID, { value: hashed }, function (err, body) {
if (!err) {
// send recovery email
smtpTransport.sendMail(mail, function (err, response) {
if (!err) {
req.flash('recovery-info','A recovery email has been sent. Check your Inbox or Spam folder');
res.redirect('/recovery');
} else {
logError(err);
req.flash('recovery-info','Something went wrong. Email not sent. Please try again later');
res.redirect('/recovery');
}
});
} else {
logError(err);
req.flash('recovery-info','Something went wrong. Please try again later');
res.redirect('/recovery');
}
});
});
} else {
req.flash('recovery-info','Sorry, there is no account associated with that email address');
res.redirect('/recovery');
}
}
});
});
// user login to home.html
app.post('/login', passport.authenticate('local', {
failureRedirect: '/login',
failureFlash: true
}), function (req, res) {
if (req.cookies['mpad-offline'] === 'true') {
return res.clearCookie('mpad-offline', { path: '/' }).redirect('/wiki/home');
}
return res.redirect('/wiki/home');
});
// account deletion
app.get('/delete_account', function (req, res) {
if (req.isAuthenticated()) {
db.show('user', 'getUser', req.user.id, function (err, body) {
if (!err) {
var docRev = body._rev;
db.destroy(req.user.id, docRev, function (err, body) {
if (!err) {
req.session.destroy(function (err) {
res.redirect('/login');
});
} else {
logError(err);
res.send(500, 'Server error. Please try again later.');
}
});
} else {
logError(err);
res.send(500, 'Server error. Please try again later.');
}
});
} else res.sendStatus(401);
});
/*
*Below are the api routes for maarfapad a la tiddlywiki to use
*/
app.get('/wiki/:name', function (req, res) {
var name = req.params.name
if (req.cookies['mpad-offline'] === 'true') return res.redirect('/login');
if (req.isAuthenticated()) {
var userid = req.user.id
db.attachment.get(userid, name, function (err, body) {
if (!err) {
res.header('Content-Type', 'text/html');
res.send(body);
} else {
logError(err);
res.redirect('/offline');
}
});
} else {
res.redirect('/');
}
});
app.put('/wiki/:name/:rev', function (req, res) {
var name = req.params.name;
var revision = req.params.rev;
if (req.isAuthenticated()) {
var userid = req.user.id
db.attachment.insert(userid, name, req.body, 'text/html', { rev: revision }, function (err, body) {
if (!err) {
res.sendStatus(200);
} else {
logError(err);
res.sendStatus(500);
}
});
} else res.sendStatus(401);
});
app.get('/:wikiType/:wikiName/:rev', function (req, res) {
var type = paramCheck(req.params.wikiType);
var name = paramCheck(req.params.wikiName);
var revision = req.params.rev;
var userid = req.user.id;
function createWiki (id,name,rev,url) {
db.show('user', 'getUser', id, function (err, body) {
if (err) {
logError(err);
return res.sendStatus(500);
}
var wikiCount = Object.keys(body._attachments).length;
if (wikiCount === 2) {
// Free accounts are restricted to 2 wikis only.
// Tiers will be added later
return res.sendStatus(204); // processed but ignored
} else {
request(url, function (error, response, data) {
if (error) {
logError(error);
return res.sendStatus(500);
}
db.attachment.insert(id, name, data, 'text/html', { rev: rev }, function (err, body) {
if (err) {
logError(err);
return res.sendStatus(500);
}
return res.sendStatus(200);
});
});
}
});
};
if (req.isAuthenticated()) {
switch (type) {
case 'empty':
createWiki(userid,name,revision,EMPTY_URL);
break;
default: res.sendStatus(404);
}
} else res.sendStatus(401);
});
// delete route
app.delete('/wiki/:name/:rev', function (req, res) {
var name = req.params.name;
var revision = req.params.rev;
if (req.isAuthenticated()) {
var userid = req.user.id;
db.attachment.destroy(userid, name, { rev: revision }, function (err, body) {
if (!err) {
nano.db.compact('maarfapad',function (err, body) {
if (err) logError('Error on compaction: '+ err);
});
res.sendStatus(200);
} else {
logError(err);
res.sendStatus(500);
}
});
} else res.sendStatus(401);
});
// send a doc snapshot for wikiManager
app.get('/user', function (req, res) {
if (req.isAuthenticated()) {
db.show('user', 'getUser', req.user.id, function (err, body) {
if (!err) {
res.send(body);
} else {
logError(err);
res.sendStatus(500);
}
});
} else res.sendStatus(401);
});
// handle options
app.options('/*', function (req, res) {
res.header('Access-Control-Allow-Origin', config.appURL);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, If-Match');
res.header('Service-Worker-Allowed', '/wiki/');
res.header('Cache-Control', 'private');
res.sendStatus(200);
});
/**
* Error handlers
*/
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;