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

Function addAuthHeaderRoundTripper

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

228// host, unless the Authorization header is already set or the request is a redirect to a different host. tokenForHost
229// encapsulates how the token is resolved, which is the only difference between the primitive and refresh-aware paths.
230func addAuthHeaderRoundTripper(rt http.RoundTripper, tokenForHost func(host string) string) http.RoundTripper {
231 return &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) {
232 // If the header is already set in the request, don't overwrite it.
233 if req.Header.Get(authorization) != "" {
234 return rt.RoundTrip(req)
235 }
236
237 var redirectHostnameChange bool
238 if req.Response != nil && req.Response.Request != nil {
239 redirectHostnameChange = getHostname(req) != getHostname(req.Response.Request)
240 }
241
242 // Only set header if an initial request or redirect request to the same host as the initial request.
243 // If the host has changed during a redirect do not add the authentication token header.
244 if redirectHostnameChange {
245 return rt.RoundTrip(req)
246 }
247
248 hostnameInRequest := ghauth.NormalizeHostname(getHostname(req))
249 token := tokenForHost(hostnameInRequest)
250 if token != "" {
251 req.Header.Set(authorization, fmt.Sprintf("token %s", token))
252 }
253
254 return rt.RoundTrip(req)
255 }}
256}
257
258// ExtractHeader extracts a named header from any response received by this client and,
259// 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