csrf
Martini cross-site request forgery protection middlware.
API Reference
Usage
package main
import (
"github.com/go-martini/martini"
"github.com/martini-contrib/csrf"
"github.com/martini-contrib/sessions"
"github.com/martini-contrib/render"
"net/http"
)
func main() {
m := martini.Classic()
store := sessions.NewCookieStore([]byte("secret123"))
m.Use(sessions.Sessions("my_session", store))
m.Use(csrf.Generate(&csrf.Options{
Secret: "token123",
SessionKey: "userID",
ErrorFunc: func(w http.ResponseWriter) {
http.Error(w, "CSRF token validation failed", http.StatusBadRequest)
}
}))
m.Use(render.Renderer())
m.Get("/", func(s sessions.Session, r render.Render) {
if s.Get("userID") == nil {
r.Redirect("/login", 302)
return
}
r.Redirect("/protected", 302)
})
m.Get("/login", func(s sessions.Session, r render.Render) {
s.Set("userID", "123456")
r.Redirect("/", 302)
})
m.Get("/protected", func(s sessions.Session, r render.Render, x csrf.CSRF) {
if s.Get("userID") == nil {
r.Redirect("/login", 401)
return
}
r.HTML(200, "protected", x.GetToken())
})
m.Post("/protected", csrf.Validate, func(s sessions.Session, r render.Render) {
if s.Get("userID") != nil {
r.HTML(200, "result", "You submitted a valid token")
return
}
r.Redirect("/login", 401)
})
m.Run()
}
Security
Applications using the method package should also validate PATCH, PUT, and DELETE requests.
Authors