-
Notifications
You must be signed in to change notification settings - Fork 6
/
management.go
169 lines (142 loc) · 4.08 KB
/
management.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
package zwibserve
import (
"crypto/subtle"
"io"
"log"
"net/http"
"time"
)
// This file implements the server management API
// https://docs.google.com/document/d/1vdUUEooti4F5Ob9rca2DVoOJOxyO2uftaCOUzKdXb4M/edit#
func (zh *Handler) serveMAPI(w http.ResponseWriter, r *http.Request) bool {
if r.Method == "POST" {
method := r.FormValue("method")
switch method {
case "addToken":
zh.handleAddToken(w, r)
case "updateUser":
zh.handleUpdateUser(w, r)
case "createDocument":
zh.handleCreateDocument(w, r)
case "deleteDocument":
zh.handleDeleteDocument(w, r)
case "dumpDocument":
zh.handleDumpDocument(w, r, true)
case "checkDocument":
zh.handleDumpDocument(w, r, false)
default:
HTTPPanic(400, "Unknown 'method' parameter")
}
return true
}
return false
}
func (zh *Handler) verifyAuth(r *http.Request) {
username, password, ok := r.BasicAuth()
eq1 := subtle.ConstantTimeCompare([]byte(username), []byte(zh.secretUser))
eq2 := subtle.ConstantTimeCompare([]byte(password), []byte(zh.secretPassword))
if !ok || eq1 == 0 || eq2 == 0 || (zh.secretUser == "" && zh.secretPassword == "") {
log.Printf(" Request not authorized; got %s/%s", username, password)
HTTPPanic(401, "Unauthorized")
}
}
func mustGet(r *http.Request, key string) string {
value := r.FormValue(key)
if value == "" {
log.Printf(" Missing " + key)
HTTPPanic(400, "Missing "+key)
}
return value
}
func (zh *Handler) handleCreateDocument(w http.ResponseWriter, r *http.Request) {
log.Printf("Got request for createDocument")
zh.verifyAuth(r)
docID := mustGet(r, "documentID")
contents := []byte(r.FormValue("contents"))
// if there is no string by that name, then try a file.
if len(contents) == 0 {
file, _, err := r.FormFile("contents")
if err != nil {
HTTPPanic(400, "Missing contents")
}
defer file.Close()
// read the entire file as a string.
contents, err = io.ReadAll(file)
if err != nil {
HTTPPanic(400, "Error reading contents: "+err.Error())
}
}
_, _, err := zh.db.GetDocument(docID, AlwaysCreate, []byte(contents))
if err == ErrExists {
w.WriteHeader(409)
return
} else if err != nil {
panic(err)
}
w.WriteHeader(200)
}
func (zh *Handler) handleDeleteDocument(w http.ResponseWriter, r *http.Request) {
log.Printf("Got request for deleteDocument")
zh.verifyAuth(r)
docID := mustGet(r, "documentID")
zh.hub.signalDocumentDeleted(docID)
err := zh.db.DeleteDocument(docID)
if err != nil {
panic(err)
}
w.WriteHeader(200)
}
func (zh *Handler) handleDumpDocument(w http.ResponseWriter, r *http.Request, dump bool) {
log.Printf("Got request for dumpDocument")
zh.verifyAuth(r)
docID := mustGet(r, "documentID")
contents, _, err := zh.db.GetDocument(docID, NeverCreate, nil)
if err == ErrMissing {
w.WriteHeader(404)
return
} else if err != nil {
log.Panic(err)
}
if dump {
w.Header().Set("Content-Type", "text")
w.Write(contents)
} else {
w.WriteHeader(200)
}
}
func (zh *Handler) handleAddToken(w http.ResponseWriter, r *http.Request) {
log.Printf("Got request for addToken")
zh.verifyAuth(r)
docID := mustGet(r, "documentID")
token := mustGet(r, "token")
userID := mustGet(r, "userID")
permissions := r.FormValue("permissions")
contents := r.FormValue("contents")
expiration := mustGet(r, "expiration")
expirationTime, err := time.Parse(time.RFC1123, expiration)
if err != nil {
HTTPPanic(400, "Incorrect expires format")
}
err = zh.db.AddToken(token, docID, userID, permissions, expirationTime.Unix(), []byte(contents))
log.Printf("AddToken %s for doc %s user %s", token, docID, userID)
if err == ErrExists || err == ErrConflict {
w.WriteHeader(409)
} else if err != nil {
log.Printf("Error is %v", err)
log.Panic(err)
} else {
w.WriteHeader(200)
}
}
func (zh *Handler) handleUpdateUser(w http.ResponseWriter, r *http.Request) {
log.Printf("Got request for updateUser")
zh.verifyAuth(r)
userID := mustGet(r, "userID")
permissions := r.FormValue("permissions")
err := zh.db.UpdateUser(userID, permissions)
if err != nil {
log.Panic(err)
}
zh.hub.updatePermissions(userID, permissions)
w.WriteHeader(200)
}