oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Echo: Custom Authentication Middleware help.

Open debuggerpk opened this issue 3 years ago • 1 comments

for the generated ServerInterfaceWrapper below, the middleware is executed before the below function is executed. Which is correct. I want to check the set APIKeyAuthScopes, but since the context is set after the middleware is called, I cannot do so.

func (w *ServerInterfaceWrapper) ValidateAPIKey(ctx echo.Context) error {
	var err error

	ctx.Set(APIKeyAuthScopes, []string{"auth"})

	// Invoke the callback with all the unmarshalled arguments
	err = w.Handler.ValidateAPIKey(ctx)
	return err
}

Here is my middleware

func Middleware(next echo.HandlerFunc) echo.HandlerFunc {
	return func(ctx echo.Context) error {
		printContext(ctx, true)
		printHeaders(ctx)

		keyScopes, requiresKey := ctx.Get(APIKeyAuthScopes).([]string)
		bearerScopes, requiresBearer := ctx.Get(BearerAuthScopes).([]string)
		// do something and return appropriate
	}
}

This is my echo server

	// web server based on echo
	e := echo.New()
	e.Use(middleware.CORS())
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())
	e.Use(auth.Middleware)
	e.Validator = &shared.EchoValidator{Validator: shared.Validator}

	auth.RegisterHandlers(e, &auth.ServerHandler{})

Is there a step I am missing?

debuggerpk avatar Dec 30 '22 01:12 debuggerpk

possible duplicate of #114

debuggerpk avatar Dec 30 '22 16:12 debuggerpk