Changed RefreshAuthToken in ConnectionService and KustoClient to follow TryParse pattern. (#1245)

This commit is contained in:
Justin M
2021-09-17 12:25:11 -07:00
committed by GitHub
parent 6f5cac4cb5
commit 60c847ff60
2 changed files with 27 additions and 9 deletions

View File

@@ -213,9 +213,13 @@ namespace Microsoft.Kusto.ServiceLayer.Connection
return completeParams; 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 var requestMessage = new RequestSecurityTokenParams
{ {
@@ -227,8 +231,8 @@ namespace Microsoft.Kusto.ServiceLayer.Connection
var response = _serviceHost.SendRequest(SecurityTokenRequest.Type, requestMessage, true).Result; var response = _serviceHost.SendRequest(SecurityTokenRequest.Type, requestMessage, true).Result;
connection.UpdateAuthToken(response.Token); connection.UpdateAuthToken(response.Token);
token = response.Token;
return response.Token; return true;
} }
private void TryCloseConnectionTemporaryConnection(ConnectParams connectionParams, ConnectionInfo connectionInfo) private void TryCloseConnectionTemporaryConnection(ConnectParams connectionParams, ConnectionInfo connectionInfo)

View File

@@ -54,9 +54,13 @@ namespace Microsoft.Kusto.ServiceLayer.DataSource.Kusto
DatabaseName = databaseName?.ToString() ?? ""; 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(); _kustoQueryProvider.Dispose();
_kustoAdminProvider.Dispose(); _kustoAdminProvider.Dispose();
@@ -69,6 +73,7 @@ namespace Microsoft.Kusto.ServiceLayer.DataSource.Kusto
}; };
Initialize(connectionDetails); Initialize(connectionDetails);
return true;
} }
private KustoConnectionStringBuilder GetKustoConnectionStringBuilder(DataSourceConnectionDetails connectionDetails) private KustoConnectionStringBuilder GetKustoConnectionStringBuilder(DataSourceConnectionDetails connectionDetails)
@@ -163,7 +168,10 @@ namespace Microsoft.Kusto.ServiceLayer.DataSource.Kusto
exception.InnerException is KustoRequestException innerException exception.InnerException is KustoRequestException innerException
&& innerException.FailureCode == 401) // Unauthorized && innerException.FailureCode == 401) // Unauthorized
{ {
RefreshAuthToken(); if (!TryRefreshAuthToken())
{
throw;
}
retryCount--; retryCount--;
return ExecuteQuery(query, cancellationToken, databaseName, retryCount); return ExecuteQuery(query, cancellationToken, databaseName, retryCount);
} }
@@ -184,7 +192,10 @@ namespace Microsoft.Kusto.ServiceLayer.DataSource.Kusto
} }
catch (KustoRequestException exception) when (retryCount > 0 && exception.FailureCode == 401) // Unauthorized catch (KustoRequestException exception) when (retryCount > 0 && exception.FailureCode == 401) // Unauthorized
{ {
RefreshAuthToken(); if (!TryRefreshAuthToken())
{
throw;
}
retryCount--; retryCount--;
await ExecuteControlCommandAsync(command, throwOnError, retryCount); await ExecuteControlCommandAsync(command, throwOnError, retryCount);
} }
@@ -232,7 +243,10 @@ namespace Microsoft.Kusto.ServiceLayer.DataSource.Kusto
} }
catch (KustoRequestException exception) when (retryCount > 0 && exception.FailureCode == 401) // Unauthorized catch (KustoRequestException exception) when (retryCount > 0 && exception.FailureCode == 401) // Unauthorized
{ {
RefreshAuthToken(); if (!TryRefreshAuthToken())
{
throw;
}
retryCount--; retryCount--;
ExecuteControlCommand(command, retryCount); ExecuteControlCommand(command, retryCount);
} }