-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.pl
More file actions
executable file
·521 lines (447 loc) · 19.2 KB
/
server.pl
File metadata and controls
executable file
·521 lines (447 loc) · 19.2 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
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
#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
use Crypt::SaltedHash;
use Mojolicious::Lite;
use Mojo::JSON qw(decode_json encode_json);
use Mango;
plugin 'basic_auth';
# Globals
my $FROM_EMAIL = 'campusfinder@thomascoe.com';
my $ROOT_DOMAIN = 'https://thomascoe.com/campus-finder';
sub send_email {
my ($to, $from, $subject, $body) = @_;
`echo "$body" | mail -s "$subject" -r "$from" $to`;
}
sub send_verification_email {
my ($user) = @_;
my $email = $user->{email};
my $code = $user->{emailvercode};
# Send email to user with confirmation link
my $link = $ROOT_DOMAIN."/v1/auth/verify?email=$email&code=$code";
my $subject = 'Welcome to Campus Finder!';
my $body = "Please verify your email to activate your account\n$link";
send_email($email, $FROM_EMAIL, $subject, $body);
}
sub is_strong_pass {
my ($pass) = @_;
# TODO: Implement pass strength check
return 1;
}
sub gen_hash {
my ($var) = @_;
my $csh = Crypt::SaltedHash->new(algorithm => 'SHA-256');
$csh->add($var);
return $csh->generate;
}
# Database connection
my $uri = 'mongodb://127.0.0.1:27017/test';
helper mango => sub { state $m = Mango->new($uri) };
get '/' => {text => 'Gatech Campus Finder'};
group {
under '/v1';
group {
under '/auth';
# User Login, returns a token for user to use as password
post '/login' => sub {
my $c = shift;
my $username = $c->param('username');
my $password = $c->param('password');
if (not defined $username or not defined $password) {
$c->respond_to(any => { json => {error => 'Bad Request'},
status => 400});
return;
}
# Lookup user in DB
my $users = $c->mango->db->collection('users');
my $doc = $users->find_one({username => $username});
# Verify Password
my $validpw = Crypt::SaltedHash->validate($doc->{password}, $password);
if (not defined $doc or not $validpw) {
$c->respond_to(any => { json => {error => 'Invalid Credentials'},
status => 401});
return;
}
# Check if Email is Verified
if ($doc->{emailverstat} ne 'verified') {
$c->respond_to(any => { json => {error => 'Awaiting Email Verification'},
status => 403});
return;
}
# Generate token for user
my $token = unpack 'h32', `head -c 16 /dev/urandom`;
my $timestring = gmtime->datetime . 'Z';
# Store session in db
my $sessions = $c->mango->db->collection('sessions');
$sessions->insert({
username => $username,
token => $token,
timestamp => $timestring
});
# Send response to client
$c->respond_to(any => { json => {username => $username, token => $token},
status => 200});
};
# User register
post '/register' => sub {
my $c = shift;
my $username = $c->param('username');
my $email = $c->param('email');
my $password = $c->param('password');
if (not defined $username or not defined $email or not defined $password) {
$c->respond_to(any => { json => {error => 'Bad Request'},
status => 400});
return;
}
# Verify password strength
if (not is_strong_pass($password)) {
$c->respond_to(any => { json => {error => 'Weak Password'},
status => 400});
return;
}
# Check if email or username already exists
my $users = $c->mango->db->collection('users');
my $doc1 = $users->find_one({username => $username});
my $doc2 = $users->find_one({email => $email});
if (defined $doc1 or defined $doc2) {
$c->respond_to(any => { json => {error => 'User Already Exists'},
status => 409});
return;
}
# Hash/salt password
my $saltedhash = gen_hash($password);
# Generate verification code
my $code = unpack 'h32', `head -c 16 /dev/urandom`;
my $user = {
username => $username,
email => $email,
emailverstat => 'unverified',
emailvercode => $code,
password => $saltedhash,
radius => 1
};
# Insert user into DB and send verification email
my $oid = $users->insert($user);
send_verification_email($user);
$c->respond_to(any => { json => {}, status => 200});
};
# Reset password for a user-> sends temp password in email
post '/resetpass' => sub {
my $c = shift;
my $email = $c->param('email');
my $users = $c->mango->db->collection('users');
my $doc = $users->find_one({email => $email});
if (not defined $doc) {
$c->respond_to(any => { json => {error => 'User Not Found'},
status => 400});
return;
}
# Generate temp password
my $pw = unpack 'h16', `head -c 8 /dev/urandom`;
my $saltedhash = gen_hash($pw);
# Update db
$users->update($doc->{_id}, {'$set' => {password => $saltedhash}});
# Send email with temp password
my $subject = 'Campus Finder Password Reset';
my $body = "Your Campus Finder password has been reset.\nUsername: $doc->{username}\nYour new temporary password is: $pw\nPlease change this immediately!";
send_email($email, $FROM_EMAIL, $subject, $body);
$c->respond_to(any => { json => {}, status => 200});
};
# Resend the verification email if a user isn't verified
post '/resendverification' => sub {
my $c = shift;
my $email = $c->param('email');
my $users = $c->mango->db->collection('users');
my $doc = $users->find_one({email => $email});
if (not defined $doc) {
$c->respond_to(any => { json => {error => 'User Not Found'},
status => 400});
return;
}
if ($doc->{emailverstat} eq 'verified') {
$c->respond_to(any => { json => {error => 'User already verified'},
status => 400});
return;
}
send_verification_email($doc);
$c->respond_to(any => { json => {}, status => 200});
};
# Verify a users email
get '/verify' => sub {
my $c = shift;
my $email = $c->param('email');
my $code = $c->param('code');
# Lookup user to get verification status and code
my $users = $c->mango->db->collection('users');
my $doc = $users->find_one({email => $email});
# Error handling
if (not defined $doc) {
$c->respond_to(any => { text => "Error: User Not Found", status => 400});
return;
} elsif ($doc->{emailverstat} eq 'verified') {
$c->respond_to(any => { text => "User already verified", status => 400});
return;
} elsif ($doc->{emailvercode} ne $code) {
$c->respond_to(any => { text => "Error: Verification code incorrect", status => 400});
return;
}
# Mark user as verified
$users->update($doc->{_id}, {'$set' => {emailverstat => 'verified'}});
$c->respond_to(any => { text => "Verification successful!", status => 200});
}
};
# Everything in this group will require authentication
group {
under sub {
my $c = shift;
return $c->basic_auth( realm => sub {
my $user = shift;
my $token = shift;
# Lookup session in the db
my $sessions = $c->mango->db->collection('sessions');
my $doc = $sessions->find_one({username => $user, token => $token});
if (defined $doc) {
return 1;
}
});
};
# Update a users password
post '/auth/updatepass' => sub {
my $c = shift;
my ($user, $pass) = split /:/, $c->req->url->to_abs->userinfo;
my $newpass = $c->param('newpass');
if (not defined $newpass) {
$c->respond_to(any => { json => {error => 'Bad Request'},
status => 400});
return;
}
# Check password strength
if (not is_strong_pass($newpass)) {
$c->respond_to(any => { json => {error => 'Weak password'},
status => 400});
return;
}
# Hash/salt password
my $saltedhash = gen_hash($newpass);
# Update DB
my $users = $c->mango->db->collection('users');
$users->update({username => $user}, {'$set' => {password => $saltedhash}});
$c->respond_to(any => { json => {}, status => 200});
};
# Logout (invalidate a session)
post '/auth/logout' => sub {
my $c = shift;
my ($user, $pass) = split /:/, $c->req->url->to_abs->userinfo;
my $sessions = $c->mango->db->collection('sessions');
$sessions->remove({username => $user, token => $pass});
$c->respond_to(any => { json => {}, status => 200});
};
# Return all of the valid location types
get '/types' => sub {
my $c = shift;
my $types = $c->mango->db->collection('types');
my $docs = $types->find({}, {_id => 0})->all;
$c->respond_to(any => { json => $docs, status => 200});
};
# Return all of the locations
get '/locations' => sub {
my $c = shift;
my $locations = $c->mango->db->collection('locations');
# TODO: Bound these locations by lat/long?
my $docs = $locations->find({}, {name => 1, type => 1, latitude => 1, longitude => 1})->all;
$c->respond_to(any => { json => $docs, status => 200});
};
# Add a new location to the db
post '/locations' => sub {
my $c = shift;
my ($user, $pass) = split /:/, $c->req->url->to_abs->userinfo;
my $loc = $c->req->json;
if (not defined $loc->{name}
or not defined $loc->{type}
or not defined $loc->{description}
or not defined $loc->{latitude}
or not defined $loc->{longitude}) {
$c->respond_to(any => { json => {error => 'Missing Required Params (name, type, descriptions, latitude, longitude)'},
status => 400});
return;
}
# Verify type is valid
my $types = $c->mango->db->collection('types');
if (not defined $types->find_one({type => $loc->{type}})) {
$c->respond_to(any => { json => {error => 'Invalid type'}, status => 400});
return;
}
# Set extra params
$loc->{username} = $user;
$loc->{timestamp} = gmtime->datetime . 'Z';
$loc->{upvotes} = 0;
$loc->{downvotes} = 0;
# Insert into database
my $locations = $c->mango->db->collection('locations');
$locations->insert($loc);
$c->respond_to(any => { json => $loc, status => 200});
};
# Get location details
get '/locations/:locationid' => sub {
my $c = shift;
my ($user, $pass) = split /:/, $c->req->url->to_abs->userinfo;
# Look up location
my $locid = $c->param('locationid');
my $oid = Mango::BSON::ObjectID->new($locid);
my $locations = $c->mango->db->collection('locations');
my $doc = $locations->find_one({_id => $oid});
if (not defined $doc) {
$c->respond_to(any => { json => {error => "Location not found"}, status => 404});
return;
}
# Look up this user's vote
my $votes = $c->mango->db->collection('votes');
my $votedoc = $votes->find_one({locationid => $locid, username => $user});
my $vote = 0;
if (defined $votedoc) {
$vote = $votedoc->{vote};
}
$doc->{myvote} = $vote;
$c->respond_to(any => { json => $doc, status => 200});
};
# Mark your vote on a location
post '/locations/:locationid/vote' => sub {
my $c = shift;
my ($user, $pass) = split /:/, $c->req->url->to_abs->userinfo;
my $vote = $c->param('vote') + 0;
my $locid = $c->param('locationid');
# Verify vote
if ((not defined $vote) or ($vote ne 1 and $vote ne 0 and $vote ne -1)) {
$c->respond_to(any => { json => {error => "Invalid vote"}, status => 400});
return;
}
# Look up location
my $oid = Mango::BSON::ObjectID->new($locid);
my $locations = $c->mango->db->collection('locations');
my $doc = $locations->find_one({_id => $oid});
if (not defined $doc) {
$c->respond_to(any => { json => {error => "Location not found"}, status => 404});
return;
}
# Look up this user's vote
my $votes = $c->mango->db->collection('votes');
my $votedoc = $votes->find_one({locationid => $locid, username => $user});
my $oldvote = 0;
if (defined $votedoc) {
$oldvote = $votedoc->{vote};
}
# Calculate the upvote/downvote differentials
my $upvotes = 0;
my $downvotes = 0;
if ($vote eq 0) {
if ($oldvote eq 1) {
$upvotes = -1;
} elsif ($oldvote eq -1) {
$downvotes = -1;
}
} elsif ($vote eq 1) {
if ($oldvote eq -1) {
$upvotes = 1;
$downvotes = -1;
} elsif ($oldvote eq 0) {
$upvotes = 1;
}
} elsif ($vote eq -1) {
if ($oldvote eq 1) {
$upvotes = -1;
$downvotes = 1;
} elsif ($oldvote eq 0) {
$downvotes = 1;
}
}
# Update the DB
$locations->update({_id => $oid}, {'$inc' => {upvotes => $upvotes, downvotes => $downvotes}});
$votes->update({locationid => $locid, username => $user}, {'$set' => {vote => $vote}}, {upsert => 1});
$c->respond_to(any => { json => {}, status => 200});
};
# Get the comments on a location
get '/locations/:locationid/comments' => sub {
my $c = shift;
my $locid = $c->param('locationid');
# optional params
my $count = $c->param('count') + 0;
my $sort = $c->param('sort');
# TODO: Use these params
# Look up location
my $oid = Mango::BSON::ObjectID->new($locid);
my $locations = $c->mango->db->collection('locations');
my $doc = $locations->find_one({_id => $oid});
if (not defined $doc) {
$c->respond_to(any => { json => {error => "Location not found"}, status => 404});
return;
}
# Look up comments for this location
my $comments = $c->mango->db->collection('comments');
my $docs = $comments->find({locationid => $locid}, {locationid => 0})->all;
$c->respond_to(any => { json => $docs, status => 200});
};
# Add a new comment to a location
post '/locations/:locationid/comments' => sub {
my $c = shift;
my ($user, $pass) = split /:/, $c->req->url->to_abs->userinfo;
my $locid = $c->param('locationid');
# Get comment content
my $content = $c->req->text;
if ($content eq "") {
$c->respond_to(any => { json => {error => "No comment body"}, status => 400});
return;
}
# Look up location
my $oid = Mango::BSON::ObjectID->new($locid);
my $locations = $c->mango->db->collection('locations');
my $doc = $locations->find_one({_id => $oid});
if (not defined $doc) {
$c->respond_to(any => { json => {error => "Location not found"}, status => 404});
return;
}
# Insert comment into db
my $timestring = gmtime->datetime . 'Z';
my $comments = $c->mango->db->collection('comments');
$comments->insert({
locationid => $locid,
username => $user,
timestamp => $timestring,
content => $content
});
$c->respond_to(any => { json => {}, status => 200});
};
# Delete a comment from a location (only works if you wrote the comment)
del '/locations/:locationid/comments/:commentid' => sub {
my $c = shift;
my ($user, $pass) = split /:/, $c->req->url->to_abs->userinfo;
my $locid = $c->param('locationid');
my $commentid = $c->param('commentid');
# Look up location
my $loc_oid = Mango::BSON::ObjectID->new($locid);
my $locations = $c->mango->db->collection('locations');
my $loc_doc = $locations->find_one({_id => $loc_oid});
if (not defined $loc_doc) {
$c->respond_to(any => { json => {error => "Location not found"}, status => 404});
return;
}
# Look up comment
my $comments = $c->mango->db->collection('comments');
my $com_oid = Mango::BSON::ObjectID->new($commentid);
my $com_doc = $comments->find_one({_id => $com_oid});
if (not defined $com_doc) {
$c->respond_to(any => { json => {error => "Comment not found"}, status => 404});
return;
}
# Verify that this user actually created this comment
if ($com_doc->{username} ne $user) {
$c->respond_to(any => { json => {error => "You are not the owner of this comment"}, status => 403});
return;
}
# Remove the comment
$comments->remove($com_oid);
$c->respond_to(any => { json => {}, status => 200});
};
};
};
app->start;