Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Code cleanup.
  • Loading branch information
aavasthy committed Aug 22, 2025
commit 0e709adddf27f77b6d8f6639e979fb243cb0b5b2
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public TokenRequestContext GetTokenRequestContext()
return new TokenRequestContext(new[] { this.currentScope });
}

public bool TryFallback(Exception ex)
public bool TryFallback(Exception exception)
{
// If override scope is set, never fallback
if (!string.IsNullOrEmpty(this.overrideScope))
Expand All @@ -40,16 +40,18 @@ public bool TryFallback(Exception ex)
}

// If already using fallback scope, do not fallback again
if (this.currentScope == AadDefaultScope)
if (this.currentScope == CosmosScopeProvider.AadDefaultScope)
{
return false;
}

if (ex.InnerException?.Message.Contains(AadInvalidScopeErrorMessage) == true)
#pragma warning disable CDX1003 // DontUseExceptionToString
if (exception.ToString().Contains(CosmosScopeProvider.AadInvalidScopeErrorMessage) == true)
{
this.currentScope = AadDefaultScope;
this.currentScope = CosmosScopeProvider.AadDefaultScope;
return true;
}
#pragma warning restore CDX1003 // DontUseExceptionToString

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal sealed class TokenCredentialCache : IDisposable
private Task<AccessToken>? currentRefreshOperation = null;
private AccessToken? cachedAccessToken = null;
private bool isBackgroundTaskRunning = false;
private bool isDisposed = false;
private bool isDisposed = false;

internal TokenCredentialCache(
TokenCredential tokenCredential,
Expand Down Expand Up @@ -163,11 +163,13 @@ private async Task<AccessToken> GetNewTokenAsync(

private async ValueTask<AccessToken> RefreshCachedTokenWithRetryHelperAsync(
ITrace trace)
{
{
try
{
Exception? lastException = null;
const int totalRetryCount = 2;
const int totalRetryCount = 2;
TokenRequestContext tokenRequestContext = default;

for (int retry = 0; retry < totalRetryCount; retry++)
{
if (this.cancellationToken.IsCancellationRequested)
Expand All @@ -182,10 +184,10 @@ private async ValueTask<AccessToken> RefreshCachedTokenWithRetryHelperAsync(
name: nameof(this.RefreshCachedTokenWithRetryHelperAsync),
component: TraceComponent.Authorization,
level: Tracing.TraceLevel.Info))
{
{
try
{
TokenRequestContext tokenRequestContext = this.scopeProvider.GetTokenRequestContext();
tokenRequestContext = this.scopeProvider.GetTokenRequestContext();

this.cachedAccessToken = await this.tokenCredential.GetTokenAsync(
requestContext: tokenRequestContext,
Expand Down Expand Up @@ -214,13 +216,13 @@ private async ValueTask<AccessToken> RefreshCachedTokenWithRetryHelperAsync(
return this.cachedAccessToken.Value;
}
catch (RequestFailedException requestFailedException)
{
{
lastException = requestFailedException;
getTokenTrace.AddDatum(
$"RequestFailedException at {DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)}",
requestFailedException.Message);

DefaultTrace.TraceError($"TokenCredential.GetToken() failed with RequestFailedException. scope = {string.Join(";", this.scopeProvider.GetTokenRequestContext().Scopes)}, retry = {retry}, Exception = {lastException.Message}");
DefaultTrace.TraceError($"TokenCredential.GetToken() failed with RequestFailedException. scope = {string.Join(";", tokenRequestContext.Scopes ?? Array.Empty<string>())}, retry = {retry}, Exception = {lastException.Message}");

// Don't retry on auth failures
if (requestFailedException.Status == (int)HttpStatusCode.Unauthorized ||
Expand All @@ -244,7 +246,7 @@ private async ValueTask<AccessToken> RefreshCachedTokenWithRetryHelperAsync(
operationCancelled.Message);

DefaultTrace.TraceError(
$"TokenCredential.GetTokenAsync() failed. scope = {string.Join(";", this.scopeProvider.GetTokenRequestContext().Scopes)}, retry = {retry}, Exception = {lastException.Message}");
$"TokenCredential.GetTokenAsync() failed. scope = {string.Join(";", tokenRequestContext.Scopes ?? Array.Empty<string>())}, retry = {retry}, Exception = {lastException.Message}");

throw CosmosExceptionFactory.CreateRequestTimeoutException(
message: ClientResources.FailedToGetAadToken,
Expand All @@ -263,7 +265,7 @@ private async ValueTask<AccessToken> RefreshCachedTokenWithRetryHelperAsync(
exception.Message);

DefaultTrace.TraceError(
$"TokenCredential.GetTokenAsync() failed. scope = {string.Join(";", this.scopeProvider.GetTokenRequestContext().Scopes)}, retry = {retry}, Exception = {lastException.Message}");
$"TokenCredential.GetTokenAsync() failed. scope = {string.Join(";", tokenRequestContext.Scopes ?? Array.Empty<string>())}, retry = {retry}, Exception = {lastException.Message}");

// Fallback logic
if (this.scopeProvider.TryFallback(exception))
Expand Down