2025-06-13 17:23:16 +08:00

23 lines
496 B
Go

package mid
import (
"fmt"
"net/http"
"github.com/justinas/nosurf"
)
func NoSurf(next http.Handler) http.Handler {
return nosurf.New(next)
}
func NoSurfContext(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := nosurf.Token(r)
ctx := setCsrfToken(r.Context(), token)
ctx = setHtmlCsrfToken(ctx, fmt.Sprintf(`<input type="hidden" name="csrf_token" value="%s" />`, token))
next.ServeHTTP(w, r.WithContext(ctx))
})
}