authorization.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package middleware
  2. import (
  3. "net/http"
  4. "github.com/Sirupsen/logrus"
  5. "github.com/docker/docker/api/server/httputils"
  6. "github.com/docker/docker/pkg/authorization"
  7. "golang.org/x/net/context"
  8. )
  9. // NewAuthorizationMiddleware creates a new Authorization middleware.
  10. func NewAuthorizationMiddleware(plugins []authorization.Plugin) Middleware {
  11. return func(handler httputils.APIFunc) httputils.APIFunc {
  12. return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  13. // FIXME: fill when authN gets in
  14. // User and UserAuthNMethod are taken from AuthN plugins
  15. // Currently tracked in https://github.com/docker/docker/pull/13994
  16. user := ""
  17. userAuthNMethod := ""
  18. authCtx := authorization.NewCtx(plugins, user, userAuthNMethod, r.Method, r.RequestURI)
  19. if err := authCtx.AuthZRequest(w, r); err != nil {
  20. logrus.Errorf("AuthZRequest for %s %s returned error: %s", r.Method, r.RequestURI, err)
  21. return err
  22. }
  23. rw := authorization.NewResponseModifier(w)
  24. if err := handler(ctx, rw, r, vars); err != nil {
  25. logrus.Errorf("Handler for %s %s returned error: %s", r.Method, r.RequestURI, err)
  26. return err
  27. }
  28. if err := authCtx.AuthZResponse(rw, r); err != nil {
  29. logrus.Errorf("AuthZResponse for %s %s returned error: %s", r.Method, r.RequestURI, err)
  30. return err
  31. }
  32. return nil
  33. }
  34. }
  35. }