summaryrefslogtreecommitdiff
path: root/csrf.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--csrf.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/csrf.go b/csrf.go
new file mode 100644
index 0000000..962a2a0
--- /dev/null
+++ b/csrf.go
@@ -0,0 +1,27 @@
+package main
+
+import (
+ "html/template"
+ "net/http"
+
+ "github.com/gorilla/csrf"
+ "github.com/gorilla/securecookie"
+)
+
+func csrfHandler(next http.Handler) http.Handler {
+ return csrf.Protect(
+ securecookie.GenerateRandomKey(32),
+ csrf.SameSite(csrf.SameSiteStrictMode),
+ csrf.FieldName("csrf.csrffield"), // should match the structure in `Csrf`
+ )(next)
+}
+
+// Csrf handles the CSRF data for a form.
+// Include it verbatim and then use `{{.CsrfField}}` in templates.
+type Csrf struct {
+ CsrfField template.HTML `form:"-"`
+}
+
+func CsrfField(r *http.Request) Csrf {
+ return Csrf{CsrfField: csrf.TemplateField(r)}
+}