@@ -20,11 +20,14 @@ import (
20
20
)
21
21
22
22
const apiURL = "https://api.cloudflare.com/client/v4"
23
+
23
24
const (
24
25
// AuthKeyEmail specifies that we should authenticate with API key and email address
25
26
AuthKeyEmail = 1 << iota
26
27
// AuthUserService specifies that we should authenticate with a User-Service key
27
28
AuthUserService
29
+ // AuthToken specifies that we should authenticate with an API Token
30
+ AuthToken
28
31
)
29
32
30
33
// API holds the configuration for the current API client. A client should not
@@ -33,6 +36,7 @@ type API struct {
33
36
APIKey string
34
37
APIEmail string
35
38
APIUserServiceKey string
39
+ APIToken string
36
40
BaseURL string
37
41
OrganizationID string
38
42
UserAgent string
@@ -92,6 +96,23 @@ func New(key, email string, opts ...Option) (*API, error) {
92
96
return api , nil
93
97
}
94
98
99
+ // NewWithAPIToken creates a new Cloudflare v4 API client using API Tokens
100
+ func NewWithAPIToken (token string , opts ... Option ) (* API , error ) {
101
+ if token == "" {
102
+ return nil , errors .New (errEmptyAPIToken )
103
+ }
104
+
105
+ api , err := newClient (opts ... )
106
+ if err != nil {
107
+ return nil , err
108
+ }
109
+
110
+ api .APIToken = token
111
+ api .authType = AuthToken
112
+
113
+ return api , nil
114
+ }
115
+
95
116
// NewWithUserServiceKey creates a new Cloudflare v4 API client using service key authentication.
96
117
func NewWithUserServiceKey (key string , opts ... Option ) (* API , error ) {
97
118
if key == "" {
@@ -109,7 +130,7 @@ func NewWithUserServiceKey(key string, opts ...Option) (*API, error) {
109
130
return api , nil
110
131
}
111
132
112
- // SetAuthType sets the authentication method (AuthyKeyEmail or AuthUserService).
133
+ // SetAuthType sets the authentication method (AuthKeyEmail, AuthToken, or AuthUserService).
113
134
func (api * API ) SetAuthType (authType int ) {
114
135
api .authType = authType
115
136
}
@@ -141,7 +162,7 @@ func (api *API) ZoneIDByName(zoneName string) (string, error) {
141
162
}
142
163
143
164
// makeRequest makes a HTTP request and returns the body as a byte slice,
144
- // closing it before returnng . params will be serialized to JSON.
165
+ // closing it before returning . params will be serialized to JSON.
145
166
func (api * API ) makeRequest (method , uri string , params interface {}) ([]byte , error ) {
146
167
return api .makeRequestWithAuthType (context .TODO (), method , uri , params , api .authType )
147
168
}
@@ -186,7 +207,7 @@ func (api *API) makeRequestWithAuthTypeAndHeaders(ctx context.Context, method, u
186
207
}
187
208
if i > 0 {
188
209
// expect the backoff introduced here on errored requests to dominate the effect of rate limiting
189
- // dont need a random component here as the rate limiter should do something similar
210
+ // don't need a random component here as the rate limiter should do something similar
190
211
// nb time duration could truncate an arbitrary float. Since our inputs are all ints, we should be ok
191
212
sleepDuration := time .Duration (math .Pow (2 , float64 (i - 1 )) * float64 (api .retryPolicy .MinRetryDelay ))
192
213
@@ -277,13 +298,18 @@ func (api *API) request(ctx context.Context, method, uri string, reqBody io.Read
277
298
copyHeader (combinedHeaders , api .headers )
278
299
copyHeader (combinedHeaders , headers )
279
300
req .Header = combinedHeaders
301
+
280
302
if authType & AuthKeyEmail != 0 {
281
303
req .Header .Set ("X-Auth-Key" , api .APIKey )
282
304
req .Header .Set ("X-Auth-Email" , api .APIEmail )
283
305
}
284
306
if authType & AuthUserService != 0 {
285
307
req .Header .Set ("X-Auth-User-Service-Key" , api .APIUserServiceKey )
286
308
}
309
+ if authType & AuthToken != 0 {
310
+ req .Header .Set ("Authorization" , "Bearer " + api .APIToken )
311
+ }
312
+
287
313
if api .UserAgent != "" {
288
314
req .Header .Set ("User-Agent" , api .UserAgent )
289
315
}
0 commit comments