From 49b36038aead362ce78f09337dc5872000bd048a Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Thu, 14 Jul 2022 13:42:19 +0200 Subject: [PATCH] Improve OAuth2 user agent handling (#391) Instead of relying on the User Agent in the configuration, take the User Agent of the original request. This enables compatibility with existing user agents. Signed-off-by: Julien Pivotto --- config/http_config.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/config/http_config.go b/config/http_config.go index bcda953a..b47347e4 100644 --- a/config/http_config.go +++ b/config/http_config.go @@ -505,13 +505,14 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, optFuncs ...HT rt = NewBasicAuthRoundTripper(cfg.BasicAuth.Username, cfg.BasicAuth.Password, cfg.BasicAuth.PasswordFile, rt) } + if cfg.OAuth2 != nil { + rt = NewOAuth2RoundTripper(cfg.OAuth2, rt, &opts) + } + if opts.userAgent != "" { rt = NewUserAgentRoundTripper(opts.userAgent, rt) } - if cfg.OAuth2 != nil { - rt = NewOAuth2RoundTripper(cfg.OAuth2, rt, &opts) - } // Return a new configured RoundTripper. return rt, nil } @@ -701,8 +702,8 @@ func (rt *oauth2RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro } } - if rt.opts.userAgent != "" { - t = NewUserAgentRoundTripper(rt.opts.userAgent, t) + if ua := req.UserAgent(); ua != "" { + t = NewUserAgentRoundTripper(ua, t) } client := &http.Client{Transport: t}