-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
154 lines (138 loc) · 4.28 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
"use strict";
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static('public'));
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const jwt = require('jwt-simple');
const BearerStrategy = require('passport-http-bearer').Strategy;
const SECRET = 'mysecret';
var loginDetails = {"admin" : "password", "testuser" : "password"};
var people = {"testuser" : {"username":"testuser", "forename":"Delia", "surname":"Derbyshire", "password":"password", "email": "test@user.com"}}
var dowrickEvents = [{
title: 'Dowrick Event 1',
start: '2019-01-31T13:00:00',
end: '2019-01-31T14:14:00'
},
{
title: 'Dowrick Event 2',
start: '2019-01-31T15:15:00',
end: '2019-01-31T16:30:00'
}];
var mashEvents = [{
title: 'Mash Event 1',
start: '2019-02-01T08:45:00',
end: '2019-02-01T12:00:00'
}];
var gymEvents = [{
title: 'Gym Event 2',
start: '2019-02-02T08:45:00',
end: '2019-02-02T18:00:00'
},
{
title: 'Gym Event 1',
start: '2019-01-30T08:45:00',
end: '2019-01-30T18:00:00'
}
];
var events = {"dowrick" : dowrickEvents, "mash" : mashEvents, "gym" : gymEvents}
app.get('/events/:room', function(req, resp){
resp.send(events[req.params.room]);
})
passport.use(new LocalStrategy((username, password, done) => {
if (username in people && password === people[username].password) {
done(null, jwt.encode({ username }, SECRET));
return;
}
done(null, false);
}));
passport.use(new BearerStrategy((token, done) => {
try {
const { username } = jwt.decode(token, SECRET);
if (username in loginDetails) {
done(null, username);
return;
}
done(null, false);
} catch (error) {
done(null, false);
}
}));
app.post('/login', passport.authenticate('local', { session: false }),
(req, res) => {
res.send({
token: req.user,
});
},
);
app.post('/event', function(req,resp){
var clash = false;
var response = {};
var room = req.body.room;
const roomEvents = events[room];
var event = req.body;
delete event.room;
if(req.body.access_token == 'concertina'){
if (event.start >= event.end){
clash = true;
} else {
roomEvents.forEach(function(element){
if (element.start.substring(0,10) == event.start.substring(0,10)){
if((event.start <= element.start && event.end > element.start) || (event.start < element.end && event.end >= element.end) || (event.start >= element.start && event.end <= element.end)) {
clash = true;
}
}
})
}
if (!clash) {
events[room].push(event);
response = { "result" : events[room]};
} else {
response = {"result" : "clash"};
}
} else {
resp.statusCode = 403;
}
resp.send(response);
});
app.get('/people', function(req, resp){
resp.send(people);
})
app.get('/people/:username', function(req, resp){
resp.send(people[req.params.username]);
})
app.post('/checkusername', function(req, resp){
var response = {"result" : "free"}
if (req.body.username in people) {
response.result = "taken";
}
resp.send(response);
})
app.post('/people', function(req, resp){
var newPerson = req.body;
var result = JSON.stringify("new person hasn't been added");
resp.statusCode = 403;
if(newPerson.access_token == 'concertina'){
if(!(newPerson.username in people)){
delete newPerson.access_token;
people[newPerson.username]=newPerson;
result = JSON.stringify("new person added" + newPerson.username);
resp.statusCode = 200;
} else {
resp.statusCode = 400;
}
} else if (req.headers.access_token == 'concertina') {
newPerson = {"username":req.headers.username, "forename":req.headers.forename, "surename":req.headers.surname, "password":req.headers.password, "email": req.headers.email}
if(!(newPerson.username in people)){
people[req.headers.username]=newPerson;
result = JSON.stringify("new person added" + newPerson.username);
resp.statusCode = 200;
} else {
resp.statusCode = 400;
}
}
resp.send(result);
});
module.exports = app;