github.com/prysmaticlabs/prysm@v1.4.4/shared/httputils/endpoint.go (about)

     1  package httputils
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"github.com/prysmaticlabs/prysm/shared/httputils/authorizationmethod"
     8  )
     9  
    10  // Endpoint is an endpoint with authorization data.
    11  type Endpoint struct {
    12  	Url  string
    13  	Auth AuthorizationData
    14  }
    15  
    16  // AuthorizationData holds all information necessary to authorize with HTTP.
    17  type AuthorizationData struct {
    18  	Method authorizationmethod.AuthorizationMethod
    19  	Value  string
    20  }
    21  
    22  // Equals compares two endpoints for equality.
    23  func (e Endpoint) Equals(other Endpoint) bool {
    24  	return e.Url == other.Url && e.Auth.Equals(other.Auth)
    25  }
    26  
    27  // Equals compares two authorization data objects for equality.
    28  func (d AuthorizationData) Equals(other AuthorizationData) bool {
    29  	return d.Method == other.Method && d.Value == other.Value
    30  }
    31  
    32  // ToHeaderValue retrieves the value of the authorization header from AuthorizationData.
    33  func (d *AuthorizationData) ToHeaderValue() (string, error) {
    34  	switch d.Method {
    35  	case authorizationmethod.Basic:
    36  		return "Basic " + d.Value, nil
    37  	case authorizationmethod.Bearer:
    38  		return "Bearer " + d.Value, nil
    39  	case authorizationmethod.None:
    40  		return "", nil
    41  	}
    42  
    43  	return "", errors.New("could not create HTTP header for unknown authorization method")
    44  }
    45  
    46  // Method returns the authorizationmethod.AuthorizationMethod corresponding with the parameter value.
    47  func Method(auth string) authorizationmethod.AuthorizationMethod {
    48  	if strings.HasPrefix(strings.ToLower(auth), "basic") {
    49  		return authorizationmethod.Basic
    50  	}
    51  	if strings.HasPrefix(strings.ToLower(auth), "bearer") {
    52  		return authorizationmethod.Bearer
    53  	}
    54  	return authorizationmethod.None
    55  }