github.com/prebid/prebid-server/v2@v2.18.0/experiment/adscert/signer.go (about)

     1  package adscert
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/IABTechLab/adscert/pkg/adscert/api"
     7  	"github.com/IABTechLab/adscert/pkg/adscert/logger"
     8  	"github.com/IABTechLab/adscert/pkg/adscert/signatory"
     9  	"github.com/prebid/prebid-server/v2/config"
    10  )
    11  
    12  const SignHeader = "X-Ads-Cert-Auth"
    13  
    14  // Signer represents interface to access request Ads Cert signing functionality
    15  type Signer interface {
    16  	Sign(destinationURL string, body []byte) (string, error)
    17  }
    18  
    19  type NilSigner struct {
    20  }
    21  
    22  func (ns *NilSigner) Sign(destinationURL string, body []byte) (string, error) {
    23  	return "", nil
    24  }
    25  
    26  func NewAdCertsSigner(experimentAdCertsConfig config.ExperimentAdsCert) (Signer, error) {
    27  	logger.SetLoggerImpl(&SignerLogger{})
    28  	if experimentAdCertsConfig.Mode == config.AdCertsSignerModeInprocess {
    29  		return newInProcessSigner(experimentAdCertsConfig.InProcess)
    30  	}
    31  	if experimentAdCertsConfig.Mode == config.AdCertsSignerModeRemote {
    32  		return newRemoteSigner(experimentAdCertsConfig.Remote)
    33  	}
    34  	return &NilSigner{}, nil
    35  }
    36  
    37  func createRequestInfo(destinationURL string, body []byte) *api.RequestInfo {
    38  	// The RequestInfo proto contains details about the individual ad request
    39  	// being signed.  A SetRequestInfo helper function derives a hash of the
    40  	// destination URL and body, setting these value on the RequestInfo message.
    41  	reqInfo := &api.RequestInfo{}
    42  	signatory.SetRequestInfo(reqInfo, destinationURL, body)
    43  	return reqInfo
    44  }
    45  
    46  func getSignatureMessage(signatureResponse *api.AuthenticatedConnectionSignatureResponse) (string, error) {
    47  	if signatureResponse.GetSignatureOperationStatus() == api.SignatureOperationStatus_SIGNATURE_OPERATION_STATUS_OK {
    48  		signatureMessage := signatureResponse.RequestInfo.SignatureInfo[0].SignatureMessage
    49  		return signatureMessage, nil
    50  	}
    51  	return "", fmt.Errorf("error signing request: %s", signatureResponse.GetSignatureOperationStatus())
    52  }