<commit_before>package xsrf

import (
	"errors"
	"fmt"
	"html/template"

	"github.com/webx-top/echo"
	"github.com/webx-top/webx/lib/codec"
	"github.com/webx-top/webx/lib/com"
	"github.com/webx-top/webx/lib/uuid"
)

func New(args ...Manager) *Xsrf {
	x := &Xsrf{
		FieldName: `_xsrf`,
		On:        true,
	}
	if len(args) > 0 {
		x.Manager = args[0]
	} else {
		x.Manager = &CookieStorage{
			Secret: com.SelfPath() + `@webx.top`,
			Codec:  codec.Default,
		}
	}
	return x
}

type Xsrf struct {
	Manager
	FieldName string
	On        bool
}

func (c *Xsrf) Value(ctx echo.Context) string {
	var val string = c.Manager.Get(c.FieldName, ctx)
	if val == "" {
		val = uuid.NewRandom().String()
		c.Manager.Set(c.FieldName, val, ctx)
	}
	return val
}

func (c *Xsrf) Form(ctx echo.Context) template.HTML {
	var html string
	if c.On {
		html = fmt.Sprintf(`<input type="hidden" name="%v" value="%v" />`, c.FieldName, com.HtmlEncode(c.Value(ctx)))
	}
	return template.HTML(html)
}

func (c *Xsrf) Ignore(on bool, ctx echo.Context) {
	ctx.Set(`webx:ignoreXsrf`, on)
}

func (c *Xsrf) Register(ctx echo.Context) map[string]interface{} {
	ctx.SetFunc("XsrfForm", func() template.HTML {
		return c.Form(ctx)
	})
	ctx.SetFunc("XsrfValue", func() string {
		return c.Value(ctx)
	})
	ctx.SetFunc("XsrfName", func() string {
		return c.FieldName
	})
	return funcs
}

func (c *Xsrf) Middleware() echo.MiddlewareFunc {
	return func(h echo.HandlerFunc) echo.HandlerFunc {
		return func(ctx echo.Context) error {
			if !c.On {
				return h(ctx)
			}
			if ignore, _ := ctx.Get(`webx:ignoreXsrf`).(bool); ignore {
				return h(ctx)
			}
			val := c.Value(ctx)
			if ctx.Request().Method == `POST` {
				formVal := ctx.Form(c.FieldName)
				if formVal == "" || val != formVal {
					return errors.New("xsrf token error.")
				}
			}
			return h(ctx)
		}<commit_msg>fixes bug<commit_after>package xsrf

import (
	"errors"
	"fmt"
	"html/template"

	"github.com/webx-top/echo"
	"github.com/webx-top/webx/lib/codec"
	"github.com/webx-top/webx/lib/com"
	"github.com/webx-top/webx/lib/uuid"
)

func New(args ...Manager) *Xsrf {
	x := &Xsrf{
		FieldName: `_xsrf`,
		On:        true,
	}
	if len(args) > 0 {
		x.Manager = args[0]
	} else {
		x.Manager = &CookieStorage{
			Secret: com.SelfPath() + `@webx.top`,
			Codec:  codec.Default,
		}
	}
	return x
}

type Xsrf struct {
	Manager
	FieldName string
	On        bool
}

func (c *Xsrf) Value(ctx echo.Context) string {
	var val string = c.Manager.Get(c.FieldName, ctx)
	if val == "" {
		val = uuid.NewRandom().String()
		c.Manager.Set(c.FieldName, val, ctx)
	}
	return val
}

func (c *Xsrf) Form(ctx echo.Context) template.HTML {
	var html string
	if c.On {
		html = fmt.Sprintf(`<input type="hidden" name="%v" value="%v" />`, c.FieldName, com.HtmlEncode(c.Value(ctx)))
	}
	return template.HTML(html)
}

func (c *Xsrf) Ignore(on bool, ctx echo.Context) {
	ctx.Set(`webx:ignoreXsrf`, on)
}

func (c *Xsrf) Register(ctx echo.Context) {
	ctx.SetFunc("XsrfForm", func() template.HTML {
		return c.Form(ctx)
	})
	ctx.SetFunc("XsrfValue", func() string {
		return c.Value(ctx)
	})
	ctx.SetFunc("XsrfName", func() string {
		return c.FieldName
	})
}

func (c *Xsrf) Middleware() echo.MiddlewareFunc {
	return func(h echo.HandlerFunc) echo.HandlerFunc {
		return func(ctx echo.Context) error {
			if !c.On {
				return h(ctx)
			}
			if ignore, _ := ctx.Get(`webx:ignoreXsrf`).(bool); ignore {
				return h(ctx)
			}
			val := c.Value(ctx)
			if ctx.Request().Method == `POST` {
				formVal := ctx.Form(c.FieldName)
				if formVal == "" || val != formVal {
					return errors.New("xsrf token error.")
				}
			}
			return h(ctx)
		}
{'fraction_non_alphanumeric': 0.17238539665111172, 'fraction_numerical': 0.0010979961570134504, 'mean_word_length': 3.276995305164319, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '572306', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1492, 'n_words': 308}