-
Notifications
You must be signed in to change notification settings - Fork 434
Regression tests: Token Replay #2931
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e6468a4
Removed calls to MarkAsUnsafeSecurityArtifact as they was printing th…
iNinja 37017ff
Removed duplicated CreateToken method
iNinja 05bf577
Added regression/comparison tests for JWT on token replay scenarios
iNinja 2b495d2
Updated unit tests to reflect updated exception
iNinja 0e6f865
Merge branch 'dev' into iinglese/regression-tests-token-replay
iNinja 8c9dd0f
Merge branch 'dev' into iinglese/regression-tests-token-replay
iNinja 9e4e1d4
Merge branch 'dev' into iinglese/regression-tests-token-replay
iNinja ff2b352
Resolve post merge issue
iNinja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
163 changes: 163 additions & 0 deletions
163
...ntityModel.JsonWebTokens.Tests/JsonWebTokenHandler.ValidateTokenAsyncTests.TokenReplay.cs
This file contains hidden or 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,163 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #nullable enable | ||
| 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_TokenReplayTestCases), DisableDiscoveryEnumeration = true)] | ||
| public async Task ValidateTokenAsync_TokenReplay(ValidateTokenAsyncTokenReplayTheoryData theoryData) | ||
| { | ||
| var context = TestUtilities.WriteHeader($"{this}.ValidateTokenAsync_TokenReplay", theoryData); | ||
|
|
||
| string jwtString = CreateTokenForTokenReplayValidation(theoryData.TokenHasExpiration); | ||
|
|
||
| await ValidateAndCompareResults(jwtString, theoryData, context); | ||
|
|
||
| TestUtilities.AssertFailIfErrors(context); | ||
| } | ||
|
|
||
| public static TheoryData<ValidateTokenAsyncTokenReplayTheoryData> ValidateTokenAsync_TokenReplayTestCases | ||
| { | ||
| get | ||
| { | ||
| var successfulTokenReplayCache = new TokenReplayCache | ||
| { | ||
| OnAddReturnValue = true, | ||
| OnFindReturnValue = false, | ||
| }; | ||
|
|
||
| var failToAddTokenReplayCache = new TokenReplayCache | ||
| { | ||
| OnAddReturnValue = false, | ||
| OnFindReturnValue = false, | ||
| }; | ||
|
|
||
| var tokenAlreadySavedTokenReplayCache = new TokenReplayCache | ||
| { | ||
| OnAddReturnValue = true, | ||
| OnFindReturnValue = true, | ||
| }; | ||
|
|
||
| var theoryData = new TheoryData<ValidateTokenAsyncTokenReplayTheoryData>(); | ||
|
|
||
| theoryData.Add(new ValidateTokenAsyncTokenReplayTheoryData("Valid_TokenHasNotBeenReplayed") | ||
| { | ||
| TokenValidationParameters = CreateTokenValidationParameters(successfulTokenReplayCache), | ||
| ValidationParameters = CreateValidationParameters(successfulTokenReplayCache), | ||
| }); | ||
|
|
||
| theoryData.Add(new ValidateTokenAsyncTokenReplayTheoryData("Valid_TokenHasNoExpiration_TokenReplayCacheIsNull") | ||
| { | ||
| TokenHasExpiration = false, | ||
| TokenValidationParameters = CreateTokenValidationParameters(null), | ||
| ValidationParameters = CreateValidationParameters(null), | ||
| }); | ||
|
|
||
| theoryData.Add(new ValidateTokenAsyncTokenReplayTheoryData("Invalid_TokenHasNoExpiration_TokenReplayCacheIsNotNull") | ||
| { | ||
| TokenHasExpiration = false, | ||
| TokenValidationParameters = CreateTokenValidationParameters(successfulTokenReplayCache), | ||
| ValidationParameters = CreateValidationParameters(successfulTokenReplayCache), | ||
| ExpectedIsValid = false, | ||
| ExpectedException = ExpectedException.SecurityTokenNoExpirationException("IDX10227:"), | ||
| }); | ||
|
|
||
| theoryData.Add(new ValidateTokenAsyncTokenReplayTheoryData("Invalid_TokenCouldNotBeAdded") | ||
| { | ||
| TokenValidationParameters = CreateTokenValidationParameters(failToAddTokenReplayCache), | ||
| ValidationParameters = CreateValidationParameters(failToAddTokenReplayCache), | ||
| ExpectedIsValid = false, | ||
| ExpectedException = ExpectedException.SecurityTokenReplayAddFailedException("IDX10229:"), | ||
| }); | ||
|
|
||
| theoryData.Add(new ValidateTokenAsyncTokenReplayTheoryData("Invalid_TokenHasBeenReplayed") | ||
| { | ||
| TokenValidationParameters = CreateTokenValidationParameters(tokenAlreadySavedTokenReplayCache), | ||
| ValidationParameters = CreateValidationParameters(tokenAlreadySavedTokenReplayCache), | ||
| ExpectedIsValid = false, | ||
| ExpectedException = ExpectedException.SecurityTokenReplayDetectedException("IDX10228:"), | ||
| }); | ||
|
|
||
| return theoryData; | ||
|
|
||
| static TokenValidationParameters CreateTokenValidationParameters(ITokenReplayCache? tokenReplayCache) | ||
| { | ||
| // only validate that the token has not been replayed | ||
| var tokenValidationParameters = new TokenValidationParameters | ||
| { | ||
| ValidateAudience = false, | ||
| ValidateIssuer = false, | ||
| ValidateLifetime = false, | ||
| ValidateTokenReplay = true, | ||
| ValidateIssuerSigningKey = false, | ||
| RequireSignedTokens = false, | ||
| TokenReplayCache = tokenReplayCache | ||
| }; | ||
|
|
||
| return tokenValidationParameters; | ||
| } | ||
|
|
||
| static ValidationParameters CreateValidationParameters(ITokenReplayCache? tokenReplayCache) | ||
| { | ||
| ValidationParameters validationParameters = new ValidationParameters(); | ||
| validationParameters.TokenReplayCache = tokenReplayCache; | ||
|
|
||
| // Skip all validations except token replay | ||
| validationParameters.AlgorithmValidator = SkipValidationDelegates.SkipAlgorithmValidation; | ||
| validationParameters.AudienceValidator = SkipValidationDelegates.SkipAudienceValidation; | ||
| validationParameters.IssuerSigningKeyValidator = SkipValidationDelegates.SkipIssuerSigningKeyValidation; | ||
| validationParameters.IssuerValidatorAsync = SkipValidationDelegates.SkipIssuerValidation; | ||
| validationParameters.LifetimeValidator = SkipValidationDelegates.SkipLifetimeValidation; | ||
| validationParameters.SignatureValidator = SkipValidationDelegates.SkipSignatureValidation; | ||
| validationParameters.TokenTypeValidator = SkipValidationDelegates.SkipTokenTypeValidation; | ||
|
|
||
| return validationParameters; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class ValidateTokenAsyncTokenReplayTheoryData : ValidateTokenAsyncBaseTheoryData | ||
| { | ||
| public ValidateTokenAsyncTokenReplayTheoryData(string testId) : base(testId) { } | ||
|
|
||
| public bool TokenHasExpiration { get; set; } = true; | ||
| } | ||
|
|
||
| private static string CreateTokenForTokenReplayValidation(bool hasExpiration = true) | ||
| { | ||
| JsonWebTokenHandler jsonWebTokenHandler = new JsonWebTokenHandler(); | ||
| // If the token has expiration, we use the default times. | ||
| jsonWebTokenHandler.SetDefaultTimesOnTokenCreation = hasExpiration; | ||
|
|
||
| SecurityTokenDescriptor securityTokenDescriptor; | ||
|
|
||
| if (!hasExpiration) | ||
| { | ||
| securityTokenDescriptor = new SecurityTokenDescriptor | ||
| { | ||
| Subject = Default.ClaimsIdentity, | ||
| Expires = null, | ||
| NotBefore = null, | ||
| IssuedAt = null, | ||
| }; | ||
| } | ||
| else | ||
| { | ||
| securityTokenDescriptor = new SecurityTokenDescriptor | ||
| { | ||
| Subject = Default.ClaimsIdentity, | ||
| }; | ||
| } | ||
|
|
||
| return jsonWebTokenHandler.CreateToken(securityTokenDescriptor); | ||
| } | ||
| } | ||
| } | ||
| #nullable restore |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.