From 869fb9691f877116d5b15a92de006d0daf4d70e5 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Thu, 17 Oct 2024 00:27:08 +0200 Subject: Restructure and change to chi as muxing framework --- csrf/csrf.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 csrf/csrf.go (limited to 'csrf/csrf.go') diff --git a/csrf/csrf.go b/csrf/csrf.go new file mode 100644 index 0000000..18fdb81 --- /dev/null +++ b/csrf/csrf.go @@ -0,0 +1,31 @@ +package csrf + +import ( + "html/template" + "net/http" + + "github.com/gorilla/csrf" + "github.com/gorilla/securecookie" +) + +func Handler() func(http.Handler) http.Handler { + return csrf.Protect( + securecookie.GenerateRandomKey(32), + csrf.SameSite(csrf.SameSiteStrictMode), + csrf.FieldName("csrf.csrffield"), // should match the structure in `Csrf` + ) +} + +// Csrf handles the CSRF data for a form. +// Include it verbatim and then use `{{.CsrfField}}` in templates. +type Csrf struct { + CsrfField template.HTML `form:"-" schema:"-"` +} + +func (c *Csrf) SetCsrfField(r *http.Request) { + c.CsrfField = csrf.TemplateField(r) +} + +type Enabled interface { + SetCsrfField(r *http.Request) +} -- cgit v1.2.3-70-g09d2