-
Notifications
You must be signed in to change notification settings - Fork 5
/
http_middleware.go
65 lines (60 loc) · 1.66 KB
/
http_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
59
60
61
62
63
64
65
package opamiddleware
import (
"github.com/Joffref/opa-middleware/config"
"github.com/Joffref/opa-middleware/internal"
"net/http"
)
// HTTPMiddleware is the middleware for http requests
type HTTPMiddleware struct {
Config *config.Config
// Next is the next handler in the request chain.
Next http.Handler
}
// NewHTTPMiddleware returns a new HTTPMiddleware
func NewHTTPMiddleware(cfg *config.Config, next http.Handler) (*HTTPMiddleware, error) {
err := cfg.Validate()
if err != nil {
return nil, err
}
if next == nil {
return nil, err
}
return &HTTPMiddleware{
Config: cfg,
Next: next,
}, nil
}
// ServeHTTP serves the http request. Act as Use acts in other frameworks.
func (h *HTTPMiddleware) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if h.Config.Debug {
h.Config.Logger.Printf("[opa-middleware-http] Request received")
}
result, err := h.query(req)
if err != nil {
if h.Config.Debug {
h.Config.Logger.Printf("[opa-middleware-http] Error: %s", err.Error())
}
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
if h.Config.Debug {
h.Config.Logger.Printf("[opa-middleware-http] Result: %t", result)
}
if result != h.Config.ExceptedResult {
http.Error(rw, h.Config.DeniedMessage, h.Config.DeniedStatusCode)
return
}
h.Next.ServeHTTP(rw, req)
}
func (h *HTTPMiddleware) query(req *http.Request) (bool, error) {
bind, err := h.Config.InputCreationMethod(req)
if err != nil {
return !h.Config.ExceptedResult, err
}
if h.Config.URL != "" {
input := make(map[string]interface{})
input["input"] = bind
return internal.QueryURL(req, h.Config, input)
}
return internal.QueryPolicy(req, h.Config, bind)
}