forked from josephspurrier/gowebapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recaptcha.go
57 lines (45 loc) · 1 KB
/
recaptcha.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
package recaptcha
import (
"html/template"
"net/http"
"github.com/haisum/recaptcha"
)
var (
recap RecaptchaInfo
)
// RecaptchaInfo has the details for the Google reCAPTCHA
type RecaptchaInfo struct {
Enabled bool
Secret string
SiteKey string
}
// Configure adds the settings for Google reCAPTCHA
func Configure(c RecaptchaInfo) {
recap = c
}
// ReadConfig returns the settings for Google reCAPTCHA
func ReadConfig() RecaptchaInfo {
return recap
}
// Verified returns whether the Google reCAPTCHA was verified or not
func Verified(r *http.Request) bool {
if !recap.Enabled {
return true
}
// Check the reCaptcha
re := recaptcha.R{
Secret: recap.Secret,
}
return re.Verify(*r)
}
// RecaptchaFuncMapp returns a map of functions that are usable in templates
func RecaptchaPlugin() template.FuncMap {
f := make(template.FuncMap)
f["RECAPTCHA_SITEKEY"] = func() template.HTML {
if ReadConfig().Enabled {
return template.HTML(ReadConfig().SiteKey)
}
return template.HTML("")
}
return f
}