Skip to content

Commit

Permalink
Changed RefreshAuthToken in ConnectionService and KustoClient to foll…
Browse files Browse the repository at this point in the history
…ow TryParse pattern. (#1245)
  • Loading branch information
JustinMDotNet authored and nofield committed Jul 19, 2022
1 parent 8356a8c commit 1db0237
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,13 @@ public async Task<ConnectionCompleteParams> Connect(ConnectParams connectionPara
return completeParams;
}

internal string RefreshAuthToken(string ownerUri)
internal bool TryRefreshAuthToken(string ownerUri, out string token)
{
TryFindConnection(ownerUri, out ConnectionInfo connection);
token = string.Empty;
if (!TryFindConnection(ownerUri, out ConnectionInfo connection))
{
return false;
}

var requestMessage = new RequestSecurityTokenParams
{
Expand All @@ -227,8 +231,8 @@ internal string RefreshAuthToken(string ownerUri)

var response = _serviceHost.SendRequest(SecurityTokenRequest.Type, requestMessage, true).Result;
connection.UpdateAuthToken(response.Token);

return response.Token;
token = response.Token;
return true;
}

private void TryCloseConnectionTemporaryConnection(ConnectParams connectionParams, ConnectionInfo connectionInfo)
Expand Down
24 changes: 19 additions & 5 deletions src/Microsoft.Kusto.ServiceLayer/DataSource/Kusto/KustoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ private void Initialize(DataSourceConnectionDetails connectionDetails)
DatabaseName = databaseName?.ToString() ?? "";
}

private void RefreshAuthToken()
private bool TryRefreshAuthToken()
{
string accountToken = ConnectionService.Instance.RefreshAuthToken(_ownerUri);
if (!ConnectionService.Instance.TryRefreshAuthToken(_ownerUri, out string accountToken))
{
return false;
}

_kustoQueryProvider.Dispose();
_kustoAdminProvider.Dispose();

Expand All @@ -69,6 +73,7 @@ private void RefreshAuthToken()
};

Initialize(connectionDetails);
return true;
}

private KustoConnectionStringBuilder GetKustoConnectionStringBuilder(DataSourceConnectionDetails connectionDetails)
Expand Down Expand Up @@ -163,7 +168,10 @@ public IDataReader ExecuteQuery(string query, CancellationToken cancellationToke
exception.InnerException is KustoRequestException innerException
&& innerException.FailureCode == 401) // Unauthorized
{
RefreshAuthToken();
if (!TryRefreshAuthToken())
{
throw;
}
retryCount--;
return ExecuteQuery(query, cancellationToken, databaseName, retryCount);
}
Expand All @@ -184,7 +192,10 @@ public async Task ExecuteControlCommandAsync(string command, bool throwOnError,
}
catch (KustoRequestException exception) when (retryCount > 0 && exception.FailureCode == 401) // Unauthorized
{
RefreshAuthToken();
if (!TryRefreshAuthToken())
{
throw;
}
retryCount--;
await ExecuteControlCommandAsync(command, throwOnError, retryCount);
}
Expand Down Expand Up @@ -232,7 +243,10 @@ public void ExecuteControlCommand(string command, int retryCount = 1)
}
catch (KustoRequestException exception) when (retryCount > 0 && exception.FailureCode == 401) // Unauthorized
{
RefreshAuthToken();
if (!TryRefreshAuthToken())
{
throw;
}
retryCount--;
ExecuteControlCommand(command, retryCount);
}
Expand Down

0 comments on commit 1db0237

Please sign in to comment.