Skip to content

Echo: Custom Authentication Middleware help. #911

Description

@debuggerpk

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions