forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_ca_certificate.go
115 lines (95 loc) · 3.37 KB
/
access_ca_certificate.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
package cloudflare
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
)
// AccessCACertificate is the structure of the CA certificate used for
// short lived certificates.
type AccessCACertificate struct {
ID string `json:"id"`
Aud string `json:"aud"`
PublicKey string `json:"public_key"`
}
// AccessCACertificateListResponse represents the response of all CA
// certificates within Access.
type AccessCACertificateListResponse struct {
Response
Result []AccessCACertificate `json:"result"`
}
// AccessCACertificateResponse represents the response of a single CA
// certificate.
type AccessCACertificateResponse struct {
Response
Result AccessCACertificate `json:"result"`
}
// AccessCACertificates returns all CA certificates within Access.
//
// API reference: https://api.cloudflare.com/#access-short-lived-certificates-list-short-lived-certificates
func (api *API) AccessCACertificates(accountID string) ([]AccessCACertificate, error) {
uri := fmt.Sprintf("/accounts/%s/access/apps/ca", accountID)
res, err := api.makeRequest("GET", uri, nil)
if err != nil {
return []AccessCACertificate{}, errors.Wrap(err, errMakeRequestError)
}
var accessCAListResponse AccessCACertificateListResponse
err = json.Unmarshal(res, &accessCAListResponse)
if err != nil {
return []AccessCACertificate{}, errors.Wrap(err, errUnmarshalError)
}
return accessCAListResponse.Result, nil
}
// AccessCACertificate returns a single CA certificate associated with an Access
// Application.
//
// API reference: https://api.cloudflare.com/#access-short-lived-certificates-short-lived-certificate-details
func (api *API) AccessCACertificate(accountID, applicationID string) (AccessCACertificate, error) {
uri := fmt.Sprintf("/accounts/%s/access/apps/%s/ca", accountID, applicationID)
res, err := api.makeRequest("GET", uri, nil)
if err != nil {
return AccessCACertificate{}, errors.Wrap(err, errMakeRequestError)
}
var accessCAResponse AccessCACertificateResponse
err = json.Unmarshal(res, &accessCAResponse)
if err != nil {
return AccessCACertificate{}, errors.Wrap(err, errUnmarshalError)
}
return accessCAResponse.Result, nil
}
// CreateAccessCACertificate creates a new CA certificate for an Access
// Application.
//
// API reference: https://api.cloudflare.com/#access-short-lived-certificates-create-short-lived-certificate
func (api *API) CreateAccessCACertificate(accountID, applicationID string) (AccessCACertificate, error) {
uri := fmt.Sprintf(
"/accounts/%s/access/apps/%s/ca",
accountID,
applicationID,
)
res, err := api.makeRequest("POST", uri, nil)
if err != nil {
return AccessCACertificate{}, errors.Wrap(err, errMakeRequestError)
}
var accessCACertificate AccessCACertificateResponse
err = json.Unmarshal(res, &accessCACertificate)
if err != nil {
return AccessCACertificate{}, errors.Wrap(err, errUnmarshalError)
}
return accessCACertificate.Result, nil
}
// DeleteAccessCACertificate deletes an Access CA certificate on a defined
// Access Application.
//
// API reference: https://api.cloudflare.com/#access-short-lived-certificates-delete-access-certificate
func (api *API) DeleteAccessCACertificate(accountID, applicationID string) error {
uri := fmt.Sprintf(
"/accounts/%s/access/apps/%s/ca",
accountID,
applicationID,
)
_, err := api.makeRequest("DELETE", uri, nil)
if err != nil {
return errors.Wrap(err, errMakeRequestError)
}
return nil
}