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

Fix for https://github.com/AzureAD/microsoft-identity-web/issues/398 #399

Merged
merged 2 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
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: 23 additions & 4 deletions src/Microsoft.Identity.Web/AuthorizeForScopesAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ public override void OnException(ExceptionContext context)
{
if (context != null)
{
MsalUiRequiredException? msalUiRequiredException =
(context.Exception as MsalUiRequiredException)
?? (context.Exception?.InnerException as MsalUiRequiredException);

MsalUiRequiredException? msalUiRequiredException = FindMsalUiRequiredExceptionIfAny(context.Exception);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Customers rethrow exceptions and pass Microsoft.Identity.Web exceptions in as inner exceptions.
In 0.2.1-preview, we've added the MicrosoftIdentityWebChallengeUserException, but we overlooked the scenario where it would be nested (for instance by the Graph SDK)

if (msalUiRequiredException != null &&
IncrementalConsentAndConditionalAccessHelper.CanBeSolvedByReSignInOfUser(msalUiRequiredException))
{
Expand Down Expand Up @@ -107,5 +104,27 @@ public override void OnException(ExceptionContext context)

base.OnException(context);
}

/// <summary>
/// Finds an MsalUiRequiredException in one of the inner exceptions.
/// </summary>
/// <param name="exception">Exception from which we look for an MsalUiRequiredException.</param>
/// <returns>The MsalUiRequiredException if there is one, null, otherwise.</returns>
internal /* for testing */ static MsalUiRequiredException? FindMsalUiRequiredExceptionIfAny(Exception exception)
{
MsalUiRequiredException? msalUiRequiredException = exception as MsalUiRequiredException;
if (msalUiRequiredException != null)
{
return msalUiRequiredException;
}
else if (exception.InnerException != null)
{
return FindMsalUiRequiredExceptionIfAny(exception.InnerException);
}
else
{
return null;
}
}
}
}
17 changes: 12 additions & 5 deletions src/Microsoft.Identity.Web/Microsoft.Identity.Web.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions tests/Microsoft.Identity.Web.Test/ExcecptionHandlingTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Linq;
using System.Security.Claims;
using Microsoft.Identity.Client;
using NSubstitute;
using Xunit;

namespace Microsoft.Identity.Web.Test
{
public class ExcecptionHandlingTest
{
[Fact]
public void AuthorizeForScopesAttribute_FindMsalUiRequiredExceptionIfAny_Tests()
{
MsalUiRequiredException msalUiRequiredException = new MsalUiRequiredException("code", "message");

MsalUiRequiredException result = AuthorizeForScopesAttribute.FindMsalUiRequiredExceptionIfAny(msalUiRequiredException);
Assert.Equal(result, msalUiRequiredException);

Exception ex = new Exception("message", msalUiRequiredException);
result = AuthorizeForScopesAttribute.FindMsalUiRequiredExceptionIfAny(ex);
Assert.Equal(result, msalUiRequiredException);

Exception ex2 = new Exception("message", ex);
result = AuthorizeForScopesAttribute.FindMsalUiRequiredExceptionIfAny(ex2);
Assert.Equal(result, msalUiRequiredException);
}
}
}