-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Renamed CreateToken methods in audience and lifetime regression tests * Added custom ValidationError class for issuer errors. Updated IssuerValidationDelegateAsync to use it. * Added JWT issuer regression tests * Update test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenHandler.ValidateTokenAsyncTests.Issuer.cs Co-authored-by: kellyyangsong <[email protected]> * Added IssuerValidationError to InternalAPI.Unshipped. Made constructor internal for the time being * Updated exception creation in IssuerValidationError * Adjusted unshipped API contents with the IDE suggestions --------- Co-authored-by: kellyyangsong <[email protected]>
- Loading branch information
1 parent
3dab668
commit 876def8
Showing
11 changed files
with
219 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
4 changes: 3 additions & 1 deletion
4
src/Microsoft.IdentityModel.Tokens/PublicAPI/net462/InternalAPI.Unshipped.txt
This file contains 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
4 changes: 3 additions & 1 deletion
4
src/Microsoft.IdentityModel.Tokens/PublicAPI/net6.0/InternalAPI.Unshipped.txt
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
static readonly Microsoft.IdentityModel.Tokens.Json.JsonWebKeySerializer.JsonWebKeyParameterNamesUpperCase -> System.Collections.Generic.HashSet<string> | ||
Microsoft.IdentityModel.Tokens.IssuerValidationError.IssuerValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, string invalidIssuer) -> void | ||
override Microsoft.IdentityModel.Tokens.IssuerValidationError.GetException() -> System.Exception | ||
static readonly Microsoft.IdentityModel.Tokens.Json.JsonWebKeySerializer.JsonWebKeyParameterNamesUpperCase -> System.Collections.Generic.HashSet<string> | ||
Microsoft.IdentityModel.Tokens.EcdhKeyExchangeProvider.GetEncryptionAlgorithm() -> string | ||
Microsoft.IdentityModel.Tokens.SignUsingSpanDelegate |
4 changes: 3 additions & 1 deletion
4
src/Microsoft.IdentityModel.Tokens/PublicAPI/net8.0/InternalAPI.Unshipped.txt
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
static readonly Microsoft.IdentityModel.Tokens.Json.JsonWebKeySerializer.JsonWebKeyParameterNamesUpperCase -> System.Collections.Frozen.FrozenSet<string> | ||
Microsoft.IdentityModel.Tokens.IssuerValidationError.IssuerValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, string invalidIssuer) -> void | ||
override Microsoft.IdentityModel.Tokens.IssuerValidationError.GetException() -> System.Exception | ||
static readonly Microsoft.IdentityModel.Tokens.Json.JsonWebKeySerializer.JsonWebKeyParameterNamesUpperCase -> System.Collections.Frozen.FrozenSet<string> | ||
Microsoft.IdentityModel.Tokens.EcdhKeyExchangeProvider.GetEncryptionAlgorithm() -> string | ||
Microsoft.IdentityModel.Tokens.SignUsingSpanDelegate |
4 changes: 3 additions & 1 deletion
4
src/Microsoft.IdentityModel.Tokens/PublicAPI/net9.0/InternalAPI.Unshipped.txt
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
static readonly Microsoft.IdentityModel.Tokens.Json.JsonWebKeySerializer.JsonWebKeyParameterNamesUpperCase -> System.Collections.Frozen.FrozenSet<string> | ||
Microsoft.IdentityModel.Tokens.IssuerValidationError.IssuerValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, string invalidIssuer) -> void | ||
override Microsoft.IdentityModel.Tokens.IssuerValidationError.GetException() -> System.Exception | ||
static readonly Microsoft.IdentityModel.Tokens.Json.JsonWebKeySerializer.JsonWebKeyParameterNamesUpperCase -> System.Collections.Frozen.FrozenSet<string> | ||
Microsoft.IdentityModel.Tokens.EcdhKeyExchangeProvider.GetEncryptionAlgorithm() -> string | ||
Microsoft.IdentityModel.Tokens.SignUsingSpanDelegate |
4 changes: 3 additions & 1 deletion
4
src/Microsoft.IdentityModel.Tokens/PublicAPI/netstandard2.0/InternalAPI.Unshipped.txt
This file contains 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 |
---|---|---|
@@ -1 +1,3 @@ | ||
static readonly Microsoft.IdentityModel.Tokens.Json.JsonWebKeySerializer.JsonWebKeyParameterNamesUpperCase -> System.Collections.Generic.HashSet<string> | ||
Microsoft.IdentityModel.Tokens.IssuerValidationError.IssuerValidationError(Microsoft.IdentityModel.Tokens.MessageDetail messageDetail, System.Type exceptionType, System.Diagnostics.StackFrame stackFrame, string invalidIssuer) -> void | ||
override Microsoft.IdentityModel.Tokens.IssuerValidationError.GetException() -> System.Exception | ||
static readonly Microsoft.IdentityModel.Tokens.Json.JsonWebKeySerializer.JsonWebKeyParameterNamesUpperCase -> System.Collections.Generic.HashSet<string> |
40 changes: 40 additions & 0 deletions
40
src/Microsoft.IdentityModel.Tokens/Validation/Results/Details/IssuerValidationError.cs
This file contains 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,40 @@ | ||
// 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; | ||
|
||
internal IssuerValidationError( | ||
MessageDetail messageDetail, | ||
Type exceptionType, | ||
StackFrame stackFrame, | ||
string? invalidIssuer) | ||
: base(messageDetail, ValidationFailureType.IssuerValidationFailed, exceptionType, stackFrame) | ||
{ | ||
_invalidIssuer = invalidIssuer; | ||
} | ||
|
||
public override Exception GetException() | ||
{ | ||
if (ExceptionType == typeof(SecurityTokenInvalidIssuerException)) | ||
{ | ||
SecurityTokenInvalidIssuerException exception = new(MessageDetail.Message, InnerException) | ||
{ | ||
InvalidIssuer = _invalidIssuer | ||
}; | ||
|
||
return exception; | ||
} | ||
|
||
return base.GetException(); | ||
} | ||
} | ||
} | ||
#nullable restore |
This file contains 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 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 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), DisableDiscoveryEnumeration = true)] | ||
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 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