/
clients_service.go
222 lines (191 loc) · 7.06 KB
/
clients_service.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
package iam
import (
"bytes"
"fmt"
"net/http"
"github.com/cenkalti/backoff/v4"
validator "github.com/go-playground/validator/v10"
)
var (
clientAPIVersion = "1"
)
// ApplicationClient represents an IAM client resource
type ApplicationClient struct {
ID string `json:"id,omitempty"`
ClientID string `json:"clientId" validate:"required,min=5,max=20"`
Type string `json:"type"`
Name string `json:"name" validate:"required,min=5,max=50"`
Password string `json:"password,omitempty" validate:"required_without=ID,max=16"`
RedirectionURIs []string `json:"redirectionURIs"`
ResponseTypes []string `json:"responseTypes"`
Scopes []string `json:"scopes,omitempty"`
DefaultScopes []string `json:"defaultScopes,omitempty"`
Disabled bool `json:"disabled,omitempty"`
Description string `json:"description" validate:"max=250"`
ApplicationID string `json:"applicationId" validate:"required"`
GlobalReferenceID string `json:"globalReferenceId" validate:"required,min=3,max=50"`
ConsentImplied bool `json:"consentImplied"`
AccessTokenLifetime int `json:"accessTokenLifetime,omitempty" validate:"min=0,max=31536000"`
RefreshTokenLifetime int `json:"refreshTokenLifetime,omitempty" validate:"min=0,max=157680000"`
IDTokenLifetime int `json:"idTokenLifetime,omitempty" validate:"min=0,max=31536000"`
Realms []string `json:"realms,omitempty" validate:"required_with=ID"`
Meta *ClientMeta `json:"meta,omitempty"`
}
type ClientMeta struct {
VersionID string `json:"versionId,omitempty"`
LastModified string `json:"lastModified,omitempty"`
}
// ClientsService provides operations on IAM roles resources
type ClientsService struct {
client *Client
validate *validator.Validate
}
// GetClientsOptions describes search criteria for looking up roles
type GetClientsOptions struct {
ID *string `url:"_id,omitempty"`
Name *string `url:"name,omitempty"`
GlobalReferenceID *string `url:"globalReferenceId,omitempty"`
ApplicationID *string `url:"applicationId,omitempty"`
}
// CreateClient creates a Client
func (c *ClientsService) CreateClient(ac ApplicationClient) (*ApplicationClient, *Response, error) {
if err := c.validate.Struct(ac); err != nil {
return nil, nil, err
}
// Remove scopes before calling create
scopes := ac.Scopes
defaultScopes := ac.DefaultScopes // Defaults to ["cn"]
ac.Scopes = []string{} // Defaults to ["mail", "sn"]
ac.DefaultScopes = []string{}
req, _ := c.client.newRequest(IDM, "POST", "authorize/identity/Client", ac, nil)
req.Header.Set("api-version", clientAPIVersion)
var createdClient ApplicationClient
resp, err := c.client.do(req, &createdClient)
ok := resp != nil && (resp.StatusCode() == http.StatusOK || resp.StatusCode() == http.StatusCreated)
if !ok {
return nil, resp, err
}
if resp == nil {
return nil, resp, fmt.Errorf("CreateClient (resp=nil): %w", ErrCouldNoReadResourceAfterCreate)
}
var id string
count, _ := fmt.Sscanf(resp.Header.Get("Location"), "/authorize/identity/Client/%s", &id)
if count == 0 {
return nil, resp, fmt.Errorf("CreateClient: %w", ErrCouldNoReadResourceAfterCreate)
}
ac.ID = id
if len(scopes) > 0 {
_, resp, err := c.UpdateScopes(ac, scopes, defaultScopes)
if err != nil {
_, _, _ = c.DeleteClient(ac) // Clean up
return nil, resp, fmt.Errorf("CreateClient.UpdateScopes: %w", err)
}
}
return c.GetClientByID(id)
}
// DeleteClient deletes the given Client
func (c *ClientsService) DeleteClient(ac ApplicationClient) (bool, *Response, error) {
req, err := c.client.newRequest(IDM, "DELETE", "authorize/identity/Client/"+ac.ID, nil, nil)
if err != nil {
return false, nil, err
}
req.Header.Set("api-version", clientAPIVersion)
var deleteResponse interface{}
resp, err := c.client.do(req, &deleteResponse)
if resp == nil || resp.StatusCode() != http.StatusNoContent {
return false, resp, err
}
return true, resp, nil
}
// GetClientByID finds a client by its ID
func (c *ClientsService) GetClientByID(id string) (*ApplicationClient, *Response, error) {
clients, resp, err := c.GetClients(&GetClientsOptions{ID: &id}, nil)
if err != nil {
return nil, resp, err
}
if clients == nil {
return nil, resp, ErrOperationFailed
}
if len(*clients) == 0 {
return nil, resp, fmt.Errorf("GetClientByID: %w", ErrEmptyResults)
}
foundClient := (*clients)[0]
return &foundClient, resp, nil
}
// GetClients looks up clients based on GetClientsOptions
func (c *ClientsService) GetClients(opt *GetClientsOptions, options ...OptionFunc) (*[]ApplicationClient, *Response, error) {
req, err := c.client.newRequest(IDM, "GET", "authorize/identity/Client", opt, options)
if err != nil {
return nil, nil, err
}
req.Header.Set("api-version", clientAPIVersion)
req.Header.Set("Content-Type", "application/json")
var bundleResponse struct {
Total int `json:"total"`
Entry []ApplicationClient `json:"entry"`
}
resp, err := c.client.do(req, &bundleResponse)
if err != nil {
return nil, resp, err
}
return &bundleResponse.Entry, resp, err
}
// UpdateScope updates a clients scope
func (c *ClientsService) UpdateScopes(ac ApplicationClient, scopes []string, defaultScopes []string) (bool, *Response, error) {
var requestBody = struct {
Scopes []string `json:"scopes"`
DefaultScopes []string `json:"defaultScopes"`
}{
scopes,
defaultScopes,
}
// We suspect IAM sometimes has consistency issues when the UpdateScopes call is performed immediately after
// a create call of the client. Therefore, we always try to retrieve the client first
var err error
var resp *Response
operation := func() error {
_, resp, err = c.GetClientByID(ac.ID)
if err != nil {
return err
}
if resp != nil && resp.StatusCode() == http.StatusNotFound {
return ErrNotFound
}
return nil
}
err = backoff.Retry(operation, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 5))
if err != nil {
return false, resp, err
}
req, err := c.client.newRequest(IDM, "PUT", "authorize/identity/Client/"+ac.ID+"/$scopes", requestBody, nil)
if err != nil {
return false, nil, err
}
req.Header.Set("api-version", clientAPIVersion)
var putResponse bytes.Buffer
resp, err = c.client.do(req, &putResponse)
if err != nil {
return false, resp, err
}
if resp.StatusCode() != http.StatusNoContent {
return false, resp, ErrOperationFailed
}
return true, resp, nil
}
// UpdateClient updates a client
func (c *ClientsService) UpdateClient(ac ApplicationClient) (*ApplicationClient, *Response, error) {
if err := c.validate.Struct(ac); err != nil {
return nil, nil, err
}
req, err := c.client.newRequest(IDM, "PUT", "authorize/identity/Client/"+ac.ID, ac, nil)
if err != nil {
return nil, nil, err
}
req.Header.Set("api-version", clientAPIVersion)
var updatedClient ApplicationClient
resp, err := c.client.do(req, &updatedClient)
if err != nil {
return nil, resp, err
}
return &updatedClient, resp, nil
}