Skip to content

Commit

Permalink
Fall back to local cache if broker fails to return result for silent …
Browse files Browse the repository at this point in the history
…token call (#4395)

* Update SilentRequest.cs

* Fix failing unit tests
  • Loading branch information
ashok672 authored Nov 2, 2023
1 parent 4b1c58a commit 4b0b6cd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,12 @@ protected override async Task<AuthenticationResult> ExecuteAsync(CancellationTok
_logger.Info("Broker is configured and enabled, attempting to use broker instead.");
var brokerResult = await _brokerStrategyLazy.Value.ExecuteAsync(cancellationToken).ConfigureAwait(false);

// fallback to local cache if broker fails
if (brokerResult != null)
{
_logger.Verbose(() => "Broker responded to silent request.");
return brokerResult;
}
else
{
_logger.Verbose(() => "Broker could not satisfy the silent request.");
throw new MsalUiRequiredException(
MsalError.FailedToAcquireTokenSilentlyFromBroker,
"Broker could not satisfy the silent request.",
null,
UiRequiredExceptionClassification.AcquireTokenSilentFailed);
}
}

_logger.Verbose(() => "Attempting to acquire token using local cache.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ public async Task NullBroker_AcquireSilentInteractive_Async()
}
catch (MsalUiRequiredException e)
{
Assert.AreEqual("failed_to_acquire_token_silently_from_broker", e.ErrorCode);
Assert.AreEqual("no_tokens_found", e.ErrorCode);

harness.HttpManager.AddInstanceDiscoveryMockHandler();
harness.HttpManager.AddSuccessTokenResponseMockHandlerForPost();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,15 @@ public async Task BrokerSilentRequestLocalCacheTestAsync(bool brokerConfiguredBy

var request = new SilentRequest(harness.ServiceBundle, parameters, silentParameters);

if (brokerConfiguredByUser) //When broker is enabled, local cache should not be used
{
var exception = await AssertException.TaskThrowsAsync<MsalUiRequiredException>(() =>
request.RunAsync(default)
,false
).ConfigureAwait(false);

Assert.IsNotNull(exception);
Assert.AreEqual(MsalError.FailedToAcquireTokenSilentlyFromBroker, exception.ErrorCode);
Assert.AreEqual("Broker could not satisfy the silent request.", exception.Message);
}
else
{
var result = await request.RunAsync(default).ConfigureAwait(false);
var result = await request.RunAsync(default).ConfigureAwait(false);

Assert.IsNotNull(result);
Assert.IsNotNull(result.AccessToken);
Assert.AreEqual(TestConstants.s_scope.AsSingleString(), result.Scopes.AsSingleString());
Assert.AreEqual(brokerID, result.Account.Username);
// even if broker is configured by user but not installed, the result will be from the local cache
Assert.IsNotNull(result);
Assert.IsNotNull(result.AccessToken);
Assert.AreEqual(TestConstants.s_scope.AsSingleString(), result.Scopes.AsSingleString());
Assert.AreEqual(brokerID, result.Account.Username);
if (!brokerConfiguredByUser)
{
await mockBroker.DidNotReceiveWithAnyArgs().AcquireTokenSilentAsync(null, null).ConfigureAwait(false);
}
}
Expand Down

0 comments on commit 4b0b6cd

Please sign in to comment.