-
Notifications
You must be signed in to change notification settings - Fork 1
/
recaptcha.go
57 lines (46 loc) · 1.48 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
//go:generate ffjson -noencoder $GOFILE
import (
"encoding/json"
"net/http"
"net/url"
)
const endpoint = `https://www.google.com/recaptcha/api/siteverify`
// Recaptcha must be created by the function New.
type Recaptcha struct {
// secret is stored as slice of strings to avoid new alloc on each verification
secret []string
}
type response struct {
Success bool `json:"success"`
ErrorCodes []string `json:"error-codes"`
}
// New creates a new recaptcha instance with given secret.
func New(secret string) *Recaptcha {
return &Recaptcha{
secret: []string{secret},
}
}
// Verify validates a captcha response with the google servers. When returns true: the catpcha was valid.
// The returned error might be of type Error, indicating that the API request had invalid information in it.
// See https://developers.google.com/recaptcha/docs/verify for a list of error codes.
func (r *Recaptcha) Verify(captchaResponse string, remoteIP string) (bool, error) {
values := url.Values{"secret": r.secret, "response": []string{captchaResponse}}
if remoteIP != "" {
values["remoteip"] = []string{remoteIP}
}
resp, err := http.PostForm(endpoint, values)
if err != nil {
return false, err
}
defer resp.Body.Close()
verification := response{}
err = json.NewDecoder(resp.Body).Decode(&verification)
if err != nil {
return false, err
}
if len(verification.ErrorCodes) != 0 {
return false, &Error{Codes: verification.ErrorCodes}
}
return verification.Success, nil
}