Skip to content

Commit

Permalink
Update csrf.go
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon authored Nov 26, 2024
1 parent eb711f7 commit 32e7151
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions csrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,32 +160,28 @@ func Csrfer(opts ...Options) flamego.Handler {
opt = opts[0]
}

parseOptions := func(opts Options) Options {
if opt.Secret == "" {
opt.Secret = string(randomBytes(10))
}
if opt.Secret == "" {
opt.Secret = string(randomBytes(10))
}

if opt.Header == "" {
opt.Header = defaultHeader
}
if opt.Header == "" {
opt.Header = defaultHeader
}

if opt.Form == "" {
opt.Form = defaultForm
}
if opt.Form == "" {
opt.Form = defaultForm
}

if opt.SessionKey == "" {
opt.SessionKey = defaultSessionKey
}
if opt.SessionKey == "" {
opt.SessionKey = defaultSessionKey
}

if opt.ErrorFunc == nil {
opt.ErrorFunc = func(w http.ResponseWriter) {
http.Error(w, "Bad Request: invalid CSRF token", http.StatusBadRequest)
}
if opt.ErrorFunc == nil {
opt.ErrorFunc = func(w http.ResponseWriter) {
http.Error(w, "Bad Request: invalid CSRF token", http.StatusBadRequest)
}
return opt
}

opt = parseOptions(opt)
return csrfInvoker(func(c flamego.Context, s session.Session) {
x := &csrf{
secret: opt.Secret,
Expand Down

0 comments on commit 32e7151

Please sign in to comment.