-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_config.go
87 lines (61 loc) · 1.98 KB
/
api_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Configuration API
package main
import (
"encoding/json"
"net/http"
)
func api_getConfig(response http.ResponseWriter, request *http.Request) {
session := GetSessionFromRequest(request)
if session == nil {
ReturnAPIError(response, 401, "UNAUTHORIZED", "You must provide a valid active session to use this API.")
return
}
config, err := GetVault().config.Read(session.key)
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
jsonResult, err := json.Marshal(config)
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
ReturnAPI_JSON(response, request, jsonResult)
}
func api_setConfig(response http.ResponseWriter, request *http.Request) {
session := GetSessionFromRequest(request)
if session == nil {
ReturnAPIError(response, 401, "UNAUTHORIZED", "You must provide a valid active session to use this API.")
return
}
if !session.root {
ReturnAPIError(response, 403, "ACCESS_DENIED", "Your current session does not have permission to make use of this API.")
return
}
request.Body = http.MaxBytesReader(response, request.Body, JSON_BODY_MAX_LENGTH)
var p UserConfig
err := json.NewDecoder(request.Body).Decode(&p)
if err != nil {
response.WriteHeader(400)
return
}
if p.Resolutions == nil {
p.Resolutions = make([]UserConfigResolution, 0)
}
err = GetVault().config.Write(&p, session.key)
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
err = GetVault().tasks.LoadUserConfigParams(session.key)
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
LogSecurity("[ADMIN] [From IP: " + GetClientIP(request) + "] Updated vault internal configuration")
response.WriteHeader(200)
}