From de8cb1b4872d20e379cd88cbfdcf4a9a95b87635 Mon Sep 17 00:00:00 2001 From: Stas Dmytryshyn Date: Thu, 31 Aug 2023 20:42:59 +0200 Subject: [PATCH] feat: with http client for wallet cli (#1394) * feat: with http client for wallet cli * chore: comment * fix: ctx * fix: bdd --- component/wallet-cli/cmd/oidc4vp.go | 3 ++- component/wallet-cli/pkg/walletrunner/http.go | 27 +++++++++++++++++++ .../pkg/walletrunner/wallet_runner_oidc4vp.go | 25 ++++++++++++----- test/bdd/pkg/v1/oidc4vc/oidc4vp.go | 4 ++- test/bdd/pkg/v1/oidc4vp/oidc4vp.go | 3 ++- test/bdd/pkg/v1/oidc4vp/stress_request.go | 7 ++--- test/stress/pkg/stress/stress_test_case.go | 3 ++- 7 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 component/wallet-cli/pkg/walletrunner/http.go diff --git a/component/wallet-cli/cmd/oidc4vp.go b/component/wallet-cli/cmd/oidc4vp.go index 15769c55b..6b80ab4eb 100644 --- a/component/wallet-cli/cmd/oidc4vp.go +++ b/component/wallet-cli/cmd/oidc4vp.go @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0 package cmd import ( + "context" "fmt" "image" _ "image/gif" @@ -70,7 +71,7 @@ func NewOIDC4VPCommand() *cobra.Command { return fmt.Errorf("unable to create wallet runner: %v", err) } - return runner.RunOIDC4VPFlow(oidc4vpAuthorizationRequest, nil) + return runner.RunOIDC4VPFlow(context.TODO(), oidc4vpAuthorizationRequest, nil) }, } diff --git a/component/wallet-cli/pkg/walletrunner/http.go b/component/wallet-cli/pkg/walletrunner/http.go new file mode 100644 index 000000000..0a7eed2a1 --- /dev/null +++ b/component/wallet-cli/pkg/walletrunner/http.go @@ -0,0 +1,27 @@ +/* +Copyright Avast Software. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package walletrunner + +import ( + "context" + "net/http" +) + +type httpClientKey = struct{} + +func WithHttpClient(ctx context.Context, client *http.Client) context.Context { + return context.WithValue(ctx, httpClientKey{}, client) +} + +func HttpClientFromContext(ctx context.Context, fallback *http.Client) *http.Client { + val := ctx.Value(httpClientKey{}) + if val != nil { + return val.(*http.Client) + } + + return fallback +} diff --git a/component/wallet-cli/pkg/walletrunner/wallet_runner_oidc4vp.go b/component/wallet-cli/pkg/walletrunner/wallet_runner_oidc4vp.go index be0e0db68..34c3db790 100644 --- a/component/wallet-cli/pkg/walletrunner/wallet_runner_oidc4vp.go +++ b/component/wallet-cli/pkg/walletrunner/wallet_runner_oidc4vp.go @@ -8,6 +8,7 @@ package walletrunner import ( "bytes" + "context" "crypto/tls" "encoding/json" "fmt" @@ -51,7 +52,7 @@ type OIDC4VPHooks struct { CreateAuthorizedResponse []RPConfigOverride } -func (s *Service) RunOIDC4VPFlow(authorizationRequest string, hooks *OIDC4VPHooks) error { +func (s *Service) RunOIDC4VPFlow(ctx context.Context, authorizationRequest string, hooks *OIDC4VPHooks) error { log.Println("Start OIDC4VP flow") log.Println("AuthorizationRequest:", authorizationRequest) @@ -131,7 +132,7 @@ func (s *Service) RunOIDC4VPFlow(authorizationRequest string, hooks *OIDC4VPHook log.Println("Sending authorized response") startTime = time.Now() - dur, err = s.vpFlowExecutor.SendAuthorizedResponse(authorizedResponse) + dur, err = s.vpFlowExecutor.SendAuthorizedResponse(ctx, authorizedResponse) s.perfInfo.SendAuthorizedResponse = dur s.perfInfo.VcsVPFlowDuration += dur if err != nil { @@ -155,6 +156,7 @@ type VPFlowExecutor struct { requestPresentationSubmission *presexch.PresentationSubmission skipSchemaValidation bool + httpClient *http.Client } func (s *Service) NewVPFlowExecutor(skipSchemaValidation bool) *VPFlowExecutor { @@ -167,6 +169,11 @@ func (s *Service) NewVPFlowExecutor(skipSchemaValidation bool) *VPFlowExecutor { walletDidKeyID: s.vcProviderConf.WalletParams.DidKeyID, walletSignType: s.vcProviderConf.WalletParams.SignType, skipSchemaValidation: skipSchemaValidation, + httpClient: &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: s.vcProviderConf.TLS, + }, + }, } } @@ -646,20 +653,24 @@ func signTokenJWT(claims interface{}, didKeyID string, crpt crypto.Crypto, return tokenBytes, nil } -func (e *VPFlowExecutor) SendAuthorizedResponse(responseBody string) (time.Duration, error) { +func (e *VPFlowExecutor) SendAuthorizedResponse(ctx context.Context, responseBody string) (time.Duration, error) { log.Printf("auth req: %s\n", responseBody) - req, err := http.NewRequest(http.MethodPost, e.requestObject.RedirectURI, bytes.NewBuffer([]byte(responseBody))) + req, err := http.NewRequestWithContext( + ctx, + http.MethodPost, + e.requestObject.RedirectURI, + bytes.NewBuffer([]byte(responseBody)), + ) if err != nil { return 0, err } req.Header.Add("Content-Type", "application/x-www-form-urlencoded") - c := &http.Client{Transport: &http.Transport{TLSClientConfig: e.tlsConfig}} - + client := HttpClientFromContext(ctx, e.httpClient) st := time.Now() - resp, err := c.Do(req) + resp, err := client.Do(req) dur := time.Since(st) if err != nil { diff --git a/test/bdd/pkg/v1/oidc4vc/oidc4vp.go b/test/bdd/pkg/v1/oidc4vc/oidc4vp.go index e52bf3d4f..8b9992770 100644 --- a/test/bdd/pkg/v1/oidc4vc/oidc4vp.go +++ b/test/bdd/pkg/v1/oidc4vc/oidc4vp.go @@ -8,6 +8,7 @@ package oidc4vc import ( "bytes" + "context" "encoding/json" "errors" "fmt" @@ -18,6 +19,7 @@ import ( "github.com/hyperledger/aries-framework-go/component/models/presexch" "github.com/hyperledger/aries-framework-go/component/models/verifiable" + "github.com/trustbloc/vcs/component/wallet-cli/pkg/walletrunner" vcs "github.com/trustbloc/vcs/pkg/doc/verifiable" "github.com/trustbloc/vcs/pkg/event/spi" @@ -78,7 +80,7 @@ func (s *Steps) runOIDC4VPFlow(profileVersionedID, organizationName, pdID, field return fmt.Errorf("OIDC4Vp fetch authorization request: %w", err) } - err = s.walletRunner.RunOIDC4VPFlow(initiateInteractionResult.AuthorizationRequest, s.oidc4vpHooks) + err = s.walletRunner.RunOIDC4VPFlow(context.TODO(), initiateInteractionResult.AuthorizationRequest, s.oidc4vpHooks) if err != nil { return fmt.Errorf("s.walletRunner.RunOIDC4VPFlow: %w", err) } diff --git a/test/bdd/pkg/v1/oidc4vp/oidc4vp.go b/test/bdd/pkg/v1/oidc4vp/oidc4vp.go index f01df650c..ed68446bb 100644 --- a/test/bdd/pkg/v1/oidc4vp/oidc4vp.go +++ b/test/bdd/pkg/v1/oidc4vp/oidc4vp.go @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0 package oidc4vp import ( + "context" "encoding/json" "errors" "fmt" @@ -93,7 +94,7 @@ func (e *Steps) sendAuthorizedResponse() error { return err } - _, err = e.vpFlowExecutor.SendAuthorizedResponse(body) + _, err = e.vpFlowExecutor.SendAuthorizedResponse(context.TODO(), body) return err } diff --git a/test/bdd/pkg/v1/oidc4vp/stress_request.go b/test/bdd/pkg/v1/oidc4vp/stress_request.go index 7f35dcd5f..e2d937096 100644 --- a/test/bdd/pkg/v1/oidc4vp/stress_request.go +++ b/test/bdd/pkg/v1/oidc4vp/stress_request.go @@ -8,6 +8,7 @@ package oidc4vp import ( "bytes" + "context" "fmt" "time" @@ -70,7 +71,7 @@ func (r *stressRequest) Invoke() (string, interface{}, error) { startTime = time.Now() - err = r.sendAuthorizedResponse(authorizedResponse) + err = r.sendAuthorizedResponse(context.TODO(), authorizedResponse) if err != nil { return "", nil, err } @@ -123,8 +124,8 @@ func (r *stressRequest) createAuthorizedResponse() (string, error) { return r.vpFlowExecutor.CreateAuthorizedResponse() } -func (r *stressRequest) sendAuthorizedResponse(responseBody string) error { - _, err := r.vpFlowExecutor.SendAuthorizedResponse(responseBody) +func (r *stressRequest) sendAuthorizedResponse(ctx context.Context, responseBody string) error { + _, err := r.vpFlowExecutor.SendAuthorizedResponse(ctx, responseBody) return err } diff --git a/test/stress/pkg/stress/stress_test_case.go b/test/stress/pkg/stress/stress_test_case.go index 21996e560..cb3df6fe3 100644 --- a/test/stress/pkg/stress/stress_test_case.go +++ b/test/stress/pkg/stress/stress_test_case.go @@ -8,6 +8,7 @@ package stress import ( "bytes" + "context" "crypto/tls" "encoding/json" "fmt" @@ -237,7 +238,7 @@ func (c *TestCase) Invoke() (string, interface{}, error) { return credID, nil, fmt.Errorf("CredId [%v]. fetch authorization request: %w", credID, err) } - err = c.walletRunner.RunOIDC4VPFlow(authorizationRequest, nil) + err = c.walletRunner.RunOIDC4VPFlow(context.TODO(), authorizationRequest, nil) if err != nil { return credID, nil, fmt.Errorf("CredId [%v]. run vp: %w", credID, err) }