diff --git a/internal/cloudapi/v2/middleware.go b/internal/cloudapi/v2/middleware.go index 3d2336512..d60923107 100644 --- a/internal/cloudapi/v2/middleware.go +++ b/internal/cloudapi/v2/middleware.go @@ -1,6 +1,7 @@ package v2 import ( + "github.com/getkin/kin-openapi/openapi3filter" "github.com/labstack/echo/v4" "github.com/osbuild/osbuild-composer/internal/auth" ) @@ -19,3 +20,31 @@ func (s *Server) getTenantChannel(ctx echo.Context) (string, error) { } return channel, nil } + +func (s *Server) ValidateRequest(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + request := c.Request() + + // extract route and parameters from request + route, params, err := s.router.FindRoute(request) + if err != nil { + return HTTPErrorWithInternal(ErrorResourceNotFound, err) + } + + input := &openapi3filter.RequestValidationInput{ + Request: request, + PathParams: params, + Route: route, + Options: &openapi3filter.Options{ + AuthenticationFunc: openapi3filter.NoopAuthenticationFunc, + }, + } + + ctx := request.Context() + if err := openapi3filter.ValidateRequest(ctx, input); err != nil { + return HTTPErrorWithInternal(ErrorInvalidRequest, err) + } + + return next(c) + } +} diff --git a/internal/cloudapi/v2/server.go b/internal/cloudapi/v2/server.go index 055370789..672a5b876 100644 --- a/internal/cloudapi/v2/server.go +++ b/internal/cloudapi/v2/server.go @@ -9,7 +9,6 @@ import ( "time" "github.com/getkin/kin-openapi/openapi3" - "github.com/getkin/kin-openapi/openapi3filter" "github.com/getkin/kin-openapi/routers" legacyrouter "github.com/getkin/kin-openapi/routers/legacy" "github.com/google/uuid" @@ -91,34 +90,6 @@ func (s *Server) Handler(path string) http.Handler { return e } -func (s *Server) ValidateRequest(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { - request := c.Request() - - // extract route and parameters from request - route, params, err := s.router.FindRoute(request) - if err != nil { - return HTTPErrorWithInternal(ErrorResourceNotFound, err) - } - - input := &openapi3filter.RequestValidationInput{ - Request: request, - PathParams: params, - Route: route, - Options: &openapi3filter.Options{ - AuthenticationFunc: openapi3filter.NoopAuthenticationFunc, - }, - } - - ctx := request.Context() - if err := openapi3filter.ValidateRequest(ctx, input); err != nil { - return HTTPErrorWithInternal(ErrorInvalidRequest, err) - } - - return next(c) - } -} - func (s *Server) Shutdown() { s.goroutinesCtxCancel() s.goroutinesGroup.Wait()