/
captcha.go
64 lines (57 loc) · 1.41 KB
/
captcha.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
package kgo
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"time"
)
const (
siteVerifyURL = "https://www.google.com/recaptcha/api/siteverify"
)
var (
// ErrInvalidRecaptchaToken ...
ErrInvalidRecaptchaToken = errors.New("invalid google recaptcha token")
)
// RecaptchaResponse ...
type RecaptchaResponse struct {
Success bool `json:"success"`
Score float64 `json:"score"`
Action string `json:"action"`
ChallengeTS time.Time `json:"challenge_ts"`
Hostname string `json:"hostname"`
ErrorCodes []string `json:"error-codes"`
}
// VerifyRecaptcha ...
func VerifyRecaptcha(ip, token, key string) (*RecaptchaResponse, error) {
if len(token) == 0 {
return nil, ErrInvalidRecaptchaToken
}
resp, err := http.PostForm(
siteVerifyURL,
url.Values{"secret": {key}, "remoteip": {ip}, "response": {token}},
)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var x RecaptchaResponse
err = json.Unmarshal(body, &x)
if err != nil {
return nil, err
}
return &x, nil
}
// VerifyRecaptchaV2 ...
func VerifyRecaptchaV2(ip, token, secretKey string) (*RecaptchaResponse, error) {
return VerifyRecaptcha(ip, token, secretKey)
}
// VerifyRecaptchaV3 ...
func VerifyRecaptchaV3(ip, token, secretKey string) (*RecaptchaResponse, error) {
return VerifyRecaptcha(ip, token, secretKey)
}