Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensibility Testing: Refactor #3011

Merged
merged 13 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -50,9 +50,9 @@ internal ValidationResult<string> DecryptToken(
StackFrame headerMissingStackFrame = StackFrames.DecryptionHeaderMissing ??= new StackFrame(true);
return new ValidationError(
new MessageDetail(TokenLogMessages.IDX10612),
ValidationFailureType.TokenDecryptionFailed,
typeof(SecurityTokenException),
headerMissingStackFrame);
headerMissingStackFrame,
ValidationFailureType.TokenDecryptionFailed);
}

(IList<SecurityKey>? contentEncryptionKeys, ValidationError? validationError) result =
Expand All @@ -71,9 +71,9 @@ internal ValidationResult<string> DecryptToken(
new MessageDetail(
TokenLogMessages.IDX10609,
LogHelper.MarkAsSecurityArtifact(jwtToken, JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.TokenDecryptionFailed,
typeof(SecurityTokenDecryptionFailedException),
noKeysTriedStackFrame);
noKeysTriedStackFrame,
ValidationFailureType.TokenDecryptionFailed);
}

return JwtTokenUtilities.DecryptJwtToken(
Expand Down Expand Up @@ -218,9 +218,9 @@ internal ValidationResult<string> DecryptToken(
keysAttempted?.ToString() ?? "",
exceptionStrings?.ToString() ?? "",
LogHelper.MarkAsSecurityArtifact(jwtToken, JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.TokenDecryptionFailed,
typeof(SecurityTokenKeyWrapException),
decryptionKeyUnwrapFailedStackFrame);
decryptionKeyUnwrapFailedStackFrame,
ValidationFailureType.TokenDecryptionFailed);

return (null, validationError);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ internal static ValidationResult<SecurityToken> ReadToken(
StackFrame malformedTokenStackFrame = StackFrames.ReadTokenMalformed ?? new StackFrame(true);
return new ValidationError(
new MessageDetail(LogMessages.IDX14107),
ValidationFailureType.TokenReadingFailed,
typeof(SecurityTokenMalformedException),
malformedTokenStackFrame,
ValidationFailureType.TokenReadingFailed,
ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ internal static ValidationResult<SecurityKey> ValidateSignature(
LogHelper.MarkAsSecurityArtifact(
jwtToken.EncodedToken,
JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.SignatureValidationFailed,
typeof(SecurityTokenInvalidSignatureException),
new StackFrame(true));
new StackFrame(true),
ValidationFailureType.SignatureValidationFailed);

SecurityKey? key = null;
if (validationParameters.IssuerSigningKeyResolver is not null)
Expand Down Expand Up @@ -101,17 +101,17 @@ internal static ValidationResult<SecurityKey> ValidateSignature(
LogHelper.MarkAsNonPII(validationParameters.IssuerSigningKeys.Count),
LogHelper.MarkAsNonPII(configuration?.SigningKeys.Count ?? 0),
LogHelper.MarkAsSecurityArtifact(jwtToken.EncodedToken, JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.SignatureValidationFailed,
typeof(SecurityTokenSignatureKeyNotFoundException),
kidNotMatchedNoTryAllStackFrame);
kidNotMatchedNoTryAllStackFrame,
ValidationFailureType.SignatureValidationFailed);
}

StackFrame noKeysProvidedStackFrame = StackFrames.NoKeysProvided ??= new StackFrame(true);
return new ValidationError(
new MessageDetail(TokenLogMessages.IDX10500),
ValidationFailureType.SignatureValidationFailed,
typeof(SecurityTokenSignatureKeyNotFoundException),
noKeysProvidedStackFrame);
noKeysProvidedStackFrame,
ValidationFailureType.SignatureValidationFailed);
}
}

Expand Down Expand Up @@ -146,9 +146,9 @@ private static ValidationResult<SecurityKey> ValidateSignatureUsingAllKeys(
if (vpFailedResult is null && configFailedResult is null) // No keys were attempted
return new ValidationError(
new MessageDetail(TokenLogMessages.IDX10500),
ValidationFailureType.SignatureValidationFailed,
typeof(SecurityTokenSignatureKeyNotFoundException),
new StackFrame(true));
new StackFrame(true),
ValidationFailureType.SignatureValidationFailed);

StringBuilder exceptionStrings = new();
StringBuilder keysAttempted = new();
Expand Down Expand Up @@ -228,9 +228,9 @@ private static ValidationResult<SecurityKey> ValidateSignatureWithKey(
TokenLogMessages.IDX10400,
LogHelper.MarkAsNonPII(jsonWebToken.Alg),
key),
ValidationFailureType.SignatureValidationFailed,
typeof(SecurityTokenInvalidAlgorithmException),
new StackFrame(true));
new StackFrame(true),
ValidationFailureType.SignatureValidationFailed);
}

ValidationResult<string> result = validationParameters.AlgorithmValidator(
Expand Down Expand Up @@ -259,9 +259,9 @@ private static ValidationResult<SecurityKey> ValidateSignatureWithKey(
new MessageDetail(
TokenLogMessages.IDX10518,
result.UnwrapError().MessageDetail.Message),
ValidationFailureType.SignatureAlgorithmValidationFailed,
typeof(SecurityTokenInvalidAlgorithmException),
new StackFrame(true));
new StackFrame(true),
ValidationFailureType.SignatureAlgorithmValidationFailed);
}
}

Expand All @@ -274,9 +274,9 @@ private static ValidationResult<SecurityKey> ValidateSignatureWithKey(
TokenLogMessages.IDX10636,
key?.ToString() ?? "Null",
LogHelper.MarkAsNonPII(jsonWebToken.Alg)),
ValidationFailureType.SignatureValidationFailed,
typeof(InvalidOperationException),
new StackFrame(true));
new StackFrame(true),
ValidationFailureType.SignatureValidationFailed);

bool valid = EncodingUtils.PerformEncodingDependentOperation<bool, string, int, SignatureProvider>(
jsonWebToken.EncodedToken,
Expand All @@ -297,9 +297,9 @@ private static ValidationResult<SecurityKey> ValidateSignatureWithKey(
LogHelper.MarkAsSecurityArtifact(
jsonWebToken.EncodedToken,
JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.SignatureValidationFailed,
typeof(SecurityTokenInvalidSignatureException),
new StackFrame(true));
new StackFrame(true),
ValidationFailureType.SignatureValidationFailed);
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
Expand All @@ -311,9 +311,9 @@ private static ValidationResult<SecurityKey> ValidateSignatureWithKey(
LogHelper.MarkAsSecurityArtifact(
jsonWebToken.EncodedToken,
JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.SignatureValidationFailed,
typeof(SecurityTokenInvalidSignatureException),
new StackFrame(true),
ValidationFailureType.SignatureValidationFailed,
ex);
}
finally
Expand Down Expand Up @@ -352,9 +352,9 @@ private static ValidationError GetSignatureValidationError(
LogHelper.MarkAsNonPII(jwtToken.Kid),
exceptionStrings.ToString(),
LogHelper.MarkAsSecurityArtifact(jwtToken.EncodedToken, JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.SignatureValidationFailed,
typeof(SecurityTokenSignatureKeyNotFoundException),
new StackFrame(true));
new StackFrame(true),
ValidationFailureType.SignatureValidationFailed);
}

if (kidExists)
Expand All @@ -367,9 +367,9 @@ private static ValidationError GetSignatureValidationError(
LogHelper.MarkAsNonPII(numKeysInConfiguration),
exceptionStrings.ToString(),
LogHelper.MarkAsSecurityArtifact(jwtToken.EncodedToken, JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.SignatureValidationFailed,
typeof(SecurityTokenSignatureKeyNotFoundException),
new StackFrame(true));
new StackFrame(true),
ValidationFailureType.SignatureValidationFailed);

return new ValidationError(
new MessageDetail(
Expand All @@ -379,9 +379,9 @@ private static ValidationError GetSignatureValidationError(
LogHelper.MarkAsNonPII(numKeysInConfiguration),
exceptionStrings.ToString(),
LogHelper.MarkAsSecurityArtifact(jwtToken.EncodedToken, JwtTokenUtilities.SafeLogJwtToken)),
ValidationFailureType.SignatureValidationFailed,
typeof(SecurityTokenSignatureKeyNotFoundException),
new StackFrame(true));
new StackFrame(true),
ValidationFailureType.SignatureValidationFailed);
}

private static void PopulateFailedResults(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
TokenLogMessages.IDX10209,
LogHelper.MarkAsNonPII(token.Length),
LogHelper.MarkAsNonPII(MaximumTokenSizeInBytes)),
ValidationFailureType.InvalidSecurityToken,
typeof(ArgumentException),
invalidTokenLengthStackFrame);
invalidTokenLengthStackFrame,
ValidationFailureType.InvalidSecurityToken);
}

ValidationResult<SecurityToken> readResult = ReadToken(token, callContext);
Expand Down Expand Up @@ -118,9 +118,9 @@ internal async Task<ValidationResult<ValidatedToken>> ValidateTokenAsync(
StackFrame notJwtStackFrame = StackFrames.TokenNotJWT ??= new StackFrame(true);
return new ValidationError(
new MessageDetail(TokenLogMessages.IDX10001, nameof(token), nameof(JsonWebToken)),
ValidationFailureType.InvalidSecurityToken,
typeof(ArgumentException),
notJwtStackFrame);
notJwtStackFrame,
ValidationFailureType.InvalidSecurityToken);
}

BaseConfiguration? currentConfiguration =
Expand Down Expand Up @@ -294,10 +294,10 @@ private async ValueTask<ValidationResult<ValidatedToken>> ValidateJWSAsync(
{
return new IssuerValidationError(
new MessageDetail(TokenLogMessages.IDX10269),
ValidationFailureType.IssuerValidatorThrew,
typeof(SecurityTokenInvalidIssuerException),
ValidationError.GetCurrentStackFrame(),
jsonWebToken.Issuer,
ValidationFailureType.IssuerValidatorThrew,
ex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ internal static ValidationResult<string> DecryptJwtToken(
{
return new ValidationError(
new MessageDetail(TokenLogMessages.IDX10679, zipAlgorithm),
ValidationFailureType.TokenDecryptionFailed,
typeof(SecurityTokenDecompressionFailedException),
new StackFrame(true),
ValidationFailureType.TokenDecryptionFailed,
ex);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Microsoft.IdentityModel.JsonWebTokens/JwtTokenUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,26 +371,26 @@ private static ValidationError GetDecryptionError(
keysAttempted.ToString(),
exceptionStrings?.ToString() ?? string.Empty,
LogHelper.MarkAsSecurityArtifact(decryptionParameters.EncodedToken, SafeLogJwtToken)),
ValidationFailureType.TokenDecryptionFailed,
typeof(SecurityTokenDecryptionFailedException),
new StackFrame(true));
new StackFrame(true),
ValidationFailureType.TokenDecryptionFailed);
else if (algorithmNotSupportedByCryptoProvider)
return new ValidationError(
new MessageDetail(
TokenLogMessages.IDX10619,
LogHelper.MarkAsNonPII(decryptionParameters.Alg),
LogHelper.MarkAsNonPII(decryptionParameters.Enc)),
ValidationFailureType.TokenDecryptionFailed,
typeof(SecurityTokenDecryptionFailedException),
new StackFrame(true));
new StackFrame(true),
ValidationFailureType.TokenDecryptionFailed);
else
return new ValidationError(
new MessageDetail(
TokenLogMessages.IDX10609,
LogHelper.MarkAsSecurityArtifact(decryptionParameters.EncodedToken, SafeLogJwtToken)),
ValidationFailureType.TokenDecryptionFailed,
typeof(SecurityTokenDecryptionFailedException),
new StackFrame(true));
new StackFrame(true),
ValidationFailureType.TokenDecryptionFailed);
}

private static byte[] DecryptToken(CryptoProviderFactory cryptoProviderFactory, SecurityKey key, string encAlg, byte[] ciphertext, byte[] headerAscii, byte[] initializationVector, byte[] authenticationTag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidatedConditions
Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidateTokenAsync(Microsoft.IdentityModel.Tokens.Saml.SamlSecurityToken samlToken, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.ValidatedToken>>
Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.ValidateTokenAsync(string token, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.ValidatedToken>>
Microsoft.IdentityModel.Tokens.Saml.SamlValidationError
Microsoft.IdentityModel.Tokens.Saml.SamlValidationError.SamlValidationError(Microsoft.IdentityModel.Tokens.MessageDetail MessageDetail, Microsoft.IdentityModel.Tokens.ValidationFailureType failureType, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame) -> void
Microsoft.IdentityModel.Tokens.Saml.SamlValidationError.SamlValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, Microsoft.IdentityModel.Tokens.ValidationFailureType failureType, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, System.Exception innerException) -> void
Microsoft.IdentityModel.Tokens.Saml.SamlValidationError.SamlValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, Microsoft.IdentityModel.Tokens.ValidationFailureType failureType, System.Exception innerException = null) -> void
Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.StackFrames
Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.ValidateTokenAsync(Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityToken samlToken, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.ValidatedToken>>
Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityTokenHandler.ValidateTokenAsync(string token, Microsoft.IdentityModel.Tokens.ValidationParameters validationParameters, Microsoft.IdentityModel.Tokens.CallContext callContext, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.IdentityModel.Tokens.ValidationResult<Microsoft.IdentityModel.Tokens.ValidatedToken>>
Microsoft.IdentityModel.Tokens.Saml2.Saml2ValidationError
Microsoft.IdentityModel.Tokens.Saml2.Saml2ValidationError.Saml2ValidationError(Microsoft.IdentityModel.Tokens.MessageDetail MessageDetail, Microsoft.IdentityModel.Tokens.ValidationFailureType failureType, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame) -> void
Microsoft.IdentityModel.Tokens.Saml2.Saml2ValidationError.Saml2ValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, Microsoft.IdentityModel.Tokens.ValidationFailureType failureType, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, System.Exception innerException) -> void
Microsoft.IdentityModel.Tokens.Saml2.Saml2ValidationError.Saml2ValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, Microsoft.IdentityModel.Tokens.ValidationFailureType failureType, System.Exception innerException = null) -> void
override Microsoft.IdentityModel.Tokens.Saml.SamlValidationError.GetException() -> System.Exception
override Microsoft.IdentityModel.Tokens.Saml2.Saml2ValidationError.GetException() -> System.Exception
static Microsoft.IdentityModel.Tokens.Saml.SamlSecurityTokenHandler.StackFrames.IssuerSigningKeyValidationFailed -> System.Diagnostics.StackFrame
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
using System;
using System.Diagnostics;

#nullable enable
iNinja marked this conversation as resolved.
Show resolved Hide resolved
namespace Microsoft.IdentityModel.Tokens.Saml
{
internal class SamlValidationError : ValidationError
{
internal SamlValidationError(MessageDetail messageDetail, ValidationFailureType failureType, Type exceptionType, StackFrame stackFrame, Exception innerException) : base(messageDetail, failureType, exceptionType, stackFrame, innerException)
internal SamlValidationError(
MessageDetail messageDetail,
Type exceptionType,
StackFrame stackFrame,
ValidationFailureType failureType,
Exception? innerException = null)
: base(messageDetail, exceptionType, stackFrame, failureType, innerException)
{
}

Expand All @@ -24,3 +31,4 @@ internal override Exception GetException()
}
}
}
#nullable restore
Loading
Loading