2021-09-16 19:12:27 +00:00
|
|
|
package network
|
2021-04-15 11:02:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
2021-09-16 19:12:27 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/network/authorization"
|
2021-04-15 11:02:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Endpoint is an endpoint with authorization data.
|
|
|
|
type Endpoint struct {
|
|
|
|
Url string
|
|
|
|
Auth AuthorizationData
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizationData holds all information necessary to authorize with HTTP.
|
|
|
|
type AuthorizationData struct {
|
2021-09-16 19:12:27 +00:00
|
|
|
Method authorization.AuthorizationMethod
|
2021-04-15 11:02:02 +00:00
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// Equals compares two endpoints for equality.
|
2021-04-15 11:02:02 +00:00
|
|
|
func (e Endpoint) Equals(other Endpoint) bool {
|
|
|
|
return e.Url == other.Url && e.Auth.Equals(other.Auth)
|
|
|
|
}
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// Equals compares two authorization data objects for equality.
|
2021-04-15 11:02:02 +00:00
|
|
|
func (d AuthorizationData) Equals(other AuthorizationData) bool {
|
|
|
|
return d.Method == other.Method && d.Value == other.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToHeaderValue retrieves the value of the authorization header from AuthorizationData.
|
|
|
|
func (d *AuthorizationData) ToHeaderValue() (string, error) {
|
|
|
|
switch d.Method {
|
2021-09-16 19:12:27 +00:00
|
|
|
case authorization.Basic:
|
2021-04-15 11:02:02 +00:00
|
|
|
return "Basic " + d.Value, nil
|
2021-09-16 19:12:27 +00:00
|
|
|
case authorization.Bearer:
|
2021-04-15 11:02:02 +00:00
|
|
|
return "Bearer " + d.Value, nil
|
2021-09-16 19:12:27 +00:00
|
|
|
case authorization.None:
|
2021-04-15 11:02:02 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("could not create HTTP header for unknown authorization method")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method returns the authorizationmethod.AuthorizationMethod corresponding with the parameter value.
|
2021-09-16 19:12:27 +00:00
|
|
|
func Method(auth string) authorization.AuthorizationMethod {
|
2021-04-15 11:02:02 +00:00
|
|
|
if strings.HasPrefix(strings.ToLower(auth), "basic") {
|
2021-09-16 19:12:27 +00:00
|
|
|
return authorization.Basic
|
2021-04-15 11:02:02 +00:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(strings.ToLower(auth), "bearer") {
|
2021-09-16 19:12:27 +00:00
|
|
|
return authorization.Bearer
|
2021-04-15 11:02:02 +00:00
|
|
|
}
|
2021-09-16 19:12:27 +00:00
|
|
|
return authorization.None
|
2021-04-15 11:02:02 +00:00
|
|
|
}
|