-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
104 lines (90 loc) · 3.01 KB
/
client.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
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
package friendlycaptcha
import (
"fmt"
"net/http"
)
// A ClientOption is a function that can be passed to NewClient to configure a new Client.
type ClientOption func(*Client) error
// A client for the Friendly Captcha API, see also the API docs at https://developer.friendlycaptcha.com
type Client struct {
APIKey string
Sitekey string
SiteverifyEndpoint string
// If Strict is set to true only strictly verified captcha response will be allowed.
// For example: if your server can not reach the Friendly Captcha endpoint, it will still advise to accept the response
// regardless.
//
// By default Strict is false: `ShouldAccept()` will return true when for instance the Friendly Captcha API
// could not be reached.
Strict bool
// The HTTP client to use for making requests to the Friendly Captcha API.
// Defaults to `http.DefaultClient`
HTTPClient *http.Client
}
// The name of the form field that, by default, the widget will put the captcha response in.
const ResponseFormFieldName = "frc-captcha-response"
const (
globalSiteverifyEndpointURL = "https://global.frcapi.com/api/v2/captcha/siteverify"
euSiteverifyEndpointURL = "https://eu.frcapi.com/api/v2/captcha/siteverify"
)
// NewClient creates a new Friendly Captcha client with the given options.
func NewClient(opts ...ClientOption) (*Client, error) {
const (
defaultSiteverifyEndpoint = globalSiteverifyEndpointURL
)
c := &Client{
HTTPClient: http.DefaultClient,
SiteverifyEndpoint: defaultSiteverifyEndpoint,
}
// Loop through each option
for _, opt := range opts {
// Call the option giving the instantiated *Client as the argument
err := opt(c)
if err != nil {
return nil, err
}
}
if c.APIKey == "" {
return nil, fmt.Errorf(
"you must set your Friendly Captcha API key using `WithAPIKey()` when creating a new client",
)
}
return c, nil
}
// WithAPIKey sets the API key for the client.
func WithAPIKey(apiKey string) ClientOption {
return func(c *Client) error {
c.APIKey = apiKey
return nil
}
}
// WithSitekey sets the sitekey for the client. This is optional.
func WithSitekey(sitekey string) ClientOption {
return func(c *Client) error {
c.Sitekey = sitekey
return nil
}
}
// In strict mode only strictly verified captcha response are allowed. If your API key is invalid or your server can not reach the API endpoint all requests will be rejected.
//
// This defaults to `false`.
func WithStrictMode(strict bool) ClientOption {
return func(c *Client) error {
c.Strict = strict
return nil
}
}
// Takes a full URL, or the shorthands `"global"` or `"eu"`.
func WithSiteverifyEndpoint(siteverifyEndpoint string) ClientOption {
return func(c *Client) error {
if siteverifyEndpoint == "global" {
siteverifyEndpoint = globalSiteverifyEndpointURL
} else if siteverifyEndpoint == "eu" {
siteverifyEndpoint = euSiteverifyEndpointURL
} else if siteverifyEndpoint == "" {
return fmt.Errorf("siteverifyEndpoint must not be empty")
}
c.SiteverifyEndpoint = siteverifyEndpoint
return nil
}
}