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)
| 204 | // token before attaching it, and is the attach helper gh's own code uses so its requests go through the |
| 205 | // refresh path rather than the backward-compatible primitive seam. |
| 206 | func AddAuthTokenHeaderWithRefresh(rt http.RoundTripper, cfg refreshingConfig) http.RoundTripper { |
| 207 | return addAuthHeaderRoundTripper(rt, func(host string) string { |
| 208 | // Refreshing is best effort because it happens before actual expiry, and expiry checks can be unreliable due |
| 209 | // to clock skew. Continue with the returned token and let the API determine whether it is valid. |
| 210 | cred, _, _ := cfg.ActiveTokenWithRefresh(host) |
| 211 | token := cred.Token |
| 212 | if token == "" { |
| 213 | // The request may be aimed at a host's api_host, which gh is |
| 214 | // not logged in to and so has no token of its own. Fall back |
| 215 | // to the token of the host it stands in for. This only ever |
| 216 | // adds a token where there would have been none, so hosts we |
| 217 | // already authenticate keep resolving exactly as before. |
| 218 | if canonicalHost, ok := cfg.HostForAPIHost(host); ok { |
| 219 | cred, _, _ = cfg.ActiveTokenWithRefresh(canonicalHost) |
| 220 | token = cred.Token |
| 221 | } |
| 222 | } |
| 223 | return token |
| 224 | }) |
| 225 | } |
| 226 | |
| 227 | // addAuthHeaderRoundTripper wraps rt so each request receives the token returned by tokenForHost for its (normalized) |
| 228 | // host, unless the Authorization header is already set or the request is a redirect to a different host. tokenForHost |