-
Notifications
You must be signed in to change notification settings - Fork 70
/
api_clients_credentials.go
436 lines (355 loc) · 14.1 KB
/
api_clients_credentials.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package iam
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/edgegriderr"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// CreateCredentialRequest contains request parameters for the CreateCredential endpoint.
CreateCredentialRequest struct {
ClientID string
}
// ListCredentialsRequest contains request parameters for the ListCredentials endpoint.
ListCredentialsRequest struct {
ClientID string
Actions bool
}
// GetCredentialRequest contains request parameters for the GetCredential endpoint.
GetCredentialRequest struct {
CredentialID int64
ClientID string
Actions bool
}
// UpdateCredentialRequest contains request parameters for the UpdateCredential endpoint.
UpdateCredentialRequest struct {
CredentialID int64
ClientID string
Body UpdateCredentialRequestBody
}
// UpdateCredentialRequestBody contains request body parameters for the UpdateCredential endpoint.
UpdateCredentialRequestBody struct {
Description string `json:"description,omitempty"`
ExpiresOn time.Time `json:"expiresOn"`
Status CredentialStatus `json:"status"`
}
// DeleteCredentialRequest contains request parameters for the DeleteCredential endpoint.
DeleteCredentialRequest struct {
CredentialID int64
ClientID string
}
// DeactivateCredentialRequest contains request parameters for the DeactivateCredential endpoint.
DeactivateCredentialRequest struct {
CredentialID int64
ClientID string
}
// DeactivateCredentialsRequest contains request parameters for the DeactivateCredentials endpoint.
DeactivateCredentialsRequest struct {
ClientID string
}
// CreateCredentialResponse holds response from the CreateCredentials endpoint.
CreateCredentialResponse struct {
ClientSecret string `json:"clientSecret"`
ClientToken string `json:"clientToken"`
CreatedOn time.Time `json:"createdOn"`
CredentialID int64 `json:"credentialId"`
Description string `json:"description"`
ExpiresOn time.Time `json:"expiresOn"`
Status CredentialStatus `json:"status"`
}
// ListCredentialsResponse holds response from the ListCredentials endpoint.
ListCredentialsResponse []Credential
// Credential represents single credential information.
Credential struct {
ClientToken string `json:"clientToken"`
CreatedOn time.Time `json:"createdOn"`
CredentialID int64 `json:"credentialId"`
Description string `json:"description"`
ExpiresOn time.Time `json:"expiresOn"`
Status CredentialStatus `json:"status"`
MaxAllowedExpiry time.Time `json:"maxAllowedExpiry"`
Actions *CredentialActions `json:"actions"`
}
// CredentialActions describes the actions that can be performed on the credential.
CredentialActions struct {
Deactivate bool `json:"deactivate"`
Delete bool `json:"delete"`
Activate bool `json:"activate"`
EditDescription bool `json:"editDescription"`
EditExpiration bool `json:"editExpiration"`
}
// GetCredentialResponse holds response from the GetCredential endpoint.
GetCredentialResponse Credential
// UpdateCredentialResponse holds response from the UpdateCredential endpoint.
UpdateCredentialResponse struct {
Status CredentialStatus `json:"status"`
ExpiresOn time.Time `json:"expiresOn"`
Description *string `json:"description"`
}
// CredentialStatus represents the status of the credential.
CredentialStatus string
)
const (
// CredentialActive represents active credential.
CredentialActive CredentialStatus = "ACTIVE"
// CredentialInactive represents inactive credential.
CredentialInactive CredentialStatus = "INACTIVE"
// CredentialDeleted represents deleted credential.
CredentialDeleted CredentialStatus = "DELETED"
)
// Validate validates CredentialStatus.
func (c CredentialStatus) Validate() error {
return validation.In(CredentialActive, CredentialInactive, CredentialDeleted).
Error(fmt.Sprintf("value '%s' is invalid. Must be one of: '%s', '%s' or '%s'",
c, CredentialActive, CredentialInactive, CredentialDeleted)).
Validate(c)
}
// Validate validates GetCredentialRequest.
func (r GetCredentialRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"CredentialID": validation.Validate(r.CredentialID, validation.Required),
})
}
// Validate validates UpdateCredentialRequest.
func (r UpdateCredentialRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"CredentialID": validation.Validate(r.CredentialID, validation.Required),
"Body": validation.Validate(r.Body, validation.Required),
})
}
// Validate validates UpdateCredentialRequestBody.
func (r UpdateCredentialRequestBody) Validate() error {
return validation.Errors{
"ExpiresOn": validation.Validate(r.ExpiresOn, validation.Required),
"Status": validation.Validate(r.Status, validation.Required),
}.Filter()
}
// Validate validates DeleteCredentialRequest.
func (r DeleteCredentialRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"CredentialID": validation.Validate(r.CredentialID, validation.Required),
})
}
// Validate validates DeactivateCredentialRequest.
func (r DeactivateCredentialRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"CredentialID": validation.Validate(r.CredentialID, validation.Required),
})
}
var (
// ErrCreateCredential is returned when CreateCredential fails.
ErrCreateCredential = errors.New("create credential")
// ErrListCredentials is returned when ListCredentials fails.
ErrListCredentials = errors.New("list credentials")
// ErrGetCredential is returned when GetCredential fails.
ErrGetCredential = errors.New("get credential")
// ErrUpdateCredential is returned when UpdateCredential fails.
ErrUpdateCredential = errors.New("update credential")
// ErrDeleteCredential is returned when DeleteCredential fails.
ErrDeleteCredential = errors.New("delete credential")
// ErrDeactivateCredential is returned when DeactivateCredential fails.
ErrDeactivateCredential = errors.New("deactivate credential")
// ErrDeactivateCredentials is returned when DeactivateCredentials fails.
ErrDeactivateCredentials = errors.New("deactivate credentials")
)
func (i *iam) CreateCredential(ctx context.Context, params CreateCredentialRequest) (*CreateCredentialResponse, error) {
logger := i.Log(ctx)
logger.Debug("CreateCredential")
if params.ClientID == "" {
params.ClientID = "self"
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/api-clients/%s/credentials", params.ClientID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrCreateCredential, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrCreateCredential, err)
}
var result CreateCredentialResponse
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrCreateCredential, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s: %w", ErrCreateCredential, i.Error(resp))
}
return &result, nil
}
func (i *iam) ListCredentials(ctx context.Context, params ListCredentialsRequest) (ListCredentialsResponse, error) {
logger := i.Log(ctx)
logger.Debug("ListCredentials")
if params.ClientID == "" {
params.ClientID = "self"
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/api-clients/%s/credentials", params.ClientID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListCredentials, err)
}
q := uri.Query()
q.Add("actions", strconv.FormatBool(params.Actions))
uri.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListCredentials, err)
}
var result ListCredentialsResponse
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListCredentials, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListCredentials, i.Error(resp))
}
return result, nil
}
func (i *iam) GetCredential(ctx context.Context, params GetCredentialRequest) (*GetCredentialResponse, error) {
logger := i.Log(ctx)
logger.Debug("GetCredential")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetCredential, ErrStructValidation, err)
}
if params.ClientID == "" {
params.ClientID = "self"
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/api-clients/%s/credentials/%d", params.ClientID, params.CredentialID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetCredential, err)
}
q := uri.Query()
q.Add("actions", strconv.FormatBool(params.Actions))
uri.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetCredential, err)
}
var result GetCredentialResponse
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetCredential, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetCredential, i.Error(resp))
}
return &result, nil
}
func (i *iam) UpdateCredential(ctx context.Context, params UpdateCredentialRequest) (*UpdateCredentialResponse, error) {
logger := i.Log(ctx)
logger.Debug("UpdateCredential")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrUpdateCredential, ErrStructValidation, err)
}
if params.ClientID == "" {
params.ClientID = "self"
}
// Because API does not accept date without providing milliseconds, if there are no millisecond add a small duration to allow the request to
// be processed. Only applicable when no milliseconds are provided, or they are equal to zero. Ticket for tracking: IDM-3347.
if params.Body.ExpiresOn.Nanosecond() == 0 {
params.Body.ExpiresOn = params.Body.ExpiresOn.Add(time.Nanosecond)
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/api-clients/%s/credentials/%d", params.ClientID, params.CredentialID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrUpdateCredential, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrUpdateCredential, err)
}
var result UpdateCredentialResponse
resp, err := i.Exec(req, &result, params.Body)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrUpdateCredential, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrUpdateCredential, i.Error(resp))
}
return &result, nil
}
func (i *iam) DeleteCredential(ctx context.Context, params DeleteCredentialRequest) error {
logger := i.Log(ctx)
logger.Debug("DeleteCredential")
if err := params.Validate(); err != nil {
return fmt.Errorf("%s: %w: %s", ErrDeleteCredential, ErrStructValidation, err)
}
if params.ClientID == "" {
params.ClientID = "self"
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/api-clients/%s/credentials/%d", params.ClientID, params.CredentialID))
if err != nil {
return fmt.Errorf("%w: failed to parse url: %s", ErrDeleteCredential, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri.String(), nil)
if err != nil {
return fmt.Errorf("%w: failed to create request: %s", ErrDeleteCredential, err)
}
resp, err := i.Exec(req, nil, nil)
if err != nil {
return fmt.Errorf("%w: request failed: %s", ErrDeleteCredential, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%s: %w", ErrDeleteCredential, i.Error(resp))
}
return nil
}
func (i *iam) DeactivateCredential(ctx context.Context, params DeactivateCredentialRequest) error {
logger := i.Log(ctx)
logger.Debug("DeactivateCredential")
if err := params.Validate(); err != nil {
return fmt.Errorf("%s: %w: %s", ErrDeactivateCredential, ErrStructValidation, err)
}
if params.ClientID == "" {
params.ClientID = "self"
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/api-clients/%s/credentials/%d/deactivate", params.ClientID, params.CredentialID))
if err != nil {
return fmt.Errorf("%w: failed to parse url: %s", ErrDeactivateCredential, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), nil)
if err != nil {
return fmt.Errorf("%w: failed to create request: %s", ErrDeactivateCredential, err)
}
resp, err := i.Exec(req, nil, nil)
if err != nil {
return fmt.Errorf("%w: request failed: %s", ErrDeactivateCredential, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%s: %w", ErrDeactivateCredential, i.Error(resp))
}
return nil
}
func (i *iam) DeactivateCredentials(ctx context.Context, params DeactivateCredentialsRequest) error {
logger := i.Log(ctx)
logger.Debug("DeactivateCredentials")
if params.ClientID == "" {
params.ClientID = "self"
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/api-clients/%s/credentials/deactivate", params.ClientID))
if err != nil {
return fmt.Errorf("%w: failed to parse url: %s", ErrDeactivateCredentials, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), nil)
if err != nil {
return fmt.Errorf("%w: failed to create request: %s", ErrDeactivateCredentials, err)
}
resp, err := i.Exec(req, nil, nil)
if err != nil {
return fmt.Errorf("%w: request failed: %s", ErrDeactivateCredentials, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%s: %w", ErrDeactivateCredentials, i.Error(resp))
}
return nil
}