Skip to content

Commit

Permalink
Adjusted ValidationError parameters' position
Browse files Browse the repository at this point in the history
  • Loading branch information
iNinja committed Nov 16, 2024
1 parent 33e6a57 commit 558a00b
Show file tree
Hide file tree
Showing 27 changed files with 237 additions and 237 deletions.
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 @@ -30,9 +30,9 @@ internal virtual ValidationResult<SamlSecurityToken> ReadSamlToken(string token,
TokenLogMessages.IDX10209,
LogHelper.MarkAsNonPII(token.Length),
LogHelper.MarkAsNonPII(MaximumTokenSizeInBytes)),
ValidationFailureType.TokenExceedsMaximumSize,
typeof(ArgumentOutOfRangeException),
ValidationError.GetCurrentStackFrame());
ValidationError.GetCurrentStackFrame(),
ValidationFailureType.TokenExceedsMaximumSize);

try
{
Expand All @@ -47,9 +47,9 @@ internal virtual ValidationResult<SamlSecurityToken> ReadSamlToken(string token,
{
return new SamlValidationError(
new MessageDetail(LogMessages.IDX11402, ex.Message),
ValidationFailureType.TokenReadingFailed,
typeof(SamlSecurityTokenReadException),
ValidationError.GetCurrentStackFrame(),
ValidationFailureType.TokenReadingFailed,
ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ internal static ValidationResult<SecurityKey> ValidateSignature(
new MessageDetail(
TokenLogMessages.IDX10504,
samlToken.Assertion.CanonicalString),
ValidationFailureType.TokenIsNotSigned,
typeof(SecurityTokenValidationException),
ValidationError.GetCurrentStackFrame());
ValidationError.GetCurrentStackFrame(),
ValidationFailureType.TokenIsNotSigned);

SecurityKey? resolvedKey = null;
bool keyMatched = false;
Expand Down Expand Up @@ -110,9 +110,9 @@ internal static ValidationResult<SecurityKey> ValidateSignature(
samlToken.Assertion.Signature.KeyInfo,
GetErrorString(error, errors),
samlToken),
ValidationFailureType.SignatureValidationFailed,
typeof(SecurityTokenInvalidSignatureException),
ValidationError.GetCurrentStackFrame());
ValidationError.GetCurrentStackFrame(),
ValidationFailureType.SignatureValidationFailed);

string? keysAttemptedString = null;
if (resolvedKey is not null)
Expand All @@ -127,15 +127,15 @@ internal static ValidationResult<SecurityKey> ValidateSignature(
keysAttemptedString,
GetErrorString(error, errors),
samlToken),
ValidationFailureType.SignatureValidationFailed,
typeof(SecurityTokenSignatureKeyNotFoundException),
ValidationError.GetCurrentStackFrame());
ValidationError.GetCurrentStackFrame(),
ValidationFailureType.SignatureValidationFailed);

return new XmlValidationError(
new MessageDetail(TokenLogMessages.IDX10500),
ValidationFailureType.SignatureValidationFailed,
typeof(SecurityTokenSignatureKeyNotFoundException),
ValidationError.GetCurrentStackFrame());
ValidationError.GetCurrentStackFrame(),
ValidationFailureType.SignatureValidationFailed);
}

private static ValidationResult<SecurityKey> ValidateSignatureUsingKey(SecurityKey key, SamlSecurityToken samlToken, ValidationParameters validationParameters, CallContext callContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ internal virtual ValidationResult<Saml2SecurityToken> ReadSaml2Token(string toke
TokenLogMessages.IDX10209,
LogHelper.MarkAsNonPII(token.Length),
LogHelper.MarkAsNonPII(MaximumTokenSizeInBytes)),
ValidationFailureType.TokenReadingFailed,
typeof(ArgumentException),
ValidationError.GetCurrentStackFrame());
ValidationError.GetCurrentStackFrame(),
ValidationFailureType.TokenReadingFailed);

try
{
Expand All @@ -49,9 +49,9 @@ internal virtual ValidationResult<Saml2SecurityToken> ReadSaml2Token(string toke
{
return new Saml2ValidationError(
new MessageDetail(LogMessages.IDX13003, ex.Message),
ValidationFailureType.TokenReadingFailed,
typeof(Saml2SecurityTokenReadException),
ValidationError.GetCurrentStackFrame(),
ValidationFailureType.TokenReadingFailed,
ex);
}
}
Expand Down
Loading

0 comments on commit 558a00b

Please sign in to comment.