-
Notifications
You must be signed in to change notification settings - Fork 1
/
recaptcha.go
56 lines (45 loc) · 1.08 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
package main
import (
"encoding/json"
"errors"
"log"
"net/http"
)
var ReCaptchaConf ReCaptchaConfig
const siteVerifyURL = "https://www.google.com/recaptcha/api/siteverify"
func CheckRecaptcha(secret, response string) error {
req, err := http.NewRequest(http.MethodPost, siteVerifyURL, nil)
if err != nil {
return err
}
// Add necessary request parameters.
q := req.URL.Query()
q.Add("secret", secret)
q.Add("response", response)
req.URL.RawQuery = q.Encode()
// Make request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// Decode response.
var body SiteVerifyResponse
if err = json.NewDecoder(resp.Body).Decode(&body); err != nil {
return err
}
log.Println(body)
// Check recaptcha verification success.
if !body.Success {
return errors.New("unsuccessful recaptcha verify request")
}
// Check response score.
if body.Score < 0.5 {
return errors.New("lower received score than expected")
}
// Check response action.
if body.Action != "submit" {
return errors.New("mismatched recaptcha action")
}
return nil
}