-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
622 lines (557 loc) · 17.2 KB
/
handlers.go
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package main
import (
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
)
// Auth
func createAccount(c *gin.Context) {
// get sanatized parameters
username := htmlStripper.Sanitize(c.PostForm("username"))
password := htmlStripper.Sanitize(c.PostForm("password"))
// check for empty params
if strings.Trim(username, " ") == "" || strings.Trim(password, " ") == "" {
c.IndentedJSON(http.StatusBadRequest, "Empty parameters in Request Body")
return
}
// check for illegal params
if username != c.PostForm("username") || password != c.PostForm("password") {
c.IndentedJSON(http.StatusBadRequest, "Illegal username or password!")
return
}
// check is username already used
if checkUsernameExists(username) {
c.IndentedJSON(http.StatusConflict, "Username Already Exists")
return
}
authToken := registerNewUser(username, password)
data := createAccountSuccessResponse{
Username: username,
AuthToken: authToken,
}
c.IndentedJSON(http.StatusOK, data)
}
func deleteAccount(c *gin.Context) {
authToken := extractAuthToken(c)
if authToken == "" {
c.IndentedJSON(http.StatusUnauthorized, "Auth token missing!")
return
}
userId, validToken := isTokenValid(authToken)
if !validToken {
c.IndentedJSON(http.StatusUnauthorized, "Invalid Auth Token")
return
}
deleteUserAccount(userId)
c.IndentedJSON(http.StatusOK, "User Account Deleted Successfully!")
}
func updateUsername(c *gin.Context) {
authToken := extractAuthToken(c)
if authToken == "" {
c.IndentedJSON(http.StatusUnauthorized, "Auth token missing!")
return
}
userId, validToken := isTokenValid(authToken)
if !validToken {
c.IndentedJSON(http.StatusUnauthorized, "Invalid Auth Token")
return
}
// get sanatized parameters
newUsername := htmlStripper.Sanitize(c.PostForm("newUsername"))
// check for empty params
if strings.Trim(newUsername, " ") == "" {
c.IndentedJSON(http.StatusBadRequest, "Empty parameters in Request Body")
return
}
// check for illegal params
if newUsername != c.PostForm("newUsername") {
c.IndentedJSON(http.StatusBadRequest, "Illegal username provided!")
return
}
// check is username already used
if checkUsernameExists(newUsername) {
c.IndentedJSON(http.StatusConflict, "This username is already taken!")
return
}
updateUsernameById(int(userId), newUsername)
c.IndentedJSON(http.StatusOK, "Username updated successfully!")
}
func updatePassword(c *gin.Context) {
authToken := extractAuthToken(c)
if authToken == "" {
c.IndentedJSON(http.StatusUnauthorized, "Auth token missing!")
return
}
userId, validToken := isTokenValid(authToken)
if !validToken {
c.IndentedJSON(http.StatusUnauthorized, "Invalid Auth Token")
return
}
// get sanatized parameters
newPassword := htmlStripper.Sanitize(c.PostForm("newPassword"))
// check for empty params
if strings.Trim(newPassword, " ") == "" {
c.IndentedJSON(http.StatusBadRequest, "Empty parameters in Request Body")
return
}
// check for illegal params
if newPassword != c.PostForm("newPassword") {
c.IndentedJSON(http.StatusBadRequest, "Password contain illegal charachters!")
return
}
// check if same password
if checkIfSamePassword(int(userId), newPassword) {
c.IndentedJSON(http.StatusConflict, "New Password cannot be same as old Password!")
return
}
authToken = updatePasswordById(int(userId), newPassword)
data := passwordUpdateSuccessResponse{
AuthToken: authToken,
}
c.IndentedJSON(http.StatusOK, data)
}
func login(c *gin.Context) {
// get sanatized parameters
username := htmlStripper.Sanitize(c.PostForm("username"))
password := htmlStripper.Sanitize(c.PostForm("password"))
// check for empty params
if strings.Trim(username, " ") == "" || strings.Trim(password, " ") == "" {
c.IndentedJSON(http.StatusBadRequest, "Empty parameters in Request Body")
return
}
// check for illegal params
if username != c.PostForm("username") || password != c.PostForm("password") {
c.IndentedJSON(http.StatusBadRequest, "Illegal username or password!")
return
}
// check if username exists
if !checkUsernameExists(username) {
c.IndentedJSON(http.StatusConflict, "Username does not exist!")
return
}
authenticated, authToken := verifyAndLogin(username, password)
if !authenticated {
c.IndentedJSON(http.StatusUnauthorized, "Invalid login credentials!")
return
} else {
data := loginSuccessResponse{
Username: username,
AuthToken: authToken,
}
c.IndentedJSON(http.StatusOK, data)
return
}
}
func logout(c *gin.Context) {
authToken := extractAuthToken(c)
if authToken == "" {
c.IndentedJSON(http.StatusUnauthorized, "Auth token missing!")
return
}
userId, validToken := isTokenValid(authToken)
if !validToken {
c.IndentedJSON(http.StatusUnauthorized, "Invalid Auth Token")
return
}
logoutUser(userId)
c.IndentedJSON(http.StatusOK, "Logged out!")
}
// Dashboard
func getApps(c *gin.Context) {
authToken := extractAuthToken(c)
if authToken == "" {
c.IndentedJSON(http.StatusUnauthorized, "Auth token missing!")
return
}
userId, validToken := isTokenValid(authToken)
if !validToken {
c.IndentedJSON(http.StatusUnauthorized, "Invalid Auth Token")
return
}
apps := getAppsOfUser(userId)
c.IndentedJSON(http.StatusOK, apps)
}
func createApp(c *gin.Context) {
authToken := extractAuthToken(c)
if authToken == "" {
c.IndentedJSON(http.StatusUnauthorized, "Auth token missing!")
return
}
userId, validToken := isTokenValid(authToken)
if !validToken {
c.IndentedJSON(http.StatusUnauthorized, "Invalid Auth Token")
return
}
// get input name
appName := htmlStripper.Sanitize(c.PostForm("appName"))
if appName != c.PostForm("appName") {
c.IndentedJSON(http.StatusBadRequest, "Illegal app Name")
return
}
if strings.Trim(appName, " ") == "" {
c.IndentedJSON(http.StatusBadRequest, "Empty app Name is not allowed.")
return
}
app := createAppForUser(int(userId), appName)
c.IndentedJSON(http.StatusOK, app)
}
func deleteApp(c *gin.Context) {
authToken := extractAuthToken(c)
if authToken == "" {
c.IndentedJSON(http.StatusUnauthorized, "Auth token missing!")
return
}
userId, validToken := isTokenValid(authToken)
if !validToken {
c.IndentedJSON(http.StatusUnauthorized, "Invalid Auth Token")
return
}
// get input id
appId := htmlStripper.Sanitize(c.Query("appId"))
println(appId)
if appId != c.Query("appId") {
c.IndentedJSON(http.StatusBadRequest, "Illegal app Id")
return
}
intAppId, err := strconv.Atoi(appId)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "appId must be an Integer.")
return
}
if isAppOfUser(intAppId, int(userId)) {
deleteAppById(intAppId)
c.IndentedJSON(http.StatusOK, "App deleted successfully!")
return
}
c.IndentedJSON(http.StatusUnauthorized, "Unauthorized deletion!")
}
func updateApp(c *gin.Context) {
authToken := extractAuthToken(c)
if authToken == "" {
c.IndentedJSON(http.StatusUnauthorized, "Auth token missing!")
return
}
userId, validToken := isTokenValid(authToken)
if !validToken {
c.IndentedJSON(http.StatusUnauthorized, "Invalid Auth Token")
return
}
// get sanatized parameters
// get input id
appId := htmlStripper.Sanitize(c.PostForm("appId"))
newName := htmlStripper.Sanitize(c.PostForm("name"))
newHidden := htmlStripper.Sanitize(c.PostForm("hidden"))
// check for empty params
if strings.Trim(appId, " ") == "" || strings.Trim(newName, " ") == "" || strings.Trim(newHidden, " ") == "" {
c.IndentedJSON(http.StatusBadRequest, "Empty parameters in Request Body")
return
}
// check for illegal params
if newName != c.PostForm("name") || newHidden != c.PostForm("hidden") || appId != c.PostForm("appId") {
c.IndentedJSON(http.StatusBadRequest, "Illegal values provided!")
return
}
var hidden int
switch newHidden {
case "true":
hidden = 1
case "false":
hidden = 0
default:
c.IndentedJSON(http.StatusBadRequest, "Hiddden parameter must have a 'true' or 'false' value")
}
intAppId, err := strconv.Atoi(appId)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "appId must be an Integer.")
return
}
if isAppOfUser(intAppId, int(userId)) {
updateAppById(intAppId, newName, hidden)
c.IndentedJSON(http.StatusOK, "App updated successfully!")
return
}
c.IndentedJSON(http.StatusUnauthorized, "Unauthorized update!")
}
// App
func getReleases(c *gin.Context) {
authToken := extractAuthToken(c)
if authToken == "" {
c.IndentedJSON(http.StatusUnauthorized, "Auth token missing!")
return
}
userId, validToken := isTokenValid(authToken)
if !validToken {
c.IndentedJSON(http.StatusUnauthorized, "Invalid Auth Token")
return
}
// get input id
appId := htmlStripper.Sanitize(c.Query("appId"))
if appId != c.Query("appId") {
c.IndentedJSON(http.StatusBadRequest, "Illegal app Id")
return
}
i_appId, err := strconv.Atoi(appId)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "App ID must be an Integer.")
return
}
if isAppOfUser(i_appId, int(userId)) {
releases := getReleasesOfApp(i_appId)
c.IndentedJSON(http.StatusOK, releases)
return
}
c.IndentedJSON(http.StatusUnauthorized, "Unauthorized access!")
}
func createRelease(c *gin.Context) {
authToken := extractAuthToken(c)
if authToken == "" {
c.IndentedJSON(http.StatusUnauthorized, "Auth token missing!")
return
}
userId, validToken := isTokenValid(authToken)
if !validToken {
c.IndentedJSON(http.StatusUnauthorized, "Invalid Auth Token")
return
}
// get inputs
appId := htmlStripper.Sanitize(c.PostForm("appId"))
versionName := htmlStripper.Sanitize(c.PostForm("versionName"))
versionCode := htmlStripper.Sanitize(c.PostForm("versionCode"))
if appId != c.PostForm("appId") || versionName != c.PostForm("versionName") || versionCode != c.PostForm("versionCode") {
c.IndentedJSON(http.StatusBadRequest, "Illegal input parameter values")
return
}
i_appId, err := strconv.Atoi(appId)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "App ID must be an integer")
return
}
i_versionCode, err := strconv.Atoi(versionCode)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "Version Code must be an integer")
return
}
if strings.Trim(versionName, " ") == "" {
c.IndentedJSON(http.StatusBadRequest, "Empty Version Name is not allowed.")
return
}
if isAppOfUser(i_appId, int(userId)) {
if !isReleaseAlreadyPresent(i_appId, i_versionCode) {
release := createReleaseForApp(int(userId), i_appId, i_versionCode, versionName)
c.IndentedJSON(http.StatusOK, release)
return
} else {
c.IndentedJSON(http.StatusBadRequest, "This Version Code already exists")
return
}
} else {
c.IndentedJSON(http.StatusUnauthorized, "Unauthorized Request!")
return
}
}
func deleteRelease(c *gin.Context) {
authToken := extractAuthToken(c)
if authToken == "" {
c.IndentedJSON(http.StatusUnauthorized, "Auth token missing!")
return
}
userId, validToken := isTokenValid(authToken)
if !validToken {
c.IndentedJSON(http.StatusUnauthorized, "Invalid Auth Token")
return
}
// get input id
releaseId := htmlStripper.Sanitize(c.Query("releaseId"))
if releaseId != c.Query("releaseId") {
c.IndentedJSON(http.StatusBadRequest, "Illegal Release Id")
return
}
i_releaseId, err := strconv.Atoi(releaseId)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "releaseId must be an Integer.")
return
}
if isReleaseOfUser(i_releaseId, int(userId)) {
deleteAppRelease(i_releaseId)
c.IndentedJSON(http.StatusOK, "Release deleted successfully!")
return
}
c.IndentedJSON(http.StatusUnauthorized, "Delete Request Unauthorized!")
}
func updateRelease(c *gin.Context) {
authToken := extractAuthToken(c)
if authToken == "" {
c.IndentedJSON(http.StatusUnauthorized, "Auth token missing!")
return
}
userId, validToken := isTokenValid(authToken)
if !validToken {
c.IndentedJSON(http.StatusUnauthorized, "Invalid Auth Token")
return
}
// get sanatized parameters
// get input id
releaseId := htmlStripper.Sanitize(c.Request.PostFormValue("releaseId"))
newName := htmlStripper.Sanitize(c.Request.PostFormValue("versionName"))
newCode := htmlStripper.Sanitize(c.Request.PostFormValue("versionCode"))
newHidden := htmlStripper.Sanitize(c.Request.PostFormValue("hidden"))
data := htmlStripper.Sanitize(c.Request.PostFormValue("data"))
println(releaseId, newName, newCode, newHidden, data)
// check for empty params
if strings.Trim(releaseId, " ") == "" || strings.Trim(newName, " ") == "" || strings.Trim(newHidden, " ") == "" || strings.Trim(newCode, " ") == "" {
c.IndentedJSON(http.StatusBadRequest, "Empty parameters in Request Body")
return
}
// check for illegal params
if newName != c.Request.PostFormValue("versionName") || newCode != c.Request.PostFormValue("versionCode") || newHidden != c.Request.PostFormValue("hidden") || releaseId != c.Request.PostFormValue("releaseId") || data != c.Request.PostFormValue("data") {
c.IndentedJSON(http.StatusBadRequest, "Illegal values provided!")
return
}
var hidden int
switch newHidden {
case "true":
hidden = 1
case "false":
hidden = 0
default:
c.IndentedJSON(http.StatusBadRequest, "Hiddden parameter must have a 'true' or 'false' value")
return
}
intReleaseId, err := strconv.Atoi(releaseId)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "Release Id must be an Integer.")
return
}
intVersionCode, err := strconv.Atoi(newCode)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "Version Code must be an Integer.")
return
}
if isReleaseOfUser(intReleaseId, int(userId)) {
updateReleaseById(intReleaseId, newName, intVersionCode, data, hidden)
c.IndentedJSON(http.StatusOK, "Release Details updated successfully!")
return
}
c.IndentedJSON(http.StatusUnauthorized, "Unauthorized update!")
}
// Release
func getReleaseNotes(c *gin.Context) {
// unprotected endpoint
// 2 methods, ordered by priority:
// 1) Directly by release id
// 2) By app id and version code (latest keyword allowed)
releaseId := htmlStripper.Sanitize(c.Query("releaseId"))
appId := htmlStripper.Sanitize(c.Query("appId"))
versionCode := htmlStripper.Sanitize(c.Query("versionCode"))
if releaseId != "" {
if releaseId != c.Query("releaseId") {
c.IndentedJSON(http.StatusBadRequest, "Illegal Release ID")
return
}
releaseId, err := strconv.Atoi(releaseId)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "Release ID must be an Integer!")
return
}
releaseNotes, exists := getReleaseNotesOfRelease(releaseId)
if !exists {
c.IndentedJSON(http.StatusNotFound, "Release Notes not found!")
return
}
c.IndentedJSON(http.StatusOK, releaseNotes)
return
} else if appId != "" && versionCode != "" {
latestFlag := false
if appId != c.Query("appId") || versionCode != c.Query("versionCode") {
c.IndentedJSON(http.StatusBadRequest, "Illegal App ID or Version Code")
return
}
appId, err := strconv.Atoi(appId)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "App ID must be an Integer!")
return
}
i_versionCode := -1
if versionCode == "latest" {
latestFlag = true
} else {
versionCode, err := strconv.Atoi(versionCode)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "Invalid Version Code")
return
}
i_versionCode = versionCode
}
notes, exists := getReleaseNotesByAppIdAndVersionCode(appId, i_versionCode, latestFlag)
if !exists {
c.IndentedJSON(http.StatusNotFound, "Release Notes not found!")
return
}
c.IndentedJSON(http.StatusOK, notes)
return
}
c.IndentedJSON(http.StatusBadRequest, "Missing Parameters!")
}
func updateReleaseNotes(c *gin.Context) {
authToken := extractAuthToken(c)
if authToken == "" {
c.IndentedJSON(http.StatusUnauthorized, "Auth token missing!")
return
}
userId, validToken := isTokenValid(authToken)
if !validToken {
c.IndentedJSON(http.StatusUnauthorized, "Invalid Auth Token")
return
}
// get sanatized parameters
// get input id
releaseId := htmlStripper.Sanitize(c.Request.PostFormValue("releaseId"))
notesTxt := htmlStripper.Sanitize(c.Request.PostFormValue("notesTxt"))
notesMd := notesSanitizer.Sanitize(c.Request.PostFormValue("notesMd"))
notesHtml := notesSanitizer.Sanitize(c.Request.PostFormValue("notesHtml"))
println("notesTxt: ", notesTxt)
// check for empty params
if strings.Trim(releaseId, " ") == "" {
c.IndentedJSON(http.StatusBadRequest, "Missing Release ID")
return
}
// check for illegal params
if releaseId != c.Request.PostFormValue("releaseId") {
c.IndentedJSON(http.StatusBadRequest, "Illegal values for Release ID provided!")
return
}
intReleaseId, err := strconv.Atoi(releaseId)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "Release Id must be an Integer.")
return
}
if isReleaseOfUser(intReleaseId, int(userId)) {
updateReleaseNotesById(intReleaseId, notesTxt, notesMd, notesHtml)
c.IndentedJSON(http.StatusOK, "Release Notes updated successfully!")
return
}
c.IndentedJSON(http.StatusUnauthorized, "Unauthorized update!")
}
// frontend
func getHomePage(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
}
func getSignupPage(c *gin.Context) {
c.HTML(http.StatusOK, "signup.html", nil)
}
func getLoginPage(c *gin.Context) {
c.HTML(http.StatusOK, "login.html", nil)
}
func getDashboardPage(c *gin.Context) {
c.HTML(http.StatusOK, "dashboard.html", nil)
}
func getProfilePage(c *gin.Context) {
c.HTML(http.StatusOK, "profile.html", nil)
}
func getAppPage(c *gin.Context) {
c.HTML(http.StatusOK, "app.html", nil)
}
func getReleasePage(c *gin.Context) {
c.HTML(http.StatusOK, "release.html", nil)
}