Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Use it with Authboss #27

Closed
frederikhors opened this issue Oct 19, 2020 · 13 comments
Closed

Use it with Authboss #27

frederikhors opened this issue Oct 19, 2020 · 13 comments

Comments

@frederikhors
Copy link

I'm trying to use authboss with Fiber.

I can't figure out how to use LoadClientStateMiddleware with adoptor like this:

app := fiber.New()
app.Use(adaptor.HTTPHandler(authboss.LoadClientStateMiddleware))

the error is:

Cannot use 'authb.LoadClientStateMiddleware' (type func(h http.Handler) http.Handler) as type http.Handler Type does not implement 'http.Handler' as some methods are missing: ServeHTTP(ResponseWriter, *Request)

Is there a way I can fix this?

@Fenny
Copy link
Member

Fenny commented Oct 19, 2020

Try HTTPHandlerFunc

app := fiber.New()
app.Use(adaptor.HTTPHandlerFunc(authboss.LoadClientStateMiddleware))
Name Signature Description
HTTPHandler HTTPHandler(h http.Handler) fiber.Handler http.Handler -> Fiber handler
HTTPHandlerFunc HTTPHandlerFunc(h http.HandlerFunc) fiber.Handler http.HandlerFunc -> Fiber handler
FiberHandler FiberHandler(h fiber.Handler) http.Handler Fiber handler -> http.Handler
FiberHandlerFunc FiberHandlerFunc(h fiber.Handler) http.HandlerFunc Fiber handler -> http.HandlerFunc
FiberApp FiberApp(app *fiber.App) http.HandlerFunc Fiber app -> http.HandlerFunc

@frederikhors
Copy link
Author

It doesn't work either.

@arsmn
Copy link
Contributor

arsmn commented Oct 27, 2020

Hi @frederikhors
You can use new HTTPMiddleware wrapper to convert http middlewares to Fiber middleware
But LoadClientStateMiddleware sets context value which does not transfer to Fiber handler

@frederikhors
Copy link
Author

frederikhors commented Oct 27, 2020

Thanks @arsmn, I'll try it in a few minutes...

Meanwhile, I opened gofiber/fiber#941 because I have a doubt:

I cannot understand if using Authboss with LoadClientStateMiddleware before all routes can affect the speed I get from Fiber if I start processing all requests through that middleware.

Can you help me understand this point, please?

Thanks.

@frederikhors
Copy link
Author

frederikhors commented Oct 27, 2020

The http middleware works now!

Two remaining issues:

  1. LoadClientStateMiddleware sets context value which does not transfer to Fiber handler, as you have noted; I'm trying to understand how to do

  2. how to transform this code mux.Mount("/auth", http.StripPrefix("/auth", ab.Config.Core.Router)) for Fiber; this code comes from here: https://github.com/volatiletech/authboss-sample/blob/master/blog.go#L274.

I'm trying with this code which compiles:

func main() {
	ab := SetupAuthboss()

	app := fiber.New()

	app.Use(adaptor.HTTPMiddleware(ab.LoadClientStateMiddleware), remember.Middleware(ab))

	app.Group("/auth", adaptor.HTTPHandler(http.StripPrefix("/auth", ab.Config.Core.Router)))

	_ = app.Listen(":3000")
}

but crashes at runtime:

panic: use: invalid handler func(http.Handler) http.Handler


goroutine 1 [running]:
github.com/gofiber/fiber/v2.(*App).Use(0xc00011b200, 0xc00010ff58, 0x2, 0x2, 0x1, 0xbdcd94)
  C:/go/pkg/mod/github.com/gofiber/fiber/[email protected]/app.go:391 +0x2d7
main.main()
  C:/projects/go/fiberAndAuthboss/main.go:18 +0x129
exit status 2

FULL REPRODUCTION PROJECT HERE: https://github.com/frederikhors/fiber-and-authboss

@Fenny
Copy link
Member

Fenny commented Oct 27, 2020

Hi @frederikhors, thanks to @arsmn you now can use adaptor.HTTPMiddleware in v2.0.2

app := fiber.New()
app.Use(adaptor.HTTPMiddleware(authboss.LoadClientStateMiddleware))

@Fenny Fenny closed this as completed Oct 27, 2020
@frederikhors
Copy link
Author

Oh, thanks @Fenny.

I know that. I answered with another issue. Can you please re-open this, please?

Thanks.

@Fenny Fenny reopened this Oct 28, 2020
@arsmn
Copy link
Contributor

arsmn commented Oct 29, 2020

  1. of course using middleware affects performance but I have not used Authboss and don't know how much is it.
  2. I'm trying to figuring out a way to transfer context values
  3. No idea about strip prefix right now, I guess you can handle it with a custom middleware
  4. about your app crash, remember.Middleware(ab) is not a valid Fiber middleware. You should use adaptor.HTTPMiddleware for this too

@frederikhors
Copy link
Author

  1. you're right. My mistake. I was incredibly distracted!

@mrsufgi
Copy link

mrsufgi commented Aug 30, 2021

For future reference; there is an issue with transfering context values when using a middleware (lets say you want to use auth0 jwt middleware). I'll try and find a solution and report here :)

@TheBeachMaster
Copy link

@mrsufgi did you find the solution?

@joshnies
Copy link

joshnies commented May 8, 2022

@mrsufgi Wondering about that solution as well, without context values Auth0's go-jwt-middleware is essentially broken

@mrsufgi
Copy link

mrsufgi commented May 8, 2022

He, Sorry for the late response, I ended up copying the HTTPMiddleware

// NOTE: we are copying the HTTPMiddleware adaptor because the request context doesn't get propegated to fiber context
func HTTPMiddleware(mw func(http.Handler) http.Handler) fiber.Handler {
	return func(c *fiber.Ctx) error {
		var next bool
		nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			next = true
			// Convert again in case request may modify by middleware
			c.Request().Header.SetMethod(r.Method)
			c.Request().SetRequestURI(r.RequestURI)
			c.Request().SetHost(r.Host)
			for key, val := range r.Header {
				for _, v := range val {
					c.Request().Header.Set(key, v)
				}
			}

			reqCtx := r.Context().Value(jwtmiddleware.ContextKey{})
			c.SetUserContext(r.Context())
			c.Locals("user", reqCtx)
			// more code that relates to ad-hoc custom claims)
		})
		_ = adaptor.HTTPHandler(mw(nextHandler))(c)
		if next {
			return c.Next()
		}
		return nil
	}
}

Please note that I wrote it very long time ago (still works) but there were some changes.
image

Honestly I think I'll just submit a PR with the important change

reqCtx := r.Context().Value(jwtmiddleware.ContextKey{})
c.SetUserContext(r.Context())
c.Locals("user", reqCtx)

and see that it propagates OK :)
Please note that "user" will only be available in Locals.

ping me on discord if you need: mrsufgi#7685

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants