Skip to content

Commit

Permalink
Fix for #398 (#399)
Browse files Browse the repository at this point in the history
* Fix for #398

* Adding unit tests
  • Loading branch information
jmprieur authored Aug 4, 2020
1 parent 9fefbb3 commit 26e69d3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
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);
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);
}
}
}

0 comments on commit 26e69d3

Please sign in to comment.