Skip to content

Echo: Custom Authentication Middleware help. #911

@debuggerpk

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?

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy