|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.IdentityModel.Protocols.WsFederation; |
| 7 | +using Microsoft.IdentityModel.TestUtils; |
| 8 | +using Microsoft.IdentityModel.Tokens.Saml2; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Microsoft.IdentityModel.Tokens.Saml.Tests |
| 12 | +{ |
| 13 | +#nullable enable |
| 14 | + public partial class Saml2SecurityTokenHandlerTests |
| 15 | + { |
| 16 | + [Theory, MemberData(nameof(ValidateTokenAsync_IssuerTestCases), DisableDiscoveryEnumeration = true)] |
| 17 | + public async Task ValidateTokenAsync_IssuerComparison(ValidateTokenAsyncIssuerTheoryData theoryData) |
| 18 | + { |
| 19 | + var context = TestUtilities.WriteHeader($"{this}.ValidateTokenAsync_IssuerComparison", theoryData); |
| 20 | + |
| 21 | + var saml2Token = CreateTokenWithIssuer(theoryData.TokenIssuer); |
| 22 | + |
| 23 | + var tokenValidationParameters = CreateTokenValidationParametersForIssuerValidationOnly( |
| 24 | + saml2Token, |
| 25 | + theoryData.NullTokenValidationParameters, |
| 26 | + theoryData.ValidationParametersIssuer, |
| 27 | + theoryData.ConfigurationIssuer); |
| 28 | + |
| 29 | + Saml2SecurityTokenHandler saml2TokenHandler = new Saml2SecurityTokenHandler(); |
| 30 | + |
| 31 | + // Validate token using TokenValidationParameters |
| 32 | + TokenValidationResult tokenValidationResult = |
| 33 | + await saml2TokenHandler.ValidateTokenAsync(saml2Token.Assertion.CanonicalString, tokenValidationParameters); |
| 34 | + |
| 35 | + // Validate token using ValidationParameters. |
| 36 | + ValidationResult<ValidatedToken> validationResult = |
| 37 | + await saml2TokenHandler.ValidateTokenAsync( |
| 38 | + saml2Token, |
| 39 | + theoryData.ValidationParameters!, |
| 40 | + theoryData.CallContext, |
| 41 | + CancellationToken.None); |
| 42 | + |
| 43 | + // Ensure validity of the results match the expected result. |
| 44 | + if (tokenValidationResult.IsValid != validationResult.IsValid) |
| 45 | + { |
| 46 | + context.AddDiff($"tokenValidationResult.IsValid != validationResult.IsSuccess"); |
| 47 | + theoryData.ExpectedExceptionValidationParameters!.ProcessException(validationResult.UnwrapError().GetException(), context); |
| 48 | + theoryData.ExpectedException.ProcessException(tokenValidationResult.Exception, context); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + if (tokenValidationResult.IsValid) |
| 53 | + { |
| 54 | + // Verify validated tokens from both paths match. |
| 55 | + ValidatedToken validatedToken = validationResult.UnwrapResult(); |
| 56 | + IdentityComparer.AreEqual(validatedToken.SecurityToken, tokenValidationResult.SecurityToken, context); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + // Verify the exception provided by both paths match. |
| 61 | + var tokenValidationResultException = tokenValidationResult.Exception; |
| 62 | + theoryData.ExpectedException.ProcessException(tokenValidationResult.Exception, context); |
| 63 | + var validationResultException = validationResult.UnwrapError().GetException(); |
| 64 | + theoryData.ExpectedExceptionValidationParameters!.ProcessException(validationResult.UnwrapError().GetException(), context); |
| 65 | + } |
| 66 | + |
| 67 | + TestUtilities.AssertFailIfErrors(context); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static TheoryData<ValidateTokenAsyncIssuerTheoryData> ValidateTokenAsync_IssuerTestCases |
| 72 | + { |
| 73 | + get |
| 74 | + { |
| 75 | + var theoryData = new TheoryData<ValidateTokenAsyncIssuerTheoryData>(); |
| 76 | + |
| 77 | + theoryData.Add(new ValidateTokenAsyncIssuerTheoryData("Valid_IssuerIsValidIssuer") |
| 78 | + { |
| 79 | + TokenIssuer = Default.Issuer, |
| 80 | + ValidationParametersIssuer = Default.Issuer, |
| 81 | + ValidationParameters = CreateValidationParameters(validIssuer: Default.Issuer), |
| 82 | + }); |
| 83 | + |
| 84 | + theoryData.Add(new ValidateTokenAsyncIssuerTheoryData("Valid_IssuerIsConfigurationIssuer") |
| 85 | + { |
| 86 | + TokenIssuer = Default.Issuer, |
| 87 | + ConfigurationIssuer = Default.Issuer, |
| 88 | + ValidationParameters = CreateValidationParameters(configurationIssuer: Default.Issuer), |
| 89 | + }); |
| 90 | + |
| 91 | + theoryData.Add(new ValidateTokenAsyncIssuerTheoryData("Invalid_IssuerIsNotValid") |
| 92 | + { |
| 93 | + TokenIssuer = "InvalidIssuer", |
| 94 | + ValidationParametersIssuer = Default.Issuer, |
| 95 | + ValidationParameters = CreateValidationParameters(validIssuer: Default.Issuer), |
| 96 | + ExpectedIsValid = false, |
| 97 | + ExpectedException = new ExpectedException(typeof(SecurityTokenInvalidIssuerException), "IDX10205:"), |
| 98 | + ExpectedExceptionValidationParameters = new ExpectedException(typeof(SecurityTokenInvalidIssuerException), "IDX10212:") |
| 99 | + }); |
| 100 | + |
| 101 | + theoryData.Add(new ValidateTokenAsyncIssuerTheoryData("Invalid_IssuerIsWhitespace") |
| 102 | + { |
| 103 | + //This test will cover the case where the issuer is null or empty as well since, we do not allow tokens to be created with null or empty issuer. |
| 104 | + TokenIssuer = " ", |
| 105 | + ValidationParametersIssuer = Default.Issuer, |
| 106 | + ValidationParameters = CreateValidationParameters(validIssuer: Default.Issuer), |
| 107 | + ExpectedIsValid = false, |
| 108 | + ExpectedException = new ExpectedException(typeof(SecurityTokenInvalidIssuerException), "IDX10211:") |
| 109 | + }); |
| 110 | + |
| 111 | + theoryData.Add(new ValidateTokenAsyncIssuerTheoryData("Invalid_NoValidIssuersProvided") |
| 112 | + { |
| 113 | + TokenIssuer = Default.Issuer, |
| 114 | + ValidationParametersIssuer = string.Empty, |
| 115 | + ValidationParameters = CreateValidationParameters(), |
| 116 | + ExpectedIsValid = false, |
| 117 | + ExpectedException = new ExpectedException(typeof(SecurityTokenInvalidIssuerException), "IDX10204:"), |
| 118 | + ExpectedExceptionValidationParameters = new ExpectedException(typeof(SecurityTokenInvalidIssuerException), "IDX10211:") |
| 119 | + }); |
| 120 | + |
| 121 | + return theoryData; |
| 122 | + |
| 123 | + static ValidationParameters CreateValidationParameters( |
| 124 | + string? validIssuer = null, |
| 125 | + string? configurationIssuer = null) |
| 126 | + { |
| 127 | + ValidationParameters validationParameters = new ValidationParameters(); |
| 128 | + |
| 129 | + // Skip all validations except issuer |
| 130 | + validationParameters.AlgorithmValidator = SkipValidationDelegates.SkipAlgorithmValidation; |
| 131 | + validationParameters.AudienceValidator = SkipValidationDelegates.SkipAudienceValidation; |
| 132 | + validationParameters.LifetimeValidator = SkipValidationDelegates.SkipLifetimeValidation; |
| 133 | + validationParameters.IssuerSigningKeyValidator = SkipValidationDelegates.SkipIssuerSigningKeyValidation; |
| 134 | + validationParameters.SignatureValidator = SkipValidationDelegates.SkipSignatureValidation; |
| 135 | + |
| 136 | + return validationParameters; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public class ValidateTokenAsyncIssuerTheoryData : TheoryDataBase |
| 142 | + { |
| 143 | + public ValidateTokenAsyncIssuerTheoryData(string testId) : base(testId) { } |
| 144 | + |
| 145 | + internal ValidationParameters? ValidationParameters { get; set; } |
| 146 | + |
| 147 | + internal ExpectedException? ExpectedExceptionValidationParameters { get; set; } = ExpectedException.NoExceptionExpected; |
| 148 | + |
| 149 | + internal bool ExpectedIsValid { get; set; } = true; |
| 150 | + |
| 151 | + public bool NullTokenValidationParameters { get; internal set; } = false; |
| 152 | + |
| 153 | + public string? TokenIssuer { get; set; } |
| 154 | + |
| 155 | + public string? ValidationParametersIssuer { get; set; } = null; |
| 156 | + |
| 157 | + public string? ConfigurationIssuer { get; set; } = null; |
| 158 | + } |
| 159 | + |
| 160 | + private static Saml2SecurityToken CreateTokenWithIssuer(string? issuer) |
| 161 | + { |
| 162 | + Saml2SecurityTokenHandler saml2TokenHandler = new Saml2SecurityTokenHandler(); |
| 163 | + |
| 164 | + SecurityTokenDescriptor securityTokenDescriptor = new SecurityTokenDescriptor |
| 165 | + { |
| 166 | + SigningCredentials = Default.AsymmetricSigningCredentials, |
| 167 | + Audience = Default.Audience, |
| 168 | + Issuer = issuer, |
| 169 | + Subject = Default.SamlClaimsIdentity |
| 170 | + }; |
| 171 | + |
| 172 | + return (Saml2SecurityToken)saml2TokenHandler.CreateToken(securityTokenDescriptor); |
| 173 | + } |
| 174 | + |
| 175 | + private static TokenValidationParameters? CreateTokenValidationParametersForIssuerValidationOnly( |
| 176 | + Saml2SecurityToken saml2SecurityToken, |
| 177 | + bool nullTokenValidationParameters, |
| 178 | + string? validIssuer, |
| 179 | + string? configurationIssuer) |
| 180 | + { |
| 181 | + if (nullTokenValidationParameters) |
| 182 | + { |
| 183 | + return null; |
| 184 | + } |
| 185 | + |
| 186 | + var tokenValidationParameters = new TokenValidationParameters() |
| 187 | + { |
| 188 | + ValidateAudience = false, |
| 189 | + ValidateIssuer = false, |
| 190 | + ValidateLifetime = false, |
| 191 | + ValidateTokenReplay = false, |
| 192 | + ValidateIssuerSigningKey = false, |
| 193 | + IssuerSigningKey = Default.AsymmetricSigningKey, |
| 194 | + ValidAudiences = [Default.Audience], |
| 195 | + ValidIssuer = validIssuer, |
| 196 | + SignatureValidator = delegate (string token, TokenValidationParameters validationParameters) |
| 197 | + { |
| 198 | + return saml2SecurityToken; |
| 199 | + } |
| 200 | + }; |
| 201 | + |
| 202 | + if (configurationIssuer is not null) |
| 203 | + { |
| 204 | + var validConfig = new WsFederationConfiguration() { Issuer = configurationIssuer }; |
| 205 | + tokenValidationParameters.ConfigurationManager = new MockConfigurationManager<WsFederationConfiguration>(validConfig); |
| 206 | + } |
| 207 | + |
| 208 | + return tokenValidationParameters; |
| 209 | + } |
| 210 | + } |
| 211 | +} |
| 212 | +#nullable restore |
0 commit comments