-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
105 lines (89 loc) · 2.77 KB
/
http.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
package guardedbeaconproxy
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
// HTTPAuthenticator is a function type which can authenticate HTTP requests.
// For example, by checking the contents of the BasicAuth header.
//
// Returning an AuthenticationStatus other than Allowed will prevent the request
// from being proxied. You may optionally return a Context, which will be passed
// to the PrepareBeaconProposerGuard/RegisterValidatorGuard functions provided.
// In particular, conext.WithValue allows the authentication method to share state
// with the guard methods.
//
// Any error returned will be sent back to the client, so do not encode sensitive
// information.
type HTTPAuthenticator func(*http.Request) (AuthenticationStatus, context.Context, error)
func (gbp *GuardedBeaconProxy) authenticationMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status, context, err := gbp.HTTPAuthenticator(r)
if status == Allowed {
if context != nil {
next.ServeHTTP(w, r.WithContext(context))
return
}
next.ServeHTTP(w, r)
return
}
gbp.httpError(w, status.httpStatus(), err)
})
}
func cloneRequestBody(r *http.Request) (io.ReadCloser, error) {
// Read the body
buf, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
original := io.NopCloser(bytes.NewBuffer(buf))
clone := io.NopCloser(bytes.NewBuffer(buf))
r.Body = original
return clone, nil
}
func (gbp *GuardedBeaconProxy) httpError(w http.ResponseWriter, code int, err error) {
w.WriteHeader(code)
if err != nil {
escaped, _ := json.Marshal(err.Error())
fmt.Fprintf(w, "{\"error\":%s}\n", escaped)
}
}
func (gbp *GuardedBeaconProxy) prepareBeaconProposer(w http.ResponseWriter, r *http.Request) {
buf, err := cloneRequestBody(r)
if err != nil {
gbp.httpError(w, http.StatusInternalServerError, nil)
return
}
var proposers PrepareBeaconProposerRequest
if err := json.NewDecoder(buf).Decode(&proposers); err != nil {
gbp.httpError(w, http.StatusBadRequest, nil)
return
}
status, err := gbp.PrepareBeaconProposerGuard(proposers, r.Context())
if status != Allowed {
gbp.httpError(w, status.httpStatus(), err)
return
}
gbp.proxy.ServeHTTP(w, r)
}
func (gbp *GuardedBeaconProxy) registerValidator(w http.ResponseWriter, r *http.Request) {
buf, err := cloneRequestBody(r)
if err != nil {
gbp.httpError(w, http.StatusInternalServerError, nil)
return
}
var validators RegisterValidatorRequest
if err := json.NewDecoder(buf).Decode(&validators); err != nil {
gbp.httpError(w, http.StatusBadRequest, nil)
return
}
status, err := gbp.RegisterValidatorGuard(validators, r.Context())
if status != Allowed {
gbp.httpError(w, status.httpStatus(), err)
return
}
gbp.proxy.ServeHTTP(w, r)
}