|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#nullable enable |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.IdentityModel.TestUtils; |
| 7 | +using Microsoft.IdentityModel.Tokens; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Microsoft.IdentityModel.JsonWebTokens.Tests |
| 11 | +{ |
| 12 | + public partial class JsonWebTokenHandlerValidateTokenAsyncTests |
| 13 | + { |
| 14 | + [Theory, MemberData(nameof(ValidateTokenAsync_TokenReplayTestCases), DisableDiscoveryEnumeration = true)] |
| 15 | + public async Task ValidateTokenAsync_TokenReplay(ValidateTokenAsyncTokenReplayTheoryData theoryData) |
| 16 | + { |
| 17 | + var context = TestUtilities.WriteHeader($"{this}.ValidateTokenAsync_TokenReplay", theoryData); |
| 18 | + |
| 19 | + string jwtString = CreateTokenForTokenReplayValidation(theoryData.TokenHasExpiration); |
| 20 | + |
| 21 | + await ValidateAndCompareResults(jwtString, theoryData, context); |
| 22 | + |
| 23 | + TestUtilities.AssertFailIfErrors(context); |
| 24 | + } |
| 25 | + |
| 26 | + public static TheoryData<ValidateTokenAsyncTokenReplayTheoryData> ValidateTokenAsync_TokenReplayTestCases |
| 27 | + { |
| 28 | + get |
| 29 | + { |
| 30 | + var successfulTokenReplayCache = new TokenReplayCache |
| 31 | + { |
| 32 | + OnAddReturnValue = true, |
| 33 | + OnFindReturnValue = false, |
| 34 | + }; |
| 35 | + |
| 36 | + var failToAddTokenReplayCache = new TokenReplayCache |
| 37 | + { |
| 38 | + OnAddReturnValue = false, |
| 39 | + OnFindReturnValue = false, |
| 40 | + }; |
| 41 | + |
| 42 | + var tokenAlreadySavedTokenReplayCache = new TokenReplayCache |
| 43 | + { |
| 44 | + OnAddReturnValue = true, |
| 45 | + OnFindReturnValue = true, |
| 46 | + }; |
| 47 | + |
| 48 | + var theoryData = new TheoryData<ValidateTokenAsyncTokenReplayTheoryData>(); |
| 49 | + |
| 50 | + theoryData.Add(new ValidateTokenAsyncTokenReplayTheoryData("Valid_TokenHasNotBeenReplayed") |
| 51 | + { |
| 52 | + TokenValidationParameters = CreateTokenValidationParameters(successfulTokenReplayCache), |
| 53 | + ValidationParameters = CreateValidationParameters(successfulTokenReplayCache), |
| 54 | + }); |
| 55 | + |
| 56 | + theoryData.Add(new ValidateTokenAsyncTokenReplayTheoryData("Valid_TokenHasNoExpiration_TokenReplayCacheIsNull") |
| 57 | + { |
| 58 | + TokenHasExpiration = false, |
| 59 | + TokenValidationParameters = CreateTokenValidationParameters(null), |
| 60 | + ValidationParameters = CreateValidationParameters(null), |
| 61 | + }); |
| 62 | + |
| 63 | + theoryData.Add(new ValidateTokenAsyncTokenReplayTheoryData("Invalid_TokenHasNoExpiration_TokenReplayCacheIsNotNull") |
| 64 | + { |
| 65 | + TokenHasExpiration = false, |
| 66 | + TokenValidationParameters = CreateTokenValidationParameters(successfulTokenReplayCache), |
| 67 | + ValidationParameters = CreateValidationParameters(successfulTokenReplayCache), |
| 68 | + ExpectedIsValid = false, |
| 69 | + ExpectedException = ExpectedException.SecurityTokenNoExpirationException("IDX10227:"), |
| 70 | + }); |
| 71 | + |
| 72 | + theoryData.Add(new ValidateTokenAsyncTokenReplayTheoryData("Invalid_TokenCouldNotBeAdded") |
| 73 | + { |
| 74 | + TokenValidationParameters = CreateTokenValidationParameters(failToAddTokenReplayCache), |
| 75 | + ValidationParameters = CreateValidationParameters(failToAddTokenReplayCache), |
| 76 | + ExpectedIsValid = false, |
| 77 | + ExpectedException = ExpectedException.SecurityTokenReplayAddFailedException("IDX10229:"), |
| 78 | + }); |
| 79 | + |
| 80 | + theoryData.Add(new ValidateTokenAsyncTokenReplayTheoryData("Invalid_TokenHasBeenReplayed") |
| 81 | + { |
| 82 | + TokenValidationParameters = CreateTokenValidationParameters(tokenAlreadySavedTokenReplayCache), |
| 83 | + ValidationParameters = CreateValidationParameters(tokenAlreadySavedTokenReplayCache), |
| 84 | + ExpectedIsValid = false, |
| 85 | + ExpectedException = ExpectedException.SecurityTokenReplayDetectedException("IDX10228:"), |
| 86 | + }); |
| 87 | + |
| 88 | + return theoryData; |
| 89 | + |
| 90 | + static TokenValidationParameters CreateTokenValidationParameters(ITokenReplayCache? tokenReplayCache) |
| 91 | + { |
| 92 | + // only validate that the token has not been replayed |
| 93 | + var tokenValidationParameters = new TokenValidationParameters |
| 94 | + { |
| 95 | + ValidateAudience = false, |
| 96 | + ValidateIssuer = false, |
| 97 | + ValidateLifetime = false, |
| 98 | + ValidateTokenReplay = true, |
| 99 | + ValidateIssuerSigningKey = false, |
| 100 | + RequireSignedTokens = false, |
| 101 | + TokenReplayCache = tokenReplayCache |
| 102 | + }; |
| 103 | + |
| 104 | + return tokenValidationParameters; |
| 105 | + } |
| 106 | + |
| 107 | + static ValidationParameters CreateValidationParameters(ITokenReplayCache? tokenReplayCache) |
| 108 | + { |
| 109 | + ValidationParameters validationParameters = new ValidationParameters(); |
| 110 | + validationParameters.TokenReplayCache = tokenReplayCache; |
| 111 | + |
| 112 | + // Skip all validations except token replay |
| 113 | + validationParameters.AlgorithmValidator = SkipValidationDelegates.SkipAlgorithmValidation; |
| 114 | + validationParameters.AudienceValidator = SkipValidationDelegates.SkipAudienceValidation; |
| 115 | + validationParameters.IssuerSigningKeyValidator = SkipValidationDelegates.SkipIssuerSigningKeyValidation; |
| 116 | + validationParameters.IssuerValidatorAsync = SkipValidationDelegates.SkipIssuerValidation; |
| 117 | + validationParameters.LifetimeValidator = SkipValidationDelegates.SkipLifetimeValidation; |
| 118 | + validationParameters.SignatureValidator = SkipValidationDelegates.SkipSignatureValidation; |
| 119 | + validationParameters.TokenTypeValidator = SkipValidationDelegates.SkipTokenTypeValidation; |
| 120 | + |
| 121 | + return validationParameters; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public class ValidateTokenAsyncTokenReplayTheoryData : ValidateTokenAsyncBaseTheoryData |
| 127 | + { |
| 128 | + public ValidateTokenAsyncTokenReplayTheoryData(string testId) : base(testId) { } |
| 129 | + |
| 130 | + public bool TokenHasExpiration { get; set; } = true; |
| 131 | + } |
| 132 | + |
| 133 | + private static string CreateTokenForTokenReplayValidation(bool hasExpiration = true) |
| 134 | + { |
| 135 | + JsonWebTokenHandler jsonWebTokenHandler = new JsonWebTokenHandler(); |
| 136 | + // If the token has expiration, we use the default times. |
| 137 | + jsonWebTokenHandler.SetDefaultTimesOnTokenCreation = hasExpiration; |
| 138 | + |
| 139 | + SecurityTokenDescriptor securityTokenDescriptor; |
| 140 | + |
| 141 | + if (!hasExpiration) |
| 142 | + { |
| 143 | + securityTokenDescriptor = new SecurityTokenDescriptor |
| 144 | + { |
| 145 | + Subject = Default.ClaimsIdentity, |
| 146 | + Expires = null, |
| 147 | + NotBefore = null, |
| 148 | + IssuedAt = null, |
| 149 | + }; |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + securityTokenDescriptor = new SecurityTokenDescriptor |
| 154 | + { |
| 155 | + Subject = Default.ClaimsIdentity, |
| 156 | + }; |
| 157 | + } |
| 158 | + |
| 159 | + return jsonWebTokenHandler.CreateToken(securityTokenDescriptor); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | +#nullable restore |
0 commit comments