forked from lance13c/DB-330-FRD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
704 lines (617 loc) · 20.3 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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*
Start Web Server Here and take in command line arguments, sets up the database
*/
//Notes: Make sure the config file has the correct data or a Knex error will
// be displayed.
"use strict"
var config = require('./config');
var User = require('./src/models/user_model.js');
var search_mid = require('./src/middleware/search_mid');
var user_mid = require('./src/middleware/user_mid');
var paper_mid = require('./src/middleware/paper_mid');
var express = require('express');
var _ = require('lodash');
var crypto = require('crypto');
var bodyParser = require('body-parser');
var jwt = require('jsonwebtoken');
var ejs = require('ejs');
var app = express();
//Redefine where views are located
app.set('views',`${__dirname}/src/views/html`);
//Sets the view engine, it will render html as ejs
app.set('view engine', 'ejs');
app.engine('ejs', ejs.renderFile);
//varriable to keep track of compiled html
//var html = {value : {}};
//Compiles the jade templates in the view
//app.use('/public/views', function(){
//var options = {cache: true};
//jade.compileFile('/public/views/index.jade', options);
//jade.renderFile('/public/views/index.jade');
//});
//Hosts the static files in public under /assets
app.use('/assets', express.static(`${__dirname}/src/views/assets`));
app.use(bodyParser.urlencoded({ extended: false}));
app.use(bodyParser.json());
//Middleware
// Checks if token exist on input,
//Checks token if valid (experation) and user exists, goes next
var authorize = function(req, res, next) {
var token = req.body.token || req.query.token || req.headers['x-access-token'];
console.log("balh");
// decode token
if (token) {
// verifies secret, algorithms used, and checks experation time
jwt.verify(token, config.secret, { algorithms: ['HS256']}, function(err, decoded) {
if (err) {
console.log(err);
res.redirect('login');
//res.status(401).send({message : 'missing token'});
return;
} else {
console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaaa');
req.body.userId = decoded.userId;
req.body.permission = decoded.permission;
next();
}
});
} else {
console.log('bbbbbbbbbbbbbbbbbbbbbbbbbbbb');
// if there is no token
res.redirect('login');
//res.status(401).send({message : 'missing token'});
return;
}
};
//Default route gives back login page
app.get('/', function(req, res){
//if no successful auth token
res.render('login',{
message: 'login successful',
success: true
});
});
//Brings the user to the login page
app.get('/login', function(req, res){
res.render('login',{
message: 'login successful',
success: true
});
});
app.use('/papers/delete', authorize);
app.use('/papers/delete', paper_mid.hasCreationPermission);
//Renders the delete papers page
app.get('/papers/delete', function(req, res){
res.render('delete');
});
//Middleware for deleting papers
app.use('/api/papers/delete', authorize);
app.use('/api/papers/delete', paper_mid.hasPermission);
//Delete a paper, requires the proper premissions to do so.
// The request just needs an object with a papers_id inside, as well as a jwt token.
app.post('/api/papers/delete', function(req, res){
var data = {
papers_id : req.body.papers_id,
users_id : req.body.userId
}
paper_mid.deletePaper(data, function(err, result){
if (err){
console.log(err);
res.status(401).send({message : 'Paper Deletion Failed'});
}else if(result.affectedRows <= 0){
res.status(404).send({message : 'Paper Does Not Exist'});
}else{
res.status(200).send({message : 'Paper Deletion Successful'});
}
});
});
app.use('/api/papers/authers/delete', authorize);
app.use('/api/papers/authers/delete', paper_mid.hasPermission);
//Strictly deletes just an author from a given paper
//Needs authors_id, papers_id, and the token
app.post('/api/papers/authors/delete', function(req, res){
var data = {
papers_id : req.body.papers_id,
users_id : req.body.userId,
authors_id : req.body.authors_id
}
console.log('here123');
paper_mid.deleteAuthor(data, function(err, result){
if (err){
console.log(err);
res.status(401).send({message : 'Author Deletion Failed'});
}else if(result.affectedRows <= 0){
res.status(404).send({message : 'Author Does Not Exist'});
}else{
res.status(200).send({message : `Author was Deleted Successfully`});
}
});
});
//Strictly adds just the authors from a given paper
// Takes a list of author ids as authors
app.post('/api/papers/authors/add', function(req, res){
var data = {
papers_id : req.body.papers_id,
users_id : req.body.userId,
authors : req.body.authors // Equals a list of authors_id
}
console.log(data.authors);
if (!data.authors){
data.authors = [];
}
paper_mid.addAuthors(data, function(err, result){
if (err){
console.log(err);
res.status(401).send({message : 'Adding Authors Failed'});
}else if(result.affectedRows >= 0){
res.status(200).send({message : `${result.affectedRows} Authors were Added Successfully`});
}else{
res.status(404).send({message : `No Authors Were Added`});
}
});
});
//Strictly deletes just the keywords from a given paper
app.post('/api/papers/keywords/delete', function(req, res){
var data = {
papers_id : req.body.papers_id,
users_id : req.body.userId,
keywords : req.body.keywords
}
paper_mid.deleteStrictlyKeywords(data, function(err, result){
if (err){
console.log(err);
res.status(401).send({message : 'Keywords Deletions Failed'});
}else if(result.affectedRows <= 0){
res.status(404).send({message : 'One or More Keywords Does Not Exist'});
}else{
res.status(200).send({message : `${result.affectedRows} Keywords were Deleted Successfully`});
}
});
});
//Strictly adds just the keywords from a given paper
app.post('/api/papers/keywords/add', function(req, res){
var data = {
papers_id : req.body.papers_id,
users_id : req.body.userId,
keywords : req.body.keywords
}
paper_mid.addKeywords(data, function(err, result){
if (err){
console.log(err);
res.status(401).send({message : 'Adding Keywords Failed'});
}else if(result.affectedRows >= 0){
res.status(200).send({message : `${result.affectedRows} Keywords were Added Successfully`});
}else{
res.status(404).send({message : `No Keywords Were Added`});
}
});
});
app.use('/papers/edit', authorize);
app.use('/papers/edit', paper_mid.hasCreationPermission);
// Renders the edit papers page
app.get('/papers/edit', function(req, res){
user_mid.getPapers({users_id : req.body.userId}, function(err, papers){
if (err){
console.log(err);
res.status(401).send({message: 'Invalid Request'});
}else{
papers.forEach(function(paper, index, arr){
paper_mid.getKeywords(paper, function(err, keywords){
if (err){
console.log(err);
res.status(401).send({message: 'Paper Keywords Error'});
}else{
paper.keywords = keywords;
if (index >= arr.length-1){
// Gets authors
papers.forEach(function(paper2, index2, arr2){
paper_mid.getAuthors(paper2, function(err, authors){
if (err){
console.log(err);
res.status(401).send({message: 'Paper Keywords Error'});
}else{
paper2.authors = authors;
if (index2 >= arr2.length-1){
if (!papers.authors){
papers.authors = [];
}
if (!papers.keywords){
papers.keywords = [];
}
res.render('edit',{
papers : papers
//test : 'helloworld'
});
}else{
index2 += 1;
}
}
});
});
}else{
index += 1;
}
}
});
});
};
});
});
//Middleware for editing papers
app.use('/api/papers/edit', authorize);
app.use('/api/papers/edit', paper_mid.hasCreationPermission);
// Edits a paper, requires a paper object with the updated parameters
// The request just needs an object with a papers_id inside, as well as a jwt token.
app.post('/api/papers/edit', function(req, res){
var paperData = req.body;
paperData.users_id = req.body.userId;
paper_mid.editPaper(paperData, function(err, message){
if (err){
console.log(err);
res.status(401).send({message : 'Paper Edit Failed'});
}else{
res.status(200).send({message : 'Paper Edit Successful'});
}
});
});
//Renders the create papers page
app.use('/papers/create', authorize);
app.use('/papers/create', paper_mid.hasCreationPermission);
app.get('/papers/create', function(req, res){
res.render('add');
});
//Create a paper, requires paper object data specified below, and jwt token
// {
// title,
// abstract,
// citation
// }
app.use('/api/papers/create', authorize);
app.use('/api/papers/create', paper_mid.hasCreationPermission);
app.post('/api/papers/create', function(req, res){
var paperData = req.body;
paperData.users_id = req.body.userId;
paper_mid.createPaper(paperData, function(err, message){
if (err){
console.log(err);
res.status(401).send({message : 'Paper Creation Failed'});
}else{
res.status(200).send({message : 'Paper Creation Successful'});
}
});
});
//Renders the mypapers page
app.get('/papers', authorize, function(req, res){
var data = {
users_id : req.body.userId,
permission : req.body.permission
}
user_mid.getPapers(data, function(err, papers){
if (err){
console.log(err);
res.status(401).send({message: 'Invalid Request'});
}else{
papers.forEach(function(paper, index, arr){
paper_mid.getKeywords(paper, function(err, keywords){
if (err){
console.log(err);
res.status(401).send({message: 'Paper Keywords Error'});
}else{
paper.keywords = keywords;
if (index >= arr.length-1){
// Gets authors
papers.forEach(function(paper2, index2, arr2){
paper_mid.getAuthors(paper2, function(err, authors){
if (err){
console.log(err);
res.status(401).send({message: 'Paper Keywords Error'});
}else{
paper2.authors = authors;
if (index2 >= arr2.length-1){
console.log(paper2.authors);
if (!papers.authors){
papers.authors = [];
}
if (!papers.keywords){
papers.keywords = [];
}
res.render('mypapers',{
papers : papers
//test : 'helloworld'
});
}else{
index2 += 1;
}
}
});
});
}else{
index += 1;
}
}
});
});
};
});
});
//app.use('/') authorize middleware is working
app.get('/controlpanel', authorize, function(req, res){
var data = {
users_id : req.body.userId
};
res.render('controlpanel', data);
});
app.get('/search', function(req, res){
res.render('search', {
papers: []
});
});
//searching, Expects an array of values EX: test?array=a&array=b&array=c
//Returns and array of objects example object: {
// title -> String
// authors -> array of strings
// abstract -> String
// citations -> String
// paper_keywords -> array of strings
//}
app.get('/search/:keywords', function(req, res, next){
var keywords = req.query.keywords;
if (!keywords){
res.render('search', {
papers : [],
keywords : []
});
}else{
search_mid.getPapers({keywords : keywords}, function(err, papers){
if (err || !papers){
console.log(err);
res.status(401).send({message: 'Invalid Request'});
}else{
//Gets keywords
papers.forEach(function(paper, index, arr){
paper_mid.getKeywords(paper, function(err, keywords){
if (err){
console.log(err);
res.status(401).send({message: 'Paper Authors Error'});
}else{
paper.keywords = _.uniq(keywords);
if (index >= arr.length-1){
// Gets authors
papers.forEach(function(paper2, index2, arr2){
paper_mid.getAuthors(paper2, function(err, authors){
if (err){
console.log(err);
res.status(401).send({message: 'Paper Keywords Error'});
}else{
paper2.authors = authors;
if (index2 >= arr2.length-1){
if (!papers.authors){
papers.authors = [];
}
if (!papers.keywords){
papers.keywords = [];
}
res.render('search',{
papers : papers,
keywords : keywords // Keywords from the search
//test : 'helloworld'
});
}else{
index2 += 1;
}
}
});
});
}else{
index += 1;
}
}
});
});
};
});
}
if (req.params === Array){
console.log(true);
}
});
app.get('/api/search/:keywords', function(req, res, next){
var keywords = req.query.keywords;
if (!keywords){
res.json({
papers : []
});
}else{
search_mid.getPapers({keywords : keywords}, function(err, papers){
if (err || !papers){
console.log(err);
res.status(401).send({message: 'Invalid Request'});
}else{
//Gets keywords
papers.forEach(function(paper, index, arr){
paper_mid.getKeywords(paper, function(err, keywords){
if (err){
console.log(err);
res.status(401).send({message: 'Paper Authors Error'});
}else{
paper.keywords = _.uniq(keywords);
if (index >= arr.length-1){
// Gets authors
papers.forEach(function(paper2, index2, arr2){
paper_mid.getAuthors(paper2, function(err, authors){
if (err){
console.log(err);
res.status(401).send({message: 'Paper Keywords Error'});
}else{
paper2.authors = authors;
if (index2 >= arr2.length-1){
res.json({
papers : papers
});
}else{
index2 += 1;
}
}
});
});
}else{
index += 1;
}
}
});
});
};
});
}
if (req.params === Array){
console.log(true);
}
});
// Get the user based on the user id in the jwt token
app.get('/api/users/', authorize, function(req, res){
user_mid.exist({users_id : req.body.userId}, function(err, user){
if (err){
console.log(err);
res.status(401).send({message: 'User does not exist'});
}else{
delete user['salt'];
delete user['pass_hash'];
res.json(user);
}
});
});
// Gets all users, only grabs first name, last name, and users_id
// Must be a valid user in order to get all other users
app.get('/api/users/all/', authorize, function(req, res){
user_mid.getAllUsers({users_id : req.body.userId}, function(err, users){
if (err){
console.log(err);
res.status(401).send({message: 'Users do not exist'});
}else{
res.json({users : users});
}
});
});
//This provides the paper information based on the user id in the jwt token.
//Give back all papers associated with the given user
app.use('/api/papers', authorize);
app.get('/api/papers/', function(req, res){
var data = {
users_id : req.body.userId,
permission : req.body.permission
}
console.log("mydata" + data);
user_mid.getPapers(data, function(err, papers){
console.log('adfadfadf2222222222222');
if (err){
console.log(err);
res.status(401).send({message: 'Invalid Request'});
}else{
papers.forEach(function(paper, index, arr){
paper_mid.getKeywords(paper, function(err, keywords){
if (err){
console.log(err);
res.status(401).send({message: 'Paper Keywords Error'});
}else{
paper.keywords = keywords;
if (index >= arr.length-1){
// Gets authors
papers.forEach(function(paper2, index2, arr2){
paper_mid.getAuthors(paper2, function(err, authors){
if (err){
console.log(err);
res.status(401).send({message: 'Paper Keywords Error'});
}else{
paper2.authors = authors;
if (index2 >= arr2.length-1){
console.log(paper2.authors);
if (!papers.authors){
papers.authors = [];
}
if (!papers.keywords){
papers.keywords = [];
}
res.status(200).json({
papers : papers
//test : 'helloworld'
});
}else{
index2 += 1;
}
}
});
});
}else{
index += 1;
}
}
});
});
};
});
});
app.delete('/api/papers/:id', function(req, res){
//Use paper mid
});
//This will varify if a username and password provided are valid
//If the they both are valid it will return a jwt token in the body response
//else it will return a 401
app.post('/api/authenticate', function(req, res){
var username = req.body.username;
var password = req.body.password;
if (!username){
//throw new Error('No Username Found')
res.status(401).send({message: 'No Username Found'});
}else if (!password){
//throw new Error('No Password Found');
res.status(401).send({message: 'No Password Found'});
}else{
//Search for user
user_mid.exist({ username: username }, function(err, user) {
if (err) {
// user not found
console.log(err);
res.status(404).send({message: 'User Not Found'});
return;
}
if (!user) {
// incorrect user credentials
res.status(401).send({message: 'No user found'});
return;
}
if (!user.pass_hash || !user.salt){
res.status(401).send({message: 'User password or salt error'});
return;
}
if (!user.users_id){
res.status(401).send({message: 'User ID does not exist'});
return;
}
// User has authenticated OK
// Generate password hash with user's salt(32bit)
var hash = crypto
.createHash("sha256")
.update(password+user.salt)
.digest('hex');
console.log(`hash: ${hash}`);
console.log(`user pass: ${user.pass_hash}`);
if (hash === user.pass_hash){
//Create JWT Token
var token = jwt.sign({userId: user.users_id, permission: user.permission}, config.secret, {algorithm: 'HS256'}, {
expiresIn: 43200 // 24 hours
});
res.setHeader("x-access-token", token);
res.json({
success: true,
user_id: user.users_id,
message: 'Authentication successful',
token: token
});
}else{
res.status(401).send({message: 'Username or password is incorrect'});
}
});
}
});
app.listen(config.server.port);
console.log(`app running`);