-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmiddleware.go
61 lines (53 loc) · 1.34 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"log"
"net/http"
"github.com/davecgh/go-spew/spew"
"github.com/justinas/nosurf"
"github.com/volatiletech/authboss/v3"
)
func nosurfing(h http.Handler) http.Handler {
surfing := nosurf.New(h)
surfing.SetFailureHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("Failed to validate CSRF token:", nosurf.Reason(r))
w.WriteHeader(http.StatusBadRequest)
}))
return surfing
}
func logger(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("\n%s %s %s\n", r.Method, r.URL.Path, r.Proto)
if *flagDebug {
session, err := sessionStore.Get(r, sessionCookieName)
if err == nil {
fmt.Print("Session: ")
first := true
for k, v := range session.Values {
if first {
first = false
} else {
fmt.Print(", ")
}
fmt.Printf("%s = %v", k, v)
}
fmt.Println()
}
}
if *flagDebugDB {
fmt.Println("Database:")
for _, u := range database.Users {
fmt.Printf("! %#v\n", u)
}
}
if *flagDebugCTX {
if val := r.Context().Value(authboss.CTXKeyData); val != nil {
fmt.Printf("CTX Data: %s", spew.Sdump(val))
}
if val := r.Context().Value(authboss.CTXKeyValues); val != nil {
fmt.Printf("CTX Values: %s", spew.Sdump(val))
}
}
h.ServeHTTP(w, r)
})
}