Skip to content

Commit

Permalink
Fix LKG tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinzo committed Feb 23, 2024
1 parent 1c21104 commit 42c88ac
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3475,6 +3475,13 @@ public async Task ValidateJWSWithConfigAsync(JwtTheoryData theoryData)
var context = TestUtilities.WriteHeader($"{this}.ValidateJWSWithConfigAsync", theoryData);
try
{
// clear up static state.
AadIssuerValidator.s_issuerValidators[Default.AadV1Authority] = new AadIssuerValidator(null, Default.AadV1Authority);

// previous instance is captured in a closure during theorydata set setup.
if (theoryData.ValidationParameters.IssuerValidator != null)
theoryData.ValidationParameters.IssuerValidator = AadIssuerValidator.GetAadIssuerValidator(Default.AadV1Authority).Validate;

var handler = new JsonWebTokenHandler();
var jwt = handler.ReadJsonWebToken(theoryData.Token);
AadIssuerValidator.GetAadIssuerValidator(Default.AadV1Authority).ConfigurationManagerV1 = theoryData.ValidationParameters.ConfigurationManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2218,8 +2218,38 @@ public void ValidateJWSWithLastKnownGood(JwtTheoryData theoryData)

try
{
// clear up static state.
AadIssuerValidator.s_issuerValidators[Default.AadV1Authority] = new AadIssuerValidator(null, Default.AadV1Authority);

// previous instance is captured in a closure during theorydata set setup.
if (theoryData.ValidationParameters.IssuerValidator != null)
theoryData.ValidationParameters.IssuerValidator = AadIssuerValidator.GetAadIssuerValidator(Default.AadV1Authority).Validate;

var handler = new JwtSecurityTokenHandler();

if (theoryData.SetupIssuerLkg)
{
// make a valid pass to initiate issuer LKG.
var issuerValidator = AadIssuerValidator.GetAadIssuerValidator(Default.AadV1Authority);
issuerValidator.ConfigurationManagerV1 = theoryData.SetupIssuerLkgConfigurationManager;

var previousValidateWithLKG = theoryData.ValidationParameters.ValidateWithLKG;
theoryData.ValidationParameters.ValidateWithLKG = false;

var setupValidationResult = handler.ValidateTokenAsync(theoryData.Token, theoryData.ValidationParameters).Result;

theoryData.ValidationParameters.ValidateWithLKG = previousValidateWithLKG;

if (setupValidationResult.Exception != null)
{
if (setupValidationResult.IsValid)
context.AddDiff("setupValidationResult.IsValid, setupValidationResult.Exception != null");
throw setupValidationResult.Exception;
}
}

AadIssuerValidator.GetAadIssuerValidator(Default.AadV1Authority).ConfigurationManagerV1 = theoryData.ValidationParameters.ConfigurationManager;
new JwtSecurityTokenHandler().ValidateToken(theoryData.Token, theoryData.ValidationParameters, out _);
handler.ValidateToken(theoryData.Token, theoryData.ValidationParameters, out _);
theoryData.ExpectedException.ProcessNoException(context);
}
catch (Exception ex)
Expand All @@ -2239,8 +2269,38 @@ public void ValidateJWEWithLastKnownGood(JwtTheoryData theoryData)

try
{
// clear up static state.
AadIssuerValidator.s_issuerValidators[Default.AadV1Authority] = new AadIssuerValidator(null, Default.AadV1Authority);

// previous instance is captured in a closure during theorydata set setup.
if (theoryData.ValidationParameters.IssuerValidator != null)
theoryData.ValidationParameters.IssuerValidator = AadIssuerValidator.GetAadIssuerValidator(Default.AadV1Authority).Validate;

var handler = new JwtSecurityTokenHandler();

if (theoryData.SetupIssuerLkg)
{
// make a valid pass to initiate issuer LKG.
var issuerValidator = AadIssuerValidator.GetAadIssuerValidator(Default.AadV1Authority);
issuerValidator.ConfigurationManagerV1 = theoryData.SetupIssuerLkgConfigurationManager;

var previousValidateWithLKG = theoryData.ValidationParameters.ValidateWithLKG;
theoryData.ValidationParameters.ValidateWithLKG = false;

var setupValidationResult = handler.ValidateTokenAsync(theoryData.Token, theoryData.ValidationParameters).Result;

theoryData.ValidationParameters.ValidateWithLKG = previousValidateWithLKG;

if (setupValidationResult.Exception != null)
{
if (setupValidationResult.IsValid)
context.AddDiff("setupValidationResult.IsValid, setupValidationResult.Exception != null");
throw setupValidationResult.Exception;
}
}

AadIssuerValidator.GetAadIssuerValidator(Default.AadV1Authority).ConfigurationManagerV1 = theoryData.ValidationParameters.ConfigurationManager;
new JwtSecurityTokenHandler().ValidateToken(theoryData.Token, theoryData.ValidationParameters, out _);
handler.ValidateToken(theoryData.Token, theoryData.ValidationParameters, out _);
theoryData.ExpectedException.ProcessNoException(context);
}
catch (Exception ex)
Expand Down

0 comments on commit 42c88ac

Please sign in to comment.