1111using Microsoft . IdentityModel . Protocols . OpenIdConnect ;
1212using Microsoft . IdentityModel . TestUtils ;
1313using Microsoft . IdentityModel . Tokens ;
14+ using Microsoft . IdentityModel . Tokens . Saml2 ;
1415using Xunit ;
1516
1617#pragma warning disable CS3016 // Arrays as attribute arguments is not CLS-compliant
1718
1819namespace Microsoft . IdentityModel . Validators . Tests
1920{
21+ // Serialize as one of the tests depends on static state (app context)
22+ [ Collection ( nameof ( AadSigningKeyIssuerValidatorTests ) ) ]
2023 public class AadSigningKeyIssuerValidatorTests
2124 {
2225 [ Theory , MemberData ( nameof ( EnableAadSigningKeyIssuerValidationTestCases ) ) ]
@@ -167,6 +170,7 @@ public void ValidateIssuerSigningKeyTests(AadSigningKeyIssuerTheoryData theoryDa
167170
168171 try
169172 {
173+ theoryData . SetupAction ? . Invoke ( ) ;
170174 var result = AadTokenValidationParametersExtension . ValidateIssuerSigningKey ( theoryData . SecurityKey , theoryData . SecurityToken , theoryData . OpenIdConnectConfiguration ) ;
171175 theoryData . ExpectedException . ProcessNoException ( context ) ;
172176 Assert . True ( result ) ;
@@ -175,6 +179,10 @@ public void ValidateIssuerSigningKeyTests(AadSigningKeyIssuerTheoryData theoryDa
175179 {
176180 theoryData . ExpectedException . ProcessException ( ex , context ) ;
177181 }
182+ finally
183+ {
184+ theoryData . TearDownAction ? . Invoke ( ) ;
185+ }
178186
179187 TestUtilities . AssertFailIfErrors ( context ) ;
180188 }
@@ -294,7 +302,17 @@ public static TheoryData<AadSigningKeyIssuerTheoryData> ValidateIssuerSigningKey
294302 TestId = "MissingTenantIdClaimInToken" ,
295303 SecurityKey = KeyingMaterial . JsonWebKeyP256 ,
296304 SecurityToken = new JwtSecurityToken ( ) ,
297- OpenIdConnectConfiguration = mockConfiguration
305+ OpenIdConnectConfiguration = mockConfiguration ,
306+ ExpectedException = ExpectedException . SecurityTokenInvalidIssuerException ( "IDX40009" )
307+ } ) ;
308+
309+ theoryData . Add ( new AadSigningKeyIssuerTheoryData
310+ {
311+ TestId = "WrongSecurityKeyType" ,
312+ SecurityKey = KeyingMaterial . JsonWebKeyP256 ,
313+ SecurityToken = new Saml2SecurityToken ( new Saml2Assertion ( new Saml2NameIdentifier ( "nameIdentifier" ) ) ) ,
314+ OpenIdConnectConfiguration = mockConfiguration ,
315+ ExpectedException = ExpectedException . SecurityTokenInvalidIssuerException ( "IDX40010" )
298316 } ) ;
299317
300318 theoryData . Add ( new AadSigningKeyIssuerTheoryData
@@ -321,6 +339,94 @@ public static TheoryData<AadSigningKeyIssuerTheoryData> ValidateIssuerSigningKey
321339 ExpectedException = ExpectedException . SecurityTokenInvalidIssuerException ( "IDX40004" )
322340 } ) ;
323341
342+ theoryData . Add ( new AadSigningKeyIssuerTheoryData
343+ {
344+ TestId = "Doesnt_Fail_With_Switch" ,
345+ SecurityKey = KeyingMaterial . JsonWebKeyP256 ,
346+ SecurityToken = new JwtSecurityToken ( ) ,
347+ OpenIdConnectConfiguration = mockConfiguration ,
348+ SetupAction = ( ) => AppContext . SetSwitch ( AadTokenValidationParametersExtension . DontFailOnMissingTidSwitch , true ) ,
349+ TearDownAction = ( ) => AppContext . SetSwitch ( AadTokenValidationParametersExtension . DontFailOnMissingTidSwitch , false )
350+ } ) ;
351+
352+ theoryData . Add ( new AadSigningKeyIssuerTheoryData
353+ {
354+ TestId = "Fail_With_Switch_False" ,
355+ SecurityKey = KeyingMaterial . JsonWebKeyP256 ,
356+ SecurityToken = new JwtSecurityToken ( ) ,
357+ OpenIdConnectConfiguration = mockConfiguration ,
358+ ExpectedException = ExpectedException . SecurityTokenInvalidIssuerException ( "IDX40009" ) ,
359+ SetupAction = ( ) => AppContext . SetSwitch ( AadTokenValidationParametersExtension . DontFailOnMissingTidSwitch , false ) ,
360+ TearDownAction = ( ) => AppContext . SetSwitch ( AadTokenValidationParametersExtension . DontFailOnMissingTidSwitch , isEnabled : false )
361+ } ) ;
362+
363+ theoryData . Add ( new AadSigningKeyIssuerTheoryData
364+ {
365+ TestId = "Doesnt_Fail_With_Switch" ,
366+ SecurityKey = KeyingMaterial . JsonWebKeyP256 ,
367+ SecurityToken = new JwtSecurityToken ( ) ,
368+ OpenIdConnectConfiguration = mockConfiguration ,
369+ SetupAction = ( ) => AppContext . SetSwitch ( AadTokenValidationParametersExtension . DontFailOnMissingTidSwitch , true ) ,
370+ TearDownAction = ( ) => AppContext . SetSwitch ( AadTokenValidationParametersExtension . DontFailOnMissingTidSwitch , false )
371+ } ) ;
372+
373+ theoryData . Add ( new AadSigningKeyIssuerTheoryData
374+ {
375+ TestId = "Fail_With_Switch_False_JsonWebToken" ,
376+ SecurityKey = KeyingMaterial . JsonWebKeyP256 ,
377+ SecurityToken = new JsonWebToken ( Default . Jwt ( Default . SecurityTokenDescriptor ( Default . SymmetricSigningCredentials , [ issClaim ] ) ) ) ,
378+ OpenIdConnectConfiguration = mockConfiguration ,
379+ ExpectedException = ExpectedException . SecurityTokenInvalidIssuerException ( "IDX40009" ) ,
380+ SetupAction = ( ) => AppContext . SetSwitch ( AadTokenValidationParametersExtension . DontFailOnMissingTidSwitch , false ) ,
381+ TearDownAction = ( ) => AppContext . SetSwitch ( AadTokenValidationParametersExtension . DontFailOnMissingTidSwitch , isEnabled : false )
382+ } ) ;
383+
384+ theoryData . Add ( new AadSigningKeyIssuerTheoryData
385+ {
386+ TestId = "Doesnt_Fail_With_Switch_JsonWebToken" ,
387+ SecurityKey = KeyingMaterial . JsonWebKeyP256 ,
388+ SecurityToken = new JsonWebToken ( Default . Jwt ( Default . SecurityTokenDescriptor ( Default . SymmetricSigningCredentials , [ issClaim ] ) ) ) ,
389+ OpenIdConnectConfiguration = mockConfiguration ,
390+ SetupAction = ( ) => AppContext . SetSwitch ( AadTokenValidationParametersExtension . DontFailOnMissingTidSwitch , true ) ,
391+ TearDownAction = ( ) => AppContext . SetSwitch ( AadTokenValidationParametersExtension . DontFailOnMissingTidSwitch , false )
392+ } ) ;
393+
394+ theoryData . Add ( new AadSigningKeyIssuerTheoryData
395+ {
396+ TestId = "Fails_With_Multiple_tids" ,
397+ SecurityKey = KeyingMaterial . JsonWebKeyP256 ,
398+ SecurityToken = new JsonWebToken (
399+ Default . Jwt ( Default . SecurityTokenDescriptor (
400+ Default . SymmetricSigningCredentials ,
401+ [ tidClaim , issClaim , new Claim ( "TID" , Guid . NewGuid ( ) . ToString ( ) ) ] ) ) ) ,
402+ ExpectedException = ExpectedException . SecurityTokenInvalidIssuerException ( "IDX40011" ) ,
403+ OpenIdConnectConfiguration = mockConfiguration
404+ } ) ;
405+
406+ theoryData . Add ( new AadSigningKeyIssuerTheoryData
407+ {
408+ TestId = "Fails_With_Multiple_tids_alternate_order" ,
409+ SecurityKey = KeyingMaterial . JsonWebKeyP256 ,
410+ SecurityToken = new JsonWebToken (
411+ Default . Jwt ( Default . SecurityTokenDescriptor (
412+ Default . SymmetricSigningCredentials ,
413+ [ issClaim , new Claim ( "TID" , Guid . NewGuid ( ) . ToString ( ) ) , tidClaim ] ) ) ) ,
414+ ExpectedException = ExpectedException . SecurityTokenInvalidIssuerException ( "IDX40011" ) ,
415+ OpenIdConnectConfiguration = mockConfiguration
416+ } ) ;
417+
418+ theoryData . Add ( new AadSigningKeyIssuerTheoryData
419+ {
420+ TestId = "Fails_With_no standard_tid" ,
421+ SecurityKey = KeyingMaterial . JsonWebKeyP256 ,
422+ SecurityToken = new JsonWebToken (
423+ Default . Jwt ( Default . SecurityTokenDescriptor (
424+ Default . SymmetricSigningCredentials ,
425+ [ issClaim , new Claim ( "TID" , Guid . NewGuid ( ) . ToString ( ) ) ] ) ) ) ,
426+ ExpectedException = ExpectedException . SecurityTokenInvalidIssuerException ( "IDX40009" ) ,
427+ OpenIdConnectConfiguration = mockConfiguration
428+ } ) ;
429+
324430 return theoryData ;
325431 }
326432 }
@@ -346,6 +452,10 @@ public class AadSigningKeyIssuerTheoryData : TheoryDataBase
346452 public bool SetDelegateUsingConfig { get ; set ; } = false ;
347453
348454 public bool SetDelegateWithoutConfig { get ; set ; } = false ;
455+
456+ public Action SetupAction { get ; set ; }
457+
458+ public Action TearDownAction { get ; set ; }
349459 }
350460 }
351461}
0 commit comments