-
Notifications
You must be signed in to change notification settings - Fork 148
/
log_access_middleware.go
58 lines (46 loc) · 1.33 KB
/
log_access_middleware.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
package proxy
import (
"net/http"
"regexp"
"code.cloudfoundry.org/loggregator-release/src/trafficcontroller/internal/auth"
"github.com/gorilla/mux"
)
type LogAccessMiddleware struct {
authorize auth.LogAccessAuthorizer
disableAccessControl bool
}
func NewLogAccessMiddleware(authorize auth.LogAccessAuthorizer, disableAccessControl bool) *LogAccessMiddleware {
return &LogAccessMiddleware{
authorize: authorize,
disableAccessControl: disableAccessControl,
}
}
func (m *LogAccessMiddleware) Wrap(h http.Handler) http.Handler {
guid := regexp.MustCompile("^[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}$")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
appID := mux.Vars(r)["appID"]
if m.disableAccessControl {
h.ServeHTTP(w, r)
return
}
if !guid.MatchString(appID) {
w.WriteHeader(http.StatusUnauthorized)
return
}
authToken := getAuthToken(r)
status, _ := m.authorize(authToken, appID)
if status != http.StatusOK {
switch status {
case http.StatusUnauthorized:
w.Header().Set("WWW-Authenticate", "Basic")
case http.StatusForbidden, http.StatusNotFound:
status = http.StatusNotFound
default:
status = http.StatusInternalServerError
}
w.WriteHeader(status)
return
}
h.ServeHTTP(w, r)
})
}