addAuthHeaderRoundTripper wraps rt so each request receives the token returned by tokenForHost for its (normalized) host, unless the Authorization header is already set or the request is a redirect to a different host. tokenForHost encapsulates how the token is resolved, which is the only difference
(rt http.RoundTripper, tokenForHost func(host string) string)
| 249 | // host, unless the Authorization header is already set or the request is a redirect to a different host. tokenForHost |
| 250 | // encapsulates how the token is resolved, which is the only difference between the primitive and refresh-aware paths. |
| 251 | func addAuthHeaderRoundTripper(rt http.RoundTripper, tokenForHost func(host string) string) http.RoundTripper { |
| 252 | return &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) { |
| 253 | // If the header is already set in the request, don't overwrite it. |
| 254 | if req.Header.Get(authorization) != "" { |
| 255 | return rt.RoundTrip(req) |
| 256 | } |
| 257 | |
| 258 | var redirectHostnameChange bool |
| 259 | if req.Response != nil && req.Response.Request != nil { |
| 260 | redirectHostnameChange = getHostname(req) != getHostname(req.Response.Request) |
| 261 | } |
| 262 | |
| 263 | // Only set header if an initial request or redirect request to the same host as the initial request. |
| 264 | // If the host has changed during a redirect do not add the authentication token header. |
| 265 | if redirectHostnameChange { |
| 266 | return rt.RoundTrip(req) |
| 267 | } |
| 268 | |
| 269 | hostnameInRequest := ghauth.NormalizeHostname(getHostname(req)) |
| 270 | token := tokenForHost(hostnameInRequest) |
| 271 | if token != "" { |
| 272 | req.Header.Set(authorization, fmt.Sprintf("token %s", token)) |
| 273 | } |
| 274 | |
| 275 | return rt.RoundTrip(req) |
| 276 | }} |
| 277 | } |
| 278 | |
| 279 | // ExtractHeader extracts a named header from any response received by this client and, |
| 280 | // if non-blank, saves it to dest. |
no test coverage detected