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

Function AddAuthTokenHeaderWithRefresh

api/http_client.go:227–246  ·  view source on GitHub ↗

AddAuthTokenHeaderWithRefresh is the refresh-aware counterpart of AddAuthTokenHeader. It renews a near-expiry token before attaching it, and is the attach helper gh's own code uses so its requests go through the refresh path rather than the backward-compatible primitive seam.

(rt http.RoundTripper, cfg refreshingConfig)

Source from the content-addressed store, hash-verified

225// token before attaching it, and is the attach helper gh's own code uses so its requests go through the
226// refresh path rather than the backward-compatible primitive seam.
227func AddAuthTokenHeaderWithRefresh(rt http.RoundTripper, cfg refreshingConfig) http.RoundTripper {
228 return addAuthHeaderRoundTripper(rt, func(host string) string {
229 // Refreshing is best effort because it happens before actual expiry, and expiry checks can be unreliable due
230 // to clock skew. Continue with the returned token and let the API determine whether it is valid.
231 cred, _, _ := cfg.ActiveTokenWithRefresh(host)
232 token := cred.Token
233 if token == "" {
234 // The request may be aimed at a host's api_host, which gh is
235 // not logged in to and so has no token of its own. Fall back
236 // to the token of the host it stands in for. This only ever
237 // adds a token where there would have been none, so hosts we
238 // already authenticate keep resolving exactly as before.
239 if canonicalHost, ok := cfg.HostForAPIHost(host); ok {
240 cred, _, _ = cfg.ActiveTokenWithRefresh(canonicalHost)
241 token = cred.Token
242 }
243 }
244 return token
245 })
246}
247
248// addAuthHeaderRoundTripper wraps rt so each request receives the token returned by tokenForHost for its (normalized)
249// host, unless the Authorization header is already set or the request is a redirect to a different host. tokenForHost

Calls 3

HostForAPIHostMethod · 0.65