-
Notifications
You must be signed in to change notification settings - Fork 432
Regression tests: Issuer #2868
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
Regression tests: Issuer #2868
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e20bc67
Renamed CreateToken methods in audience and lifetime regression tests
iNinja 0c4f236
Added custom ValidationError class for issuer errors. Updated IssuerV…
iNinja 15fc33c
Added JWT issuer regression tests
iNinja 2ce7d30
Update test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenH…
iNinja 02db527
Merge branch 'dev' into iinglese/regression-tests-issuer
iNinja e6adf5a
Added IssuerValidationError to InternalAPI.Unshipped. Made constructo…
iNinja 7531d9e
Updated exception creation in IssuerValidationError
iNinja 06d8afb
Merge branch 'dev' into iinglese/regression-tests-issuer
iNinja b6747af
Merge branch 'dev' into iinglese/regression-tests-issuer
iNinja 276b7da
Adjusted unshipped API contents with the IDE suggestions
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
31 changes: 31 additions & 0 deletions
31
src/Microsoft.IdentityModel.Tokens/Validation/Results/Details/IssuerValidationError.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,31 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Diagnostics; | ||
|
|
||
| #nullable enable | ||
| namespace Microsoft.IdentityModel.Tokens | ||
| { | ||
| internal class IssuerValidationError : ValidationError | ||
| { | ||
| private string? _invalidIssuer; | ||
|
|
||
| public IssuerValidationError( | ||
| MessageDetail messageDetail, | ||
| Type exceptionType, | ||
| StackFrame stackFrame, | ||
| string? invalidIssuer) | ||
| : base(messageDetail, ValidationFailureType.IssuerValidationFailed, exceptionType, stackFrame) | ||
| { | ||
| _invalidIssuer = invalidIssuer; | ||
| } | ||
|
|
||
| internal override void AddAdditionalInformation(ISecurityTokenException exception) | ||
| { | ||
| if (exception is SecurityTokenInvalidIssuerException invalidIssuerException) | ||
| invalidIssuerException.InvalidIssuer = _invalidIssuer; | ||
| } | ||
| } | ||
| } | ||
| #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
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
149 changes: 149 additions & 0 deletions
149
...t.IdentityModel.JsonWebTokens.Tests/JsonWebTokenHandler.ValidateTokenAsyncTests.Issuer.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,149 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #nullable enable | ||
| using System.Threading.Tasks; | ||
| using Microsoft.IdentityModel.Protocols.OpenIdConnect; | ||
| using Microsoft.IdentityModel.TestUtils; | ||
| using Microsoft.IdentityModel.Tokens; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.IdentityModel.JsonWebTokens.Tests | ||
| { | ||
| public partial class JsonWebTokenHandlerValidateTokenAsyncTests | ||
| { | ||
| [Theory, MemberData(nameof(ValidateTokenAsync_IssuerTestCases))] | ||
| public async Task ValidateTokenAsync_Issuer(ValidateTokenAsyncIssuerTheoryData theoryData) | ||
| { | ||
| var context = TestUtilities.WriteHeader($"{this}.ValidateTokenAsync_Issuer", theoryData); | ||
|
|
||
| string jwtString = CreateTokenWithIssuer(theoryData.TokenIssuer); | ||
|
|
||
| await ValidateAndCompareResults(jwtString, theoryData, context); | ||
|
|
||
| TestUtilities.AssertFailIfErrors(context); | ||
| } | ||
|
|
||
| public static TheoryData<ValidateTokenAsyncIssuerTheoryData> ValidateTokenAsync_IssuerTestCases | ||
| { | ||
| get | ||
| { | ||
| return new TheoryData<ValidateTokenAsyncIssuerTheoryData> | ||
| { | ||
| new ValidateTokenAsyncIssuerTheoryData("Valid_IssuerIsValidIssuer") | ||
| { | ||
| TokenIssuer = Default.Issuer, | ||
| TokenValidationParameters = CreateTokenValidationParameters(validIssuer: Default.Issuer), | ||
| ValidationParameters = CreateValidationParameters(validIssuer: Default.Issuer), | ||
| }, | ||
| new ValidateTokenAsyncIssuerTheoryData("Valid_IssuerIsConfigurationIssuer") | ||
| { | ||
| TokenIssuer = Default.Issuer, | ||
| TokenValidationParameters = CreateTokenValidationParameters(configurationIssuer: Default.Issuer), | ||
| ValidationParameters = CreateValidationParameters(configurationIssuer: Default.Issuer), | ||
| }, | ||
| new ValidateTokenAsyncIssuerTheoryData("Invalid_IssuerIsNotValid") | ||
| { | ||
| TokenIssuer = "InvalidIssuer", | ||
| TokenValidationParameters = CreateTokenValidationParameters(validIssuer: Default.Issuer), | ||
| ValidationParameters = CreateValidationParameters(validIssuer: Default.Issuer), | ||
| ExpectedIsValid = false, | ||
| ExpectedException = new ExpectedException(typeof(SecurityTokenInvalidIssuerException), "IDX10205:"), | ||
| ExpectedExceptionValidationParameters = new ExpectedException(typeof(SecurityTokenInvalidIssuerException), "IDX10212:"), | ||
| }, | ||
| new ValidateTokenAsyncIssuerTheoryData("Invalid_IssuerIsNull") | ||
| { | ||
| TokenIssuer = null, | ||
| TokenValidationParameters = CreateTokenValidationParameters(validIssuer: Default.Issuer), | ||
| ValidationParameters = CreateValidationParameters(validIssuer: Default.Issuer), | ||
| ExpectedIsValid = false, | ||
| ExpectedException = new ExpectedException(typeof(SecurityTokenInvalidIssuerException), "IDX10211:"), | ||
| }, | ||
| new ValidateTokenAsyncIssuerTheoryData("Invalid_IssuerIsEmpty") | ||
| { | ||
| TokenIssuer = string.Empty, | ||
| TokenValidationParameters = CreateTokenValidationParameters(validIssuer: Default.Issuer), | ||
| ValidationParameters = CreateValidationParameters(validIssuer: Default.Issuer), | ||
| ExpectedIsValid = false, | ||
| ExpectedException = new ExpectedException(typeof(SecurityTokenInvalidIssuerException), "IDX10211:"), | ||
| }, | ||
| new ValidateTokenAsyncIssuerTheoryData("Invalid_NoValidIssuersProvided") | ||
| { | ||
| TokenIssuer = Default.Issuer, | ||
| TokenValidationParameters = CreateTokenValidationParameters(), | ||
| ValidationParameters = CreateValidationParameters(), | ||
| ExpectedIsValid = false, | ||
| ExpectedException = new ExpectedException(typeof(SecurityTokenInvalidIssuerException), "IDX10204:"), | ||
| ExpectedExceptionValidationParameters = new ExpectedException(typeof(SecurityTokenInvalidIssuerException), "IDX10211:"), | ||
| }, | ||
| }; | ||
|
|
||
| static TokenValidationParameters CreateTokenValidationParameters( | ||
| string? validIssuer = null, string? configurationIssuer = null) | ||
| { | ||
| var tokenValidationParameters = new TokenValidationParameters | ||
| { | ||
| ValidateAudience = true, | ||
| ValidateIssuer = true, | ||
| ValidateLifetime = true, | ||
| ValidateTokenReplay = true, | ||
| ValidateIssuerSigningKey = true, | ||
| IssuerSigningKey = Default.AsymmetricSigningKey, | ||
| ValidAudiences = [Default.Audience], | ||
| ValidIssuer = validIssuer | ||
| }; | ||
|
|
||
| if (configurationIssuer is not null) | ||
| { | ||
| var validConfig = new OpenIdConnectConfiguration() { Issuer = configurationIssuer }; | ||
| tokenValidationParameters.ConfigurationManager = new MockConfigurationManager<OpenIdConnectConfiguration>(validConfig); | ||
| } | ||
|
|
||
| return tokenValidationParameters; | ||
| } | ||
|
|
||
| static ValidationParameters CreateValidationParameters( | ||
| string? validIssuer = null, string? configurationIssuer = null) | ||
| { | ||
| ValidationParameters validationParameters = new ValidationParameters(); | ||
| validationParameters.ValidAudiences.Add(Default.Audience); | ||
| validationParameters.IssuerSigningKeys.Add(Default.AsymmetricSigningKey); | ||
|
|
||
| if (configurationIssuer is not null) | ||
| { | ||
| var validConfig = new OpenIdConnectConfiguration() { Issuer = configurationIssuer }; | ||
| validationParameters.ConfigurationManager = new MockConfigurationManager<OpenIdConnectConfiguration>(validConfig); | ||
| } | ||
|
|
||
| if (validIssuer is not null) | ||
| validationParameters.ValidIssuers.Add(validIssuer); | ||
|
|
||
| return validationParameters; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class ValidateTokenAsyncIssuerTheoryData : ValidateTokenAsyncBaseTheoryData | ||
| { | ||
| public ValidateTokenAsyncIssuerTheoryData(string testId) : base(testId) { } | ||
|
|
||
| public string? TokenIssuer { get; set; } | ||
| } | ||
|
|
||
| private static string CreateTokenWithIssuer(string? issuer) | ||
| { | ||
| JsonWebTokenHandler jsonWebTokenHandler = new JsonWebTokenHandler(); | ||
|
|
||
| SecurityTokenDescriptor securityTokenDescriptor = new SecurityTokenDescriptor | ||
| { | ||
| Subject = Default.ClaimsIdentity, | ||
| SigningCredentials = Default.AsymmetricSigningCredentials, | ||
| Audience = Default.Audience, | ||
| Issuer = issuer, | ||
| }; | ||
|
|
||
| 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.