github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cps/pre_verification_warnings.go (about)

     1  package cps
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  	"net/url"
     9  )
    10  
    11  type (
    12  	// PreVerification is a CPS API enabling management of pre-verification-warnings
    13  	PreVerification interface {
    14  		// GetChangePreVerificationWarnings gets detailed information about Domain Validation challenges
    15  		//
    16  		// See: https://techdocs.akamai.com/cps/reference/get-change-allowed-input-param
    17  		GetChangePreVerificationWarnings(ctx context.Context, params GetChangeRequest) (*PreVerificationWarnings, error)
    18  
    19  		// AcknowledgePreVerificationWarnings sends acknowledgement request to CPS informing that the warnings should be ignored
    20  		//
    21  		// See: https://techdocs.akamai.com/cps/reference/post-change-allowed-input-param
    22  		AcknowledgePreVerificationWarnings(context.Context, AcknowledgementRequest) error
    23  	}
    24  
    25  	// PreVerificationWarnings is a response object containing all warnings encountered during enrollment pre-verification
    26  	PreVerificationWarnings struct {
    27  		Warnings string `json:"warnings"`
    28  	}
    29  )
    30  
    31  var (
    32  	// ErrGetChangePreVerificationWarnings is returned when GetChangeLetsEncryptChallenges fails
    33  	ErrGetChangePreVerificationWarnings = errors.New("fetching pre-verification-warnings")
    34  	// ErrAcknowledgePreVerificationWarnings when AcknowledgeDVChallenges fails
    35  	ErrAcknowledgePreVerificationWarnings = errors.New("acknowledging pre-verification-warnings")
    36  )
    37  
    38  func (c *cps) GetChangePreVerificationWarnings(ctx context.Context, params GetChangeRequest) (*PreVerificationWarnings, error) {
    39  	if err := params.Validate(); err != nil {
    40  		return nil, fmt.Errorf("%s: %w: %s", ErrGetChangePreVerificationWarnings, ErrStructValidation, err)
    41  	}
    42  
    43  	var rval PreVerificationWarnings
    44  
    45  	logger := c.Log(ctx)
    46  	logger.Debug("GetChangePreVerificationWarnings")
    47  
    48  	uri, err := url.Parse(fmt.Sprintf(
    49  		"/cps/v2/enrollments/%d/changes/%d/input/info/pre-verification-warnings",
    50  		params.EnrollmentID,
    51  		params.ChangeID),
    52  	)
    53  	if err != nil {
    54  		return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetChangePreVerificationWarnings, err)
    55  	}
    56  
    57  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
    58  	if err != nil {
    59  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetChangePreVerificationWarnings, err)
    60  	}
    61  	req.Header.Set("Accept", "application/vnd.akamai.cps.warnings.v1+json")
    62  
    63  	resp, err := c.Exec(req, &rval)
    64  	if err != nil {
    65  		return nil, fmt.Errorf("%w: request failed: %s", ErrGetChangePreVerificationWarnings, err)
    66  	}
    67  
    68  	if resp.StatusCode != http.StatusOK {
    69  		return nil, fmt.Errorf("%s: %w", ErrGetChangePreVerificationWarnings, c.Error(resp))
    70  	}
    71  
    72  	return &rval, nil
    73  }
    74  
    75  func (c *cps) AcknowledgePreVerificationWarnings(ctx context.Context, params AcknowledgementRequest) error {
    76  	if err := params.Validate(); err != nil {
    77  		return fmt.Errorf("%s: %w: %s", ErrAcknowledgePreVerificationWarnings, ErrStructValidation, err)
    78  	}
    79  
    80  	logger := c.Log(ctx)
    81  	logger.Debug("AcknowledgePreVerificationWarnings")
    82  
    83  	uri, err := url.Parse(fmt.Sprintf(
    84  		"/cps/v2/enrollments/%d/changes/%d/input/update/pre-verification-warnings-ack",
    85  		params.EnrollmentID, params.ChangeID))
    86  	if err != nil {
    87  		return fmt.Errorf("%w: parsing URL: %s", ErrAcknowledgePreVerificationWarnings, err)
    88  	}
    89  
    90  	req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), nil)
    91  	if err != nil {
    92  		return fmt.Errorf("%w: failed to create request: %s", ErrAcknowledgePreVerificationWarnings, err)
    93  	}
    94  	req.Header.Set("Accept", "application/vnd.akamai.cps.change-id.v1+json")
    95  	req.Header.Set("Content-Type", "application/vnd.akamai.cps.acknowledgement.v1+json; charset=utf-8")
    96  
    97  	resp, err := c.Exec(req, nil, params.Acknowledgement)
    98  	if err != nil {
    99  		return fmt.Errorf("%w: request failed: %s", ErrAcknowledgePreVerificationWarnings, err)
   100  	}
   101  
   102  	if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusAccepted && resp.StatusCode != http.StatusOK {
   103  		return fmt.Errorf("%s: %w", ErrAcknowledgePreVerificationWarnings, c.Error(resp))
   104  	}
   105  
   106  	return nil
   107  }