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

Function addAuthHeaderRoundTripper

api/http_client.go:251–277  ·  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

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

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