-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_admin.go
186 lines (135 loc) · 4.45 KB
/
api_admin.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
// Accounts admin API
package main
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
)
type ApiAdminAccountEntry struct {
Username string `json:"username"`
Write bool `json:"write"`
}
func api_getAccounts(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
}
accounts := GetVault().credentials.GetAccountsInfo()
jsonResult, err := json.Marshal(accounts)
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
ReturnAPI_JSON(response, request, jsonResult)
}
type ApiAdminCreateAccountBody struct {
Username string `json:"username"`
Password string `json:"password"`
Write bool `json:"write"`
}
func api_createAccount(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, AUTH_API_BODY_MAX_LENGTH)
var p ApiAdminCreateAccountBody
err := json.NewDecoder(request.Body).Decode(&p)
if err != nil {
response.WriteHeader(400)
return
}
if len(p.Username) == 0 || len(p.Username) > 255 {
ReturnAPIError(response, 400, "USERNAME_INVALID", "Invalid username.")
return
}
if len(p.Password) == 0 || len(p.Password) > 255 {
ReturnAPIError(response, 400, "PASSWORD_INVALID", "Invalid password.")
return
}
// Check username
exists := GetVault().credentials.CheckUserExists(p.Username)
if exists {
ReturnAPIError(response, 400, "USERNAME_IN_USE", "The username is already in use.")
return
}
err = GetVault().credentials.SetAccountCredentials(p.Username, p.Password, session.key, p.Write)
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
err = GetVault().credentials.SaveCredentials()
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) + "] Created account. Username: " + p.Username)
response.WriteHeader(200)
}
type ApiAdminDeleteAccountBody struct {
Username string `json:"username"`
}
func api_deleteAccount(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, AUTH_API_BODY_MAX_LENGTH)
var p ApiAdminDeleteAccountBody
err := json.NewDecoder(request.Body).Decode(&p)
if err != nil {
response.WriteHeader(400)
return
}
// Check username
exists := GetVault().credentials.CheckUserExists(p.Username)
if !exists {
ReturnAPIError(response, 404, "USER_NOT_FOUND", "User not found")
return
}
err = GetVault().credentials.RemoveAccount(p.Username)
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
err = GetVault().credentials.SaveCredentials()
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
GetVault().sessions.RemoveUserSessions(p.Username)
GetVault().invites.ClearCode(p.Username)
LogSecurity("[ADMIN] [From IP: " + GetClientIP(request) + "] Deleted account. Username: " + p.Username)
response.WriteHeader(200)
}
var (
LAUNCHER_TAG = ""
)
func api_checkLauncherTag(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
if vars["tag"] == LAUNCHER_TAG {
response.WriteHeader(200)
return
}
response.WriteHeader(404)
}