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 
2

Related questions 5 CSRF in Go Web Applications 0 Add middleware on HTTP 59 Go gin framework CORS Related questions 5 CSRF in Go Web Applications 0 Add middleware on HTTP 59 Go gin framework CORS 0 How to use Gin-gonic middleware with Google App Engine? 1 Implement the JWT middleware in golang using gin package and secure the other APIs? 9 Gin-Gonic middleware declaration 2 How do I secure a REST API from CSRF when both frontend and API are on the same Go server? 2 How to set a cookie with Gin JWT middleware 2 Testing CORS Middleware with Gin 1 Golang Gin: Middleware with CORS not working Load 7 more related questions Show fewer related questions

Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.