client: Remove constructor.

The constructor contained no important logic and removing it makes the upcoming test code slightly more clean/convenient.
This commit is contained in:
jholdstock 2022-12-31 13:04:43 +00:00 committed by Jamie Holdstock
parent 33bb68c3df
commit 36505b529c

View File

@ -21,22 +21,21 @@ type Client struct {
http.Client http.Client
URL string URL string
PubKey []byte PubKey []byte
sign func(context.Context, string, stdaddr.Address) ([]byte, error) Sign SignFunc
log slog.Logger Validate ValidateFunc
Log slog.Logger
} }
type Signer interface { // SignFunc is the signature of a function must be provided to an instance of
SignMessage(ctx context.Context, message string, address stdaddr.Address) ([]byte, error) // 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)
func NewClient(url string, pub []byte, s Signer, log slog.Logger) *Client { // ValidateFunc is the signature of a function which must be provided to an
return &Client{ // instance of Client so that it can validate that HTTP responses are signed
URL: url, // with the provided pubkey. This package provides a default implementation of
PubKey: pub, // ValidateFunc named ValidateServerResponse.
sign: s.SignMessage, type ValidateFunc func(resp *http.Response, body []byte, serverPubkey []byte) error
log: log,
}
}
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
@ -153,7 +152,7 @@ func (c *Client) do(ctx context.Context, method, path string, addr stdaddr.Addre
if err != nil { if err != nil {
return fmt.Errorf("marshal request: %w", err) return fmt.Errorf("marshal request: %w", err)
} }
sig, err = c.sign(ctx, string(body), addr) sig, err = c.Sign(ctx, string(body), addr)
if err != nil { if err != nil {
return fmt.Errorf("sign request: %w", err) return fmt.Errorf("sign request: %w", err)
} }
@ -168,12 +167,12 @@ func (c *Client) do(ctx context.Context, method, path string, addr stdaddr.Addre
httpReq.Header.Set("VSP-Client-Signature", base64.StdEncoding.EncodeToString(sig)) httpReq.Header.Set("VSP-Client-Signature", base64.StdEncoding.EncodeToString(sig))
} }
if c.log.Level() == slog.LevelTrace { if c.Log.Level() == slog.LevelTrace {
dump, err := httputil.DumpRequestOut(httpReq, sendBody) dump, err := httputil.DumpRequestOut(httpReq, sendBody)
if err == nil { if err == nil {
c.log.Tracef("Request to %s\n%s\n", c.URL, dump) c.Log.Tracef("Request to %s\n%s\n", c.URL, dump)
} else { } else {
c.log.Tracef("VSP request dump failed: %v", err) c.Log.Tracef("VSP request dump failed: %v", err)
} }
} }
@ -183,12 +182,12 @@ func (c *Client) do(ctx context.Context, method, path string, addr stdaddr.Addre
} }
defer reply.Body.Close() defer reply.Body.Close()
if c.log.Level() == slog.LevelTrace { if c.Log.Level() == slog.LevelTrace {
dump, err := httputil.DumpResponse(reply, true) dump, err := httputil.DumpResponse(reply, true)
if err == nil { if err == nil {
c.log.Tracef("Response from %s\n%s\n", c.URL, dump) c.Log.Tracef("Response from %s\n%s\n", c.URL, dump)
} else { } else {
c.log.Tracef("VSP response dump failed: %v", err) c.Log.Tracef("VSP response dump failed: %v", err)
} }
} }
@ -221,7 +220,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 = ValidateServerSignature(reply, respBody, c.PubKey) err = c.Validate(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)
} }