/
turnstile.go
245 lines (205 loc) · 7.43 KB
/
turnstile.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package cloudflare
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
var ErrMissingSiteKey = errors.New("required site key missing")
type TurnstileWidget struct {
SiteKey string `json:"sitekey,omitempty"`
Secret string `json:"secret,omitempty"`
CreatedOn *time.Time `json:"created_on,omitempty"`
ModifiedOn *time.Time `json:"modified_on,omitempty"`
Name string `json:"name,omitempty"`
Domains []string `json:"domains,omitempty"`
Mode string `json:"mode,omitempty"`
BotFightMode bool `json:"bot_fight_mode,omitempty"`
Region string `json:"region,omitempty"`
OffLabel bool `json:"offlabel,omitempty"`
}
type CreateTurnstileWidgetParams struct {
Name string `json:"name,omitempty"`
Domains []string `json:"domains,omitempty"`
Mode string `json:"mode,omitempty"`
BotFightMode bool `json:"bot_fight_mode,omitempty"`
Region string `json:"region,omitempty"`
OffLabel bool `json:"offlabel,omitempty"`
}
type UpdateTurnstileWidgetParams struct {
SiteKey string `json:"-"`
Name string `json:"name,omitempty"`
Domains []string `json:"domains,omitempty"`
Mode string `json:"mode,omitempty"`
BotFightMode bool `json:"bot_fight_mode,omitempty"`
Region string `json:"region,omitempty"`
OffLabel bool `json:"offlabel,omitempty"`
}
type TurnstileWidgetResponse struct {
Response
Result TurnstileWidget `json:"result"`
}
type ListTurnstileWidgetParams struct {
ResultInfo
Direction string `url:"direction,omitempty"`
Order OrderDirection `url:"order,omitempty"`
}
type ListTurnstileWidgetResponse struct {
Response
ResultInfo `json:"result_info"`
Result []TurnstileWidget `json:"result"`
}
type RotateTurnstileWidgetParams struct {
SiteKey string `json:"-"`
InvalidateImmediately bool `json:"invalidate_immediately,omitempty"`
}
// CreateTurnstileWidget creates a new challenge widgets.
//
// API reference: https://api.cloudflare.com/#challenge-widgets-properties
func (api *API) CreateTurnstileWidget(ctx context.Context, rc *ResourceContainer, params CreateTurnstileWidgetParams) (TurnstileWidget, error) {
if rc.Identifier == "" {
return TurnstileWidget{}, ErrMissingAccountID
}
uri := fmt.Sprintf("/accounts/%s/challenges/widgets", rc.Identifier)
res, err := api.makeRequestContext(ctx, "POST", uri, params)
if err != nil {
return TurnstileWidget{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var r TurnstileWidgetResponse
err = json.Unmarshal(res, &r)
if err != nil {
return TurnstileWidget{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// ListTurnstileWidgets lists challenge widgets.
//
// API reference: https://api.cloudflare.com/#challenge-widgets-list-challenge-widgets
func (api *API) ListTurnstileWidgets(ctx context.Context, rc *ResourceContainer, params ListTurnstileWidgetParams) ([]TurnstileWidget, *ResultInfo, error) {
if rc.Identifier == "" {
return []TurnstileWidget{}, &ResultInfo{}, ErrMissingAccountID
}
autoPaginate := true
if params.PerPage >= 1 || params.Page >= 1 {
autoPaginate = false
}
if params.PerPage < 1 {
params.PerPage = 25
}
if params.Page < 1 {
params.Page = 1
}
var widgets []TurnstileWidget
var r ListTurnstileWidgetResponse
for {
uri := buildURI(fmt.Sprintf("/accounts/%s/challenges/widgets", rc.Identifier), params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []TurnstileWidget{}, &ResultInfo{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
err = json.Unmarshal(res, &r)
if err != nil {
return []TurnstileWidget{}, &ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
widgets = append(widgets, r.Result...)
params.ResultInfo = r.ResultInfo.Next()
if params.ResultInfo.Done() || !autoPaginate {
break
}
}
return widgets, &r.ResultInfo, nil
}
// GetTurnstileWidget shows a single challenge widget configuration.
//
// API reference: https://api.cloudflare.com/#challenge-widgets-challenge-widget-details
func (api *API) GetTurnstileWidget(ctx context.Context, rc *ResourceContainer, siteKey string) (TurnstileWidget, error) {
if rc.Identifier == "" {
return TurnstileWidget{}, ErrMissingAccountID
}
if siteKey == "" {
return TurnstileWidget{}, ErrMissingSiteKey
}
uri := fmt.Sprintf("/accounts/%s/challenges/widgets/%s", rc.Identifier, siteKey)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return TurnstileWidget{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var r TurnstileWidgetResponse
err = json.Unmarshal(res, &r)
if err != nil {
return TurnstileWidget{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// UpdateTurnstileWidget update the configuration of a widget.
//
// API reference: https://api.cloudflare.com/#challenge-widgets-update-a-challenge-widget
func (api *API) UpdateTurnstileWidget(ctx context.Context, rc *ResourceContainer, params UpdateTurnstileWidgetParams) (TurnstileWidget, error) {
if rc.Identifier == "" {
return TurnstileWidget{}, ErrMissingAccountID
}
if params.SiteKey == "" {
return TurnstileWidget{}, ErrMissingSiteKey
}
uri := fmt.Sprintf("/accounts/%s/challenges/widgets/%s", rc.Identifier, params.SiteKey)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return TurnstileWidget{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var r TurnstileWidgetResponse
err = json.Unmarshal(res, &r)
if err != nil {
return TurnstileWidget{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// RotateTurnstileWidget generates a new secret key for this widget. If
// invalidate_immediately is set to false, the previous secret remains valid for
// 2 hours.
//
// Note that secrets cannot be rotated again during the grace period.
//
// API reference: https://api.cloudflare.com/#challenge-widgets-rotate-secret-for-a-challenge-widget
func (api *API) RotateTurnstileWidget(ctx context.Context, rc *ResourceContainer, param RotateTurnstileWidgetParams) (TurnstileWidget, error) {
if rc.Identifier == "" {
return TurnstileWidget{}, ErrMissingAccountID
}
if param.SiteKey == "" {
return TurnstileWidget{}, ErrMissingSiteKey
}
uri := fmt.Sprintf("/accounts/%s/challenges/widgets/%s/rotate_secret", rc.Identifier, param.SiteKey)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, param)
if err != nil {
return TurnstileWidget{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var r TurnstileWidgetResponse
err = json.Unmarshal(res, &r)
if err != nil {
return TurnstileWidget{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// DeleteTurnstileWidget delete a challenge widget.
//
// API reference: https://api.cloudflare.com/#challenge-widgets-delete-a-challenge-widget
func (api *API) DeleteTurnstileWidget(ctx context.Context, rc *ResourceContainer, siteKey string) error {
if rc.Identifier == "" {
return ErrMissingAccountID
}
if siteKey == "" {
return ErrMissingSiteKey
}
uri := fmt.Sprintf("/accounts/%s/challenges/widgets/%s", rc.Identifier, siteKey)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var r TurnstileWidgetResponse
err = json.Unmarshal(res, &r)
if err != nil {
return fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return nil
}