forked from dwayne45/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
access_application.go
297 lines (252 loc) · 11.8 KB
/
access_application.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/pkg/errors"
)
// AccessApplicationType represents the application type.
type AccessApplicationType string
// These constants represent all valid application types.
const (
SelfHosted AccessApplicationType = "self_hosted"
SSH AccessApplicationType = "ssh"
VNC AccessApplicationType = "vnc"
File AccessApplicationType = "file"
Bookmark AccessApplicationType = "bookmark"
)
// AccessApplication represents an Access application.
type AccessApplication struct {
GatewayRules []AccessApplicationGatewayRule `json:"gateway_rules,omitempty"`
AllowedIdps []string `json:"allowed_idps,omitempty"`
CustomDenyMessage string `json:"custom_deny_message,omitempty"`
LogoURL string `json:"logo_url,omitempty"`
AUD string `json:"aud,omitempty"`
Domain string `json:"domain"`
Type AccessApplicationType `json:"type,omitempty"`
SessionDuration string `json:"session_duration,omitempty"`
SameSiteCookieAttribute string `json:"same_site_cookie_attribute,omitempty"`
CustomDenyURL string `json:"custom_deny_url,omitempty"`
Name string `json:"name"`
ID string `json:"id,omitempty"`
PrivateAddress string `json:"private_address"`
CorsHeaders *AccessApplicationCorsHeaders `json:"cors_headers,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
AutoRedirectToIdentity bool `json:"auto_redirect_to_identity,omitempty"`
SkipInterstitial bool `json:"skip_interstitial,omitempty"`
AppLauncherVisible bool `json:"app_launcher_visible,omitempty"`
EnableBindingCookie bool `json:"enable_binding_cookie,omitempty"`
HttpOnlyCookieAttribute bool `json:"http_only_cookie_attribute,omitempty"`
}
type AccessApplicationGatewayRule struct {
ID string `json:"id,omitempty"`
}
// AccessApplicationCorsHeaders represents the CORS HTTP headers for an Access
// Application.
type AccessApplicationCorsHeaders struct {
AllowedMethods []string `json:"allowed_methods,omitempty"`
AllowedOrigins []string `json:"allowed_origins,omitempty"`
AllowedHeaders []string `json:"allowed_headers,omitempty"`
AllowAllMethods bool `json:"allow_all_methods,omitempty"`
AllowAllHeaders bool `json:"allow_all_headers,omitempty"`
AllowAllOrigins bool `json:"allow_all_origins,omitempty"`
AllowCredentials bool `json:"allow_credentials,omitempty"`
MaxAge int `json:"max_age,omitempty"`
}
// AccessApplicationListResponse represents the response from the list
// access applications endpoint.
type AccessApplicationListResponse struct {
Result []AccessApplication `json:"result"`
Response
ResultInfo `json:"result_info"`
}
// AccessApplicationDetailResponse is the API response, containing a single
// access application.
type AccessApplicationDetailResponse struct {
Success bool `json:"success"`
Errors []string `json:"errors"`
Messages []string `json:"messages"`
Result AccessApplication `json:"result"`
}
// AccessApplications returns all applications within an account.
//
// API reference: https://api.cloudflare.com/#access-applications-list-access-applications
func (api *API) AccessApplications(ctx context.Context, accountID string, pageOpts PaginationOptions) ([]AccessApplication, ResultInfo, error) {
return api.accessApplications(ctx, accountID, pageOpts, AccountRouteRoot)
}
// ZoneLevelAccessApplications returns all applications within a zone.
//
// API reference: https://api.cloudflare.com/#zone-level-access-applications-list-access-applications
func (api *API) ZoneLevelAccessApplications(ctx context.Context, zoneID string, pageOpts PaginationOptions) ([]AccessApplication, ResultInfo, error) {
return api.accessApplications(ctx, zoneID, pageOpts, ZoneRouteRoot)
}
func (api *API) accessApplications(ctx context.Context, id string, pageOpts PaginationOptions, routeRoot RouteRoot) ([]AccessApplication, ResultInfo, error) {
v := url.Values{}
if pageOpts.PerPage > 0 {
v.Set("per_page", strconv.Itoa(pageOpts.PerPage))
}
if pageOpts.Page > 0 {
v.Set("page", strconv.Itoa(pageOpts.Page))
}
uri := fmt.Sprintf("/%s/%s/access/apps", routeRoot, id)
if len(v) > 0 {
uri = fmt.Sprintf("%s?%s", uri, v.Encode())
}
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []AccessApplication{}, ResultInfo{}, err
}
var accessApplicationListResponse AccessApplicationListResponse
err = json.Unmarshal(res, &accessApplicationListResponse)
if err != nil {
return []AccessApplication{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError)
}
return accessApplicationListResponse.Result, accessApplicationListResponse.ResultInfo, nil
}
// AccessApplication returns a single application based on the
// application ID.
//
// API reference: https://api.cloudflare.com/#access-applications-access-applications-details
func (api *API) AccessApplication(ctx context.Context, accountID, applicationID string) (AccessApplication, error) {
return api.accessApplication(ctx, accountID, applicationID, AccountRouteRoot)
}
// ZoneLevelAccessApplication returns a single zone level application based on the
// application ID.
//
// API reference: https://api.cloudflare.com/#zone-level-access-applications-access-applications-details
func (api *API) ZoneLevelAccessApplication(ctx context.Context, zoneID, applicationID string) (AccessApplication, error) {
return api.accessApplication(ctx, zoneID, applicationID, ZoneRouteRoot)
}
func (api *API) accessApplication(ctx context.Context, id, applicationID string, routeRoot RouteRoot) (AccessApplication, error) {
uri := fmt.Sprintf(
"/%s/%s/access/apps/%s",
routeRoot,
id,
applicationID,
)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return AccessApplication{}, err
}
var accessApplicationDetailResponse AccessApplicationDetailResponse
err = json.Unmarshal(res, &accessApplicationDetailResponse)
if err != nil {
return AccessApplication{}, errors.Wrap(err, errUnmarshalError)
}
return accessApplicationDetailResponse.Result, nil
}
// CreateAccessApplication creates a new access application.
//
// API reference: https://api.cloudflare.com/#access-applications-create-access-application
func (api *API) CreateAccessApplication(ctx context.Context, accountID string, accessApplication AccessApplication) (AccessApplication, error) {
return api.createAccessApplication(ctx, accountID, accessApplication, AccountRouteRoot)
}
// CreateZoneLevelAccessApplication creates a new zone level access application.
//
// API reference: https://api.cloudflare.com/#zone-level-access-applications-create-access-application
func (api *API) CreateZoneLevelAccessApplication(ctx context.Context, zoneID string, accessApplication AccessApplication) (AccessApplication, error) {
return api.createAccessApplication(ctx, zoneID, accessApplication, ZoneRouteRoot)
}
func (api *API) createAccessApplication(ctx context.Context, id string, accessApplication AccessApplication, routeRoot RouteRoot) (AccessApplication, error) {
uri := fmt.Sprintf("/%s/%s/access/apps", routeRoot, id)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, accessApplication)
if err != nil {
return AccessApplication{}, err
}
var accessApplicationDetailResponse AccessApplicationDetailResponse
err = json.Unmarshal(res, &accessApplicationDetailResponse)
if err != nil {
return AccessApplication{}, errors.Wrap(err, errUnmarshalError)
}
return accessApplicationDetailResponse.Result, nil
}
// UpdateAccessApplication updates an existing access application.
//
// API reference: https://api.cloudflare.com/#access-applications-update-access-application
func (api *API) UpdateAccessApplication(ctx context.Context, accountID string, accessApplication AccessApplication) (AccessApplication, error) {
return api.updateAccessApplication(ctx, accountID, accessApplication, AccountRouteRoot)
}
// UpdateZoneLevelAccessApplication updates an existing zone level access application.
//
// API reference: https://api.cloudflare.com/#zone-level-access-applications-update-access-application
func (api *API) UpdateZoneLevelAccessApplication(ctx context.Context, zoneID string, accessApplication AccessApplication) (AccessApplication, error) {
return api.updateAccessApplication(ctx, zoneID, accessApplication, ZoneRouteRoot)
}
func (api *API) updateAccessApplication(ctx context.Context, id string, accessApplication AccessApplication, routeRoot RouteRoot) (AccessApplication, error) {
if accessApplication.ID == "" {
return AccessApplication{}, errors.Errorf("access application ID cannot be empty")
}
uri := fmt.Sprintf(
"/%s/%s/access/apps/%s",
routeRoot,
id,
accessApplication.ID,
)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, accessApplication)
if err != nil {
return AccessApplication{}, err
}
var accessApplicationDetailResponse AccessApplicationDetailResponse
err = json.Unmarshal(res, &accessApplicationDetailResponse)
if err != nil {
return AccessApplication{}, errors.Wrap(err, errUnmarshalError)
}
return accessApplicationDetailResponse.Result, nil
}
// DeleteAccessApplication deletes an access application.
//
// API reference: https://api.cloudflare.com/#access-applications-delete-access-application
func (api *API) DeleteAccessApplication(ctx context.Context, accountID, applicationID string) error {
return api.deleteAccessApplication(ctx, accountID, applicationID, AccountRouteRoot)
}
// DeleteZoneLevelAccessApplication deletes a zone level access application.
//
// API reference: https://api.cloudflare.com/#zone-level-access-applications-delete-access-application
func (api *API) DeleteZoneLevelAccessApplication(ctx context.Context, zoneID, applicationID string) error {
return api.deleteAccessApplication(ctx, zoneID, applicationID, ZoneRouteRoot)
}
func (api *API) deleteAccessApplication(ctx context.Context, id, applicationID string, routeRoot RouteRoot) error {
uri := fmt.Sprintf(
"/%s/%s/access/apps/%s",
routeRoot,
id,
applicationID,
)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}
// RevokeAccessApplicationTokens revokes tokens associated with an
// access application.
//
// API reference: https://api.cloudflare.com/#access-applications-revoke-access-tokens
func (api *API) RevokeAccessApplicationTokens(ctx context.Context, accountID, applicationID string) error {
return api.revokeAccessApplicationTokens(ctx, accountID, applicationID, AccountRouteRoot)
}
// RevokeZoneLevelAccessApplicationTokens revokes tokens associated with a zone level
// access application.
//
// API reference: https://api.cloudflare.com/#zone-level-access-applications-revoke-access-tokens
func (api *API) RevokeZoneLevelAccessApplicationTokens(ctx context.Context, zoneID, applicationID string) error {
return api.revokeAccessApplicationTokens(ctx, zoneID, applicationID, ZoneRouteRoot)
}
func (api *API) revokeAccessApplicationTokens(ctx context.Context, id string, applicationID string, routeRoot RouteRoot) error {
uri := fmt.Sprintf(
"/%s/%s/access/apps/%s/revoke-tokens",
routeRoot,
id,
applicationID,
)
_, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return err
}
return nil
}