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)
| 276 | // host, unless the Authorization header is already set or the request is a redirect to a different host. tokenForHost |
| 277 | // encapsulates how the token is resolved, which is the only difference between the primitive and refresh-aware paths. |
| 278 | func addAuthHeaderRoundTripper(rt http.RoundTripper, tokenForHost func(host string) string) http.RoundTripper { |
| 279 | return &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) { |
| 280 | // If the header is already set in the request, don't overwrite it. |
| 281 | if req.Header.Get(authorization) != "" { |
| 282 | return rt.RoundTrip(req) |
| 283 | } |
| 284 | |
| 285 | var redirectHostnameChange bool |
| 286 | if req.Response != nil && req.Response.Request != nil { |
| 287 | redirectHostnameChange = getHostname(req) != getHostname(req.Response.Request) |
| 288 | } |
| 289 | |
| 290 | // Only set header if an initial request or redirect request to the same host as the initial request. |
| 291 | // If the host has changed during a redirect do not add the authentication token header. |
| 292 | if redirectHostnameChange { |
| 293 | return rt.RoundTrip(req) |
| 294 | } |
| 295 | |
| 296 | hostnameInRequest := ghauth.NormalizeHostname(getHostname(req)) |
| 297 | token := tokenForHost(hostnameInRequest) |
| 298 | if token != "" { |
| 299 | req.Header.Set(authorization, fmt.Sprintf("token %s", token)) |
| 300 | } |
| 301 | |
| 302 | return rt.RoundTrip(req) |
| 303 | }} |
| 304 | } |
| 305 | |
| 306 | // ExtractHeader extracts a named header from any response received by this client and, |
| 307 | // if non-blank, saves it to dest. |
no test coverage detected