-
Notifications
You must be signed in to change notification settings - Fork 69
/
secure_tokens.go
80 lines (67 loc) · 3.26 KB
/
secure_tokens.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
package edgeworkers
import (
"context"
"errors"
"fmt"
"net/http"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// SecureTokens is EdgeWorker Secure Token API interface
SecureTokens interface {
// CreateSecureToken creates a new secure token
//
// See: https://techdocs.akamai.com/edgeworkers/reference/post-secure-token
CreateSecureToken(context.Context, CreateSecureTokenRequest) (*CreateSecureTokenResponse, error)
}
// CreateSecureTokenRequest represents parameters for CreateSecureToken
CreateSecureTokenRequest struct {
ACL string `json:"acl,omitempty"`
Expiry int `json:"expiry,omitempty"`
Hostname string `json:"hostname,omitempty"`
Network ActivationNetwork `json:"network,omitempty"`
PropertyID string `json:"propertyId,omitempty"`
URL string `json:"url,omitempty"`
}
// CreateSecureTokenResponse contains response from CreateSecureToken
CreateSecureTokenResponse struct {
AkamaiEWTrace string `json:"akamaiEwTrace"`
}
)
// Validate validates CreateSecureTokenRequest
func (c CreateSecureTokenRequest) Validate() error {
return validation.Errors{
"ACL": validation.Validate(c.ACL, validation.Empty.When(c.URL != "").Error("If you specify an acl don't specify a url.")),
"Expiry": validation.Validate(c.Expiry, validation.Min(1), validation.Max(720)),
"Hostname": validation.Validate(c.Hostname, validation.Required.When(c.PropertyID == "").Error("To create an authentication token, provide either the hostname, or the propertyId")),
"Network": validation.Validate(c.Network, validation.In(ActivationNetworkStaging, ActivationNetworkProduction).Error(
fmt.Sprintf("value '%s' is invalid. Must be one of: '%s', '%s' or '' (empty)", c.Network, ActivationNetworkStaging, ActivationNetworkProduction))), // If not specified, the token is created for the network where the last Property version activation occurred.
"PropertyID": validation.Validate(c.PropertyID, validation.Required.When(c.Hostname == "").Error("To create an authentication token, provide either the hostname, or the propertyId")),
"URL": validation.Validate(c.URL, validation.Empty.When(c.ACL != "").Error(" If you specify a url don't specify an acl")),
}.Filter()
}
var (
// ErrCreateSecureToken is returned in case an error occurs on CreateSecureToken operation
ErrCreateSecureToken = errors.New("create secure token")
)
func (e *edgeworkers) CreateSecureToken(ctx context.Context, params CreateSecureTokenRequest) (*CreateSecureTokenResponse, error) {
logger := e.Log(ctx)
logger.Debug("CreateSecureToken")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrCreateSecureToken, ErrStructValidation, err)
}
uri := "/edgeworkers/v1/secure-token"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrCreateSecureToken, err)
}
var result CreateSecureTokenResponse
resp, err := e.Exec(req, &result, params)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrCreateSecureToken, err)
}
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s: %w", ErrCreateSecureToken, e.Error(resp))
}
return &result, nil
}