forked from haruyama/golang-goji-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
126 lines (115 loc) · 3.54 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package system
import (
"crypto/rand"
"crypto/sha256"
"crypto/subtle"
"fmt"
"net/http"
"strings"
"github.com/coopernurse/gorp"
"github.com/golang/glog"
"github.com/gorilla/sessions"
"github.com/haruyama/golang-goji-sample/models"
"github.com/zenazn/goji/web"
)
// Makes sure templates are stored in the context
func (application *Application) ApplyTemplates(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
c.Env["Template"] = application.Template
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// Makes sure controllers can have access to session
func (application *Application) ApplySessions(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
session, _ := application.Store.Get(r, "session")
c.Env["Session"] = session
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func (application *Application) ApplyDbMap(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
c.Env["DbMap"] = application.DbMap
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func (application *Application) ApplyAuth(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
session := c.Env["Session"].(*sessions.Session)
if userId, ok := session.Values["UserId"]; ok {
dbMap := c.Env["DbMap"].(*gorp.DbMap)
user, err := dbMap.Get(models.User{}, userId)
if err != nil {
glog.Warningf("Auth error: %v", err)
c.Env["User"] = nil
} else {
c.Env["User"] = user
}
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func (application *Application) ApplyIsXhr(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-Requested-With") == "XMLHttpRequest" {
c.Env["IsXhr"] = true
} else {
c.Env["IsXhr"] = false
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func isValidToken(a, b string) bool {
x := []byte(a)
y := []byte(b)
if len(x) != len(y) {
return false
}
return subtle.ConstantTimeCompare(x, y) == 1
}
func (application *Application) ApplyCsrfProtection(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
session := c.Env["Session"].(*sessions.Session)
csrfProtection := application.CsrfProtection
if _, ok := session.Values["CsrfToken"]; !ok {
hash := sha256.New()
buffer := make([]byte, 32)
_, err := rand.Read(buffer)
if err != nil {
glog.Fatalf("crypt/rand.Read failed: %s", err)
}
hash.Write(buffer)
session.Values["CsrfToken"] = fmt.Sprintf("%x", hash.Sum(nil))
if err = session.Save(r, w); err != nil {
glog.Fatal("session.Save() failed")
}
}
c.Env["CsrfKey"] = csrfProtection.Key
c.Env["CsrfToken"] = session.Values["CsrfToken"]
csrfToken := c.Env["CsrfToken"].(string)
if c.Env["IsXhr"].(bool) {
if !isValidToken(csrfToken, r.Header.Get(csrfProtection.Header)) {
http.Error(w, "Invalid Csrf Header", http.StatusBadRequest)
}
} else {
method := strings.ToUpper(r.Method)
if method == "POST" || method == "PUT" || method == "DELETE" {
if !isValidToken(csrfToken, r.PostFormValue(csrfProtection.Key)) {
http.Error(w, "Invalid Csrf Token", http.StatusBadRequest)
}
}
}
http.SetCookie(w, &http.Cookie{
Name: csrfProtection.Cookie,
Value: csrfToken,
Secure: csrfProtection.Secure,
})
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}