diff --git a/src/Microsoft.IdentityModel.Tokens/TokenUtilities.cs b/src/Microsoft.IdentityModel.Tokens/TokenUtilities.cs index e2ff597b41..fe7765fce7 100644 --- a/src/Microsoft.IdentityModel.Tokens/TokenUtilities.cs +++ b/src/Microsoft.IdentityModel.Tokens/TokenUtilities.cs @@ -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; @@ -243,6 +243,17 @@ internal static IEnumerable MergeClaims(IEnumerable claims, IEnume return result; } + /// + /// Should not be used. Exists only for backwards compatibility. + /// Check whether the given exception type is recoverable by LKG. + /// + /// The exception to check. + /// true if the exception is certain types of exceptions otherwise, false. + internal static bool IsRecoverableException(Exception exception) + { + return IsRecoverableException(exception, false); + } + /// /// Check whether the given exception type is recoverable by LKG. /// Decryption error is only recoverable, if the configuration has decryption keys in it. @@ -291,6 +302,17 @@ internal static bool IsRecoverableConfiguration(string kid, BaseConfiguration cu return false; } + /// + /// Should not be used. Exists only for backwards compatibility. + /// Check whether the given exception type is recoverable by LKG. + /// + /// The exception type to check + /// true if the exception is certain types of exceptions otherwise, false. + internal static bool IsRecoverableExceptionType(Type exceptionType) + { + return IsRecoverableExceptionType(exceptionType, false); + } + /// /// Check whether the given exception type is recoverable by LKG. /// diff --git a/test/Microsoft.IdentityModel.Tokens.Tests/TokenUtilitiesTests.cs b/test/Microsoft.IdentityModel.Tokens.Tests/TokenUtilitiesTests.cs new file mode 100644 index 0000000000..fe6fd57a19 --- /dev/null +++ b/test/Microsoft.IdentityModel.Tokens.Tests/TokenUtilitiesTests.cs @@ -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); + } + } +}