Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jun 3, 2024
1 parent 41aff52 commit b9632ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion dispatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestDispatch(t *testing.T) {
// Try with a client that does not sign requests. The Dispatch
// instance should reject the request.
nonSigningClient := dispatchtest.NewEndpointClient(server.URL)
res, err = nonSigningClient.Run(context.Background(), &sdkv1.RunRequest{
_, err = nonSigningClient.Run(context.Background(), &sdkv1.RunRequest{
Function: "identity",
Directive: &sdkv1.RunRequest_Input{Input: input},
})
Expand Down
42 changes: 21 additions & 21 deletions internal/auth/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,33 @@ func (s *Signer) Sign(req *http.Request) error {
return nil
}

// Client wraps an HTTP client to automatically sign requests.
func (s *Signer) Client(client connect.HTTPClient) *SigningClient {
return &SigningClient{client, s}
}

// SigningClient is an HTTP client that automatically signs requests.
type SigningClient struct {
client connect.HTTPClient
signer *Signer
}

// Do signs and sends an HTTP request, and returns the HTTP response.
func (c *SigningClient) Do(req *http.Request) (*http.Response, error) {
if err := c.signer.Sign(req); err != nil {
return nil, fmt.Errorf("failed to sign request: %w", err)
}
return c.client.Do(req)
}

// Verifier verifies that requests were signed by Dispatch.
type Verifier struct {
verifier *httpsig.Verifier
}

// NewVerifier creates a Verifier that verifies that requests were
// by Dispatch using the private key associated with this public
// verification key.
// signed by Dispatch using the private key associated with this
// public verification key.
func NewVerifier(verificationKey ed25519.PublicKey) *Verifier {
verifier := httpsig.NewVerifier(
httpsig.WithVerifyEd25519("default", verificationKey),
Expand Down Expand Up @@ -114,22 +133,3 @@ func (v *Verifier) Middleware(next http.Handler) http.Handler {
next.ServeHTTP(w, r)
})
}

// Client wraps an HTTP client in order to sign requests.
func (s *Signer) Client(client connect.HTTPClient) *SigningClient {
return &SigningClient{client, s}
}

// SigningClient is an HTTP client that automatically signs requests.
type SigningClient struct {
client connect.HTTPClient
signer *Signer
}

// Do signs and sends an HTTP request, and returns the HTTP response.
func (c *SigningClient) Do(req *http.Request) (*http.Response, error) {
if err := c.signer.Sign(req); err != nil {
return nil, fmt.Errorf("failed to sign request: %w", err)
}
return c.client.Do(req)
}

0 comments on commit b9632ea

Please sign in to comment.