cloudapi: move ValidateRequest() to middleware.go

Consolidate middleware functions to a single file.
This commit is contained in:
Tomas Hozza 2022-06-03 22:29:35 +02:00 committed by Tom Gundersen
parent 6fa2aa7b4a
commit c5e1c15cca
2 changed files with 29 additions and 29 deletions

View file

@ -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)
}
}

View file

@ -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()