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
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,4 @@
<PackageReference Include="System.Text.Json" Version="$(SystemTextJson)" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\Common\TrimmingAttributes.cs" LinkBase="Common" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

#nullable enable
namespace Microsoft.IdentityModel.Tokens
Expand All @@ -18,15 +17,15 @@ internal record struct AdditionalInformation(

public LifetimeValidationError(
MessageDetail messageDetail,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type exceptionType,
Type exceptionType,
StackFrame stackFrame)
: base(messageDetail, ValidationFailureType.LifetimeValidationFailed, exceptionType, stackFrame)
{
}

public LifetimeValidationError(
MessageDetail messageDetail,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type exceptionType,
Type exceptionType,
StackFrame stackFrame,
AdditionalInformation? additionalInformation)
: base(messageDetail, ValidationFailureType.LifetimeValidationFailed, exceptionType, stackFrame)
Expand All @@ -37,7 +36,7 @@ public LifetimeValidationError(

public LifetimeValidationError(
MessageDetail messageDetail,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type exceptionType,
Type exceptionType,
StackFrame stackFrame,
Exception innerException,
AdditionalInformation? additionalInformation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.IdentityModel.Tokens
{
Expand All @@ -13,7 +12,6 @@ namespace Microsoft.IdentityModel.Tokens
/// </summary>
internal class ValidationError
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
private Type _exceptionType;

/// <summary>
Expand All @@ -26,7 +24,7 @@ internal class ValidationError
public ValidationError(
MessageDetail MessageDetail,
ValidationFailureType failureType,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type exceptionType,
Type exceptionType,
StackFrame stackFrame)
: this(MessageDetail, failureType, exceptionType, stackFrame, innerException: null)
{
Expand All @@ -43,7 +41,7 @@ public ValidationError(
public ValidationError(
MessageDetail messageDetail,
ValidationFailureType failureType,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type exceptionType,
Type exceptionType,
StackFrame stackFrame,
Exception innerException)
{
Expand All @@ -60,7 +58,7 @@ public ValidationError(
public ValidationError(
MessageDetail messageDetail,
ValidationFailureType failureType,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type exceptionType,
Type exceptionType,
StackFrame stackFrame,
ValidationError innerValidationError)
{
Expand All @@ -77,18 +75,10 @@ public ValidationError(
/// <summary>
/// Creates an instance of an <see cref="Exception"/> using <see cref="ValidationError"/>
/// </summary>
/// <returns>An instantance of an Exception.</returns>
/// <returns>An instance of an Exception.</returns>
public Exception GetException()
{
Exception exception;
if (InnerException == null && InnerValidationError == null)
exception = Activator.CreateInstance(_exceptionType, MessageDetail.Message) as Exception;
else
exception = Activator.CreateInstance(
_exceptionType,
MessageDetail.Message,
InnerException ?? InnerValidationError.GetException()) as Exception;

Exception exception = GetException(ExceptionType, InnerException);
if (exception is SecurityTokenException securityTokenException)
securityTokenException.ValidationError = this;

Expand All @@ -97,6 +87,106 @@ public Exception GetException()
return exception;
}

private Exception GetException(Type exceptionType, Exception innerException)
{
Exception exception = null;

if (innerException == null && InnerValidationError == null)
{
if (exceptionType == typeof(SecurityTokenInvalidAudienceException))
exception = new SecurityTokenInvalidAudienceException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenInvalidIssuerException))
exception = new SecurityTokenInvalidIssuerException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenInvalidLifetimeException))
exception = new SecurityTokenInvalidLifetimeException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenReplayDetectedException))
exception = new SecurityTokenReplayDetectedException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenReplayAddFailedException))
exception = new SecurityTokenReplayAddFailedException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenInvalidSigningKeyException))
exception = new SecurityTokenInvalidSigningKeyException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenInvalidTypeException))
exception = new SecurityTokenInvalidTypeException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenReplayDetectedException))
exception = new SecurityTokenReplayDetectedException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenExpiredException))
exception = new SecurityTokenExpiredException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenNotYetValidException))
exception = new SecurityTokenNotYetValidException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenInvalidLifetimeException))
exception = new SecurityTokenInvalidLifetimeException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenNoExpirationException))
exception = new SecurityTokenNoExpirationException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenInvalidIssuerException))
exception = new SecurityTokenInvalidIssuerException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenSignatureKeyNotFoundException))
exception = new SecurityTokenSignatureKeyNotFoundException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenDecryptionFailedException))
exception = new SecurityTokenDecryptionFailedException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenMalformedException))
exception = new SecurityTokenMalformedException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenInvalidSignatureException))
exception = new SecurityTokenInvalidSignatureException(MessageDetail.Message);
else if (exceptionType == typeof(ArgumentNullException))
exception = new ArgumentNullException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenInvalidAlgorithmException))
exception = new SecurityTokenInvalidAlgorithmException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenInvalidAlgorithmException))
exception = new SecurityTokenInvalidAlgorithmException(MessageDetail.Message);
else if (exceptionType == typeof(SecurityTokenException))
exception = new SecurityTokenException(MessageDetail.Message);
}
else
{
Exception actualException = innerException ?? InnerValidationError.GetException();

if (exceptionType == typeof(SecurityTokenInvalidAudienceException))
exception = new SecurityTokenInvalidAudienceException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenInvalidIssuerException))
exception = new SecurityTokenInvalidIssuerException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenInvalidLifetimeException))
exception = new SecurityTokenInvalidLifetimeException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenReplayDetectedException))
exception = new SecurityTokenReplayDetectedException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenReplayAddFailedException))
exception = new SecurityTokenReplayAddFailedException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenInvalidSigningKeyException))
exception = new SecurityTokenInvalidSigningKeyException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenInvalidTypeException))
exception = new SecurityTokenInvalidTypeException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenReplayDetectedException))
exception = new SecurityTokenReplayDetectedException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenExpiredException))
exception = new SecurityTokenExpiredException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenNotYetValidException))
exception = new SecurityTokenNotYetValidException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenInvalidLifetimeException))
exception = new SecurityTokenInvalidLifetimeException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenNoExpirationException))
exception = new SecurityTokenNoExpirationException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenInvalidIssuerException))
exception = new SecurityTokenInvalidIssuerException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenSignatureKeyNotFoundException))
exception = new SecurityTokenSignatureKeyNotFoundException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenDecryptionFailedException))
exception = new SecurityTokenDecryptionFailedException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenMalformedException))
exception = new SecurityTokenMalformedException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenInvalidSignatureException))
exception = new SecurityTokenInvalidSignatureException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(ArgumentNullException))
exception = new ArgumentNullException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenInvalidAlgorithmException))
exception = new SecurityTokenInvalidAlgorithmException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenInvalidAlgorithmException))
exception = new SecurityTokenInvalidAlgorithmException(MessageDetail.Message, actualException);
else if (exceptionType == typeof(SecurityTokenException))
exception = new SecurityTokenException(MessageDetail.Message, actualException);
}

return exception;
}

protected virtual void AddAdditionalInformation(Exception exception)
{
// base implementation is no-op. Derived classes can override to add additional information to the exception.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task ValidateTokenAsync(JsonWebTokenHandlerValidationParametersTheo
await jsonWebTokenHandler.ValidateTokenAsync(jwtString, theoryData.TokenValidationParameters);
ValidationResult<ValidatedToken> validationParametersResult =
await jsonWebTokenHandler.ValidateTokenAsync(
jwtString, theoryData.ValidationParameters, new CallContext(), CancellationToken.None);
jwtString, theoryData.ValidationParameters, theoryData.CallContext, CancellationToken.None);

if (tokenValidationParametersResult.IsValid != theoryData.ExpectedIsValid)
context.AddDiff($"tokenValidationParametersResult.IsValid != theoryData.ExpectedIsValid");
Expand Down
Loading