This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
age-gate.go
78 lines (68 loc) · 1.54 KB
/
age-gate.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
package middleware
import (
"net/http"
"strings"
"github.com/kirsle/blog/models/settings"
"github.com/kirsle/blog/src/responses"
"github.com/kirsle/blog/src/sessions"
"github.com/urfave/negroni"
)
var ageGateSuffixes = []string{
"/blog.rss", // Allow public access to RSS and Atom feeds.
"/blog.atom",
"/blog.json",
".js",
".css",
".txt",
".ico",
".png",
".jpg",
".jpeg",
".gif",
".mp4",
".webm",
".ttf",
".eot",
".svg",
".woff",
".woff2",
}
// AgeGate is a middleware generator that does age verification for NSFW sites.
func AgeGate(verifyHandler func(http.ResponseWriter, *http.Request)) negroni.HandlerFunc {
middleware := func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
s, _ := settings.Load()
if !s.Site.NSFW {
next(w, r)
return
}
path := r.URL.Path
if strings.HasPrefix(path, "/age-verify") {
verifyHandler(w, r) // defer to the age gate handler itself.
return
}
// Allow static files and things through.
for _, suffix := range ageGateSuffixes {
if strings.HasSuffix(path, suffix) {
next(w, r)
return
}
}
// POST requests are allowed.
if r.Method == http.MethodPost {
next(w, r)
return
}
// See if they've been cleared.
session := sessions.Get(r)
if val, _ := session.Values["age-ok"].(bool); !val {
// They haven't been verified.
// Allow single-page loads with ?over18=1 in query parameter.
if r.FormValue("over18") == "" {
responses.Redirect(w, "/age-verify?next="+r.URL.Path)
return
}
}
next(w, r)
}
return middleware
}