Skip to content

Commit

Permalink
feat(sdk): pass headers to every http call (#848)
Browse files Browse the repository at this point in the history
Signed-off-by: Volodymyr Kit <[email protected]>
  • Loading branch information
justakit authored Dec 9, 2024
1 parent f2bd855 commit 5f69ac8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 40 deletions.
8 changes: 4 additions & 4 deletions cmd/wallet-sdk-gomobile/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ require (
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/crypto v0.30.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
16 changes: 8 additions & 8 deletions cmd/wallet-sdk-gomobile/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,28 @@ golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/crypto v0.30.0 h1:RwoQn3GkWiMkzlX562cLB7OxWvjH1L8xutO2WoJcRoY=
golang.org/x/crypto v0.30.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ=
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/oauth2 v0.13.0 h1:jDDenyj+WgFtmV3zYVoi8aE2BwtXFLWOA67ZfNWftiY=
golang.org/x/oauth2 v0.13.0/go.mod h1:/JMhi4ZRXAf4HG9LiNmxvk+45+96RUlVThiH8FzNBn0=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
Expand Down
24 changes: 9 additions & 15 deletions cmd/wallet-sdk-gomobile/trustregistry/trust_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ SPDX-License-Identifier: Apache-2.0
package trustregistry

import (
"crypto/tls"
"net/http"
"time"

"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/api"
"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/wrapper"
"github.com/trustbloc/wallet-sdk/pkg/trustregistry"
)

Expand All @@ -20,6 +20,12 @@ type RegistryConfig struct {
EvaluateIssuanceURL string
EvaluatePresentationURL string
DisableHTTPClientTLSVerify bool
additionalHeaders api.Headers
}

// AddHeader adds the given HTTP header to all REST calls made to the trust registry during evaluation flow.
func (r *RegistryConfig) AddHeader(header *api.Header) {
r.additionalHeaders.Add(header)
}

// Registry implements API for trust registry.
Expand All @@ -29,19 +35,7 @@ type Registry struct {

// NewRegistry creates new trust registry API.
func NewRegistry(config *RegistryConfig) *Registry {
var httpClient *http.Client
if config.DisableHTTPClientTLSVerify {
httpClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
//nolint:gosec // The ability to disable TLS is an option we provide that
// has to be explicitly set by the user. By default, we don't disable TLS.
// This option is only intended for testing purposes.
InsecureSkipVerify: true,
},
},
}
}
httpClient := wrapper.NewHTTPClient(nil, config.additionalHeaders, config.DisableHTTPClientTLSVerify)

return &Registry{
impl: trustregistry.New(&trustregistry.RegistryConfig{
Expand Down
23 changes: 20 additions & 3 deletions cmd/wallet-sdk-gomobile/verifiable/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (

// Opts contains all optional arguments that can be passed into the Parse function.
type Opts struct {
disableProofCheck bool
documentLoader api.LDDocumentLoader
httpTimeout *time.Duration
disableProofCheck bool
documentLoader api.LDDocumentLoader
httpTimeout *time.Duration
additionalHeaders api.Headers
disableHTTPClientTLSVerification bool
}

// NewOpts returns a new Opts object for use with the Parse function.
Expand Down Expand Up @@ -48,3 +50,18 @@ func (o *Opts) SetHTTPTimeoutNanoseconds(timeout int64) *Opts {

return o
}

// AddHeader adds the given HTTP header to all REST calls made by network-based document loader
// In case SetDocumentLoader is used - this option does not affect http calls.
func (o *Opts) AddHeader(header *api.Header) *Opts {
o.additionalHeaders.Add(header)

return o
}

// DisableHTTPClientTLSVerify disables TLS verification. Should be used for testing purposes only.
func (o *Opts) DisableHTTPClientTLSVerify() *Opts {
o.disableHTTPClientTLSVerification = true

return o
}
11 changes: 1 addition & 10 deletions cmd/wallet-sdk-gomobile/verifiable/parsecredential.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ SPDX-License-Identifier: Apache-2.0
package verifiable

import (
"net/http"

"github.com/trustbloc/vc-go/verifiable"

"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/wrapper"
goapi "github.com/trustbloc/wallet-sdk/pkg/api"
"github.com/trustbloc/wallet-sdk/pkg/common"
"github.com/trustbloc/wallet-sdk/pkg/memstorage/legacy"
)
Expand All @@ -30,13 +27,7 @@ func ParseCredential(vc string, opts *Opts) (*Credential, error) {
}

if opts.documentLoader == nil {
httpClient := &http.Client{}

if opts.httpTimeout != nil {
httpClient.Timeout = *opts.httpTimeout
} else {
httpClient.Timeout = goapi.DefaultHTTPTimeout
}
httpClient := wrapper.NewHTTPClient(opts.httpTimeout, opts.additionalHeaders, opts.disableHTTPClientTLSVerification)

goAPIDocumentLoader, err := common.CreateJSONLDDocumentLoader(httpClient, legacy.NewProvider())
if err != nil {
Expand Down

0 comments on commit 5f69ac8

Please sign in to comment.