-
-
Notifications
You must be signed in to change notification settings - Fork 256
/
config.go
71 lines (60 loc) · 1.89 KB
/
config.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
package api
import (
"encoding/json"
"net/http"
"github.com/kgretzky/pwndrop/storage"
"github.com/kgretzky/pwndrop/utils"
)
func ConfigOptionsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,DELETE,OPTIONS")
}
func ConfigGetHandler(w http.ResponseWriter, r *http.Request) {
// #### CHECK IF AUTHENTICATED ####
_, err := AuthSession(r)
if err != nil {
DumpResponse(w, "unauthorized", http.StatusUnauthorized, API_ERROR_BAD_AUTHENTICATION, nil)
return
}
o, err := storage.ConfigGet(1)
if err != nil {
DumpResponse(w, err.Error(), http.StatusInternalServerError, API_ERROR_FILE_DATABASE_FAILED, nil)
return
}
DumpResponse(w, "ok", http.StatusOK, 0, o)
}
func ConfigUpdateHandler(w http.ResponseWriter, r *http.Request) {
// #### CHECK IF AUTHENTICATED ####
_, err := AuthSession(r)
if err != nil {
DumpResponse(w, "unauthorized", http.StatusUnauthorized, API_ERROR_BAD_AUTHENTICATION, nil)
return
}
old_cfg, err := storage.ConfigGet(1)
if err != nil {
DumpResponse(w, err.Error(), http.StatusInternalServerError, API_ERROR_FILE_DATABASE_FAILED, nil)
return
}
o := storage.DbConfig{}
err = json.NewDecoder(r.Body).Decode(&o)
if err != nil {
DumpResponse(w, err.Error(), http.StatusBadRequest, API_ERROR_BAD_REQUEST, nil)
return
}
if o.SecretPath == "" || o.CookieName == "" || o.CookieToken == "" {
DumpResponse(w, "missing config variables", http.StatusBadRequest, API_ERROR_BAD_REQUEST, nil)
return
}
if o.SecretPath[0] != '/' {
o.SecretPath = "/" + o.SecretPath
}
if o.SecretPath != old_cfg.SecretPath {
o.CookieName = utils.GenRandomString(4)
o.CookieToken = utils.GenRandomHash()
}
ret, err := storage.ConfigUpdate(1, &o)
if err != nil {
DumpResponse(w, err.Error(), http.StatusInternalServerError, API_ERROR_FILE_DATABASE_FAILED, nil)
return
}
DumpResponse(w, "ok", http.StatusOK, 0, ret)
}