forked from zabawaba99/firego
/
server.go
336 lines (291 loc) · 7.3 KB
/
server.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
/*
Package firetest provides utilities for Firebase testing
*/
package firetest
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"strings"
"sync/atomic"
"time"
)
var (
missingJSONExtension = []byte("append .json to your request URI to use the REST API")
missingBody = []byte(`{"error":"Error: No data supplied."}`)
invalidJSON = []byte(`{"error":"Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names."}`)
invalidAuth = []byte(`{"error" : "Could not parse auth token."}`)
)
// Firetest is a Firebase server implementation
type Firetest struct {
// URL of form http://ipaddr:port with no trailing slash
URL string
// Secret used to authenticate with server
Secret string
listener net.Listener
db *notifyDB
requireAuth *int32
}
// New creates a new Firetest server
func New() *Firetest {
secret := []byte(fmt.Sprint(time.Now().UnixNano()))
return &Firetest{
db: newNotifyDB(),
Secret: base64.URLEncoding.EncodeToString(secret),
requireAuth: new(int32),
}
}
// Start starts the server
func (ft *Firetest) Start() {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
if l, err = net.Listen("tcp6", "[::1]:0"); err != nil {
panic(fmt.Errorf("failed to listen on a port: %v", err))
}
}
ft.listener = l
s := http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ft.serveHTTP(w, req)
})}
go func() {
if err := s.Serve(l); err != nil {
log.Printf("error serving: %s", err)
}
ft.Close()
}()
ft.URL = "http://" + ft.listener.Addr().String()
}
// Close closes the server
func (ft *Firetest) Close() {
if ft.listener != nil {
ft.listener.Close()
}
}
func (ft *Firetest) serveHTTP(w http.ResponseWriter, req *http.Request) {
if !strings.HasSuffix(req.URL.Path, ".json") {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte(missingJSONExtension))
return
}
if atomic.LoadInt32(ft.requireAuth) == 1 {
var authenticated bool
authHeader := req.URL.Query().Get("auth")
switch {
case strings.Contains(authHeader, "."):
authenticated = ft.validJWT(authHeader)
default:
authenticated = authHeader == ft.Secret
}
if !authenticated {
w.WriteHeader(http.StatusUnauthorized)
w.Write(invalidAuth)
return
}
}
switch req.Method {
case "PUT":
ft.set(w, req)
case "PATCH":
ft.update(w, req)
case "POST":
ft.create(w, req)
case "GET":
switch req.Header.Get("Accept") {
case "text/event-stream":
ft.sse(w, req)
default:
ft.get(w, req)
}
case "DELETE":
ft.del(w, req)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
log.Println("not implemented yet")
}
}
func decodeSegment(seg string) ([]byte, error) {
if l := len(seg) % 4; l > 0 {
seg += strings.Repeat("=", 4-l)
}
return base64.URLEncoding.DecodeString(seg)
}
func (ft *Firetest) validJWT(val string) bool {
parts := strings.Split(val, ".")
if len(parts) != 3 {
return false
}
// validate header
hb, err := decodeSegment(parts[0])
if err != nil {
log.Println("error decoding header", err)
return false
}
var header map[string]string
if err := json.Unmarshal(hb, &header); err != nil {
log.Println("error unmarshaling header", err)
return false
}
if header["alg"] != "HS256" || header["typ"] != "JWT" {
return false
}
// validate claim
cb, err := decodeSegment(parts[1])
if err != nil {
log.Println("error decoding claim", err)
return false
}
var claim map[string]interface{}
if err := json.Unmarshal(cb, &claim); err != nil {
log.Println("error unmarshaling claim", err)
return false
}
if e, ok := claim["exp"]; ok {
// make sure not expired
exp, ok := e.(float64)
if !ok {
log.Println("expiration not a number")
return false
}
if int64(exp) < time.Now().Unix() {
log.Println("token expired")
return false
}
}
// ensure uid present
data, ok := claim["d"]
if !ok {
log.Println("missing data in claim")
return false
}
d, ok := data.(map[string]interface{})
if !ok {
log.Println("claim['data'] is not map")
return false
}
if _, ok := d["uid"]; !ok {
log.Println("claim['data'] missing uid")
return false
}
if sig, err := decodeSegment(parts[2]); err == nil {
hasher := hmac.New(sha256.New, []byte(ft.Secret))
signedString := strings.Join(parts[:2], ".")
hasher.Write([]byte(signedString))
if !hmac.Equal(sig, hasher.Sum(nil)) {
log.Println("invalid jwt signature")
return false
}
}
return true
}
func (ft *Firetest) set(w http.ResponseWriter, req *http.Request) {
body, v, ok := unmarshal(w, req.Body)
if !ok {
return
}
ft.Set(req.URL.Path, v)
w.Write(body)
}
func (ft *Firetest) update(w http.ResponseWriter, req *http.Request) {
body, v, ok := unmarshal(w, req.Body)
if !ok {
return
}
ft.Update(req.URL.Path, v)
w.Write(body)
}
func (ft *Firetest) create(w http.ResponseWriter, req *http.Request) {
_, v, ok := unmarshal(w, req.Body)
if !ok {
return
}
name := ft.Create(req.URL.Path, v)
rtn := map[string]string{"name": name}
if err := json.NewEncoder(w).Encode(rtn); err != nil {
log.Printf("Error encoding json: %s", err)
w.WriteHeader(http.StatusInternalServerError)
}
}
func (ft *Firetest) del(w http.ResponseWriter, req *http.Request) {
ft.Delete(req.URL.Path)
}
func (ft *Firetest) get(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", "application/json")
v := ft.Get(req.URL.Path)
if err := json.NewEncoder(w).Encode(v); err != nil {
log.Printf("Error encoding json: %s", err)
w.WriteHeader(http.StatusInternalServerError)
}
}
func (ft *Firetest) sse(w http.ResponseWriter, req *http.Request) {
f, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming is not supported", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
path := sanitizePath(req.URL.Path)
c := ft.db.watch(path)
defer ft.db.stopWatching(path, c)
d := eventData{Path: path, Data: ft.db.get(path)}
s, err := json.Marshal(d)
if err != nil {
fmt.Printf("Error marshaling node %s\n", err)
}
fmt.Fprintf(w, "event: put\ndata: %s\n\n", s)
f.Flush()
httpCloser := w.(http.CloseNotifier).CloseNotify()
for {
select {
case <-httpCloser:
return
case <-time.After(30 * time.Second):
fmt.Fprintf(w, "event: keep-alive\ndata: null\n\n")
f.Flush()
continue
case n, ok := <-c:
if !ok {
return
}
s, err := json.Marshal(n.Data)
if err != nil {
fmt.Printf("Error marshaling node %s\n", err)
continue
}
fmt.Fprintf(w, "event: %s\ndata: %s\n\n", n.Name, s)
f.Flush()
}
}
}
func sanitizePath(p string) string {
// remove slashes from the front and back
// /foo/.json -> foo/.json
s := strings.Trim(p, "/")
// remove .json extension
// foo/.json -> foo/
s = strings.TrimSuffix(s, ".json")
// trim an potential trailing slashes
// foo/ -> foo
return strings.TrimSuffix(s, "/")
}
func unmarshal(w http.ResponseWriter, r io.Reader) ([]byte, interface{}, bool) {
body, err := ioutil.ReadAll(r)
if err != nil || len(body) == 0 {
w.WriteHeader(http.StatusBadRequest)
w.Write(missingBody)
return nil, nil, false
}
var v interface{}
if err := json.Unmarshal(body, &v); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write(invalidJSON)
return nil, nil, false
}
return body, v, true
}