client: Hard-code validate func.

The client supported overriding the default server signature validation func. This was originally added to facilitate testing, but at the moment only serves to make using the client more clunky and error prone.
This commit is contained in:
jholdstock 2023-03-01 08:37:32 +00:00 committed by Jamie Holdstock
parent 2fec8cf5d8
commit be79a8e279
2 changed files with 11 additions and 31 deletions

View File

@ -19,24 +19,15 @@ import (
type Client struct { type Client struct {
http.Client http.Client
URL string URL string
PubKey []byte PubKey []byte
Sign SignFunc // Sign is a function which must be provided to an instance of Client so
Validate ValidateFunc // that it can sign request bodies using the PrivKey of the specified
Log slog.Logger // address.
Sign func(context.Context, string, stdaddr.Address) ([]byte, error)
Log slog.Logger
} }
// SignFunc is the signature of a function must be provided to an instance of
// Client so that it can sign request bodies using the PrivKey of the specified
// address.
type SignFunc func(context.Context, string, stdaddr.Address) ([]byte, error)
// ValidateFunc is the signature of a function which must be provided to an
// instance of Client so that it can validate that HTTP responses are signed
// with the provided pubkey. This package provides a default implementation of
// ValidateFunc named ValidateServerResponse.
type ValidateFunc func(resp *http.Response, body []byte, serverPubkey []byte) error
func (c *Client) VspInfo(ctx context.Context) (*types.VspInfoResponse, error) { func (c *Client) VspInfo(ctx context.Context) (*types.VspInfoResponse, error) {
var resp *types.VspInfoResponse var resp *types.VspInfoResponse
err := c.get(ctx, "/api/v3/vspinfo", &resp) err := c.get(ctx, "/api/v3/vspinfo", &resp)
@ -223,7 +214,7 @@ func (c *Client) do(ctx context.Context, method, path string, addr stdaddr.Addre
status, http.StatusText(status), respBody) status, http.StatusText(status), respBody)
} }
err = c.Validate(reply, respBody, c.PubKey) err = ValidateServerSignature(reply, respBody, c.PubKey)
if err != nil { if err != nil {
return fmt.Errorf("authenticate server response: %v", err) return fmt.Errorf("authenticate server response: %v", err)
} }

View File

@ -7,19 +7,10 @@ import (
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"github.com/decred/dcrd/txscript/v4/stdaddr"
"github.com/decred/slog" "github.com/decred/slog"
"github.com/decred/vspd/types" "github.com/decred/vspd/types"
) )
func NoopSign(ctx context.Context, message string, address stdaddr.Address) ([]byte, error) {
return nil, nil
}
func NoopValidate(resp *http.Response, body []byte, serverPubkey []byte) error {
return nil
}
// TestErrorDetails ensures errors returned by client.do contain adequate // TestErrorDetails ensures errors returned by client.do contain adequate
// information for debugging (HTTP status and response body). // information for debugging (HTTP status and response body).
func TestErrorDetails(t *testing.T) { func TestErrorDetails(t *testing.T) {
@ -68,11 +59,9 @@ func TestErrorDetails(t *testing.T) {
})) }))
client := Client{ client := Client{
URL: testServer.URL, URL: testServer.URL,
PubKey: []byte("fake pubkey"), PubKey: []byte("fake pubkey"),
Sign: NoopSign, Log: slog.Disabled,
Validate: NoopValidate,
Log: slog.Disabled,
} }
var resp interface{} var resp interface{}