-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.go
483 lines (412 loc) · 14 KB
/
api.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
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/gorilla/securecookie"
)
//GetCode sets the cookie if none is found, returns json of salted hash and pw
func GetCode(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
if err != nil {
var sc = securecookie.New(hashKey, blockKey)
var pw = RandomString(10)
var pwh = pw + config.Salt
h := sha256.New()
h.Write([]byte(pwh))
hash := hex.EncodeToString(h.Sum(nil))
token := map[string]string{
"password": pw,
"hash": hash[:30],
}
if encoded, err := sc.Encode("token", token); err == nil {
cookie := &http.Cookie{
Name: "token",
Value: encoded,
Path: "/",
}
http.SetCookie(w, cookie)
}
output, _ := json.Marshal(token)
fmt.Fprintln(w, string(output[:]))
} else if token.Value == "" {
var sc = securecookie.New(hashKey, blockKey)
var pw = RandomString(10)
var pwh = pw + config.Salt
h := sha256.New()
h.Write([]byte(pwh))
hash := hex.EncodeToString(h.Sum(nil))
token := map[string]string{
"password": pw,
"hash": hash[:30],
}
if encoded, err := sc.Encode("token", token); err == nil {
cookie := &http.Cookie{
Name: "token",
Value: encoded,
Path: "/",
}
http.SetCookie(w, cookie)
}
output, _ := json.Marshal(token)
fmt.Fprintln(w, string(output[:]))
} else {
var sc = securecookie.New(hashKey, blockKey)
value := make(map[string]string)
if err = sc.Decode("token", token.Value, &value); err == nil {
output, _ := json.Marshal(value)
fmt.Fprintln(w, string(output[:]))
}
}
}
//SetCode handles the hash and cookie if a user changes their password
func SetCode(w http.ResponseWriter, r *http.Request) {
var jstoken Token
_ = json.NewDecoder(r.Body).Decode(&jstoken)
var sc = securecookie.New(hashKey, blockKey)
var pw = jstoken.Password
var pwh = pw + config.Salt
h := sha256.New()
h.Write([]byte(pwh))
hash := hex.EncodeToString(h.Sum(nil))
token := map[string]string{
"password": pw,
"hash": hash[:30],
}
if encoded, err := sc.Encode("token", token); err == nil {
cookie := &http.Cookie{
Name: "token",
Value: encoded,
Path: "/",
}
http.SetCookie(w, cookie)
}
output, _ := json.Marshal(token)
fmt.Fprintln(w, string(output[:]))
}
//DeleteCode expires the token cookie
func DeleteCode(w http.ResponseWriter, r *http.Request) {
cookie := &http.Cookie{
Name: "token",
Value: "",
MaxAge: 0,
Path: "/",
}
http.SetCookie(w, cookie)
}
//CreateBoard creates a new board
func CreateBoard(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
var sc = securecookie.New(hashKey, blockKey)
value := make(map[string]string)
if err = sc.Decode("token", token.Value, &value); err == nil {
if IsAdmin(value["hash"]) == true {
var board Board
_ = json.NewDecoder(r.Body).Decode(&board)
boardCollection := dbsession.Collection("boards")
boardCollection.Insert(DBBoard{Title: board.Title, Description: board.Description, Position: board.Position})
}
}
}
//ListBoard provides a list of available boards
func ListBoard(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
var sc = securecookie.New(hashKey, blockKey)
value := make(map[string]string)
if err = sc.Decode("token", token.Value, &value); err == nil {
var boards []Board
var dbboards []DBBoard
var err error
q := dbsession.SelectFrom("boards").OrderBy("position")
err = q.All(&dbboards)
if err != nil {
log.Fatalf("q.All(): %q\n", err)
}
for _, b := range dbboards {
boards = append(boards, Board{b.ID, b.Title, b.Description, b.Position})
}
output, _ := json.Marshal(boards)
fmt.Fprintln(w, string(output[:]))
}
}
//DeleteBoard deletes a board matching {id} in url
func DeleteBoard(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
var sc = securecookie.New(hashKey, blockKey)
value := make(map[string]string)
if err = sc.Decode("token", token.Value, &value); err == nil {
if IsAdmin(value["hash"]) == true {
var err error
vars := mux.Vars(r)
q := dbsession.DeleteFrom("boards").Where("id", vars["id"])
_, err = q.Exec()
if err != nil {
log.Fatalf("q.Exec(): %q\n", err)
}
}
}
}
//UpdateBoard updates the board with {id} in the url
func UpdateBoard(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
var sc = securecookie.New(hashKey, blockKey)
value := make(map[string]string)
if err = sc.Decode("token", token.Value, &value); err == nil {
if IsAdmin(value["hash"]) == true {
var board Board
var err error
vars := mux.Vars(r)
_ = json.NewDecoder(r.Body).Decode(&board)
q := dbsession.Update("boards").Set("title", board.Title, "description", board.Description, "position", board.Position).Where("id", vars["id"])
_, err = q.Exec()
if err != nil {
log.Fatalf("q.Exec(): %q\n", err)
}
}
}
}
//CreateThread creates a thread under board {id}
func CreateThread(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
var sc = securecookie.New(hashKey, blockKey)
value := make(map[string]string)
if err = sc.Decode("token", token.Value, &value); err == nil {
var thread Thread
vars := mux.Vars(r)
_ = json.NewDecoder(r.Body).Decode(&thread)
threadCollection := dbsession.Collection("threads")
boardid, _ := strconv.ParseUint(vars["id"], 10, 64)
threadCollection.Insert(DBThread{Board: uint16(boardid), Author: value["hash"], Title: thread.Title, Body: thread.Body, Created: time.Now(), LastModified: time.Now(), IP: r.RemoteAddr})
}
}
//ListThread lists the threads of board {id} greater than {start} limit {amount}
func ListThread(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
var sc = securecookie.New(hashKey, blockKey)
value := make(map[string]string)
if err = sc.Decode("token", token.Value, &value); err == nil {
var thread Thread
var threads []Thread
var dbthreads []DBThread
var err error
vars := mux.Vars(r)
_ = json.NewDecoder(r.Body).Decode(&thread)
boardid, _ := strconv.ParseUint(vars["id"], 10, 64)
start, _ := strconv.ParseUint(vars["start"], 10, 64)
amount, _ := strconv.Atoi(vars["amount"])
q := dbsession.SelectFrom("threads").Where("board = ? AND id > ?", uint16(boardid), start).Limit(amount)
err = q.All(&dbthreads)
if err != nil {
log.Fatalf("q.All(): %q\n", err)
}
if IsAdmin(value["hash"]) == true || IsMod(value["hash"]) == true {
for _, b := range dbthreads {
threads = append(threads, Thread{b.ID, b.Board, b.Author, b.Title, b.Body, uint64(b.Created.Unix()), uint64(b.LastModified.Unix()), b.IP, b.IsSticky, b.IsLocked})
}
} else {
for _, b := range dbthreads {
threads = append(threads, Thread{b.ID, b.Board, b.Author, b.Title, b.Body, uint64(b.Created.Unix()), uint64(b.LastModified.Unix()), "", b.IsSticky, b.IsLocked})
}
}
output, _ := json.Marshal(threads)
fmt.Fprintln(w, string(output[:]))
}
}
//GetThread lists the posts of thread {id} greater than {start} limit {amount}
func GetThread(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
var sc = securecookie.New(hashKey, blockKey)
value := make(map[string]string)
if err = sc.Decode("token", token.Value, &value); err == nil {
var thread ThreadFull
var dbthread DBThread
var posts []Post
var dbposts []DBPost
var err error
vars := mux.Vars(r)
_ = json.NewDecoder(r.Body).Decode(&thread)
threadid, _ := strconv.ParseUint(vars["id"], 10, 64)
start, _ := strconv.ParseUint(vars["start"], 10, 64)
amount, _ := strconv.Atoi(vars["amount"])
q := dbsession.SelectFrom("threads").Where("id", threadid)
err = q.One(&dbthread)
if err != nil {
log.Fatalf("q.One(): %q\n", err)
}
q2 := dbsession.SelectFrom("posts").Where("thread = ? AND id > ?", threadid, start).Limit(amount)
err = q2.All(&dbposts)
if err != nil {
log.Fatalf("q2.All(): %q\n", err)
}
if IsAdmin(value["hash"]) == true || IsMod(value["hash"]) == true {
for _, b := range dbposts {
posts = append(posts, Post{b.ID, b.Thread, b.Author, b.Body, uint64(b.Created.Unix()), uint64(b.LastModified.Unix()), b.IP})
}
} else {
for _, b := range dbposts {
posts = append(posts, Post{b.ID, b.Thread, b.Author, b.Body, uint64(b.Created.Unix()), uint64(b.LastModified.Unix()), ""})
}
}
thread = ThreadFull{dbthread.ID, dbthread.Board, dbthread.Author, dbthread.Title, dbthread.Body, uint64(dbthread.Created.Unix()), uint64(dbthread.LastModified.Unix()), "", posts, dbthread.IsSticky, dbthread.IsLocked}
output, _ := json.Marshal(thread)
fmt.Fprintln(w, string(output[:]))
}
}
//DeleteThread deletes thread {id} and all related posts - very messy
func DeleteThread(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
var sc = securecookie.New(hashKey, blockKey)
value := make(map[string]string)
if err = sc.Decode("token", token.Value, &value); err == nil {
if IsAdmin(value["hash"]) == true || IsMod(value["hash"]) == true {
var err error
vars := mux.Vars(r)
q := dbsession.DeleteFrom("threads").Where("id", vars["id"])
_, err = q.Exec()
if err != nil {
log.Fatalf("q.Exec(): %q\n", err)
}
q2 := dbsession.DeleteFrom("posts").Where("thread", vars["id"])
_, err = q2.Exec()
if err != nil {
log.Fatalf("q2.Exec(): %q\n", err)
}
}
}
}
//UpdateThread changes thread data
func UpdateThread(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
var sc = securecookie.New(hashKey, blockKey)
value := make(map[string]string)
if err = sc.Decode("token", token.Value, &value); err == nil {
vars := mux.Vars(r)
threadid, _ := strconv.ParseUint(vars["id"], 10, 64)
if IsAdmin(value["hash"]) == true || IsMod(value["hash"]) == true || IsThreadOwner(value["hash"], threadid) == true {
var thread Thread
var err error
_ = json.NewDecoder(r.Body).Decode(&thread)
q := dbsession.Update("threads").Set("title", thread.Title, "body", thread.Body, "last_modified", time.Now(), "is_sticky", thread.IsSticky, "is_locked", thread.IsLocked).Where("id", threadid)
_, err = q.Exec()
if err != nil {
log.Fatalf("q.Exec(): %q\n", err)
}
}
}
}
//CreatePost creates a post under thread {id}
func CreatePost(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
var sc = securecookie.New(hashKey, blockKey)
value := make(map[string]string)
if err = sc.Decode("token", token.Value, &value); err == nil {
var post Post
var err error
vars := mux.Vars(r)
_ = json.NewDecoder(r.Body).Decode(&post)
postCollection := dbsession.Collection("posts")
threadid, _ := strconv.ParseUint(vars["id"], 10, 64)
postCollection.Insert(DBPost{Thread: threadid, Author: value["hash"], Body: post.Body, Created: time.Now(), LastModified: time.Now(), IP: r.RemoteAddr})
q := dbsession.Update("threads").Set("last_modified", time.Now()).Where("id", threadid)
_, err = q.Exec()
if err != nil {
log.Fatalf("q.Exec(): %q\n", err)
}
}
}
//GetPost gets post {id} and dumps the data on it
func GetPost(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
var sc = securecookie.New(hashKey, blockKey)
value := make(map[string]string)
if err = sc.Decode("token", token.Value, &value); err == nil {
vars := mux.Vars(r)
postid, _ := strconv.ParseUint(vars["id"], 10, 64)
var post Post
var dbpost DBPost
var err error
q := dbsession.SelectFrom("posts").Where("id", postid)
err = q.One(&dbpost)
if err != nil {
log.Fatalf("q.One(): %q\n", err)
}
if IsAdmin(value["hash"]) == true || IsMod(value["hash"]) == true {
post = Post{dbpost.ID, dbpost.Thread, dbpost.Author, dbpost.Body, uint64(dbpost.Created.Unix()), uint64(dbpost.LastModified.Unix()), dbpost.IP}
} else {
post = Post{dbpost.ID, dbpost.Thread, dbpost.Author, dbpost.Body, uint64(dbpost.Created.Unix()), uint64(dbpost.LastModified.Unix()), ""}
}
output, _ := json.Marshal(post)
fmt.Fprintln(w, string(output[:]))
}
}
//DeletePost deletes a post {id} if admin, mod, or userhash is author of post
func DeletePost(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
var sc = securecookie.New(hashKey, blockKey)
value := make(map[string]string)
if err = sc.Decode("token", token.Value, &value); err == nil {
vars := mux.Vars(r)
postid, _ := strconv.ParseUint(vars["id"], 10, 64)
if IsAdmin(value["hash"]) == true || IsMod(value["hash"]) == true || IsPostOwner(value["hash"], postid) == true {
var err error
q := dbsession.DeleteFrom("posts").Where("id", postid)
_, err = q.Exec()
if err != nil {
log.Fatalf("q.Exec(): %q\n", err)
}
}
}
}
//UpdatePost changes post body
func UpdatePost(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
var sc = securecookie.New(hashKey, blockKey)
value := make(map[string]string)
if err = sc.Decode("token", token.Value, &value); err == nil {
vars := mux.Vars(r)
postid, _ := strconv.ParseUint(vars["id"], 10, 64)
if IsAdmin(value["hash"]) == true || IsMod(value["hash"]) == true || IsPostOwner(value["hash"], postid) == true {
var post Post
var err error
_ = json.NewDecoder(r.Body).Decode(&post)
q := dbsession.Update("posts").Set("body", post.Body, "last_modified", time.Now()).Where("id", postid)
_, err = q.Exec()
if err != nil {
log.Fatalf("q.Exec(): %q\n", err)
}
}
}
}
//SetupBBS initializes the first admin
func SetupBBS(w http.ResponseWriter, r *http.Request) {
if IsSetup() == false {
var sc = securecookie.New(hashKey, blockKey)
var pw = RandomString(10)
var pwh = pw + config.Salt
h := sha256.New()
h.Write([]byte(pwh))
hash := hex.EncodeToString(h.Sum(nil))
token := map[string]string{
"password": pw,
"hash": hash[:30],
}
if encoded, err := sc.Encode("token", token); err == nil {
cookie := &http.Cookie{
Name: "token",
Value: encoded,
Path: "/",
}
http.SetCookie(w, cookie)
}
userCollection := dbsession.Collection("users")
userCollection.Insert(DBUser{Hash: token["hash"], IsAdmin: true, IsMod: true})
output, _ := json.Marshal(token)
fmt.Fprintln(w, string(output[:]))
}
}