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

Method Refresh

internal/authflow/refresh.go:45–79  ·  view source on GitHub ↗

Refresh exchanges refreshToken for a renewed credential on hostname. A refresh token the server rejects surfaces as an error wrapping gh.ErrRefreshTokenInvalid, so callers can distinguish an unusable refresh token from a transient failure without depending on the underlying OAuth library. When the s

(refreshToken, hostname string)

Source from the content-addressed store, hash-verified

43// failure without depending on the underlying OAuth library. When the server omits a new refresh token, the returned
44// credential keeps refreshToken, since RFC 6749 treats the previous refresh token as still valid in that case.
45func (r *TokenRefresher) Refresh(refreshToken, hostname string) (gh.RefreshableCredential, error) {
46 host, err := oauth.NewGitHubHost(ghinstance.HostPrefix(hostname))
47 if err != nil {
48 return gh.RefreshableCredential{}, err
49 }
50
51 opts := oauth.RefreshOptions{
52 Host: host,
53 ClientID: r.clientID,
54 ClientSecret: r.clientSecret,
55 RefreshToken: refreshToken,
56 }
57 // Only set HTTPClient when we actually have one. RefreshOptions.HTTPClient is an interface, so assigning a nil
58 // *http.Client would store a non-nil typed nil that defeats the library's own http.DefaultClient fallback and
59 // panics when the request is made.
60 if r.httpClient != nil {
61 opts.HTTPClient = r.httpClient
62 }
63
64 // Anchor the token lifetime to the instant just before the exchange. The server reports expires_in relative to
65 // when it issues the token, which is at or after this point, so measuring from here never overestimates the
66 // expiry. Use UTC so the derived absolute expiry matches the canonical UTC form gh stores and displays.
67 requestedAt := timeNow().UTC()
68 token, err := oauth.Refresh(opts)
69 if err != nil {
70 // Translate the library's rejection sentinel to gh's own so callers can classify a rejected refresh token
71 // without importing the OAuth library, while preserving the original error in the chain for debugging.
72 if errors.Is(err, oauth.ErrRefreshTokenInvalid) {
73 return gh.RefreshableCredential{}, fmt.Errorf("%w: %w", gh.ErrRefreshTokenInvalid, err)
74 }
75 return gh.RefreshableCredential{}, err
76 }
77
78 return refreshableCredentialFromAccessToken(token, requestedAt), nil
79}
80
81// refreshableCredentialFromAccessToken projects an OAuth access token onto gh.RefreshableCredential. It derives each
82// absolute expiry by adding the server-reported lifetime (ExpiresIn) to requestedAt, the instant the exchange was

Calls 4

HostPrefixFunction · 0.92
RefreshMethod · 0.65
ErrorfMethod · 0.65