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

Added sample tests for the new validation path #2801

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -4,3 +4,4 @@
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.IdentityModel.Validators, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.IdentityModel.JsonWebTokens.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.IdentityModel.TestUtils, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.IdentityModel.SampleTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal static Result<SecurityToken, ExceptionDetail> ReadToken(
{
StackFrame malformedTokenStackFrame = StackFrames.ReadTokenMalformed ?? new StackFrame(true);
return new ExceptionDetail(
new MessageDetail(LogMessages.IDX14107),
new MessageDetail(LogMessages.IDX14100),
ValidationFailureType.TokenReadingFailed,
ExceptionType.SecurityTokenMalformed,
malformedTokenStackFrame,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,7 @@ private static Result<SecurityKey, ExceptionDetail> ValidateSignatureWithKey(
else
return new ExceptionDetail(
new MessageDetail(
TokenLogMessages.IDX10504,
LogHelper.MarkAsSecurityArtifact(
jsonWebToken.EncodedToken,
JwtTokenUtilities.SafeLogJwtToken)),
TokenLogMessages.IDX10500),
iNinja marked this conversation as resolved.
Show resolved Hide resolved
ValidationFailureType.SignatureValidationFailed,
ExceptionType.SecurityTokenInvalidSignature,
new StackFrame(true));
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.IdentityModel.Tokens/InternalsVisibleTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.IdentityModel.S2S.Tokens.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.IdentityModel.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.IdentityModel.Tokens.Saml.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.IdentityModel.SampleTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ public static TheoryData<TokenReadingTheoryData> JsonWebTokenHandlerReadTokenTes
TestId = "Invalid_MalformedToken",
Token = "malformed-token",
ExpectedException = ExpectedException.SecurityTokenMalformedTokenException(
"IDX14107:",
"IDX14100:",
typeof(SecurityTokenMalformedException)),
Result = new ExceptionDetail(
new MessageDetail(
LogMessages.IDX14107,
LogMessages.IDX14100,
LogHelper.MarkAsNonPII("token")),
ValidationFailureType.TokenReadingFailed,
ExceptionType.SecurityTokenMalformed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Threading;
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need a new assembly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CancellationToken requires it.

using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.TestUtils;
using Microsoft.IdentityModel.Tokens;
Expand Down Expand Up @@ -33,13 +34,19 @@ public SampleTokenValidationClass()
ValidIssuer = "http://Default.Issuer.com",
IssuerSigningKey = KeyingMaterial.JsonWebKeyRsa256SigningCredentials.Key
};
ValidationParameters = new ValidationParameters();
ValidationParameters.ValidAudiences.Add("http://Default.Audience.com");
ValidationParameters.ValidIssuers.Add("http://Default.Issuer.com");
ValidationParameters.IssuerSigningKeys.Add(KeyingMaterial.JsonWebKeyRsa256SigningCredentials.Key);
}

/// <summary>
/// Gets or sets the <see cref="TokenValidationParameters"/> used for the validation operations.
/// </summary>
public TokenValidationParameters TokenValidationParameters { get; set; }

public ValidationParameters ValidationParameters { get; set; }

/// <summary>
/// Gets or sets the <see cref="JsonWebTokenHandler"/> instance used for the validation operations.
/// </summary>
Expand All @@ -59,6 +66,11 @@ public void ValidateTokenShim(string token)
ValidateTokenShim(token, TokenValidationParameters);
}

public Result<ValidationResult, ExceptionDetail> ValidateTokenShimWithNewPath(string token)
{
return ValidateTokenShimWithNewPath(token, ValidationParameters);
}

/// <summary>
/// Validates the passed token using the instance of the deprecated <see cref="JwtSecurityTokenHandler"/>.
/// </summary>
Expand All @@ -75,14 +87,20 @@ public void ValidateTokenShimWithDeprecatedModel(string token)
/// <param name="tokenValidationParameters">
/// The <see cref="TokenValidationParameters"/> to use instead of the instance's value.
/// </param>
public void ValidateTokenShim(string token, TokenValidationParameters tokenValidationParameters)
public TokenValidationResult ValidateTokenShim(string token, TokenValidationParameters tokenValidationParameters)
{
var result = JsonWebTokenHandler.ValidateTokenAsync(token, tokenValidationParameters).Result;

if (!result.IsValid)
{
throw new SampleTestTokenValidationException("Validation Issue Encountered", result.Exception);
}

return result;
}

internal Result<ValidationResult, ExceptionDetail> ValidateTokenShimWithNewPath(string token, ValidationParameters validationParameters)
{
CallContext callContext = new CallContext();
return JsonWebTokenHandler.ValidateTokenAsync(token, validationParameters, callContext, CancellationToken.None).Result;
}

/// <summary>
Expand Down
Loading
Loading