github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/edgeworkers/secure_tokens.go (about) 1 package edgeworkers 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "net/http" 8 9 validation "github.com/go-ozzo/ozzo-validation/v4" 10 ) 11 12 type ( 13 // SecureTokens is EdgeWorker Secure Token API interface 14 SecureTokens interface { 15 // CreateSecureToken creates a new secure token 16 // 17 // See: https://techdocs.akamai.com/edgeworkers/reference/post-secure-token 18 CreateSecureToken(context.Context, CreateSecureTokenRequest) (*CreateSecureTokenResponse, error) 19 } 20 21 // CreateSecureTokenRequest represents parameters for CreateSecureToken 22 CreateSecureTokenRequest struct { 23 ACL string `json:"acl,omitempty"` 24 Expiry int `json:"expiry,omitempty"` 25 Hostname string `json:"hostname,omitempty"` 26 Network ActivationNetwork `json:"network,omitempty"` 27 PropertyID string `json:"propertyId,omitempty"` 28 URL string `json:"url,omitempty"` 29 } 30 31 // CreateSecureTokenResponse contains response from CreateSecureToken 32 CreateSecureTokenResponse struct { 33 AkamaiEWTrace string `json:"akamaiEwTrace"` 34 } 35 ) 36 37 // Validate validates CreateSecureTokenRequest 38 func (c CreateSecureTokenRequest) Validate() error { 39 return validation.Errors{ 40 "ACL": validation.Validate(c.ACL, validation.Empty.When(c.URL != "").Error("If you specify an acl don't specify a url.")), 41 "Expiry": validation.Validate(c.Expiry, validation.Min(1), validation.Max(720)), 42 "Hostname": validation.Validate(c.Hostname, validation.Required.When(c.PropertyID == "").Error("To create an authentication token, provide either the hostname, or the propertyId")), 43 "Network": validation.Validate(c.Network, validation.In(ActivationNetworkStaging, ActivationNetworkProduction).Error( 44 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. 45 "PropertyID": validation.Validate(c.PropertyID, validation.Required.When(c.Hostname == "").Error("To create an authentication token, provide either the hostname, or the propertyId")), 46 "URL": validation.Validate(c.URL, validation.Empty.When(c.ACL != "").Error(" If you specify a url don't specify an acl")), 47 }.Filter() 48 } 49 50 var ( 51 // ErrCreateSecureToken is returned in case an error occurs on CreateSecureToken operation 52 ErrCreateSecureToken = errors.New("create secure token") 53 ) 54 55 func (e *edgeworkers) CreateSecureToken(ctx context.Context, params CreateSecureTokenRequest) (*CreateSecureTokenResponse, error) { 56 logger := e.Log(ctx) 57 logger.Debug("CreateSecureToken") 58 59 if err := params.Validate(); err != nil { 60 return nil, fmt.Errorf("%s: %w:\n%s", ErrCreateSecureToken, ErrStructValidation, err) 61 } 62 63 uri := "/edgeworkers/v1/secure-token" 64 req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil) 65 if err != nil { 66 return nil, fmt.Errorf("%w: failed to create request: %s", ErrCreateSecureToken, err) 67 } 68 69 var result CreateSecureTokenResponse 70 resp, err := e.Exec(req, &result, params) 71 if err != nil { 72 return nil, fmt.Errorf("%w: request failed: %s", ErrCreateSecureToken, err) 73 } 74 75 if resp.StatusCode != http.StatusCreated { 76 return nil, fmt.Errorf("%s: %w", ErrCreateSecureToken, e.Error(resp)) 77 } 78 79 return &result, nil 80 }