Skip to content
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

Fix JwtSecurityToken Missing Mapping When Creating a Token. #2578

Merged
merged 16 commits into from
May 20, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class JwtSecurityTokenHandler : SecurityTokenHandler
private Dictionary<string, string> _outboundAlgorithmMap = null;
private static string _shortClaimType = _namespace + "/ShortTypeName";
private bool _mapInboundClaims = DefaultMapInboundClaims;
internal const string _enableRsaOaepMappingSwitch = "Switch.Microsoft.IdentityModel.EnableRsaOaepShortFormMapping";
FuPingFranco marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Default claim type mapping for inbound claims.
Expand Down Expand Up @@ -70,8 +71,16 @@ public class JwtSecurityTokenHandler : SecurityTokenHandler
{ SecurityAlgorithms.RsaSha256Signature, SecurityAlgorithms.RsaSha256 },
{ SecurityAlgorithms.RsaSha384Signature, SecurityAlgorithms.RsaSha384 },
{ SecurityAlgorithms.RsaSha512Signature, SecurityAlgorithms.RsaSha512 },
{ SecurityAlgorithms.RsaOaepKeyWrap, GetRsaOaepMapping() },
FuPingFranco marked this conversation as resolved.
Show resolved Hide resolved
};

private static string GetRsaOaepMapping()
{
bool useRsaOaepMapping = AppContext.TryGetSwitch(_enableRsaOaepMappingSwitch, out bool isEnabled) && isEnabled;

return useRsaOaepMapping ? SecurityAlgorithms.RsaOAEP : SecurityAlgorithms.RsaOaepKeyWrap;
}

/// <summary>
/// Static initializer for a new object. Static initializers run before the first instance of the type is created.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@
Assert.NotNull(aTypeClaims.SingleOrDefault(c => c.Value == value));
}

[Fact]
public void JwtSecurityTokenHandler_CreateToken_AddShortFormMappingForRsaOAEP()
{
AppContext.SetSwitch(JwtSecurityTokenHandler._enableRsaOaepMappingSwitch, true); //Set to false to test the default behavior and adjust the expected values accordingly.
FuPingFranco marked this conversation as resolved.
Show resolved Hide resolved
FuPingFranco marked this conversation as resolved.
Show resolved Hide resolved
var encryptingCredentials = new X509EncryptingCredentials(Default.Certificate);
SecurityTokenDescriptor tokenDescriptor = new SecurityTokenDescriptor
{
Issuer = Default.Issuer,
IssuedAt = DateTime.UtcNow.Subtract(new TimeSpan(1, 0, 0)),
Subject = new ClaimsIdentity(Default.PayloadJsonClaims),
NotBefore = DateTime.UtcNow.Subtract(new TimeSpan(1, 0, 0)),
Expires = DateTime.UtcNow.Subtract(new TimeSpan(0, 10, 0)),
SigningCredentials = Default.AsymmetricSigningCredentials,
EncryptingCredentials = encryptingCredentials,
TokenType = "JWE"
};

JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
JwtSecurityToken token = tokenHandler.CreateJwtSecurityToken(tokenDescriptor);

Assert.NotNull(token);
Assert.NotEqual(token.Header.Alg, encryptingCredentials.Alg);

Check failure on line 79 in test/System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenHandlerTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

System.IdentityModel.Tokens.Jwt.Tests.JwtSecurityTokenHandlerTests.JwtSecurityTokenHandler_CreateToken_AddShortFormMappingForRsaOAEP() Assert.NotEqual() Failure

Check failure on line 79 in test/System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenHandlerTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Expected: Not "http://www.w3.org/2001/04/xmlenc#rsa-oaep"

Check failure on line 79 in test/System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenHandlerTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Actual: "http://www.w3.org/2001/04/xmlenc#rsa-oaep"

Check failure on line 79 in test/System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenHandlerTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

System.IdentityModel.Tokens.Jwt.Tests.JwtSecurityTokenHandlerTests.JwtSecurityTokenHandler_CreateToken_AddShortFormMappingForRsaOAEP() Assert.NotEqual() Failure

Check failure on line 79 in test/System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenHandlerTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Expected: Not "http://www.w3.org/2001/04/xmlenc#rsa-oaep"

Check failure on line 79 in test/System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenHandlerTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Actual: "http://www.w3.org/2001/04/xmlenc#rsa-oaep"

Check failure on line 79 in test/System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenHandlerTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

System.IdentityModel.Tokens.Jwt.Tests.JwtSecurityTokenHandlerTests.JwtSecurityTokenHandler_CreateToken_AddShortFormMappingForRsaOAEP() Assert.NotEqual() Failure

Check failure on line 79 in test/System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenHandlerTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Expected: Not "http://www.w3.org/2001/04/xmlenc#rsa-oaep"

Check failure on line 79 in test/System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenHandlerTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

Actual: "http://www.w3.org/2001/04/xmlenc#rsa-oaep"

Check failure on line 79 in test/System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenHandlerTests.cs

View workflow job for this annotation

GitHub Actions / Wilson GitHub Action Test

System.IdentityModel.Tokens.Jwt.Tests.JwtSecurityTokenHandlerTests.JwtSecurityTokenHandler_CreateToken_AddShortFormMappingForRsaOAEP() Assert.NotEqual() Failure
Assert.Equal(token.Header.Alg, SecurityAlgorithms.RsaOAEP);
}
FuPingFranco marked this conversation as resolved.
Show resolved Hide resolved

[Theory, MemberData(nameof(CreateJWEWithPayloadStringTheoryData))]
public void CreateJWEWithAdditionalHeaderClaims(CreateTokenTheoryData theoryData)
{
Expand Down
Loading