-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
337 lines (304 loc) · 13 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
const path = require('path')
require('dotenv').config({ path: path.join(__dirname, '.env') })
const winston = require('winston')
winston.level = process.env.LOG_LEVEL
// { error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5 }
// setting level to error will show only winston.error()
// setting level to info will show winston.info(), winston.warn() and winston. error()
// setting level to debug will show winston.info(), winston.verbose(), winston.info(), winston.warn() and winston.error()
// you get the idea
const db = require('./db')
const express = require('express')
const app = express()
var server = app.listen(9886)
const WebSocketServer = require('ws').Server
const wss = new WebSocketServer({ server : server })
app.use(express.static(path.join(__dirname, 'public')))
var bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
var auth = require('./routes/auth')
app.use('/', auth)
const jwt = require('jsonwebtoken')
var authUser = {}
var authenticatedClients = {}
// from: https://stackoverflow.com/a/46878342/4932305
wss.getUniqueID = () => {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)
}
return s4() + s4() + '-' + s4()
}
wss.on('connection', client => {
client.id = wss.getUniqueID()
client.sendJSON = obj => {
client.send(JSON.stringify(obj))
}
client.on('message', message => {
try {
var receivedJSON = JSON.parse(message)
winston.verbose(receivedJSON)
var authToken = receivedJSON.authToken
var method = receivedJSON.method
var payload = receivedJSON.payload
if(checkAuth(authToken)) {
// assign the key value to itself if it exists, else assign an empty array
authenticatedClients[authUser.id] = authenticatedClients[authUser.id] || []
if(authenticatedClients[authUser.id].length == 0) {
authenticatedClients[authUser.id].push(client)
}
var clientAlreadyExists = false
authenticatedClients[authUser.id].forEach(preExistingClient => {
if(client.id == preExistingClient.id) {
clientAlreadyExists = true
}
})
if(!clientAlreadyExists) {
authenticatedClients[authUser.id].push(client)
}
if(method) {
switch(method) {
case 'get-links':
winston.info({ method: 'get-links', client: client.id })
getLinks(client)
break
case 'add-link':
winston.info({ method: 'add-link', payload: payload, client: client.id })
addLink(payload)
break
case 'add-links':
winston.info({ method: 'add-links', payload: payload, client: client.id })
addLinks(payload)
break
case 'delete-link':
winston.info({ method: 'delete-link', payload: payload, client: client.id })
deleteLink(payload, client)
break
case 'change-link-group':
winston.info({ method: 'change-link-group', payload: payload, client: client.id })
changeLinkGroup(payload, client)
break
case 'rename-link-group':
winston.info({ method: 'rename-link-group', payload: payload, client: client.id })
renameLinkGroup(payload, client)
break
}
}
} else {
if(method && payload) {
client.sendJSON({ event: 'need-valid-token', payload: { method: method, payload: payload } })
} else if(method && !payload) {
client.sendJSON({ event: 'need-valid-token', payload: { method: method } })
} else {
client.sendJSON({ event: 'need-valid-token' })
}
}
} catch(err) {
if(err instanceof SyntaxError) {
winston.warn('Invalid JSON received:', message)
} else {
winston.error(err)
}
}
})
client.on('close', () => {
if(Object.keys(authUser).length !== 0) {
authenticatedClients[authUser.id] = authenticatedClients[authUser.id].filter(preExistingClient => preExistingClient.id !== client.id)
}
})
// need this so the server doesn't crash
// see: https://github.com/websockets/ws/issues/1256#issuecomment-352288884
// also useful when close isn't sent by the client
client.on('error', error => {
if(Object.keys(authUser).length !== 0) {
authenticatedClients[authUser.id] = authenticatedClients[authUser.id].filter(preExistingClient => preExistingClient.id !== client.id)
}
})
})
function checkAuth(authToken, middlewareRequestBody=null) {
if(authToken) {
try {
var decoded = jwt.verify(authToken, process.env.JWT_SECRET)
authUser = decoded
if(middlewareRequestBody) {
middlewareRequestBody.authUser = decoded
}
return true
} catch(err) {
if(err.name == 'JsonWebTokenError') {
return false
} else if(err.name == 'TokenExpiredError') {
winston.info('JWT Token Expired')
} else {
winston.error(err)
}
}
}
}
// WebSocket Methods
function getLinks(client) {
db.query('SELECT * FROM links WHERE user_id = $1 ORDER BY updated_at DESC', [authUser.id])
.then(rows => {
var group_to_values = rows.reduce((obj, item) => {
obj[item.link_group_id] = obj[item.link_group_id] || []
obj[item.link_group_id].push(item)
return obj;
}, {})
var groups = Object.keys(group_to_values).map(key => {
return { linkGroup: { id: key }, links: group_to_values[key] }
})
db.query('SELECT * FROM link_groups WHERE user_id = $1', [authUser.id])
.then(rows2 => {
rows2.forEach(row => {
groups.forEach(group => {
if(group.linkGroup.id == row.id) {
group.linkGroup = row
}
})
})
// payload == links
client.sendJSON({ event: 'receive-links', payload: { linkGroups: groups.reverse(), linkCount: rows.length } })
})
.catch(error => winston.error(error.stack))
})
.catch(error => winston.error(error.stack))
}
// payload == linkObject { title: title, link: link }
async function addLink(payload, userId=null) {
if(userId === null) {
userId = authUser.id
}
try {
var linkGroupId = null
try {
var lastAddedLink = await db.one('SELECT link_group_id FROM links WHERE user_id = $1 ORDER BY id DESC LIMIT 1', [userId])
linkGroupId = lastAddedLink.link_group_id
} catch(error) {
if(error.name === 'QueryResultError') {
var newLinkGroup = await db.one('INSERT INTO link_groups(user_id) VALUES ($1) RETURNING id', [userId])
linkGroupId = newLinkGroup.id
} else {
winston.error(error)
}
}
await db.none('INSERT INTO links(title, link, link_group_id, user_id) VALUES ($1, $2, $3, $4)', [payload.title, payload.link, linkGroupId, userId])
authenticatedClients[userId].forEach(client => {
client.sendJSON({ event: 'link-added' })
})
} catch(error) {
winston.error(error)
}
}
// payload == linkObject Array
async function addLinks(payload, userId=null) {
if(userId === null) {
userId = authUser.id
}
try {
var linkGroup = await db.one('INSERT INTO link_groups(user_id) VALUES ($1) RETURNING id', [userId])
for(let link of payload) {
await db.none('INSERT INTO links(title, link, link_group_id, user_id) VALUES ($1, $2, $3, $4)', [link.title, link.link, linkGroup.id, userId])
}
authenticatedClients[userId].forEach(client => {
client.sendJSON({ event: 'links-added' })
})
} catch(error) {
winston.error(error)
}
}
// payload == linkId
async function deleteLink(payload, client) {
try {
var result = await db.result('DELETE FROM links WHERE id = $1 AND user_id = $2 RETURNING link_group_id', [payload, authUser.id])
if(result.rowCount !== 0) {
authenticatedClients[authUser.id].forEach(client => {
winston.info({ event: 'link-deleted', payload: payload, client: client.id })
client.sendJSON({ event: 'link-deleted', payload: payload })
})
// housekeeping by deleting linkGroup of the just deleted link if there's no other links associated to it
var linkGroupId = result.rows[0].link_group_id
var linkGroupAssociatedLinks = await db.result('SELECT EXISTS (SELECT 1 FROM links WHERE link_group_id = $1)', [linkGroupId])
if(!linkGroupAssociatedLinks.rows[0].exists) {
db.none('DELETE from link_groups WHERE id = $1', [linkGroupId])
}
// housekeeping by deleting linkGroup of the just deleted link if there's no other links associated to it
} else {
winston.info({ event: 'link-already-deleted', payload: payload, client: client.id })
client.sendJSON({ event: 'link-already-deleted', payload: payload })
}
} catch(error) {
winston.error(error)
}
}
// payload == { linkId: linkId, oldLinkGroupId: oldLinkGroupId, newLinkGroupId: newLinkGroupId }
async function changeLinkGroup(payload, client) {
try {
await db.none('UPDATE links SET link_group_id = $1 WHERE id = $2 AND user_id = $3', [payload.newLinkGroupId, payload.linkId, authUser.id])
authenticatedClients[authUser.id].forEach(client => {
winston.info({ event: 'link-updated', payload: payload.linkId, client: client.id })
client.sendJSON({ event: 'link-updated', payload: payload.linkId })
})
// housekeeping by deleting linkGroup of the just updated link if there's no other links associated to it
var linkGroupAssociatedLinks = await db.result('SELECT EXISTS (SELECT 1 FROM links WHERE link_group_id = $1)', [payload.oldLinkGroupId])
if(!linkGroupAssociatedLinks.rows[0].exists) {
db.none('DELETE from link_groups WHERE id = $1', [payload.oldLinkGroupId])
}
// housekeeping by deleting linkGroup of the just updated link if there's no other links associated to it
} catch(error) {
winston.error(error)
}
}
// payload == { linkGroupId: linkGroupId, linkGroupName:linkGroupName }
async function renameLinkGroup(payload, client) {
try {
await db.none('UPDATE link_groups SET title = $1 WHERE id = $2 AND user_id = $3', [payload.linkGroupName, payload.linkGroupId, authUser.id])
authenticatedClients[authUser.id].forEach(client => {
winston.info({ event: 'link-group-updated', payload: payload, client: client.id })
client.sendJSON({ event: 'link-group-updated', payload: payload })
})
} catch(error) {
winston.error(error)
}
}
function checkAuthMiddleware(req, res, next) {
if(!checkAuth(req.header('authToken'), req.body)) {
return res.json({
success: false,
message: 'Authentication failed'
})
}
next()
}
var apiKeyRoutes = require('./routes/api-key')
app.use('/api-key', checkAuthMiddleware, apiKeyRoutes)
async function checkIfValidAPIToken(req) {
if(req.body.username && req.body.apiKey) {
try {
let user = await db.one('SELECT id FROM users WHERE username = $1', [req.body.username])
await db.one('SELECT id FROM api_keys WHERE api_key = $1 and user_id = $2', [req.body.apiKey, user.id])
return user.id
} catch(error) {
return false
}
}
return false
}
// request body must be of the format { username: username, apiKey: apiKey, title: title, link: link }
app.post('/add-link', async(req, res, next) => {
try {
let userId = await checkIfValidAPIToken(req)
if(userId) {
if(req.body.title && req.body.link) {
await addLink(req.body, userId)
res.send('Success')
} else {
res.send('Invalid link format')
}
} else {
res.send('Invalid API Key or Username')
}
next()
} catch(error) {
next(error)
}
})