Skip to content

Commit

Permalink
generic: catch OAuth refresh token errors
Browse files Browse the repository at this point in the history
In case we have an existing refresh token, but it has expired or been
revoked, we should gracefully fall back to performing an interactive
OAuth flow.
  • Loading branch information
mjcheetham committed Apr 11, 2023
1 parent dc1edca commit eb2de63
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/shared/Core/GenericHostProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,26 @@ private async Task<ICredential> GetOAuthAccessToken(Uri remoteUri, string userNa
ICredential refreshToken = Context.CredentialStore.Get(refreshService, userName);
if (refreshToken != null)
{
var refreshResult = await client.GetTokenByRefreshTokenAsync(refreshToken.Password, CancellationToken.None);
try
{
var refreshResult = await client.GetTokenByRefreshTokenAsync(refreshToken.Password, CancellationToken.None);

// Store new refresh token if we have been given one
if (!string.IsNullOrWhiteSpace(refreshResult.RefreshToken))
{
Context.CredentialStore.AddOrUpdate(refreshService, refreshToken.Account, refreshToken.Password);
}
// Store new refresh token if we have been given one
if (!string.IsNullOrWhiteSpace(refreshResult.RefreshToken))
{
Context.CredentialStore.AddOrUpdate(refreshService, refreshToken.Account, refreshToken.Password);
}

// Return the new access token
return new GitCredential(oauthUser,refreshResult.AccessToken);
// Return the new access token
return new GitCredential(oauthUser,refreshResult.AccessToken);
}
catch (OAuth2Exception ex)
{
// Failed to use refresh token. It may have expired or been revoked.
// Fall through to an interactive OAuth flow.
Context.Trace.WriteLine("Failed to use refresh token.");
Context.Trace.WriteException(ex);
}
}

// Determine which interactive OAuth mode to use. Start by checking for mode preference in config
Expand Down

0 comments on commit eb2de63

Please sign in to comment.