Skip to content

Commit

Permalink
updated JwtSecurityTokenHandler, reverted tests and removed method.
Browse files Browse the repository at this point in the history
  • Loading branch information
HP712 committed Jul 15, 2024
1 parent ae67205 commit eb6526a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 45 deletions.
40 changes: 0 additions & 40 deletions src/Microsoft.IdentityModel.Tokens/ClaimsIdentityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,5 @@ internal static ClaimsIdentity Create(string authenticationType, string nameType
SecurityToken = securityToken,
};
}

internal static ClaimsIdentity Create(SecurityToken securityToken, TokenValidationParameters validationParameters, string issuer)
{
ClaimsIdentity claimsIdentity = validationParameters.CreateClaimsIdentity(securityToken, issuer);

// Set the SecurityToken in cases where derived TokenValidationParameters created a CaseSensitiveClaimsIdentity.
if (claimsIdentity is CaseSensitiveClaimsIdentity caseSensitiveClaimsIdentity && caseSensitiveClaimsIdentity.SecurityToken == null)
{
caseSensitiveClaimsIdentity.SecurityToken = securityToken;
}
else if (claimsIdentity is not CaseSensitiveClaimsIdentity && !AppContextSwitches.UseClaimsIdentityType())
{
claimsIdentity = new CaseSensitiveClaimsIdentity(claimsIdentity)
{
SecurityToken = securityToken,
};
}

return claimsIdentity;
}

internal static ClaimsIdentity Create(TokenHandler tokenHandler, SecurityToken securityToken, TokenValidationParameters validationParameters, string issuer)
{
ClaimsIdentity claimsIdentity = tokenHandler.CreateClaimsIdentityInternal(securityToken, validationParameters, issuer);

// Set the SecurityToken in cases where derived TokenHandler created a CaseSensitiveClaimsIdentity.
if (claimsIdentity is CaseSensitiveClaimsIdentity caseSensitiveClaimsIdentity && caseSensitiveClaimsIdentity.SecurityToken == null)
{
caseSensitiveClaimsIdentity.SecurityToken = securityToken;
}
else if (claimsIdentity is not CaseSensitiveClaimsIdentity && !AppContextSwitches.UseClaimsIdentityType())
{
claimsIdentity = new CaseSensitiveClaimsIdentity(claimsIdentity)
{
SecurityToken = securityToken,
};
}

return claimsIdentity;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ protected virtual ClaimsIdentity CreateClaimsIdentity(JwtSecurityToken jwtToken,

private ClaimsIdentity CreateClaimsIdentityWithMapping(JwtSecurityToken jwtToken, string actualIssuer, TokenValidationParameters validationParameters)
{
ClaimsIdentity identity = ClaimsIdentityFactory.Create(jwtToken, validationParameters, actualIssuer);
ClaimsIdentity identity = validationParameters.CreateClaimsIdentity(jwtToken, actualIssuer);
foreach (Claim jwtClaim in jwtToken.Claims)
{
if (_inboundClaimFilter.Contains(jwtClaim.Type))
Expand Down Expand Up @@ -1573,7 +1573,7 @@ private ClaimsIdentity CreateClaimsIdentityWithMapping(JwtSecurityToken jwtToken

private ClaimsIdentity CreateClaimsIdentityWithoutMapping(JwtSecurityToken jwtToken, string actualIssuer, TokenValidationParameters validationParameters)
{
ClaimsIdentity identity = ClaimsIdentityFactory.Create(jwtToken, validationParameters, actualIssuer);
ClaimsIdentity identity = validationParameters.CreateClaimsIdentity(jwtToken, actualIssuer);
foreach (Claim jwtClaim in jwtToken.Claims)
{
if (_inboundClaimFilter.Contains(jwtClaim.Type))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Create_FromTokenValidationParameters_ReturnsCorrectClaimsIdentity(bo
tokenValidationParameters.NameClaimType = "custom-name";
tokenValidationParameters.RoleClaimType = "custom-role";

var actualClaimsIdentity = ClaimsIdentityFactory.Create(jsonWebToken, tokenValidationParameters, Default.Issuer);
var actualClaimsIdentity = tokenValidationParameters.CreateClaimsIdentity(jsonWebToken, Default.Issuer);

Assert.Equal(tokenValidationParameters.AuthenticationType, actualClaimsIdentity.AuthenticationType);
Assert.Equal(tokenValidationParameters.NameClaimType, actualClaimsIdentity.NameClaimType);
Expand Down Expand Up @@ -54,7 +54,7 @@ public void Create_FromDerivedTokenValidationParameters_HonorsSetSecurityToken()
tokenValidationParameters.NameClaimType = "custom-name";
tokenValidationParameters.RoleClaimType = "custom-role";

var actualClaimsIdentity = ClaimsIdentityFactory.Create(jsonWebToken, tokenValidationParameters, Default.Issuer);
var actualClaimsIdentity = tokenValidationParameters.CreateClaimsIdentity(jsonWebToken, Default.Issuer);

// The SecurityToken set in derived TokenValidationParameters is honored.
Assert.IsType<CaseSensitiveClaimsIdentity>(actualClaimsIdentity);
Expand All @@ -80,7 +80,7 @@ public void Create_FromDerivedTokenValidationParameters_ReturnsCorrectClaimsIden
tokenValidationParameters.NameClaimType = "custom-name";
tokenValidationParameters.RoleClaimType = "custom-role";

var actualClaimsIdentity = ClaimsIdentityFactory.Create(jsonWebToken, tokenValidationParameters, Default.Issuer);
var actualClaimsIdentity = tokenValidationParameters.CreateClaimsIdentity(jsonWebToken, Default.Issuer);

Assert.IsType<CaseSensitiveClaimsIdentity>(actualClaimsIdentity);

Check failure on line 85 in test/Microsoft.IdentityModel.Tokens.Tests/ClaimsIdentityFactoryTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Microsoft.IdentityModel.Tokens.Tests.ClaimsIdentityFactoryTests.Create_FromDerivedTokenValidationParameters_ReturnsCorrectClaimsIdentity(tvpReturnsCaseSensitiveClaimsIdentityWithoutToken: False): Microsoft.IdentityModel.Tokens.Tests.ClaimsIdentityFactoryTests.Create_FromDerivedTokenValidationParameters_ReturnsCorrectClaimsIdentity(Boolean tvpReturnsCaseSensitiveClaimsIdentityWithoutToken) Assert.IsType() Failure

Check failure on line 85 in test/Microsoft.IdentityModel.Tokens.Tests/ClaimsIdentityFactoryTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Expected: Microsoft.IdentityModel.Tokens.CaseSensitiveClaimsIdentity

Check failure on line 85 in test/Microsoft.IdentityModel.Tokens.Tests/ClaimsIdentityFactoryTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Actual: System.Security.Claims.ClaimsIdentity

Check failure on line 85 in test/Microsoft.IdentityModel.Tokens.Tests/ClaimsIdentityFactoryTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Microsoft.IdentityModel.Tokens.Tests.ClaimsIdentityFactoryTests.Create_FromDerivedTokenValidationParameters_ReturnsCorrectClaimsIdentity(tvpReturnsCaseSensitiveClaimsIdentityWithoutToken: False): Microsoft.IdentityModel.Tokens.Tests.ClaimsIdentityFactoryTests.Create_FromDerivedTokenValidationParameters_ReturnsCorrectClaimsIdentity(Boolean tvpReturnsCaseSensitiveClaimsIdentityWithoutToken) Assert.IsType() Failure

Check failure on line 85 in test/Microsoft.IdentityModel.Tokens.Tests/ClaimsIdentityFactoryTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Expected: Microsoft.IdentityModel.Tokens.CaseSensitiveClaimsIdentity

Check failure on line 85 in test/Microsoft.IdentityModel.Tokens.Tests/ClaimsIdentityFactoryTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Actual: System.Security.Claims.ClaimsIdentity

Check failure on line 85 in test/Microsoft.IdentityModel.Tokens.Tests/ClaimsIdentityFactoryTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Microsoft.IdentityModel.Tokens.Tests.ClaimsIdentityFactoryTests.Create_FromDerivedTokenValidationParameters_ReturnsCorrectClaimsIdentity(tvpReturnsCaseSensitiveClaimsIdentityWithoutToken: False): Microsoft.IdentityModel.Tokens.Tests.ClaimsIdentityFactoryTests.Create_FromDerivedTokenValidationParameters_ReturnsCorrectClaimsIdentity(Boolean tvpReturnsCaseSensitiveClaimsIdentityWithoutToken) Assert.IsType() Failure

Expand Down

0 comments on commit eb6526a

Please sign in to comment.