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)
| 252 | // token before attaching it, and is the attach helper gh's own code uses so its requests go through the |
| 253 | // refresh path rather than the backward-compatible primitive seam. |
| 254 | func AddAuthTokenHeaderWithRefresh(rt http.RoundTripper, cfg refreshingConfig) http.RoundTripper { |
| 255 | return addAuthHeaderRoundTripper(rt, func(host string) string { |
| 256 | // Refreshing is best effort because it happens before actual expiry, and expiry checks can be unreliable due |
| 257 | // to clock skew. Continue with the returned token and let the API determine whether it is valid. |
| 258 | cred, _, _ := cfg.ActiveTokenWithRefresh(host) |
| 259 | token := cred.Token |
| 260 | if token == "" { |
| 261 | // The request may be aimed at a host's api_host, which gh is |
| 262 | // not logged in to and so has no token of its own. Fall back |
| 263 | // to the token of the host it stands in for. This only ever |
| 264 | // adds a token where there would have been none, so hosts we |
| 265 | // already authenticate keep resolving exactly as before. |
| 266 | if canonicalHost, ok := cfg.HostForAPIHost(host); ok { |
| 267 | cred, _, _ = cfg.ActiveTokenWithRefresh(canonicalHost) |
| 268 | token = cred.Token |
| 269 | } |
| 270 | } |
| 271 | return token |
| 272 | }) |
| 273 | } |
| 274 | |
| 275 | // addAuthHeaderRoundTripper wraps rt so each request receives the token returned by tokenForHost for its (normalized) |
| 276 | // host, unless the Authorization header is already set or the request is a redirect to a different host. tokenForHost |