-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
415 lines (377 loc) · 14.5 KB
/
Copy pathserver.js
File metadata and controls
415 lines (377 loc) · 14.5 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
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
"use strict";
import express from "express";
const app = express();
app.use('/', express.static('public'));
app.use(express.json());
import pgPromise from 'pg-promise';
//Considered earlier that maybe this should be moved into database.js
//Decided against it though ultimately since then we'd just end up needing a ton of methods that are basically just used once for the various totally different queries to the database
//So it seemed neater just to access it directly
//My (G.E.) initial comments on the subject:
// maybe should move into separate database.js? I'm finding this way to be easier though
// I think just gonna leave this way for now; have a database.js with functions for Find, Update etc. but the operations
// I'm doing are too specific for me to use that without having to write like a separate function/if statement for every
// time it's called at which point it'd just make the code harder to follow
const pgp = pgPromise();
const db = pgp({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
async function initializeDatabase() {
try {
await db.none({text:"CREATE TABLE IF NOT EXISTS users (email text UNIQUE, display_name text, phone_number text, salt text NOT NULL, hash text NOT NULL)"});
}
catch(err) {
console.error(err);
}
try {
await db.none({text:"CREATE TABLE IF NOT EXISTS tasks (title text, description text, user_name text, location text, email text, phone_number text, req_status text, id serial UNIQUE)"});
}
catch(err) {
console.error(err);
}
try {
await db.none({text:"CREATE TABLE IF NOT EXISTS comments (task_id integer, user_name text, contents text)"});
}
catch(err) {
console.error(err);
}
}
initializeDatabase();
import expressSession from 'express-session'; // for managing session state
import passport from 'passport'; // handles authentication
import {Strategy as LocalStrategy} from 'passport-local'; // username/password strategy
import minicrypt from './miniCrypt.js';
const mc = new minicrypt();
// Session configuration
const session = {
//Leaving the session storing as-is for now, but if wanting to make this scale up (to like thousands of users though; works fine small-scale) should replace it
secret : process.env.SECRET || 'SECRET', // set this encryption key in Heroku config (never in GitHub)!
resave : false,
saveUninitialized: false
};
// Passport configuration
const strategy = new LocalStrategy(
{
usernameField: 'user_email',
passwordField: 'password'
},
async (username, password, done) => {
try {
if (!(await findUser(username))) {
// no such user
await new Promise((r) => setTimeout(r, 2000)); // two second delay
return done(null, false, { 'message' : 'Wrong username' });
}
} catch(err) {
console.error(err);
}
try {
if (!(await validatePassword(username, password))) {
// invalid password
// should disable logins after N messages
// delay return to rate-limit brute-force attacks
await new Promise((r) => setTimeout(r, 2000)); // two second delay
return done(null, false, { 'message' : 'Wrong password' });
}
} catch(err) {
console.error(err);
}
// success!
// should create a user object here, associated with a unique identifier
return done(null, username);
});
// App configuration
app.use(expressSession(session));
passport.use(strategy);
app.use(passport.initialize());
app.use(passport.session());
// Convert user object to a unique identifier.
passport.serializeUser((user, done) => {
done(null, user);
});
// Convert a unique identifier to a user object.
passport.deserializeUser((uid, done) => {
done(null, uid);
});
//code here modified from provided code for exercise
// Returns true iff the user exists.
async function findUser(email) {
try {
const userData = await db.any({text:"SELECT email FROM users WHERE email = $1 LIMIT 1", values:[email]});
if (userData.length <= 0) {
return false;
}
return true
}
catch(err) {
console.error(err);
return false; //should really return something else, like just undefined I guess, to clarify it just failed and didn't actually confirm they didn't exist, but doesn't really matter for this.
}
}
// Returns true iff the password is the one we have stored.
async function validatePassword(email, pwd) {
// CHECK PASSWORD
try {
const userData = await db.any({text:"SELECT email, salt, hash FROM users WHERE email = $1 LIMIT 1", values:[email]});
if (userData.length <= 0) {
return false;
}
const userSalt = userData[0].salt;
const userHash = userData[0].hash;
return mc.check(pwd, userSalt, userHash);
} catch(err) {
console.error(err);
return false; //not sure best approach for what to return here; see comment in findUser
}
}
function checkLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
// If we are authenticated, run the next route.
next();
} else {
// Otherwise, redirect to the login page.
res.redirect('/index.html');
}
}
//Command used to test:
//curl -d '{ "user_email" : "Test", "password" : "ABCDEF" }' -H "Content-Type: application/json" http://localhost:3000/user/new/
app.post("/user/new", async (req, res) => {
const email = req.body["user_email"];
const password = req.body["password"];
if (email === "" || password === "") {
res.status(400)
res.send("Empty parameters.")
} else if (await findUser(email)) {
res.status(304);
res.send("Account already exists.")
} else {
const [salt, hash] = mc.hash(password);
try {
await db.none({text:"INSERT INTO users(email, salt, hash) VALUES ($1, $2, $3)", values:[email, salt, hash]});
console.log(`Created account: ${email}`);
res.status(201);
res.send('Created account.');
} catch(err)
{
console.error(err);
res.status(500);
res.send('Failed to add account.');
}
}
});
//Not totally sure if this is setup right, but works with the command:
//curl -H 'user_email : Test password : ABCDEF Content-Type: application/json' http://localhost:3000/user/login/
app.post("/user/login",
passport.authenticate("local")
, (req, res) => {
res.status(200);
res.send(JSON.stringify({
"login_status": "valid"
}));
}
);
// curl -X PUT -d '{ "session_token" : "Test" }' -H "Content-Type: application/json" http://localhost:3000/user/edit
app.put("/user/edit",
checkLoggedIn, //Authentication
async (req, res) => {
const email = req.body["user_email"];
const displayName = req.body["display_name"];
const phoneNumber = req.body["phone_number"];
//check if proper user
if (email === req.user) {
try {
//Note for now I'm leaving tip_link out of it since none of our API stuff from last time mentioned it, I can re-add if people want it
await db.none({text:"UPDATE users SET display_name = $2, phone_number = $3 WHERE email = $1", values:[email, displayName, phoneNumber]});
console.log(`Updated account: ${email} ${displayName} ${phoneNumber}`);
res.status(204);
res.send('Updated account details.');
} catch(err) {
console.error(err);
res.status(500);
res.send('Failed to add account.');
}
} else {
res.status(403);
res.send('Invalid session.');
}
});
//curl -X DELETE -d '{ "dsession_token" : "Test" }' -H "Content-Type: application/json" http://localhost:3000/user/delete
//not sure I should actually make this accessible to user since it'll cause issues presumably if someone were to make a task then delete the account might cause issues? IDK
app.delete("/user/delete",
checkLoggedIn, //Authentication
async (req, res) => {
const email = req.body["user_email"];
//check if proper user
if (email === req.user) {
//Considered adding a check for if the user has open tasks but not going to right now since the tasks aren't really set up for that
try {
await db.none({text:"DELETE FROM users WHERE email = $1", values:[email]});
console.log(`Deleted account ${email}`);
res.status(204);
res.send('Deleted account.');
} catch(err) {
console.error(err);
res.status(500);
res.send('Failed to delete account.');
}
} else {
res.status(403);
res.send('Invalid session.');
}
});
//Test curl:
//curl -H "Content-Type: application/json" http://localhost:3000/user/data?target_email=test@umass.edu
app.get("/user/data", async (req, res) => {
const email = req.query["target_email"];
let details = null;
try {
const userData = await db.any({text:"SELECT email, display_name, phone_number FROM users WHERE email = $1 LIMIT 1", values:[email]});
if (userData.length > 0) {
details = userData[0];
}
} catch(err) {
console.error(err);
// details = null; //< line not needed but leaving here as reminder that it will still be null
}
if (details !== null) {
let output = {};
output.email = details.email;
output.display_name = details.display_name || "";
output.phone_number = details.phone_number || "";
res.status(200);
res.send(JSON.stringify(output));
} else {
res.status(404);
res.send();
}
});
app.get('/user/logout', (req, res) => {
req.logout(); // Logs us out!
res.redirect('/index.html'); // back to login
});
//for submiting request quarantiining.html
app.post("/task", async (req, res) => {
const requestTitle = req.body["title"];
const requestDescription = req.body["description"];
const name = req.body["user_name"];
const req_location = req.body["location"];
const email = req.body["email"];
const phoneNumber = req.body["phone_number"];
try {
await db.query ("INSERT INTO tasks(title, description, user_name, location, email, phone_number, req_status) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *",
[requestTitle, requestDescription, name, req_location, email, phoneNumber, "pending"]);
console.log(`Created task: ${requestTitle}`);
res.status(201);
res.send('Created task.');
}catch(err) {
console.error(err);
res.status(500);
res.send('Failed to add request.');
}
});
app.put("/task/:id", async (req, res) => {
const id = req.params["id"];
const requestTitle = req.body["title"];
const requestDescription = req.body["description"];
const name = req.body["user_name"];
const req_location = req.body["location"];
const email = req.body["email"];
const phoneNumber = req.body["phone_number"];
try {
await db.query (
"UPDATE tasks SET email = $1, title = $2, description = $3, name = $4, location = $5, phone_number = $6 WHERE id = $7",
[email, requestTitle, requestDescription, name, req_location, phoneNumber, id]);
res.status(204);
res.send('Updated related task details.');
} catch(err) {
console.error(err);
res.status(500);
res.send('Failed to update task details.');
}
});
//for geting request quarantiining.html
app.get("/task", async (req, res) => {
try {
const results = await db.query("SELECT * FROM tasks");// db named task
res.json(results.rows);
} catch (err) {
console.error(err);
res.status(500);
res.send('Failed to get list of tasks.');
}
});
//for deleting request quarantining.html
app.delete("/task/:id",
// checkLoggedIn, //Authentication here is needed
async (req, res) => {
const id = req.params["id"];
try {
await db.query ("DELETE FROM tasks WHERE id = $1", [id]);
await db.query ("DELETE FROM comments WHERE task_id = $1", [id]); //remove associated comments
console.log(`Deleted task ${id}`);
res.status(204);
res.send('Deleted task.');
} catch(err) {
console.error(err);
res.status(500);
res.send('Failed to delete task.');
}
});
//for submiting request requestProgress.html
app.put("/markProgress/:id",
async (req, res) => {
const id = req.params["id"];
try {
await db.query ("UPDATE tasks SET req_status = $1 WHERE id = $2", ["in progress", id]);
res.status(204);
res.send('submitted, you are all set!!!!');
}catch(err) {
console.error(err);
res.status(500);
res.send('Failed to update request status.');
}
});
// create comment
app.post("/comment",
checkLoggedIn,
async (req, res) => {
const task_id = req.body["task_id"];
const user_name = req.body["user_name"];
const contents = req.body["contents"];
try {
await db.none ({text:"INSERT INTO comments(task_id, user_name, contents) VALUES ($1, $2, $3)", values:[task_id, user_name, contents]});
console.log(`Created comment for ${task_id}`);
res.status(201);
res.send('Created comment.');
}
catch(err) {
console.error(err);
res.status(500);
res.send('Failed to add comment.');
}
});
// get comment
app.get("/comment", async (req, res) => {
const task_id = req.body["task_id"];
try {
const comms = await db.query("SELECT * FROM comments WHERE task_id = $1", [task_id]);
res.status(200);
res.json(comms.rows);
}
catch (err) {
console.error(err);
res.status(500);
res.send('Failed to load comments.');
}
});
const port = process.env.PORT || 3000;
app.listen(port, (err) => {
if (err) {
console.log("problem!!", err);
return;
}
console.log("listening to port 3000");
});