@@ -672,7 +672,7 @@ public void GetEncryptionKeys(CreateTokenTheoryData theoryData)
672672 var jwtTokenFromJsonHandlerWithKid = new JsonWebToken ( jweFromJsonHandlerWithKid ) ;
673673 var encryptionKeysFromJsonHandlerWithKid = theoryData . JsonWebTokenHandler . GetContentEncryptionKeys ( jwtTokenFromJsonHandlerWithKid , theoryData . ValidationParameters , theoryData . Configuration ) ;
674674
675- IdentityComparer . AreEqual ( encryptionKeysFromJsonHandlerWithKid , theoryData . ExpectedDecryptionKeys ) ;
675+ Assert . True ( IdentityComparer . AreEqual ( encryptionKeysFromJsonHandlerWithKid , theoryData . ExpectedDecryptionKeys ) ) ;
676676 theoryData . ExpectedException . ProcessNoException ( context ) ;
677677 }
678678 catch ( Exception ex )
@@ -696,15 +696,19 @@ public static TheoryData<CreateTokenTheoryData> SecurityTokenDecryptionTheoryDat
696696 configurationWithDecryptionKeys . TokenDecryptionKeys . Add ( KeyingMaterial . DefaultSymmetricSecurityKey_256 ) ;
697697 configurationWithDecryptionKeys . TokenDecryptionKeys . Add ( KeyingMaterial . DefaultSymmetricSecurityKey_512 ) ;
698698
699- var configurationThatThrows = CreateCustomConfigurationThatThrows ( ) ;
699+ var rsaKey = new RsaSecurityKey ( KeyingMaterial . RsaParameters_2048 ) { KeyId = "CustomRsaSecurityKey_2048" } ;
700+ var configurationThatThrows = CreateCustomConfigurationThatThrows ( rsaKey ) ;
701+
702+ var configurationWithMismatchedKeys = new OpenIdConnectConfiguration ( ) ;
703+ configurationWithMismatchedKeys . TokenDecryptionKeys . Add ( rsaKey ) ;
700704
701705 tokenHandler . InboundClaimTypeMap . Clear ( ) ;
702706 return new TheoryData < CreateTokenTheoryData >
703707 {
704- new CreateTokenTheoryData
705- {
708+ new CreateTokenTheoryData
709+ {
706710 First = true ,
707- TestId = "EncryptionKeyInConfig " ,
711+ TestId = "ValidKeyInConfig_KeysSetInConfig " ,
708712 Payload = Default . PayloadString ,
709713 TokenDescriptor = new SecurityTokenDescriptor
710714 {
@@ -720,10 +724,10 @@ public static TheoryData<CreateTokenTheoryData> SecurityTokenDecryptionTheoryDat
720724 } ,
721725 Configuration = configurationWithDecryptionKeys ,
722726 ExpectedDecryptionKeys = new List < SecurityKey > ( ) { KeyingMaterial . DefaultSymmetricSecurityKey_256 } ,
723- } ,
724- new CreateTokenTheoryData
725- {
726- TestId = "ValidEncryptionKeyInConfig " ,
727+ } ,
728+ new CreateTokenTheoryData
729+ {
730+ TestId = "ValidKeyInConfig_KeysSetInConfigAndTvp " ,
727731 Payload = Default . PayloadString ,
728732 TokenDescriptor = new SecurityTokenDescriptor
729733 {
@@ -740,10 +744,10 @@ public static TheoryData<CreateTokenTheoryData> SecurityTokenDecryptionTheoryDat
740744 } ,
741745 Configuration = configurationWithDecryptionKeys ,
742746 ExpectedDecryptionKeys = new List < SecurityKey > ( ) { KeyingMaterial . DefaultSymmetricSecurityKey_256 } ,
743- } ,
744- new CreateTokenTheoryData
745- {
746- TestId = "Valid " ,
747+ } ,
748+ new CreateTokenTheoryData
749+ {
750+ TestId = "ValidKeyInTvp_KeysSetInTvp " ,
747751 Payload = Default . PayloadString ,
748752 TokenDescriptor = new SecurityTokenDescriptor
749753 {
@@ -759,10 +763,10 @@ public static TheoryData<CreateTokenTheoryData> SecurityTokenDecryptionTheoryDat
759763 ValidIssuer = Default . Issuer
760764 } ,
761765 ExpectedDecryptionKeys = new List < SecurityKey > ( ) { KeyingMaterial . DefaultSymmetricSecurityKey_256 } ,
762- } ,
763- new CreateTokenTheoryData
764- {
765- TestId = "AlgorithmMismatch " ,
766+ } ,
767+ new CreateTokenTheoryData
768+ {
769+ TestId = "AlgorithmMismatch_ReturnsNoKeys " ,
766770 Payload = Default . PayloadString ,
767771 // There is no error, just no decryption keys are returned.
768772 ExpectedException = ExpectedException . NoExceptionExpected ,
@@ -780,10 +784,10 @@ public static TheoryData<CreateTokenTheoryData> SecurityTokenDecryptionTheoryDat
780784 ValidAudience = Default . Audience ,
781785 ValidIssuer = Default . Issuer
782786 } ,
783- } ,
784- new CreateTokenTheoryData
785- {
786- TestId = "EncryptionKeyInConfig_OneKeysThrows_SuccessfulKeyReturned " ,
787+ } ,
788+ new CreateTokenTheoryData
789+ {
790+ TestId = "ValidKeyInConfig_OneKeyThrows_SuccessfulKeyReturned " ,
787791 Payload = Default . PayloadString ,
788792 TokenDescriptor = new SecurityTokenDescriptor
789793 {
@@ -798,13 +802,53 @@ public static TheoryData<CreateTokenTheoryData> SecurityTokenDecryptionTheoryDat
798802 ValidIssuer = Default . Issuer
799803 } ,
800804 Configuration = configurationThatThrows ,
801- ExpectedDecryptionKeys = new List < SecurityKey > ( ) { KeyingMaterial . DefaultSymmetricSecurityKey_256 } ,
802- }
805+ ExpectedDecryptionKeys = new List < SecurityKey > ( ) { rsaKey } ,
806+ } ,
807+ new CreateTokenTheoryData
808+ {
809+ TestId = "KeyIdMismatch_TryAllDecryptionKeysTrue_ReturnsKey" ,
810+ Payload = Default . PayloadString ,
811+ TokenDescriptor = new SecurityTokenDescriptor
812+ {
813+ SigningCredentials = KeyingMaterial . JsonWebKeyRsa256SigningCredentials ,
814+ EncryptingCredentials = new EncryptingCredentials ( KeyingMaterial . RsaSecurityKey_2048 , SecurityAlgorithms . RsaPKCS1 , SecurityAlgorithms . Aes128CbcHmacSha256 ) ,
815+ Claims = Default . PayloadDictionary
816+ } ,
817+ ValidationParameters = new TokenValidationParameters
818+ {
819+ IssuerSigningKey = KeyingMaterial . JsonWebKeyRsa256SigningCredentials . Key ,
820+ TryAllDecryptionKeys = true ,
821+ ValidAudience = Default . Audience ,
822+ ValidIssuer = Default . Issuer
823+ } ,
824+ Configuration = configurationWithMismatchedKeys ,
825+ ExpectedDecryptionKeys = new List < SecurityKey > ( ) { rsaKey } ,
826+ } ,
827+ new CreateTokenTheoryData
828+ {
829+ TestId = "KeyIdMismatch_TryAllDecryptionKeysFalse_ReturnsNoKeys" ,
830+ Payload = Default . PayloadString ,
831+ TokenDescriptor = new SecurityTokenDescriptor
832+ {
833+ SigningCredentials = KeyingMaterial . JsonWebKeyRsa256SigningCredentials ,
834+ EncryptingCredentials = new EncryptingCredentials ( KeyingMaterial . RsaSecurityKey_2048 , SecurityAlgorithms . RsaPKCS1 , SecurityAlgorithms . Aes128CbcHmacSha256 ) ,
835+ Claims = Default . PayloadDictionary
836+ } ,
837+ ValidationParameters = new TokenValidationParameters
838+ {
839+ IssuerSigningKey = KeyingMaterial . JsonWebKeyRsa256SigningCredentials . Key ,
840+ TryAllDecryptionKeys = false ,
841+ ValidAudience = Default . Audience ,
842+ ValidIssuer = Default . Issuer
843+ } ,
844+ Configuration = configurationWithMismatchedKeys ,
845+ ExpectedDecryptionKeys = new List < SecurityKey > ( ) ,
846+ }
803847 } ;
804848 }
805849 }
806850
807- private static OpenIdConnectConfiguration CreateCustomConfigurationThatThrows ( )
851+ private static OpenIdConnectConfiguration CreateCustomConfigurationThatThrows ( SecurityKey rsaKey )
808852 {
809853 var customCryptoProviderFactory = new DerivedCryptoProviderFactory
810854 {
@@ -815,8 +859,6 @@ private static OpenIdConnectConfiguration CreateCustomConfigurationThatThrows()
815859 var sym512Hey = new SymmetricSecurityKey ( KeyingMaterial . DefaultSymmetricKeyBytes_512 ) { KeyId = "CustomSymmetricSecurityKey_512" } ;
816860 sym512Hey . CryptoProviderFactory = customCryptoProviderFactory ;
817861
818- var rsaKey = new RsaSecurityKey ( KeyingMaterial . RsaParameters_2048 ) { KeyId = "CustomRsaSecurityKey_2048" } ;
819-
820862 var configurationWithCustomCryptoProviderFactory = new OpenIdConnectConfiguration ( ) ;
821863 configurationWithCustomCryptoProviderFactory . TokenDecryptionKeys . Add ( rsaKey ) ;
822864 configurationWithCustomCryptoProviderFactory . TokenDecryptionKeys . Add ( sym512Hey ) ;
0 commit comments