-
Notifications
You must be signed in to change notification settings - Fork 432
Regression tests: Audience #2838
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
Changes from 10 commits
1296457
76e6b9b
8d2dba3
2f98fc7
c3ee1e7
cef517e
57079d9
f0ea745
dd83685
b6c4385
d518e5f
a7eff15
08a8800
33f4c7c
c47d3b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
|
|
||
| #nullable enable | ||
| namespace Microsoft.IdentityModel.Tokens | ||
| { | ||
| internal class AudienceValidationError : ValidationError | ||
| { | ||
| private string? _invalidAudience; | ||
|
|
||
| public AudienceValidationError( | ||
| MessageDetail messageDetail, | ||
| Type exceptionType, | ||
| StackFrame stackFrame, | ||
| IList<string>? invalidAudiences) | ||
| : base(messageDetail, ValidationFailureType.AudienceValidationFailed, exceptionType, stackFrame) | ||
| { | ||
| _invalidAudience = Utility.SerializeAsSingleCommaDelimitedString(invalidAudiences); | ||
| } | ||
|
|
||
| internal override void AddAdditionalInformation(ISecurityTokenException exception) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect that since we are creating derived ValidationError types, we do not need the method AddAdditionalInformation or the interface ISecurityTokenException. For example, when we create the AudienceValidationError, we call the ctor that takes the list of available audiences. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let’s keep the conversation going on that to find the best solution that can support any exception our users may decide to return. |
||
| { | ||
| if (exception is SecurityTokenInvalidAudienceException invalidAudienceException) | ||
| invalidAudienceException.InvalidAudience = _invalidAudience; | ||
| } | ||
| } | ||
| } | ||
| #nullable restore | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| #nullable enable | ||
| namespace Microsoft.IdentityModel.Tokens | ||
| { | ||
| public static partial class Validators | ||
iNinja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| internal static AlgorithmValidationDelegate SkipAlgorithmValidation = delegate ( | ||
| string algorithm, | ||
| SecurityKey securityKey, | ||
| SecurityToken securityToken, | ||
| ValidationParameters | ||
| validationParameters, | ||
| CallContext callContext) | ||
| { | ||
| return algorithm; | ||
| }; | ||
|
|
||
| internal static AudienceValidationDelegate SkipAudienceValidation = delegate ( | ||
| IList<string> audiences, | ||
| SecurityToken? securityToken, | ||
| ValidationParameters validationParameters, | ||
| CallContext callContext) | ||
| { | ||
| return "skipped"; // The audience that was validated. | ||
| }; | ||
|
|
||
| internal static IssuerValidationDelegateAsync SkipIssuerValidation = delegate ( | ||
| string issuer, | ||
| SecurityToken securityToken, | ||
| ValidationParameters validationParameters, | ||
| CallContext callContext, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| return Task.FromResult(new ValidationResult<ValidatedIssuer>( | ||
| new ValidatedIssuer(issuer, IssuerValidationSource.NotValidated))); | ||
| }; | ||
|
|
||
| internal static IssuerSigningKeyValidationDelegate SkipIssuerSigningKeyValidation = delegate ( | ||
| SecurityKey signingKey, | ||
| SecurityToken securityToken, | ||
| ValidationParameters validationParameters, | ||
| BaseConfiguration? configuration, | ||
| CallContext? callContext) | ||
| { | ||
| return new ValidatedSigningKeyLifetime( | ||
| null, // ValidFrom | ||
| null, // ValidTo | ||
| null);// ValidationTime | ||
| }; | ||
|
|
||
| internal static LifetimeValidationDelegate SkipLifetimeValidation = delegate ( | ||
| DateTime? notBefore, | ||
| DateTime? expires, | ||
| SecurityToken? securityToken, | ||
| ValidationParameters validationParameters, | ||
| CallContext callContext) | ||
| { | ||
| return new ValidatedLifetime(notBefore, expires); | ||
| }; | ||
|
|
||
| internal static SignatureValidationDelegate SkipSignatureValidation = delegate ( | ||
| SecurityToken securityToken, | ||
| ValidationParameters validationParameters, | ||
| BaseConfiguration? configuration, | ||
| CallContext? callContext) | ||
| { | ||
| // This key is not used during the validation process. It is only used to satisfy the delegate signature. | ||
| // Follow up PR will change this to remove the SecurityKey return value. | ||
| return new(result: new JsonWebKey()); | ||
| }; | ||
|
|
||
| internal static TokenReplayValidationDelegate SkipTokenReplayValidation = delegate ( | ||
| DateTime? expirationTime, | ||
| string securityToken, | ||
| ValidationParameters validationParameters, | ||
| CallContext callContext) | ||
| { | ||
| return expirationTime; | ||
| }; | ||
|
|
||
| internal static TokenTypeValidationDelegate SkipTokenTypeValidation = delegate ( | ||
| string? type, | ||
| SecurityToken? securityToken, | ||
| ValidationParameters validationParameters, | ||
| CallContext callContext) | ||
| { | ||
| return new ValidatedTokenType("skipped", 0); | ||
| }; | ||
| } | ||
| } | ||
| #nullable restore | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #nullable enable | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.IdentityModel.TestUtils; | ||
| using Microsoft.IdentityModel.Tokens; | ||
| using TokenValidators = Microsoft.IdentityModel.Tokens.Validators; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.IdentityModel.JsonWebTokens.Tests | ||
| { | ||
| public partial class JsonWebTokenHandlerValidateTokenAsyncTests | ||
iNinja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| [Theory, MemberData(nameof(ValidateTokenAsync_AudienceTestCases))] | ||
iNinja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public async Task ValidateTokenAsync_Audience(ValidateTokenAsyncAudienceTheoryData theoryData) | ||
| { | ||
| var context = TestUtilities.WriteHeader($"{this}.ValidateTokenAsync_Audience", theoryData); | ||
|
|
||
| string jwtString = CreateToken(theoryData.Audience); | ||
|
|
||
| await ValidateAndCompareResults(jwtString, theoryData, context); | ||
|
|
||
| TestUtilities.AssertFailIfErrors(context); | ||
| } | ||
|
|
||
| public static TheoryData<ValidateTokenAsyncAudienceTheoryData> ValidateTokenAsync_AudienceTestCases | ||
| { | ||
| get | ||
| { | ||
| return new TheoryData<ValidateTokenAsyncAudienceTheoryData> | ||
| { | ||
| new ValidateTokenAsyncAudienceTheoryData("Valid_AudiencesMatch") | ||
| { | ||
| Audience = Default.Audience, | ||
| TokenValidationParameters = CreateTokenValidationParameters([Default.Audience]), | ||
| ValidationParameters = CreateValidationParameters([Default.Audience]), | ||
| }, | ||
| new ValidateTokenAsyncAudienceTheoryData("Invalid_AudiencesDontMatch") | ||
iNinja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| // This scenario is the same if the token audience is an empty string or whitespace. | ||
| // As long as the token audience and the valid audience are not equal, the validation fails. | ||
| TokenValidationParameters = CreateTokenValidationParameters([Default.Audience]), | ||
| ValidationParameters = CreateValidationParameters([Default.Audience]), | ||
| Audience = "InvalidAudience", | ||
| ExpectedIsValid = false, | ||
| ExpectedException = ExpectedException.SecurityTokenInvalidAudienceException("IDX10214:"), | ||
| // ValidateTokenAsync with ValidationParameters returns a different error message to account for the | ||
| // removal of the ValidAudience property from the ValidationParameters class. | ||
| ExpectedExceptionValidationParameters = ExpectedException.SecurityTokenInvalidAudienceException("IDX10215:"), | ||
| }, | ||
| new ValidateTokenAsyncAudienceTheoryData("Valid_AudienceWithinValidAudiences") | ||
| { | ||
iNinja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Audience = Default.Audience, | ||
| TokenValidationParameters = CreateTokenValidationParameters(["ExtraAudience", Default.Audience, "AnotherAudience"]), | ||
| ValidationParameters = CreateValidationParameters(["ExtraAudience", Default.Audience, "AnotherAudience"]), | ||
| }, | ||
| new ValidateTokenAsyncAudienceTheoryData("Valid_AudienceWithSlash_IgnoreTrailingSlashTrue") | ||
| { | ||
| // Audience has a trailing slash, but IgnoreTrailingSlashWhenValidatingAudience is true. | ||
| Audience = Default.Audience + "/", | ||
| TokenValidationParameters = CreateTokenValidationParameters([Default.Audience], true), | ||
| ValidationParameters = CreateValidationParameters([Default.Audience], true), | ||
| }, | ||
| new ValidateTokenAsyncAudienceTheoryData("Invalid_AudienceWithSlash_IgnoreTrailingSlashFalse") | ||
| { | ||
| // Audience has a trailing slash and IgnoreTrailingSlashWhenValidatingAudience is false. | ||
| Audience = Default.Audience + "/", | ||
| TokenValidationParameters = CreateTokenValidationParameters([Default.Audience], false), | ||
| ValidationParameters = CreateValidationParameters([Default.Audience], false), | ||
| ExpectedIsValid = false, | ||
| ExpectedException = ExpectedException.SecurityTokenInvalidAudienceException("IDX10214:"), | ||
iNinja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ExpectedExceptionValidationParameters = ExpectedException.SecurityTokenInvalidAudienceException("IDX10215:"), | ||
| }, | ||
| new ValidateTokenAsyncAudienceTheoryData("Valid_ValidAudiencesWithSlash_IgnoreTrailingSlashTrue") | ||
| { | ||
| // ValidAudiences has a trailing slash, but IgnoreTrailingSlashWhenValidatingAudience is true. | ||
| Audience = Default.Audience, | ||
| TokenValidationParameters = CreateTokenValidationParameters([Default.Audience + "/"], true), | ||
| ValidationParameters = CreateValidationParameters([Default.Audience + "/"], true), | ||
| }, | ||
| new ValidateTokenAsyncAudienceTheoryData("Invalid_ValidAudiencesWithSlash_IgnoreTrailingSlashFalse") | ||
| { | ||
| // ValidAudiences has a trailing slash and IgnoreTrailingSlashWhenValidatingAudience is false. | ||
| Audience = Default.Audience, | ||
| TokenValidationParameters = CreateTokenValidationParameters([Default.Audience + "/"], false), | ||
| ValidationParameters = CreateValidationParameters([Default.Audience + "/"], false), | ||
| ExpectedIsValid = false, | ||
| ExpectedException = ExpectedException.SecurityTokenInvalidAudienceException("IDX10214:"), | ||
| ExpectedExceptionValidationParameters = ExpectedException.SecurityTokenInvalidAudienceException("IDX10215:"), | ||
| }, | ||
| new ValidateTokenAsyncAudienceTheoryData("Invalid_AudienceNullIsTreatedAsEmptyList") | ||
| { | ||
| // JsonWebToken.Audiences defaults to an empty list if no audiences are provided. | ||
| TokenValidationParameters = CreateTokenValidationParameters([Default.Audience]), | ||
| ValidationParameters = CreateValidationParameters([Default.Audience]), | ||
| Audience = null, | ||
| ExpectedIsValid = false, | ||
| ExpectedException = ExpectedException.SecurityTokenInvalidAudienceException("IDX10206:"), | ||
| }, | ||
| new ValidateTokenAsyncAudienceTheoryData("Invalid_ValidAudiencesIsNull") | ||
| { | ||
| TokenValidationParameters = CreateTokenValidationParameters(null), | ||
| ValidationParameters = CreateValidationParameters(null), | ||
| Audience = string.Empty, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Should this be a valid Audience while the Audiences property on TVP or VP is null? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current validation path considers the case where no valid audiences are provided to be invalid and throws. If you believe that having no valid audiences should be considered the same as skipping the validation, we can look into it and find out what the correct behaviour would be. |
||
| ExpectedIsValid = false, | ||
| // TVP path has a special case when ValidAudience is null or empty and ValidAudiences is null. | ||
| ExpectedException = ExpectedException.SecurityTokenInvalidAudienceException("IDX10208:"), | ||
FuPingFranco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // VP path has a default empty List for ValidAudiences, so it will always return IDX10206 if no audiences are provided. | ||
iNinja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ExpectedExceptionValidationParameters = ExpectedException.SecurityTokenInvalidAudienceException("IDX10206:"), | ||
iNinja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }; | ||
|
|
||
| static TokenValidationParameters CreateTokenValidationParameters( | ||
| List<string>? audiences, | ||
| bool ignoreTrailingSlashWhenValidatingAudience = false) => | ||
|
|
||
| // Only validate the audience. | ||
| new TokenValidationParameters | ||
iNinja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| ValidateAudience = true, | ||
| ValidateIssuer = false, | ||
| ValidateLifetime = false, | ||
| ValidateTokenReplay = false, | ||
| ValidateIssuerSigningKey = false, | ||
| RequireSignedTokens = false, | ||
| ValidAudiences = audiences, | ||
| IgnoreTrailingSlashWhenValidatingAudience = ignoreTrailingSlashWhenValidatingAudience, | ||
| }; | ||
|
|
||
| static ValidationParameters CreateValidationParameters( | ||
| List<string>? audiences, | ||
| bool ignoreTrailingSlashWhenValidatingAudience = false) | ||
| { | ||
| ValidationParameters validationParameters = new ValidationParameters(); | ||
| audiences?.ForEach(audience => validationParameters.ValidAudiences.Add(audience)); | ||
| validationParameters.IgnoreTrailingSlashWhenValidatingAudience = ignoreTrailingSlashWhenValidatingAudience; | ||
|
|
||
| // Skip all validations except audience | ||
| validationParameters.AlgorithmValidator = TokenValidators.SkipAlgorithmValidation; | ||
| validationParameters.IssuerValidatorAsync = TokenValidators.SkipIssuerValidation; | ||
| validationParameters.IssuerSigningKeyValidator = TokenValidators.SkipIssuerSigningKeyValidation; | ||
| validationParameters.LifetimeValidator = TokenValidators.SkipLifetimeValidation; | ||
| validationParameters.SignatureValidator = TokenValidators.SkipSignatureValidation; | ||
|
|
||
| return validationParameters; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class ValidateTokenAsyncAudienceTheoryData : ValidateTokenAsyncBaseTheoryData | ||
| { | ||
| public ValidateTokenAsyncAudienceTheoryData(string testId) : base(testId) { } | ||
|
|
||
| public string? Audience { get; internal set; } = Default.Audience; | ||
| } | ||
|
|
||
| private static string CreateToken(string? audience) | ||
| { | ||
| JsonWebTokenHandler jsonWebTokenHandler = new JsonWebTokenHandler(); | ||
|
|
||
| SecurityTokenDescriptor securityTokenDescriptor = new SecurityTokenDescriptor | ||
| { | ||
| Subject = Default.ClaimsIdentity, | ||
| Audience = audience, | ||
| }; | ||
|
|
||
| return jsonWebTokenHandler.CreateToken(securityTokenDescriptor); | ||
| } | ||
| } | ||
| } | ||
| #nullable restore | ||
Uh oh!
There was an error while loading. Please reload this page.