MCPcopy Create free account
hub / github.com/cli/cli / addAuthHeaderRoundTripper

Function addAuthHeaderRoundTripper

api/http_client.go:278–304  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
278func 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.

Callers 2

AddAuthTokenHeaderFunction · 0.85

Calls 4

getHostnameFunction · 0.85
GetMethod · 0.65
SetMethod · 0.65
RoundTripMethod · 0.45

Tested by

no test coverage detected