How Set Middleware Gorillas/Csrf in Framework Gin from Go
Learning Gin, Framework for Go, I Need Insert a Csrf Token, Searchin I Find the Gorillas Utils for Make Csrf and Other Thing, but My Problem Is the Next. This...
learning gin, framework for go, i need insert a csrf token, searchin i find the gorillas utils for make csrf and other thing, but my problem is the next. this
csrfMiddleware := csrf.Protect([]byte("32-byte-long-auth-key"))
make this type
func Protect(authKey []byte, opts ...Option) func(http.Handler) http.Handler
and try use this middleware function whith gin, especifically:
g := gin.Default()
g.Use(csrfMiddleware) ///set a middleware
how can use middlware csrf Protect with gin??, in the doc claims to be compatible. i confussed.
with this code yes working, but I don't know if it's correct, in the doc says if middleware works correctly, csrf.Token should generate a token, else, generate nothing
type helloWorldHandler struct{}
func (h helloWorldHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Println("entro")
}
func main() {
r := gin.Default()
csrfMiddleware := csrf.Protect([]byte("32-byte-long-auth-key"))
var d helloWorldHandler
r.Use(gin.WrapH(csrfMiddleware(d)))
r.GET("/test/session/:id", func(c *gin.Context) {
a := csrf.Token(c.Request)
fmt.Println(a)
c.Header("X-CSRF-Token", csrf.Token(c.Request))
})
// Listen and serve on 0.0.0.0:8080