-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.go
More file actions
106 lines (86 loc) · 2.34 KB
/
client.go
File metadata and controls
106 lines (86 loc) · 2.34 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package recaptcha
import (
"encoding/json"
"net/http"
"net/url"
"strings"
"time"
"github.com/pkg/errors"
)
const (
apiEndpoint = "https://www.google.com/recaptcha/api/siteverify"
defaultTimeout = 15 * time.Second
)
// Option for initializer.
type Option func(*Client)
// SetHTTPClient sets httpClient.
// Used in tests for stubing.
func SetHTTPClient(httpClient *http.Client) Option {
return func(cli *Client) {
cli.httpClient = httpClient
}
}
// SetTimeout sets timeout for the http client.
func SetTimeout(timeout time.Duration) Option {
return func(cli *Client) {
cli.httpClient.Timeout = timeout
}
}
// Client struct to verify captcha.
type Client struct {
secret string
httpClient *http.Client
}
// New returns initialized Client.
func New(secret string, options ...Option) *Client {
cli := Client{
secret: secret,
httpClient: &http.Client{
Timeout: defaultTimeout,
},
}
for _, option := range options {
option(&cli)
}
return &cli
}
// Verify verifies reCaptcha response received from frontend.
func (cli *Client) Verify(gRecaptchaResponse string) (*Response, error) {
return cli.verify(gRecaptchaResponse, nil)
}
// VerifyWithIP verifies reCaptcha response received from frontend with optional remoteip parameter.
func (cli *Client) VerifyWithIP(gRecaptchaResponse, remoteIP string) (*Response, error) {
return cli.verify(gRecaptchaResponse, &remoteIP)
}
func (cli *Client) verify(gRecaptchaResponse string, remoteIP *string) (*Response, error) {
form := url.Values{
"secret": []string{cli.secret},
"response": []string{gRecaptchaResponse},
}
if remoteIP != nil {
form.Set("remoteip", *remoteIP)
}
req, err := http.NewRequest("POST", apiEndpoint, strings.NewReader(form.Encode()))
if err != nil {
return nil, errors.Wrap(err, "new request")
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := cli.httpClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "send request")
}
defer resp.Body.Close()
response := Response{}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return &response, errors.Wrap(err, "unmarshal api response")
}
for _, errCode := range response.ErrorCodes {
if err, ok := errorsMap[errCode]; ok {
return &response, err
}
}
if !response.Success {
return &response, ErrUnsucceeded
}
return &response, nil
}