-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix JwtSecurityToken Missing Mapping When Creating a Token. (#2578)
* Added test for debugging. * Commit for draft PR * Fix with AppContext switch * Added test for debugging. * Commit for draft PR * Fix with AppContext switch * Clean-up commit * One more clean-up * Fixes JsonWebTokenHandler as well * Disable parallelization on test due to AppContext being a static dict * Move AppContext switch test to separate file * Update test/System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenHandlerNonParallelRunTests.cs Co-authored-by: kellyyangsong <69649063+kellyyangsong@users.noreply.github.com> * Update test/System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenHandlerTests.WithContextSwitches.cs Co-authored-by: kellyyangsong <69649063+kellyyangsong@users.noreply.github.com> * Fix blank space * Address PR feedback --------- Co-authored-by: Franco Fung <francofung@microsoft.com> Co-authored-by: kellyyangsong <69649063+kellyyangsong@users.noreply.github.com>
- Loading branch information
1 parent
ad64ace
commit 0e5648b
Showing
3 changed files
with
80 additions
and
2 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
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
66 changes: 66 additions & 0 deletions
66
...System.IdentityModel.Tokens.Jwt.Tests/JwtSecurityTokenHandlerTests.WithContextSwitches.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,66 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.IdentityModel.JsonWebTokens; | ||
using Microsoft.IdentityModel.TestUtils; | ||
using Microsoft.IdentityModel.Tokens; | ||
using Xunit; | ||
|
||
namespace System.IdentityModel.Tokens.Jwt.Tests | ||
{ | ||
[CollectionDefinition("JwtSecurityTokenHandlerTestsWithContextSwitches", DisableParallelization = true)] | ||
public class JwtSecurityTokenHandlerTestsWithContextSwitches | ||
{ | ||
[Theory] | ||
[InlineData(SecurityAlgorithms.RsaOAEP, true)] | ||
[InlineData(SecurityAlgorithms.RsaOaepKeyWrap, false)] | ||
public void JwtSecurityTokenHandler_CreateToken_AddShortFormMappingForRsaOAEP(string algorithm, bool useShortNameForRsaOaepKey) | ||
{ | ||
AppContext.SetSwitch(X509EncryptingCredentials._useShortNameForRsaOaepKey, useShortNameForRsaOaepKey); | ||
var encryptingCredentials = new X509EncryptingCredentials(Default.Certificate); | ||
JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler(); | ||
|
||
JwtSecurityToken token = CreateJwtSecurityToken(tokenHandler, encryptingCredentials); | ||
|
||
Assert.Equal(token.Header.Alg, algorithm); | ||
|
||
AppContext.SetSwitch(X509EncryptingCredentials._useShortNameForRsaOaepKey, false); | ||
} | ||
|
||
[Theory] | ||
[InlineData(SecurityAlgorithms.RsaOAEP, true)] | ||
[InlineData(SecurityAlgorithms.RsaOaepKeyWrap, false)] | ||
public void JsonWebTokenHandler_CreateToken_AddShortFormMappingForRsaOAEP(string algorithm, bool useShortNameForRsaOaepKey) | ||
{ | ||
AppContext.SetSwitch(X509EncryptingCredentials._useShortNameForRsaOaepKey, useShortNameForRsaOaepKey); | ||
var encryptingCredentials = new X509EncryptingCredentials(Default.Certificate); | ||
JsonWebTokenHandler tokenHandler = new JsonWebTokenHandler(); | ||
|
||
JsonWebToken jsonToken = new JsonWebToken(CreateJwtSecurityTokenAsString(tokenHandler, encryptingCredentials)); | ||
|
||
Assert.Equal(jsonToken.Alg, algorithm); | ||
|
||
AppContext.SetSwitch(X509EncryptingCredentials._useShortNameForRsaOaepKey, false); | ||
} | ||
|
||
private JwtSecurityToken CreateJwtSecurityToken(JwtSecurityTokenHandler tokenHandler, X509EncryptingCredentials encryptingCredentials) | ||
{ | ||
return tokenHandler.CreateJwtSecurityToken(CreateTokenDescriptor(encryptingCredentials)); | ||
} | ||
|
||
private string CreateJwtSecurityTokenAsString(JsonWebTokenHandler tokenHandler, X509EncryptingCredentials encryptingCredentials) | ||
{ | ||
return tokenHandler.CreateToken(CreateTokenDescriptor(encryptingCredentials)); | ||
} | ||
|
||
private SecurityTokenDescriptor CreateTokenDescriptor(X509EncryptingCredentials encryptingCredentials) | ||
{ | ||
return new SecurityTokenDescriptor | ||
{ | ||
Issuer = Default.Issuer, | ||
SigningCredentials = Default.AsymmetricSigningCredentials, | ||
EncryptingCredentials = encryptingCredentials, | ||
}; | ||
} | ||
} | ||
} |