/
router.go
435 lines (399 loc) · 11.7 KB
/
router.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
// Package api contains all the required methods and structs for serving mouthful requests.
package api
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/gofrs/uuid"
"github.com/markbates/goth/gothic"
"github.com/patrickmn/go-cache"
"github.com/vkuznecovas/mouthful/api/model"
cfg "github.com/vkuznecovas/mouthful/config"
configModel "github.com/vkuznecovas/mouthful/config/model"
"github.com/vkuznecovas/mouthful/db/abstraction"
dbModel "github.com/vkuznecovas/mouthful/db/model"
"github.com/vkuznecovas/mouthful/global"
"github.com/vkuznecovas/mouthful/oauth/provider"
)
// Router handles all the different routes as well as stores our config and db objects
type Router struct {
db *abstraction.Database
config *configModel.Config
cache *cache.Cache
clientConfig *configModel.ClientConfig
adminConfig *configModel.AdminConfig
providers map[string]*provider.Provider
}
// SetProviders sets the OAUTH providers for the router
func (r *Router) SetProviders(input map[string]*provider.Provider) {
r.providers = input
}
// OAuth initializes the OAuth flow by redirecting the user to the providers login page
func (r *Router) OAuth(c *gin.Context) {
q := c.Request.URL.Query()
q.Add("provider", c.Param("provider"))
c.Request.URL.RawQuery = q.Encode()
gothic.BeginAuthHandler(c.Writer, c.Request)
}
// OAuthCallback handles the oauth callback which finishes the auth procedure. It checks for the admin flag for the user, and if found it will set the user as admin for the rest of the session
func (r *Router) OAuthCallback(c *gin.Context) {
q := c.Request.URL.Query()
provider := c.Param("provider")
q.Add("provider", provider)
c.Request.URL.RawQuery = q.Encode()
user, err := gothic.CompleteUserAuth(c.Writer, c.Request)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
for _, v := range r.providers[provider].AdminUserIds {
if user.UserID == v {
session := sessions.Default(c)
session.Set("isAdmin", true)
session.Save()
}
}
c.Redirect(307, *r.config.Moderation.OAuthCallbackOrigin)
}
// New returns a new instance of router
func New(db *abstraction.Database, config *configModel.Config, cache *cache.Cache) *Router {
clientConfig := cfg.TransformConfigToClientConfig(config)
adminConfig := cfg.TransformToAdminConfig(config)
r := Router{db: db, config: config, cache: cache, clientConfig: clientConfig, adminConfig: adminConfig}
return &r
}
// Status responds with 200 when asked
func (r *Router) Status(c *gin.Context) {
c.JSON(200, gin.H{
"message": "OK",
})
}
// GetClientConfig returns the client config portion
func (r *Router) GetClientConfig(c *gin.Context) {
c.JSON(200, *r.clientConfig)
}
// GetAdminConfig returns the admin config portion
func (r *Router) GetAdminConfig(c *gin.Context) {
c.JSON(200, *r.adminConfig)
}
// GetComments returns the comments from thread that is passed as query parameter uri
func (r *Router) GetComments(c *gin.Context) {
path := c.Query("uri")
if path == "" {
c.AbortWithStatusJSON(400, global.ErrThreadNotFound.Error())
return
}
path = NormalizePath(path)
if r.cache != nil {
if cacheHit, found := r.cache.Get(path); found {
jsonString := cacheHit.(*[]byte)
c.Writer.Header().Set("X-Cache", "HIT")
c.Data(200, "application/json; charset=utf-8", *jsonString)
return
}
}
db := *r.db
comments, err := db.GetCommentsByThread(path)
if err != nil {
if err == global.ErrThreadNotFound {
c.AbortWithStatusJSON(404, global.ErrThreadNotFound.Error())
return
}
log.Println(err)
c.AbortWithStatusJSON(500, global.ErrInternalServerError.Error())
return
}
if comments != nil {
js, err := json.Marshal(comments)
if err != nil {
c.JSON(500, global.ErrInternalServerError.Error())
return
}
if r.cache != nil {
r.cache.Set(path, &js, cache.DefaultExpiration)
c.Writer.Header().Set("X-Cache", "MISS")
}
if len(comments) > 0 {
c.Data(200, "application/json; charset=utf-8", js)
} else {
c.JSON(404, global.ErrThreadNotFound.Error())
}
return
}
c.AbortWithStatusJSON(404, global.ErrThreadNotFound.Error())
}
// GetAllThreads returns an array of threads
func (r *Router) GetAllThreads(c *gin.Context) {
if !r.isAdmin(c) {
c.AbortWithStatusJSON(401, global.ErrUnauthorized.Error())
return
}
db := *r.db
threads, err := db.GetAllThreads()
if err != nil {
log.Println(err)
c.AbortWithStatusJSON(500, global.ErrInternalServerError.Error())
return
}
if threads == nil {
threads = make([]dbModel.Thread, 0)
}
c.JSON(200, threads)
}
// GetAllComments returns an array of comments
func (r *Router) GetAllComments(c *gin.Context) {
if !r.isAdmin(c) {
c.AbortWithStatusJSON(401, global.ErrUnauthorized.Error())
return
}
db := *r.db
comments, err := db.GetAllComments()
if err != nil {
log.Println(err)
c.AbortWithStatusJSON(500, global.ErrInternalServerError.Error())
return
}
if comments == nil {
comments = make([]dbModel.Comment, 0)
}
c.JSON(200, comments)
}
// CreateComment creates a comment from CreateCommentBody in JSON form
func (r *Router) CreateComment(c *gin.Context) {
var createCommentBody model.CreateCommentBody
err := c.BindJSON(&createCommentBody)
if err != nil {
log.Println(err)
c.AbortWithStatusJSON(400, global.ErrBadRequest.Error())
return
}
// uuid validation
var uid *uuid.UUID
if createCommentBody.ReplyTo != nil {
uid, err = global.ParseUUIDFromString(*createCommentBody.ReplyTo)
if err != nil {
c.AbortWithStatusJSON(400, global.ErrBadRequest.Error())
return
}
}
// length validation
if r.config.Moderation.MaxCommentLength != nil {
if len(createCommentBody.Body) > *r.config.Moderation.MaxCommentLength {
c.AbortWithStatusJSON(400, global.ErrBadRequest.Error())
return
}
}
// author length validation
if len(createCommentBody.Author) == 0 {
c.AbortWithStatusJSON(400, global.ErrBadRequest.Error())
return
}
maxAuthorLength := global.DefaultAuthorLengthLimit
if r.config.Moderation.MaxAuthorLength != nil {
maxAuthorLength = *r.config.Moderation.MaxAuthorLength
}
createCommentBody.Author = ShortenAuthor(createCommentBody.Author, maxAuthorLength)
// body length validation
createCommentBody.Body = global.ParseAndSaniziteMarkdown(createCommentBody.Body)
if len(createCommentBody.Body) == 0 {
c.AbortWithStatusJSON(400, global.ErrBadRequest.Error())
return
}
createCommentBody.Path = NormalizePath(createCommentBody.Path)
if r.config.Honeypot && createCommentBody.Email != nil {
c.AbortWithStatusJSON(200, model.CreateCommentResponse{
Id: uuid.Must(uuid.NewV4()).String(),
Path: createCommentBody.Path,
Body: createCommentBody.Body,
Author: createCommentBody.Author,
Email: createCommentBody.Email,
ReplyTo: createCommentBody.ReplyTo,
})
return
}
db := *r.db
confirmed := !r.config.Moderation.Enabled
commentUID, err := db.CreateComment(createCommentBody.Body, createCommentBody.Author, createCommentBody.Path, confirmed, uid)
if err != nil {
if err == global.ErrWrongReplyTo {
c.AbortWithStatusJSON(400, global.ErrWrongReplyTo.Error())
return
}
log.Println(err)
c.AbortWithStatusJSON(500, global.ErrInternalServerError.Error())
return
}
if r.config.Notification.Webhook.Enabled {
url := *r.config.Notification.Webhook.URL
go func() {
_, err := http.Post(
url,
"application/json",
bytes.NewBufferString(fmt.Sprintf(`{"message":"%s"}`, "Comment received")),
)
if err != nil {
log.Println(err)
}
}()
}
c.AbortWithStatusJSON(200, model.CreateCommentResponse{
Id: commentUID.String(),
Path: createCommentBody.Path,
Body: createCommentBody.Body,
Author: createCommentBody.Author,
Email: createCommentBody.Email,
ReplyTo: createCommentBody.ReplyTo,
})
}
// UpdateComment updates the provided comment in body
func (r *Router) UpdateComment(c *gin.Context) {
if !r.isAdmin(c) {
c.AbortWithStatusJSON(401, global.ErrUnauthorized.Error())
return
}
var updateCommentBody model.UpdateCommentBody
err := c.BindJSON(&updateCommentBody)
if err != nil {
log.Println(err)
c.AbortWithStatusJSON(400, global.ErrBadRequest.Error())
return
}
commentId, err := global.ParseUUIDFromString(updateCommentBody.CommentId)
if err != nil {
log.Println(err)
c.AbortWithStatusJSON(400, global.ErrBadRequest.Error())
return
}
if updateCommentBody.Body == nil && updateCommentBody.Author == nil && updateCommentBody.Confirmed == nil {
c.AbortWithStatusJSON(400, global.ErrBadRequest)
return
}
db := *r.db
comment, err := db.GetComment(*commentId)
if err != nil {
if err == global.ErrCommentNotFound {
c.AbortWithStatusJSON(404, global.ErrCommentNotFound.Error())
return
}
c.AbortWithStatusJSON(500, global.ErrInternalServerError.Error())
return
}
body := comment.Body
author := comment.Author
confirmed := comment.Confirmed
if updateCommentBody.Body != nil {
body = *updateCommentBody.Body
}
if updateCommentBody.Author != nil {
author = *updateCommentBody.Author
}
if updateCommentBody.Confirmed != nil {
confirmed = *updateCommentBody.Confirmed
}
err = db.UpdateComment(*commentId, body, author, confirmed)
if err != nil {
log.Println(err)
c.AbortWithStatusJSON(500, global.ErrInternalServerError.Error())
return
}
c.AbortWithStatus(204)
}
// DeleteComment deletes comment by given id
func (r *Router) DeleteComment(c *gin.Context) {
if !r.isAdmin(c) {
c.AbortWithStatusJSON(401, global.ErrUnauthorized.Error())
return
}
var deleteCommentBody model.DeleteCommentBody
err := c.BindJSON(&deleteCommentBody)
if err != nil {
log.Println(err)
c.AbortWithStatusJSON(400, global.ErrBadRequest.Error())
return
}
commentId, err := global.ParseUUIDFromString(deleteCommentBody.CommentId)
if err != nil {
log.Println(err)
c.AbortWithStatusJSON(400, global.ErrBadRequest.Error())
return
}
db := *r.db
if deleteCommentBody.Hard {
err = db.HardDeleteComment(*commentId)
} else {
err = db.DeleteComment(*commentId)
}
if err != nil {
if err == global.ErrCommentNotFound {
c.AbortWithStatusJSON(404, global.ErrCommentNotFound.Error())
return
}
log.Println(err)
c.AbortWithStatusJSON(500, global.ErrInternalServerError.Error())
return
}
c.AbortWithStatus(204)
}
// RestoreDeletedComment restores the deleted comment by given id
func (r *Router) RestoreDeletedComment(c *gin.Context) {
if !r.isAdmin(c) {
c.AbortWithStatusJSON(401, global.ErrUnauthorized.Error())
return
}
var deleteCommentBody model.DeleteCommentBody
err := c.BindJSON(&deleteCommentBody)
if err != nil {
log.Println(err)
c.AbortWithStatusJSON(400, global.ErrBadRequest.Error())
return
}
commentId, err := global.ParseUUIDFromString(deleteCommentBody.CommentId)
if err != nil {
log.Println(err)
c.AbortWithStatusJSON(400, global.ErrBadRequest.Error())
return
}
db := *r.db
err = db.RestoreDeletedComment(*commentId)
if err != nil {
if err == global.ErrCommentNotFound {
c.AbortWithStatusJSON(404, global.ErrCommentNotFound.Error())
return
}
log.Println(err)
c.AbortWithStatusJSON(500, global.ErrInternalServerError.Error())
return
}
c.AbortWithStatus(204)
}
func (r *Router) isAdmin(c *gin.Context) bool {
session := sessions.Default(c)
isAdmin := session.Get("isAdmin")
isAdminParsed, ok := isAdmin.(bool)
if !ok {
return false
}
return isAdminParsed
}
// Login logs the user in
func (r *Router) Login(c *gin.Context) {
var loginBody model.LoginBody
err := c.BindJSON(&loginBody)
if err != nil {
log.Println(err)
c.AbortWithStatusJSON(400, global.ErrBadRequest.Error())
return
}
if loginBody.Password != r.config.Moderation.AdminPassword {
c.AbortWithStatusJSON(401, global.ErrBadRequest.Error())
return
}
session := sessions.Default(c)
session.Set("isAdmin", true)
session.Save()
c.AbortWithStatus(204)
}