https://github.com/donseba/go-csrf
Basic CSRF in golang
https://github.com/donseba/go-csrf
Last synced: 6 months ago
JSON representation
Basic CSRF in golang
- Host: GitHub
- URL: https://github.com/donseba/go-csrf
- Owner: donseba
- License: mit
- Created: 2023-03-12T14:58:21.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-12T22:30:08.000Z (over 3 years ago)
- Last Synced: 2025-12-01T22:12:11.753Z (8 months ago)
- Language: Go
- Size: 3.91 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-csrf
Basic CSRF handling in golang. There are quite a few versions on csrf handling, but I needed something to cover the bare minimum.
```go
package example
import (
"net/http"
"github.com/donseba/go-csrf"
)
func exampleFormHandler(w http.ResponseWriter, r *http.Request) {
c := csrf.New()
// the token should be generated on the page that invokes the form
token := c.GenerateToken()
// in this example we are going to set the csrf token into a cookie
// you could also pass the token to the form to post the token, and store the token in a session handler.
// most session handlers however also use a cookie with (no)sql backend.
c.SetCookie(w,token)
// use the middleware. which can be called using `csrf.New().Middleware()` and wrap it
// around the handler(s) that need validation. Easiest way is to use it at the root.
// with chi you can do this.
r := chi.NewRouter()
r.Use(csrf.New().Middleware)
}
```