volatiletech / authboss

The boss of http auth.
MIT License
3.81k stars 207 forks source link

Provide a gin-gonic example for the community #224

Closed jesusvazquez closed 3 years ago

jesusvazquez commented 5 years ago

Hello!

First of all, thanks for authboss, it is a very promising project and I can feel the effort you've made all over the code.

I've done a bit of search on how to integrate authboss and gin-gonic in this repository and I've found https://github.com/volatiletech/authboss/issues/193 https://github.com/volatiletech/authboss/issues/157 and this wiki integration with gin gonic however I didn't manage to do it just yet. @aarondl made up clear that the goal of this repository is not to maintain compatibility but to maintain enough abstraction to work on any stack.

Gin together with gorilla, httprouter and chi are ones of the most common http servers out there for golang and I believe that we could work on some good examples for their users. https://github.com/volatiletech/authboss-sample is such a great example of authboss features but it only shows how to do it with chi. You'd have to guess how to do it with other libraries.

So thats what I'm going to do, I'm going to guess a way to do it until it finally works and then I'll contribute with some docs or examples. But for that I need a hand, I'm not very experienced and been struggling with this for a couple of hours already :)

Check this out https://github.com/jesusvazquez/authboss-gin-sample

❯ GIN_MODE=debug go run .
## Variables at the top
- Host: 127.0.0.1 - Port: 3000
## Debugging authboss
- ab.Config.Paths.RootURL: http://127.0.0.1:3000
- ab.Config.Paths.Mount: /auth
- Auth Module Loaded?: false
## Gin Routes loaded
- /auth/*w
- /status
- /auth/*w
- /auth/*w
- /auth/*w
- /auth/*w
- /auth/*w
- /auth/*w
- /auth/*w
- /auth/*w

From this output we can see that authboss its instantiating the routes although auth is not loaded.

Now first requests:

[GIN] 2019/03/29 - 16:54:33 | 200 |      41.929µs |       127.0.0.1 | GET      /status
[GIN] 2019/03/29 - 16:54:37 | 404 |      26.447µs |       127.0.0.1 | POST     /auth/login
 ❯ curl --silent -XGET -H "Content-Type: application/json" http://localhost:3000/status
{"status":"ok"}
❯ curl --silent -XPOST -H "Content-Type: application/json" http://localhost:3000/auth/login
404 page not found

Any ideas?

Thank you

jesusvazquez commented 5 years ago

Also please note I've disabled https://github.com/jesusvazquez/authboss-gin-sample/blob/147762e50b558e31eccd340c59e271b5d02093f8/main.go#L74-L77

    // ab.Config.Storage.SessionState = sessionStore
    // ab.Config.Storage.CookieState = cookieStore

Because when I enable those I get panics:

2019/03/29 16:59:41 [Recovery] 2019/03/29 - 16:59:41 panic recovered: POST /auth/login HTTP/1.1 Host: localhost:3000 Accept: */* Content-Type: application/json User-Agent: curl/7.64.0 runtime error: invalid memory address or nil pointer dereference /usr/lib/go/src/runtime/panic.go:513 (0x42c298) gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz)) /usr/lib/go/src/runtime/panic.go:82 (0x42b3ed) panicmem: panic(memoryError) /usr/lib/go/src/runtime/signal_unix.go:390 (0x441631) sigpanic: panicmem() /home/jesusdev/dev/personal/authboss-gin-sample/abclientstate/sessions.go:74 (0x9565d6) SessionStorer.ReadState: session, err := s.Store.Get(r, s.Name) /home/jesusdev/go/pkg/mod/github.com/volatiletech/authboss@v2.2.0+incompatible/client_state.go:138 (0x94e14c) (*Authboss).LoadClientState: state, err := a.Storage.SessionState.ReadState(r) /home/jesusdev/go/pkg/mod/github.com/volatiletech/authboss@v2.2.0+incompatible/client_state.go:112 (0x95249e) (*Authboss).LoadClientStateMiddleware.func1: request, err := a.LoadClientState(writer, r) /home/jesusdev/go/pkg/mod/github.com/volatiletech/authboss@v2.2.0+incompatible/client_state.go:112 (0x95249e) (*Authboss).LoadClientStateMiddleware.func1: request, err := a.LoadClientState(writer, r) /usr/lib/go/src/net/http/server.go:1964 (0x682333) HandlerFunc.ServeHTTP: f(w, r) /home/jesusdev/go/pkg/mod/github.com/gwatts/gin-adapter@v0.0.0-20170508204228-c44433c485ad/adapter.go:49 (0x93ff0a) New.func1.1: h.ServeHTTP(c.Writer, c.Request.WithContext(ctx)) /home/jesusdev/go/pkg/mod/github.com/gin-gonic/gin@v1.3.0/context.go:108 (0x92c992) (*Context).Next: c.handlers[c.index](c) /home/jesusdev/go/pkg/mod/github.com/gin-gonic/gin@v1.3.0/recovery.go:48 (0x93dc89) RecoveryWithWriter.func1: c.Next() /home/jesusdev/go/pkg/mod/github.com/gin-gonic/gin@v1.3.0/context.go:108 (0x92c992) (*Context).Next: c.handlers[c.index](c) /home/jesusdev/go/pkg/mod/github.com/gin-gonic/gin@v1.3.0/recovery.go:48 (0x93dc89) RecoveryWithWriter.func1: c.Next() /home/jesusdev/go/pkg/mod/github.com/gin-gonic/gin@v1.3.0/context.go:108 (0x92c992) (*Context).Next: c.handlers[c.index](c) /home/jesusdev/go/pkg/mod/github.com/gin-gonic/gin@v1.3.0/logger.go:84 (0x93cf91) LoggerWithWriter.func1: c.Next() /home/jesusdev/go/pkg/mod/github.com/gin-gonic/gin@v1.3.0/context.go:108 (0x92c992) (*Context).Next: c.handlers[c.index](c) /home/jesusdev/go/pkg/mod/github.com/gin-gonic/gin@v1.3.0/gin.go:361 (0x93513a) (*Engine).handleHTTPRequest: c.Next() /home/jesusdev/go/pkg/mod/github.com/gin-gonic/gin@v1.3.0/gin.go:326 (0x934961) (*Engine).ServeHTTP: engine.handleHTTPRequest(c) /usr/lib/go/src/net/http/server.go:2741 (0x68514a) serverHandler.ServeHTTP: handler.ServeHTTP(rw, req) /usr/lib/go/src/net/http/server.go:1847 (0x681395) (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req) /usr/lib/go/src/runtime/asm_amd64.s:1333 (0x4594c0) goexit: BYTE $0x90 // NOP [GIN] 2019/03/29 - 16:59:41 | 500 | 3.986078ms | 127.0.0.1 | POST /auth/login
jesusvazquez commented 5 years ago

Hey @aarondl could you take a look? I'm a bit stuck and perhaps you'll be able to guide me. With a quick review if you have any idea just let me know and I'll work it out.

aarondl commented 5 years ago

Hello @jesusvazquez. I'm quite busy with many projects which is why I didn't respond to this issue originally. I'm uninterested in spending any time supporting gin-gonic and as such integration in left as an exercise for people who know and love gin-gonic.

I'll mention a few things that may help you:

Good luck!

niaiwomattt commented 4 years ago

@jesusvazquez https://github.com/niaiwomattt/authboss-db-gorm-sample I implemented a simple data store, do you see it useful?

jesusvazquez commented 4 years ago

@niaiwomattt 现在我在旅游, 过几天我再看, 谢谢

(I'm off for a couple of days, I'll take a look when I come back, thank you)

siredwin commented 4 years ago

@niaiwomattt, your implementation is like mine in MongoDB. Thanks, it helped me implement my own.