-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Marked json web token as security artifact for logging * Added regression/comparison tests for signature validation scenarios
- Loading branch information
Showing
3 changed files
with
146 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
...dentityModel.JsonWebTokens.Tests/JsonWebTokenHandler.ValidateTokenAsyncTests.Signature.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#nullable enable | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.IdentityModel.TestUtils; | ||
using Microsoft.IdentityModel.Tokens; | ||
using Xunit; | ||
|
||
namespace Microsoft.IdentityModel.JsonWebTokens.Tests | ||
{ | ||
public partial class JsonWebTokenHandlerValidateTokenAsyncTests | ||
{ | ||
[Theory, MemberData(nameof(ValidateTokenAsync_SignatureTestCases), DisableDiscoveryEnumeration = true)] | ||
public async Task ValidateTokenAsync_Signature(ValidateTokenAsyncSignatureTheoryData theoryData) | ||
{ | ||
var context = TestUtilities.WriteHeader($"{this}.ValidateTokenAsync_Signature", theoryData); | ||
|
||
string jwtString = CreateTokenWithSigningCredentials(theoryData.SigningCredentials); | ||
|
||
await ValidateAndCompareResults(jwtString, theoryData, context); | ||
|
||
TestUtilities.AssertFailIfErrors(context); | ||
} | ||
|
||
public static TheoryData<ValidateTokenAsyncSignatureTheoryData> ValidateTokenAsync_SignatureTestCases | ||
{ | ||
get | ||
{ | ||
var theoryData = new TheoryData<ValidateTokenAsyncSignatureTheoryData>(); | ||
|
||
theoryData.Add(new ValidateTokenAsyncSignatureTheoryData("Valid_SignatureIsValid") | ||
{ | ||
SigningCredentials = KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2, | ||
TokenValidationParameters = CreateTokenValidationParameters(KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.Key), | ||
ValidationParameters = CreateValidationParameters(KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.Key), | ||
}); | ||
|
||
theoryData.Add(new ValidateTokenAsyncSignatureTheoryData("Invalid_TokenIsNotSigned") | ||
{ | ||
SigningCredentials = null, | ||
TokenValidationParameters = CreateTokenValidationParameters(), | ||
ValidationParameters = CreateValidationParameters(), | ||
ExpectedIsValid = false, | ||
ExpectedException = ExpectedException.SecurityTokenInvalidSignatureException("IDX10504:"), | ||
}); | ||
|
||
theoryData.Add(new ValidateTokenAsyncSignatureTheoryData("Invalid_TokenSignedWithDifferentKey_KeyIdPresent_TryAllKeysFalse") | ||
{ | ||
SigningCredentials = Default.SymmetricSigningCredentials, | ||
TokenValidationParameters = CreateTokenValidationParameters(Default.AsymmetricSigningKey), | ||
ValidationParameters = CreateValidationParameters(Default.AsymmetricSigningKey), | ||
ExpectedIsValid = false, | ||
ExpectedException = ExpectedException.SecurityTokenSignatureKeyNotFoundException("IDX10500:"), | ||
// ValidateTokenAsync with ValidationParameters returns a different error message in the case where a | ||
// key is not found in the IssuerSigningKeys collection and TryAllKeys is false. | ||
ExpectedExceptionValidationParameters = ExpectedException.SecurityTokenSignatureKeyNotFoundException("IDX10502:"), | ||
}); | ||
|
||
theoryData.Add(new ValidateTokenAsyncSignatureTheoryData("Invalid_TokenSignedWithDifferentKey_KeyIdPresent_TryAllKeysTrue") | ||
{ | ||
SigningCredentials = Default.SymmetricSigningCredentials, | ||
TokenValidationParameters = CreateTokenValidationParameters(Default.AsymmetricSigningKey, tryAllKeys: true), | ||
ValidationParameters = CreateValidationParameters(Default.AsymmetricSigningKey, tryAllKeys: true), | ||
ExpectedIsValid = false, | ||
ExpectedException = ExpectedException.SecurityTokenSignatureKeyNotFoundException("IDX10503:"), | ||
ExpectedExceptionValidationParameters = ExpectedException.SecurityTokenSignatureKeyNotFoundException("IDX10503:"), | ||
}); | ||
|
||
theoryData.Add(new ValidateTokenAsyncSignatureTheoryData("Invalid_TokenSignedWithDifferentKey_KeyIdNotPresent_TryAllKeysFalse") | ||
{ | ||
SigningCredentials = KeyingMaterial.DefaultSymmetricSigningCreds_256_Sha2_NoKeyId, | ||
TokenValidationParameters = CreateTokenValidationParameters(Default.AsymmetricSigningKey), | ||
ValidationParameters = CreateValidationParameters(Default.AsymmetricSigningKey), | ||
ExpectedIsValid = false, | ||
ExpectedException = ExpectedException.SecurityTokenSignatureKeyNotFoundException("IDX10500:"), | ||
}); | ||
|
||
theoryData.Add(new ValidateTokenAsyncSignatureTheoryData("Invalid_TokenSignedWithDifferentKey_KeyIdNotPresent_TryAllKeysTrue") | ||
{ | ||
SigningCredentials = KeyingMaterial.DefaultSymmetricSigningCreds_256_Sha2_NoKeyId, | ||
TokenValidationParameters = CreateTokenValidationParameters(Default.AsymmetricSigningKey, tryAllKeys: true), | ||
ValidationParameters = CreateValidationParameters(Default.AsymmetricSigningKey, tryAllKeys: true), | ||
ExpectedIsValid = false, | ||
ExpectedException = ExpectedException.SecurityTokenSignatureKeyNotFoundException("IDX10517:"), | ||
ExpectedExceptionValidationParameters = ExpectedException.SecurityTokenSignatureKeyNotFoundException("IDX10517:"), | ||
}); | ||
|
||
return theoryData; | ||
|
||
static TokenValidationParameters CreateTokenValidationParameters(SecurityKey? signingKey = null, bool tryAllKeys = false) | ||
{ | ||
// only validate the signature | ||
var tokenValidationParameters = new TokenValidationParameters | ||
{ | ||
ValidateAudience = false, | ||
ValidateIssuer = false, | ||
ValidateLifetime = false, | ||
ValidateTokenReplay = false, | ||
ValidateIssuerSigningKey = false, | ||
RequireSignedTokens = true, | ||
IssuerSigningKey = signingKey, | ||
TryAllIssuerSigningKeys = tryAllKeys, | ||
}; | ||
|
||
return tokenValidationParameters; | ||
} | ||
|
||
static ValidationParameters CreateValidationParameters( | ||
SecurityKey? signingKey = null, bool tryAllKeys = false) | ||
{ | ||
ValidationParameters validationParameters = new ValidationParameters(); | ||
|
||
if (signingKey is not null) | ||
validationParameters.IssuerSigningKeys.Add(signingKey); | ||
|
||
// Skip all validations except signature | ||
validationParameters.AlgorithmValidator = SkipValidationDelegates.SkipAlgorithmValidation; | ||
validationParameters.AudienceValidator = SkipValidationDelegates.SkipAudienceValidation; | ||
validationParameters.IssuerValidatorAsync = SkipValidationDelegates.SkipIssuerValidation; | ||
validationParameters.IssuerSigningKeyValidator = SkipValidationDelegates.SkipIssuerSigningKeyValidation; | ||
validationParameters.LifetimeValidator = SkipValidationDelegates.SkipLifetimeValidation; | ||
validationParameters.TokenReplayValidator = SkipValidationDelegates.SkipTokenReplayValidation; | ||
validationParameters.TypeValidator = SkipValidationDelegates.SkipTokenTypeValidation; | ||
validationParameters.TryAllIssuerSigningKeys = tryAllKeys; | ||
|
||
return validationParameters; | ||
} | ||
} | ||
} | ||
|
||
public class ValidateTokenAsyncSignatureTheoryData : ValidateTokenAsyncBaseTheoryData | ||
{ | ||
public ValidateTokenAsyncSignatureTheoryData(string testId) : base(testId) { } | ||
|
||
public SigningCredentials? SigningCredentials { get; set; } | ||
} | ||
} | ||
} | ||
#nullable restore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters