-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorecaptcha.go
75 lines (60 loc) · 1.29 KB
/
gorecaptcha.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
package gorecaptcha
import (
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type recaptcha struct {
privateKey string
verifyURL string
}
func (re *recaptcha) makeVerifyRequest(
remoteIP string,
challenge string,
response string,
) (string, error) {
resp, err := http.PostForm(re.verifyURL, url.Values{
"privatekey": {re.privateKey},
"remoteip": {remoteIP},
"challenge": {challenge},
"response": {response},
})
defer resp.Body.Close()
if err != nil {
return "", err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
// NewRecaptcha is reCaptcha client constructor
func NewRecaptcha(privateKey string) *recaptcha {
return &recaptcha{
privateKey,
"http://www.google.com/recaptcha/api/verify",
}
}
// verify data via captcha server
// https://developers.google.com/recaptcha/docs/verify
func (re *recaptcha) Verify(
remoteIP string,
challenge string,
response string,
) (recaptchaResponse, error) {
body, err := re.makeVerifyRequest(remoteIP, challenge, response)
resp := recaptchaResponse{}
if err != nil {
return resp, err
}
lines := strings.Split(string(body), "\n")
if lines[0] == "true" {
resp.Status = true
} else {
resp.Status = false
resp.Err = parseErrorLine(lines[1])
}
return resp, nil
}