2021-04-15 11:02:02 +00:00
|
|
|
package powchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"strings"
|
|
|
|
|
2021-09-16 19:12:27 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/network"
|
|
|
|
"github.com/prysmaticlabs/prysm/network/authorization"
|
2021-04-15 11:02:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HttpEndpoint extracts an httputils.Endpoint from the provider parameter.
|
2021-09-16 19:12:27 +00:00
|
|
|
func HttpEndpoint(eth1Provider string) network.Endpoint {
|
|
|
|
endpoint := network.Endpoint{
|
2021-04-15 11:02:02 +00:00
|
|
|
Url: "",
|
2021-09-16 19:12:27 +00:00
|
|
|
Auth: network.AuthorizationData{
|
|
|
|
Method: authorization.None,
|
2021-04-15 11:02:02 +00:00
|
|
|
Value: "",
|
|
|
|
}}
|
|
|
|
|
|
|
|
authValues := strings.Split(eth1Provider, ",")
|
|
|
|
endpoint.Url = strings.TrimSpace(authValues[0])
|
|
|
|
if len(authValues) > 2 {
|
|
|
|
log.Errorf(
|
|
|
|
"ETH1 endpoint string can contain one comma for specifying the authorization header to access the provider."+
|
|
|
|
" String contains too many commas: %d. Skipping authorization.", len(authValues)-1)
|
|
|
|
} else if len(authValues) == 2 {
|
2021-09-16 19:12:27 +00:00
|
|
|
switch network.Method(strings.TrimSpace(authValues[1])) {
|
|
|
|
case authorization.Basic:
|
2021-04-15 11:02:02 +00:00
|
|
|
basicAuthValues := strings.Split(strings.TrimSpace(authValues[1]), " ")
|
|
|
|
if len(basicAuthValues) != 2 {
|
|
|
|
log.Errorf("Basic Authentication has incorrect format. Skipping authorization.")
|
|
|
|
} else {
|
2021-09-16 19:12:27 +00:00
|
|
|
endpoint.Auth.Method = authorization.Basic
|
2021-04-15 11:02:02 +00:00
|
|
|
endpoint.Auth.Value = base64.StdEncoding.EncodeToString([]byte(basicAuthValues[1]))
|
|
|
|
}
|
2021-09-16 19:12:27 +00:00
|
|
|
case authorization.Bearer:
|
2021-04-15 11:02:02 +00:00
|
|
|
bearerAuthValues := strings.Split(strings.TrimSpace(authValues[1]), " ")
|
|
|
|
if len(bearerAuthValues) != 2 {
|
|
|
|
log.Errorf("Bearer Authentication has incorrect format. Skipping authorization.")
|
|
|
|
} else {
|
2021-09-16 19:12:27 +00:00
|
|
|
endpoint.Auth.Method = authorization.Bearer
|
2021-04-15 11:02:02 +00:00
|
|
|
endpoint.Auth.Value = bearerAuthValues[1]
|
|
|
|
}
|
2021-09-16 19:12:27 +00:00
|
|
|
case authorization.None:
|
2021-04-15 11:02:02 +00:00
|
|
|
log.Errorf("Authorization has incorrect format or authorization type is not supported.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return endpoint
|
|
|
|
}
|