Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ func NewRoundTripperFromConfigWithContext(ctx context.Context, cfg HTTPClientCon
return nil, fmt.Errorf("unable to use client secret: %w", err)
}
}
rt = NewOAuth2RoundTripper(oauthCredential, cfg.OAuth2, rt, &opts)
rt = NewOAuth2RoundTripper(oauthCredential, cfg.OAuth2, rt, optFuncs...)
}

if cfg.HTTPHeaders != nil {
Expand Down Expand Up @@ -942,16 +942,26 @@ type oauth2RoundTripper struct {
client *http.Client
}

func NewOAuth2RoundTripper(oauthCredential SecretReader, config *OAuth2, next http.RoundTripper, opts *httpClientOptions) http.RoundTripper {
// NewOAuth2RoundTripper returns a round tripper that performs OAuth2
// authentication. The opts variadic parameter accepts any HTTPClientOption
// (e.g. WithDialContextFunc, WithKeepAlivesDisabled) so that callers outside
// this package can fully configure the transport without needing access to the
// unexported *httpClientOptions type.
func NewOAuth2RoundTripper(oauthCredential SecretReader, config *OAuth2, next http.RoundTripper, optFuncs ...HTTPClientOption) http.RoundTripper {
if oauthCredential == nil {
oauthCredential = NewInlineSecret("")
}

opts := defaultHTTPClientOptions
for _, opt := range optFuncs {
opt.applyToHTTPClientOptions(&opts)
}

return &oauth2RoundTripper{
config: config,
// A correct tokenSource will be added later on.
lastRT: &oauth2.Transport{Base: next},
opts: opts,
opts: &opts,
oauthCredential: oauthCredential,
}
}
Expand Down
6 changes: 3 additions & 3 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,7 @@ endpoint_params:
require.Truef(t, reflect.DeepEqual(unmarshalledConfig, expectedConfig), "Got unmarshalled config %v, expected %v", unmarshalledConfig, expectedConfig)

secret := NewInlineSecret(string(expectedConfig.ClientSecret))
rt := NewOAuth2RoundTripper(secret, &expectedConfig, http.DefaultTransport, &defaultHTTPClientOptions)
rt := NewOAuth2RoundTripper(secret, &expectedConfig, http.DefaultTransport)

client := http.Client{
Transport: rt,
Expand Down Expand Up @@ -1654,7 +1654,7 @@ endpoint_params:
require.Truef(t, reflect.DeepEqual(unmarshalledConfig, expectedConfig), "Got unmarshalled config %v, expected %v", unmarshalledConfig, expectedConfig)

secret := NewFileSecret(expectedConfig.ClientSecretFile)
rt := NewOAuth2RoundTripper(secret, &expectedConfig, http.DefaultTransport, &defaultHTTPClientOptions)
rt := NewOAuth2RoundTripper(secret, &expectedConfig, http.DefaultTransport)

client := http.Client{
Transport: rt,
Expand Down Expand Up @@ -1768,7 +1768,7 @@ endpoint_params:
require.Truef(t, reflect.DeepEqual(unmarshalledConfig, expectedConfig), "Got unmarshalled config %v, expected %v", unmarshalledConfig, expectedConfig)

clientCertificateKey := NewFileSecret(expectedConfig.ClientCertificateKeyFile)
rt := NewOAuth2RoundTripper(clientCertificateKey, &expectedConfig, http.DefaultTransport, &defaultHTTPClientOptions)
rt := NewOAuth2RoundTripper(clientCertificateKey, &expectedConfig, http.DefaultTransport)

client := http.Client{
Transport: rt,
Expand Down
Loading