Skip to content
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
24 changes: 23 additions & 1 deletion src/Microsoft.IdentityModel.Tokens/TokenUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
Expand Down Expand Up @@ -243,6 +243,17 @@ internal static IEnumerable<Claim> MergeClaims(IEnumerable<Claim> claims, IEnume
return result;
}

/// <summary>
/// Should not be used. Exists only for backwards compatibility.
/// Check whether the given exception type is recoverable by LKG.
/// </summary>
/// <param name="exception">The exception to check.</param>
/// <returns><c>true</c> if the exception is certain types of exceptions otherwise, <c>false</c>.</returns>
internal static bool IsRecoverableException(Exception exception)
{
return IsRecoverableException(exception, false);
}

/// <summary>
/// Check whether the given exception type is recoverable by LKG.
/// Decryption error is only recoverable, if the configuration has decryption keys in it.
Expand Down Expand Up @@ -291,6 +302,17 @@ internal static bool IsRecoverableConfiguration(string kid, BaseConfiguration cu
return false;
}

/// <summary>
/// Should not be used. Exists only for backwards compatibility.
/// Check whether the given exception type is recoverable by LKG.
/// </summary>
/// <param name="exceptionType">The exception type to check</param>
/// <returns><c>true</c> if the exception is certain types of exceptions otherwise, <c>false</c>.</returns>
internal static bool IsRecoverableExceptionType(Type exceptionType)
{
return IsRecoverableExceptionType(exceptionType, false);
}

/// <summary>
/// Check whether the given exception type is recoverable by LKG.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Xunit;

namespace Microsoft.IdentityModel.Tokens.Tests
{
public class TokenUtilitiesTests
{
[Theory]
[InlineData(typeof(SecurityTokenInvalidSignatureException), false, true)]
[InlineData(typeof(SecurityTokenInvalidIssuerException), false, true)]
[InlineData(typeof(SecurityTokenSignatureKeyNotFoundException), false, true)]
[InlineData(typeof(SecurityTokenDecryptionFailedException), false, false)]
[InlineData(typeof(SecurityTokenDecryptionFailedException), true, true)]
[InlineData(typeof(ArgumentNullException), false, false)]
[InlineData(typeof(ArgumentException), false, false)]
[InlineData(typeof(SecurityTokenValidationException), false, false)]
public void IsRecoverableException_ReturnsExpectedResult(Type exceptionType, bool configContainsDecryptionKeys, bool expected)
{
Exception exception = (Exception)Activator.CreateInstance(exceptionType);

bool result = TokenUtilities.IsRecoverableException(exception, configContainsDecryptionKeys);

Assert.Equal(expected, result);
}

[Fact]
public void IsRecoverableException_WithoutDecryptionKeysParameter_ReturnsFalse()
{
var exception = new SecurityTokenDecryptionFailedException();

bool result = TokenUtilities.IsRecoverableException(exception);

Assert.False(result);
}

[Fact]
public void IsRecoverableException_WithNullException_ReturnsFalse()
{
bool result = TokenUtilities.IsRecoverableException(null);

Assert.False(result);
}

[Theory]
[InlineData(typeof(SecurityTokenInvalidSignatureException), false, true)]
[InlineData(typeof(SecurityTokenInvalidIssuerException), false, true)]
[InlineData(typeof(SecurityTokenSignatureKeyNotFoundException), false, true)]
[InlineData(typeof(SecurityTokenDecryptionFailedException), false, false)]
[InlineData(typeof(SecurityTokenDecryptionFailedException), true, true)]
[InlineData(typeof(ArgumentNullException), false, false)]
[InlineData(typeof(ArgumentException), false, false)]
[InlineData(typeof(SecurityTokenValidationException), false, false)]
[InlineData(null, false, false)]
public void IsRecoverableExceptionType_ReturnsExpectedResult(Type exceptionType, bool configContainsDecryptionKeys, bool expected)
{
bool result = TokenUtilities.IsRecoverableExceptionType(exceptionType, configContainsDecryptionKeys);

Assert.Equal(expected, result);
}

[Fact]
public void IsRecoverableExceptionType_WithoutDecryptionKeysParameter_ReturnsFalse()
{
var exceptionType = typeof(SecurityTokenDecryptionFailedException);

bool result = TokenUtilities.IsRecoverableExceptionType(exceptionType);

Assert.False(result);
}
}
}
Loading