github.com/avenga/couper@v1.12.2/handler/access_control.go (about) 1 package handler 2 3 import ( 4 "context" 5 "net/http" 6 7 "github.com/avenga/couper/accesscontrol" 8 "github.com/avenga/couper/config/request" 9 "github.com/avenga/couper/server/writer" 10 ) 11 12 var ( 13 _ http.Handler = &AccessControl{} 14 _ accesscontrol.ProtectedHandler = &AccessControl{} 15 ) 16 17 type AccessControl struct { 18 acl accesscontrol.List 19 protected http.Handler 20 } 21 22 func NewAccessControl(protected http.Handler, list accesscontrol.List) *AccessControl { 23 return &AccessControl{ 24 acl: list, 25 protected: protected, 26 } 27 } 28 29 func (a *AccessControl) ServeHTTP(rw http.ResponseWriter, req *http.Request) { 30 r, ok := rw.(*writer.Response) 31 32 for _, control := range a.acl { 33 if ok && !control.DisablePrivateCaching() { 34 r.AddPrivateCC() 35 } 36 37 if err := control.Validate(req); err != nil { 38 *req = *req.WithContext(context.WithValue(req.Context(), request.Error, err)) 39 control.ErrorHandler().ServeHTTP(rw, req) 40 return 41 } 42 } 43 a.protected.ServeHTTP(rw, req) 44 } 45 46 func (a *AccessControl) Child() http.Handler { 47 return a.protected 48 } 49 50 func (a *AccessControl) String() string { 51 if h, ok := a.protected.(interface{ String() string }); ok { 52 return h.String() 53 } 54 return "AccessControl" 55 }