Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overload of GetTokenAsync with an exception out param #196

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
27 changes: 18 additions & 9 deletions CommunityToolkit.Authentication.Msal/MsalProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Graph;
Expand Down Expand Up @@ -106,7 +107,11 @@ optionsMiddleware is AuthenticationHandlerOption options &&
options.AuthenticationProviderOption?.Scopes != null && options.AuthenticationProviderOption.Scopes.Length > 0)
{
var withScopes = options.AuthenticationProviderOption.Scopes;
token = await this.GetTokenWithScopesAsync(withScopes);
token = await this.GetTokenWithScopesAsync(withScopes, out var exception);
if (exception != null)
{
ExceptionDispatchInfo.Capture(exception).Throw();
}
}
else
{
Expand Down Expand Up @@ -170,10 +175,15 @@ public override async Task SignOutAsync()
State = ProviderState.SignedOut;
}

public Task<string> GetTokenAsync(out Exception exception, bool silentOnly = false)
{
return this.GetTokenWithScopesAsync(Scopes, silentOnly, out exception);
}

/// <inheritdoc/>
public override Task<string> GetTokenAsync(bool silentOnly = false)
{
return this.GetTokenWithScopesAsync(Scopes, silentOnly);
return this.GetTokenWithScopesAsync(Scopes, silentOnly, out _);
}

/// <summary>
Expand Down Expand Up @@ -221,7 +231,7 @@ protected IPublicClientApplication CreatePublicClientApplication(string clientId
/// <param name="scopes">An array of scopes to pass along with the Graph request.</param>
/// <param name="silentOnly">A value to determine whether account broker UI should be shown, if required by MSAL.</param>
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
protected async Task<string> GetTokenWithScopesAsync(string[] scopes, bool silentOnly = false)
protected async Task<string> GetTokenWithScopesAsync(string[] scopes, bool silentOnly = false, out Exception exception)
{
await SemaphoreSlim.WaitAsync();

Expand All @@ -239,10 +249,9 @@ protected async Task<string> GetTokenWithScopesAsync(string[] scopes, bool silen
catch (MsalUiRequiredException)
{
}
catch
catch (Exception e)
{
// Unexpected exception
// TODO: Send exception to a logger.
exception = e;
}

if (authResult == null && !silentOnly)
Expand All @@ -269,11 +278,11 @@ protected async Task<string> GetTokenWithScopesAsync(string[] scopes, bool silen
#endif

authResult = await paramBuilder.ExecuteAsync();
exception = null; // if we succeeded in a retry, clear the exception.
}
catch
catch (Exception e)
{
// Unexpected exception
// TODO: Send exception to a logger.
exception = e;
}
}

Expand Down