Skip to content
Closed
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
7 changes: 0 additions & 7 deletions Wilson.sln
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.IdentityModel.Val
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.IdentityModel.Validators.Tests", "test\Microsoft.IdentityModel.Validators.Tests\Microsoft.IdentityModel.Validators.Tests.csproj", "{D17F097F-6024-40BA-A7A0-015BB90F203B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.IdentityModel.SampleTests", "test\Microsoft.IdentityModel.SampleTests\Microsoft.IdentityModel.SampleTests.csproj", "{578FDF8F-6568-448A-AB93-D94269593932}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.IdentityModel.Abstractions", "src\Microsoft.IdentityModel.Abstractions\Microsoft.IdentityModel.Abstractions.csproj", "{8057C69A-3D1E-46A3-86E4-E6B26249DD25}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.IdentityModel.LoggingExtensions", "src\Microsoft.IdentityModel.LoggingExtensions\Microsoft.IdentityModel.LoggingExtensions.csproj", "{C1F5A997-FAA9-45E5-8D28-D4E92D4A034D}"
Expand Down Expand Up @@ -211,10 +209,6 @@ Global
{D17F097F-6024-40BA-A7A0-015BB90F203B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D17F097F-6024-40BA-A7A0-015BB90F203B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D17F097F-6024-40BA-A7A0-015BB90F203B}.Release|Any CPU.Build.0 = Release|Any CPU
{578FDF8F-6568-448A-AB93-D94269593932}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{578FDF8F-6568-448A-AB93-D94269593932}.Debug|Any CPU.Build.0 = Debug|Any CPU
{578FDF8F-6568-448A-AB93-D94269593932}.Release|Any CPU.ActiveCfg = Release|Any CPU
{578FDF8F-6568-448A-AB93-D94269593932}.Release|Any CPU.Build.0 = Release|Any CPU
{8057C69A-3D1E-46A3-86E4-E6B26249DD25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8057C69A-3D1E-46A3-86E4-E6B26249DD25}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8057C69A-3D1E-46A3-86E4-E6B26249DD25}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -269,7 +263,6 @@ Global
{15944563-F7DA-4150-B5F1-6144EBF2CE23} = {8905D2E3-4499-4A86-BF3E-F098F228DD59}
{DA585910-0E6C-45A5-AABD-30917130FD63} = {BD2706C5-6C57-484D-89C8-A0CF5F8E3D19}
{D17F097F-6024-40BA-A7A0-015BB90F203B} = {8905D2E3-4499-4A86-BF3E-F098F228DD59}
{578FDF8F-6568-448A-AB93-D94269593932} = {8905D2E3-4499-4A86-BF3E-F098F228DD59}
{8057C69A-3D1E-46A3-86E4-E6B26249DD25} = {BD2706C5-6C57-484D-89C8-A0CF5F8E3D19}
{C1F5A997-FAA9-45E5-8D28-D4E92D4A034D} = {EB14B99B-2255-45BC-BF14-E488DCD4A4BA}
{EF9A4431-6D2C-4DD1-BF6B-6F2CC619DEE1} = {8905D2E3-4499-4A86-BF3E-F098F228DD59}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal Result<string, ExceptionDetail> DecryptToken(
return new ExceptionDetail(
new MessageDetail(TokenLogMessages.IDX10612),
ValidationFailureType.TokenDecryptionFailed,
ExceptionType.SecurityToken,
typeof(SecurityTokenException),
headerMissingStackFrame);
}

Expand All @@ -72,7 +72,7 @@ internal Result<string, ExceptionDetail> DecryptToken(
TokenLogMessages.IDX10609,
LogHelper.MarkAsSecurityArtifact(jwtToken, JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.TokenDecryptionFailed,
ExceptionType.SecurityTokenDecryptionFailed,
typeof(SecurityTokenDecryptionFailedException),
noKeysTriedStackFrame);
}

Expand Down Expand Up @@ -214,7 +214,7 @@ internal Result<string, ExceptionDetail> DecryptToken(
exceptionStrings?.ToString() ?? "",
LogHelper.MarkAsSecurityArtifact(jwtToken, JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.TokenDecryptionFailed,
ExceptionType.SecurityTokenKeyWrap,
typeof(SecurityTokenKeyWrapException),
decryptionKeyUnwrapFailedStackFrame,
null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal static Result<SecurityToken, ExceptionDetail> ReadToken(
return new ExceptionDetail(
new MessageDetail(LogMessages.IDX14107),
ValidationFailureType.TokenReadingFailed,
ExceptionType.SecurityTokenMalformed,
typeof(SecurityTokenMalformedException),
malformedTokenStackFrame,
ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal static Result<SecurityKey, ExceptionDetail> ValidateSignature(
jwtToken.EncodedToken,
JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.SignatureValidationFailed,
ExceptionType.SecurityTokenInvalidSignature,
typeof(SecurityTokenInvalidSignatureException),
new StackFrame(true));

SecurityKey? key = null;
Expand Down Expand Up @@ -100,7 +100,7 @@ internal static Result<SecurityKey, ExceptionDetail> ValidateSignature(
return new ExceptionDetail(
new MessageDetail(TokenLogMessages.IDX10500),
ValidationFailureType.SignatureValidationFailed,
ExceptionType.SecurityTokenSignatureKeyNotFound,
typeof(SecurityTokenSignatureKeyNotFoundException),
stackFrame);
}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ private static Result<SecurityKey, ExceptionDetail> ValidateSignatureUsingAllKey
return new ExceptionDetail(
new MessageDetail(TokenLogMessages.IDX10500),
ValidationFailureType.SignatureValidationFailed,
ExceptionType.SecurityTokenSignatureKeyNotFound,
typeof(SecurityTokenSignatureKeyNotFoundException),
new StackFrame(true));

StringBuilder exceptionStrings = new();
Expand Down Expand Up @@ -219,7 +219,7 @@ private static Result<SecurityKey, ExceptionDetail> ValidateSignatureWithKey(
LogHelper.MarkAsNonPII(jsonWebToken.Alg),
key),
ValidationFailureType.SignatureValidationFailed,
ExceptionType.SecurityTokenInvalidAlgorithm,
typeof(SecurityTokenInvalidAlgorithmException),
new StackFrame(true));
}

Expand All @@ -243,7 +243,7 @@ private static Result<SecurityKey, ExceptionDetail> ValidateSignatureWithKey(
key?.ToString() ?? "Null",
LogHelper.MarkAsNonPII(jsonWebToken.Alg)),
ValidationFailureType.SignatureValidationFailed,
ExceptionType.InvalidOperation,
typeof(InvalidOperationException),
new StackFrame(true));

bool valid = EncodingUtils.PerformEncodingDependentOperation<bool, string, int, SignatureProvider>(
Expand All @@ -266,7 +266,7 @@ private static Result<SecurityKey, ExceptionDetail> ValidateSignatureWithKey(
jsonWebToken.EncodedToken,
JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.SignatureValidationFailed,
ExceptionType.SecurityTokenInvalidSignature,
typeof(SecurityTokenInvalidSignatureException),
new StackFrame(true));
}
#pragma warning disable CA1031 // Do not catch general exception types
Expand All @@ -280,7 +280,7 @@ private static Result<SecurityKey, ExceptionDetail> ValidateSignatureWithKey(
jsonWebToken.EncodedToken,
JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.SignatureValidationFailed,
ExceptionType.SecurityTokenInvalidSignature,
typeof(SecurityTokenInvalidSignatureException),
new StackFrame(true),
ex);
}
Expand Down Expand Up @@ -321,7 +321,7 @@ private static ExceptionDetail GetSignatureValidationError(
exceptionStrings.ToString(),
LogHelper.MarkAsSecurityArtifact(jwtToken.EncodedToken, JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.SignatureValidationFailed,
ExceptionType.SecurityTokenSignatureKeyNotFound,
typeof(SecurityTokenSignatureKeyNotFoundException),
new StackFrame(true));
}

Expand All @@ -336,7 +336,7 @@ private static ExceptionDetail GetSignatureValidationError(
exceptionStrings.ToString(),
LogHelper.MarkAsSecurityArtifact(jwtToken.EncodedToken, JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.SignatureValidationFailed,
ExceptionType.SecurityTokenSignatureKeyNotFound,
typeof(SecurityTokenSignatureKeyNotFoundException),
new StackFrame(true));

return new ExceptionDetail(
Expand All @@ -348,7 +348,7 @@ private static ExceptionDetail GetSignatureValidationError(
exceptionStrings.ToString(),
LogHelper.MarkAsSecurityArtifact(jwtToken.EncodedToken, JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.SignatureValidationFailed,
ExceptionType.SecurityTokenSignatureKeyNotFound,
typeof(SecurityTokenSignatureKeyNotFoundException),
new StackFrame(true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal async Task<Result<ValidationResult, ExceptionDetail>> ValidateTokenAsyn
LogHelper.MarkAsNonPII(token.Length),
LogHelper.MarkAsNonPII(MaximumTokenSizeInBytes)),
ValidationFailureType.InvalidSecurityToken,
ExceptionType.InvalidArgument,
typeof(ArgumentException),
invalidTokenLengthStackFrame);
}

Expand Down Expand Up @@ -120,7 +120,7 @@ internal async Task<Result<ValidationResult, ExceptionDetail>> ValidateTokenAsyn
return new ExceptionDetail(
new MessageDetail(TokenLogMessages.IDX10001, nameof(token), nameof(JsonWebToken)),
ValidationFailureType.InvalidSecurityToken,
ExceptionType.InvalidArgument,
typeof(ArgumentException),
notJwtStackFrame);
}

Expand Down Expand Up @@ -150,7 +150,7 @@ await ValidateJWEAsync(jsonWebToken, validationParameters, currentConfiguration,
return result;
}

if (TokenUtilities.IsRecoverableErrorType(result.UnwrapError().Type))
if (TokenUtilities.IsRecoverableException(result.UnwrapError().GetException()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we allocating the exception when we could simply check the type and save the allocation?

{
// If we were still unable to validate, attempt to refresh the configuration and validate using it
// but ONLY if the currentConfiguration is not null. We want to avoid refreshing the configuration on
Expand Down Expand Up @@ -182,13 +182,13 @@ await ValidateJWEAsync(jsonWebToken, validationParameters, currentConfiguration,
{
validationParameters.RefreshBeforeValidation = false;
validationParameters.ValidateWithLKG = true;
ExceptionType recoverableExceptionType = result.UnwrapError().Type;
Exception recoverableExceptionType = result.UnwrapError().GetException();
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question around allocating the exception.


BaseConfiguration[] validConfigurations = validationParameters.ConfigurationManager.GetValidLkgConfigurations();
for (int i = 0; i < validConfigurations.Length; i++)
{
BaseConfiguration lkgConfiguration = validConfigurations[i];
if (TokenUtilities.IsRecoverableConfigurationAndExceptionType(
if (TokenUtilities.IsRecoverableConfiguration(
jsonWebToken.Kid, currentConfiguration, lkgConfiguration, recoverableExceptionType))
{
result = jsonWebToken.IsEncrypted ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ internal static Result<string, ExceptionDetail> DecryptJwtToken(
return new ExceptionDetail(
new MessageDetail(TokenLogMessages.IDX10679, zipAlgorithm),
ValidationFailureType.TokenDecryptionFailed,
ExceptionType.SecurityTokenDecompressionFailed,
typeof(SecurityTokenDecryptionFailedException),
new StackFrame(true),
ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ private static ExceptionDetail GetDecryptionError(
exceptionStrings?.ToString() ?? string.Empty,
LogHelper.MarkAsSecurityArtifact(decryptionParameters.EncodedToken, SafeLogJwtToken)),
ValidationFailureType.TokenDecryptionFailed,
ExceptionType.SecurityTokenDecryptionFailed,
typeof(SecurityTokenDecryptionFailedException),
new StackFrame(true),
null);
else if (algorithmNotSupportedByCryptoProvider)
Expand All @@ -382,15 +382,15 @@ private static ExceptionDetail GetDecryptionError(
LogHelper.MarkAsNonPII(decryptionParameters.Alg),
LogHelper.MarkAsNonPII(decryptionParameters.Enc)),
ValidationFailureType.TokenDecryptionFailed,
ExceptionType.SecurityTokenDecryptionFailed,
typeof(SecurityTokenDecryptionFailedException),
new StackFrame(true));
else
return new ExceptionDetail(
new MessageDetail(
TokenLogMessages.IDX10609,
LogHelper.MarkAsSecurityArtifact(decryptionParameters.EncodedToken, SafeLogJwtToken)),
ValidationFailureType.TokenDecryptionFailed,
ExceptionType.SecurityTokenDecryptionFailed,
typeof(SecurityTokenDecryptionFailedException),
new StackFrame(true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ public SecurityTokenException(string message, Exception innerException)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="SecurityTokenException"/> class with a specified error message
/// and a reference to the inner exception that is the cause of this exception.
/// </summary>
/// <param name="exceptionDetail">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The <see cref="Exception"/> that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
internal SecurityTokenException(ExceptionDetail exceptionDetail, Exception innerException)
: base(string.Empty, innerException)
{
ExceptionDetail = exceptionDetail;
}

/// <summary>
/// Initializes a new instance of the <see cref="SecurityTokenException"/> class.
/// </summary>
Expand Down Expand Up @@ -95,6 +107,18 @@ public override string StackTrace
}
}

/// <inheritdoc/>
public override string Message
{
get
{
if (ExceptionDetail == null)
return base.Message;

return ExceptionDetail.MessageDetail.Message;
}
}

/// <summary>
/// Gets or sets the source of the exception.
/// </summary>
Expand All @@ -106,7 +130,7 @@ public override string Source

internal ExceptionDetail ExceptionDetail
{
get; set;
get;
}

#if NET472 || NETSTANDARD2_0 || NET6_0_OR_GREATER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public SecurityTokenInvalidAlgorithmException(string message, Exception innerExc
{
}

/// <summary>
/// Initializes a new instance of the <see cref="SecurityTokenInvalidAlgorithmException"/> class.
/// </summary>
/// <param name="exceptionDetail">Additional information to be included in the exception and displayed to user.</param>
/// <param name="innerException">A <see cref="Exception"/> that represents the root cause of the exception.</param>
internal SecurityTokenInvalidAlgorithmException(ExceptionDetail exceptionDetail, Exception innerException)
: base(exceptionDetail, innerException)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="SecurityTokenInvalidTypeException"/> class.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ public SecurityTokenValidationException(string message, Exception innerException
{
}

/// <summary>
/// Initializes a new instance of the <see cref="SecurityTokenValidationException"/> class with a specified error message
/// and a reference to the inner exception that is the cause of this exception.
/// </summary>
/// <param name="exceptionDetail">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The <see cref="Exception"/> that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
internal SecurityTokenValidationException(ExceptionDetail exceptionDetail, Exception innerException)
: base(exceptionDetail, innerException)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="SecurityTokenValidationException"/> class.
/// </summary>
Expand Down
Loading