Skip to content

Commit

Permalink
feat: with http client for wallet cli (#1394)
Browse files Browse the repository at this point in the history
* feat: with http client for wallet cli

* chore: comment

* fix: ctx

* fix: bdd
  • Loading branch information
skynet2 authored Aug 31, 2023
1 parent e84117a commit de8cb1b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 14 deletions.
3 changes: 2 additions & 1 deletion component/wallet-cli/cmd/oidc4vp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package cmd

import (
"context"
"fmt"
"image"
_ "image/gif"
Expand Down Expand Up @@ -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)
},
}

Expand Down
27 changes: 27 additions & 0 deletions component/wallet-cli/pkg/walletrunner/http.go
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 18 additions & 7 deletions component/wallet-cli/pkg/walletrunner/wallet_runner_oidc4vp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package walletrunner

import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand All @@ -155,6 +156,7 @@ type VPFlowExecutor struct {
requestPresentationSubmission *presexch.PresentationSubmission

skipSchemaValidation bool
httpClient *http.Client
}

func (s *Service) NewVPFlowExecutor(skipSchemaValidation bool) *VPFlowExecutor {
Expand All @@ -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,
},
},
}
}

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion test/bdd/pkg/v1/oidc4vc/oidc4vp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package oidc4vc

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion test/bdd/pkg/v1/oidc4vp/oidc4vp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package oidc4vp

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 4 additions & 3 deletions test/bdd/pkg/v1/oidc4vp/stress_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package oidc4vp

import (
"bytes"
"context"
"fmt"
"time"

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion test/stress/pkg/stress/stress_test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package stress

import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit de8cb1b

Please sign in to comment.