forked from justinas/nosurf
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ignore.go
39 lines (32 loc) · 853 Bytes
/
ignore.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
package nosurf
import (
"net/http"
pathModule "path"
)
// Ignores the CSRF middleware for an exact path
// With this you should take note that Go's paths
// include a leading slash.
func (h *CSRFHandler) IgnorePath(path string) {
h.ignorePaths = append(h.ignorePaths, path)
}
// Checks if the given request ignores this middleware
func (h *CSRFHandler) IsIgnored(r *http.Request) bool {
path := r.URL.Path
if sContains(h.ignorePaths, path) {
return true
}
// then the globs
for _, glob := range h.ignoreGlobs {
matched, err := pathModule.Match(glob, path)
if matched && err == nil {
return true
}
}
return false
}
func (h *CSRFHandler) IgnoreGlob(pattern string) {
h.ignoreGlobs = append(h.ignoreGlobs, pattern)
}
func (h *CSRFHandler) IgnoreGlobs(patterns ...string) {
h.ignoreGlobs = append(h.ignoreGlobs, patterns...)
}