-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Custom ClientStateReadWriter implementation #307
Comments
You can look at the authboss client state, which is an implementation of the |
Hi @ibraheemdev and thanks for pointing me to the authboss client state repo. I have looked at these implementations for the This interface ensures both objects are reading and writing states using the following structure;
Is there a possibility to i.e. port Fiber's Ctx or FastHTTP's Context object to be able to comply to the Thanks for the help! |
I'm not familiar with Fiber or FastHTTP context, but it should be as simple as creating wrapper functions to satisfy the |
@thomasvvugt did you find a solution for this? |
Hi @frederikhors, unfortunately not. |
@thomasvvugt Have you looked into import (
"github.com/gofiber/fiber"
"github.com/valyala/fasthttp/fasthttpadaptor"
"net/http"
)
...
g.Get("/", WrapHandler(pprof.Index))
func WrapHandler(f func(http.ResponseWriter, *http.Request)) func(ctx *fiber.Ctx) {
return func(ctx *fiber.Ctx) {
fasthttpadaptor.NewFastHTTPHandler(http.HandlerFunc(f))(ctx.Fasthttp)
}
} code block originally from this issue Edit: You should probably use |
@ibraheemdev I tried very hard, but I can't figure out how to do. This is func (a *Authboss) LoadClientStateMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writer := a.NewResponse(w)
request, err := a.LoadClientState(writer, r)
if err != nil {
logger := a.RequestLogger(r)
logger.Errorf("failed to load client state %+v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
h.ServeHTTP(writer, request)
})
} This is was I also tried: app := fiber.New()
app.Use(WrapHandler(authboss.LoadClientStateMiddleware))
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World 👋!")
})
_ = app.Listen(":3000")
}
func WrapHandler(f func(http.Handler) http.Handler) func(ctx *fiber.Ctx) {
return func(ctx *fiber.Ctx) {
//fasthttpadaptor.NewFastHTTPHandler(http.HandlerFunc(f))(ctx.Fasthttp)
adaptor.HTTPHandlerFunc(http.HandlerFunc(f))(ctx)
} But this is simply wrong. |
Well, this seems to work: func main() {
// New fiber app
app := fiber.New()
app.Get("/ping", adaptor.HTTPHandler(logMiddleware(adaptor.FiberHandler(ping))))
// Listen on port 3000
app.Listen(":3000")
}
func ping(c *fiber.Ctx) error {
return c.SendString("pong")
}
func logMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
println("logging...")
next.ServeHTTP(w, r)
})
}
func greet(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World!")
} So, I'm sure it's possible. It'll just take some fiddling with. |
Oh wow. Thanks for your commitment. Your code works. But what I don't understand is how to use As stated in Authboss docs, What we need is something like: app := fiber.New()
app.Use(adaptor.HTTPHandlerORSOMETHINGIDONTUNDERSTAND(initializedAuthboss.LoadClientStateMiddleware))
app.Get("/ping", ping)
app.Listen(":3000") This is the problem. Thanks again. |
Using the below code I get this error:
func main() {
app := fiber.New()
app.Use(logMiddleware)
app.Get("/ping", ping)
_ = app.Listen(":3000")
}
func ping(c *fiber.Ctx) error {
return c.SendString("pong")
}
func logMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
println("logging...")
next.ServeHTTP(w, r)
})
} |
I'm not really familiar with fiber and fasthttp. I opened up an issue there (gofiber/fiber#943), hopefully they can help you out |
Thanks to @arsmn we can make it work now: gofiber/adaptor#27 (comment). Two remaining issues:
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:
FULL REPRODUCTION PROJECT HERE: https://github.com/frederikhors/fiber-and-authboss |
Hi @frederikhors. Not sure I can help out here. Never used fiber before. |
Was there any progress on this? I'm looking into how to integrate authboss natively in GoFiber. Biggest program is the lack of fasthttp support. While the adaptor middleware should work, it does introduce overhead. |
Hi all,
I am trying to combine authboss into a popular upcoming Go web-framework, Fiber and GORM to improve my current authentication implementation.
Fiber was built with FastHTTP as its core component with the ease-of-use and API-compatibility in mind of express.
Fiber has it's own session package, based on FastHTTP's session package.
Also, Fiber has developed methods to set and retrieve cookie values.
However, when creating the required
SessionState
andCookieState
storage configuration objects, I figured theClientStateReadWriter
interface only allows Reading and Writing the defaulthttp
Request and Response package; https://github.com/volatiletech/authboss/blob/master/client_state.go#L79I would love to implement Fiber's official session middleware and cookie methods to use authboss for authentication, but I have no clue on where to start regarding the implementation of the
SessionState
andCookieState
or maybe I am even the purpose of theMiddleware2()
method.I have started development for this implementation on the develop branch of my personal repo.
Would you be able to give me a better approach into creating a flawless integration for the authboss package, using Fiber's
Ctx
(Context) object and it's session middleware?Thanks so much in advance, I would really love to collaborate on this further!
The text was updated successfully, but these errors were encountered: