-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
504 lines (405 loc) · 15.5 KB
/
index.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
//Connections
require("dotenv").config();
const express = require("express");
const connectDB = require("./connection");
const patientModel = require("./patient");
//secret handling
const secret = process.env.CRYPTO_SECRET;
const adminkey = process.env.ADMINKEY;
const crypto = require("crypto").createHmac;
const app = express();
//Important Headers for public uses
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "*");
next();
});
// configuration
app.use(express.json());
// GET
// route: /<adminkey>
// description: User's Master Data
// e-parameter: Admin Key accessible and regenerated by maintainer
app.get("/"+adminkey, async (req, res) => {
const patient = await patientModel.find();
if (!patient){
return res.json ({message: "Not Avalaible"});
}
return res.json({patient});
});
// GET
// route: /patient/:_id
// description: To get patient with 'id'
// e-parameter: _id
app.get("/patient/:_id", async (req, res) => {
try{
const { _id } = req.params;
const patient = await patientModel.findById(_id);
if (!patient){
return res.json ({message: "invalid ID"});
}
return res.json ({ patient });
}
catch(error){
return res.status(500).json({error: error.message});
}
});
// GET
// route: /geo_locate/:user
// description: To get Geo-API key, used for third party API service
// e-parameter: user
app.get("/geo_locate/:user", async (req, res) => {
try{
const { user } = req.params;
const patient = await patientModel.find({user: user});
if (!patient){
return res.json ({message: "invalid user"});
}
let api = process.env.GEO_API;
return res.json ({geo_api: api});
}
catch(error){
return res.status(500).json({error: error.message});
}
});
// GET
// route: /devtkn/portal
// description: (On Portal) To get MQTT-server URL, userName and device-token, used for third party MQTT service
// q-parameter: user & token
app.get("/devtkn/portal", async (req, res) => {
try{
const body = req.query;
const user = body.user;
const ch_token = body.token;
const patient = await patientModel.find({user: user});
if (!patient){
return res.json ({message: "User not Found"});
}
else{
const valpass = patient[0].pass;
//JIT(Just-in-time) handler
var date_ob = new Date();
var moment = date_ob.getDate()+'-'+date_ob.getMonth()+'/'+date_ob.getHours();
const token = process.env.CRYPTO_TOKEN + moment;
const auth_token = await crypto("sha256", token).update(valpass).digest("hex");
if (auth_token == ch_token)
{
let mqttserver = process.env.MQTTSERVER;
let mqttUser = process.env.MQTTUSER;
let mqttPass = process.env.MQTTPASS;
return res.json ({mqttserver: mqttserver, mqttUser: mqttUser, mqttPass: mqttPass});
}
else{
return res.json ({message: "Token does not match. Try to Login Again."});
}
}
}
catch(error){
return res.status(500).json({error: error.message});
}
});
// GET
// route: /devtkn/create
// description: To create new device token - stored in DB and used by device to receive mqtt details
// q-parameter: user & pass
app.get("/devtkn/create", async (req, res) => {
try{
const body = req.query;
const pass = body.pass;
const user = body.user;
const patient_pass = await crypto("sha256", secret).update(pass).digest("hex");
const patient = await patientModel.find({user: user, pass: patient_pass});
const check = (patient == []);
if (!check){
const valpass = patient[0].pass;
const valuser = patient[0].user;
if ((valpass == patient_pass) && (valuser == user)){
//JIT(Just-in-time) handler
var date_ob = new Date();
var moment_s = date_ob.getDate()+'-'+date_ob.getMonth()+'/'+date_ob.getHours()+':'+date_ob.getMinutes()+':'+date_ob.getSeconds();
let tkn = await crypto("sha256", moment_s).update(pass).digest("hex");
const filter = { user: user };
const update = { devtoken: tkn };
let doc = await patientModel.findOneAndUpdate(filter, update, {
new: true
});
return res.json ({device_token: tkn, time: moment_s});
}
}
}
catch(error){
return res.status(500).json({error: error.message});
}
});
// GET
// route: /devtkn/device
// description: (On Device) To get MQTT-server URL, userName and password, used for third party MQTT service
// q-parameter: user & devtoken
app.get("/devtkn/device", async (req, res) => {
try {
const body = req.query;
const dev_token = body.token;
const user = body.user;
const patient = await patientModel.find({user: user, devtoken: dev_token});
const check = (patient == []);
if (!check){
const valtoken = patient[0].devtoken;
const valuser = patient[0].user;
if ((valtoken == dev_token) && (valuser == user)){
let mqttserver = process.env.MQTTSERVER;
let mqttUser = process.env.MQTTUSER;
let mqttPass = process.env.MQTTPASS;
return res.json ({mqttserver: mqttserver, mqttUser: mqttUser, mqttPass: mqttPass});
}
}
}
catch(error) {
return res.status(500).json({error: error.message});
}
});
// POST
// route: /node/create
// description: To create NEW node device, node-xxxxxxx to be stored and used for data-exchange
// q-parameter: user & dev_token
// body:
// {
// "nodeData":{
// "devices":
// {
// "node": "node-xxxxxx",
// "type": "xxxxxx",
// "attribute": "xxxx",
// "lastUp": "xx/xx/xx"
// }
// }
// }
app.post("/node/create", async (req, res) => {
try {
const quer = req.query;
const dev_token = quer.token;
const user = quer.user;
const { nodeData } = req.body;
const patient = await patientModel.find({user: user, devtoken: dev_token});
const check = (patient == []);
if (!check){
const valtoken = patient[0].devtoken;
const valuser = patient[0].user;
if ((valtoken == dev_token) && (valuser == user)){
const filter = { user: user };
const updatepatient = await patientModel.findOneAndUpdate(
filter,
{ $push: nodeData},
{ new: true}
);
return res.json ({message: "Node Created 🎆"});
}
}
}
catch(error) {
return res.status(500).json({error: error.message});
}
});
// GET
// route: /node/device
// description: To view node-xxxxxxx and attributes to be stored and used for data-exchange
// q-parameter: user, dev_token & node-xxxxxxxx
app.get("/node/device", async (req, res) => {
try {
const quer = req.query;
const dev_token = quer.token;
const user = quer.user;
const node = "node-"+quer.node;
const patient = await patientModel.find({user: user, devtoken: dev_token});
const check = (patient == []);
if (!check){
const valtoken = patient[0].devtoken;
const valuser = patient[0].user;
if ((valtoken == dev_token) && (valuser == user)){
const devarr = patient[0].devices;
for(i=0; i<10; i++){
if (devarr[i].node == node){
return res.json ({node: devarr[i].node, attribute: devarr[i].attribute});
}
}
return res.json ({message: "Node not Found"});
}
}
}
catch(error) {
if (error.message == "Cannot read property 'node' of undefined"){
return res.status(500).json ({message: "Node not Found"});
}
return res.status(500).json({error: error.message});
}
});
// GET
// route: /data
// description: To get patient data with 'user' & 'token'
// q-parameter: user & token (generated by respose during login)
app.get("/data", async (req, res) => {
try{
const body = req.query;
const user = body.user;
const ch_token = body.token;
const patient = await patientModel.find({user: user});
if (!patient){
return res.status(500).json ({message: "No Data Found"});
}
else{
const valpass = patient[0].pass;
//JIT(Just-in-time) handler
var date_ob = new Date();
var moment = date_ob.getDate()+'-'+date_ob.getMonth()+'/'+date_ob.getHours();
const token = process.env.CRYPTO_TOKEN + moment;
const auth_token = await crypto("sha256", token).update(valpass).digest("hex");
if (auth_token == ch_token)
{
return res.json ({ patient });
}
else{
return res.status(500).json ({message: "Token does not match. Try to Login Again."});
}
}
}
catch(error){
return res.status(500).json({error: error.message});
}
});
// GET
// route: /login
// description: To get JIT token with 'user' & 'pass'
// q-parameter: username and password
app.get("/login", async (req, res) => {
try {
const body = req.query;
const pass = body.pass;
const user = body.user;
const patient_pass = await crypto("sha256", secret).update(pass).digest("hex");
const patient = await patientModel.find({user: user, pass: patient_pass});
const check = (patient == []);
if (!check){
const valpass = patient[0].pass;
const valuser = patient[0].user;
if ((valpass == patient_pass) && (valuser == user)){
const uid = patient[0]._id;
//JIT(Just-in-time) handler
var date_ob = new Date();
var moment = date_ob.getDate()+'-'+date_ob.getMonth()+'/'+date_ob.getHours();
const token = process.env.CRYPTO_TOKEN + moment;
const auth_token = await crypto("sha256", token).update(valpass).digest("hex");
return res.json({token: auth_token});
}
else{
return res.status(500).json({error: error.message});
}
}
else{
return res.status(500).json({error: error.message});
}
return res.json (patient);
}
catch(error) {
return res.status(500).json({error: error.message});
}
});
// POST
// route: /patient/signup
// description: To add new patient
// request body: patient object
app.post("/patient/signup", async(req, res) => {
try{
var { newpatient } = req.body;
const pass = newpatient.pass;
const user = newpatient.user;
const username_check = await patientModel.find({user: user});
if (!(username_check[0]===undefined)){
return res.status(500).json({error: "Username exists"});
}
const patient_pass = await crypto("sha256", secret).update(pass).digest("hex");
await patientModel.create(newpatient);
const filter = { user: user };
const update = { pass: patient_pass };
let doc = await patientModel.findOneAndUpdate(filter, update, {
new: true
});
const { _id } = res.json;
return res.json({message: "Patient Created", uid: _id});
}
catch(error){
return res.status(500).json({error: error.message});
}
});
// PUT
// route: /patient/update/:_id
// description: To update a patient
// e-parameter: _id
// request body: patient object
app.put("/patient/update/:_id", async (req, res) => {
try {
const { _id } = req.params;
const { patientData } = req.body;
const updatepatient = await patientModel.findByIdAndUpdate(
_id,
{ $set: patientData},
{ new: true}
);
return res.json({patient: updatepatient});
}
catch(error){
return res.status(500).json({error: error.message});
}
});
// DELETE
// route: /patient/delete/:_id
// description: To delete a patient
// e-parameter:
app.delete("/patient/delete/:_id", async (req, res) => {
try{
const { _id } = req.params;
await patientModel.findByIdAndDelete(_id);
return res.json({message: "Patient Deleted 🔪"});
}
catch(error){
return res.status(500).json({error: error.message});
}
});
// DELETE
// route: /node/delete
// description: To delete a node device of a user
// q-parameter: user, dev_token, node-ID
app.delete("/node/delete", async (req, res) => {
try{
const quer = req.query;
const dev_token = quer.token;
const user = quer.user;
const node = quer.node;
const patient = await patientModel.find({user: user, devtoken: dev_token});
const check = (patient == []);
if (!check){
const valtoken = patient[0].devtoken;
const valuser = patient[0].user;
if ((valtoken == dev_token) && (valuser == user)){
const filter = { user: user };
const updatepatient = await patientModel.findOneAndUpdate(
filter,
{ $pull: { devices: { $gte: {node: node} } }}
);
return res.json ({message: "Node Deleted 🔪"});
}
}
}
catch(error){
return res.status(500).json({error: error.message});
}
});
//Handle all other requests on the server unmatched with above requests
app.get('*', function(req, res){
res.sendFile(__dirname+'/404.html');
});
//Connecting server to database
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => connectDB()
.then(() => console.log("Server is Running 🚀"))
.catch((error) => console.log(error))
);