From 3eae5659d819dfe47467aacfc168562398bc9dfe Mon Sep 17 00:00:00 2001 From: "hakon.lerring" Date: Wed, 22 Nov 2023 07:26:59 +0100 Subject: [PATCH] Small refactor of Saml2SecurityTokenHandler Refactor PopulateValidationParametersWithCurrentConfigurationAsync null check to guard with an early return. Rename cloned variable to make code easier to read. --- .../Saml2/Saml2SecurityTokenHandler.cs | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.IdentityModel.Tokens.Saml/Saml2/Saml2SecurityTokenHandler.cs b/src/Microsoft.IdentityModel.Tokens.Saml/Saml2/Saml2SecurityTokenHandler.cs index 68322b403e..d9c560fb20 100644 --- a/src/Microsoft.IdentityModel.Tokens.Saml/Saml2/Saml2SecurityTokenHandler.cs +++ b/src/Microsoft.IdentityModel.Tokens.Saml/Saml2/Saml2SecurityTokenHandler.cs @@ -297,18 +297,22 @@ private ClaimsPrincipal ValidateToken(Saml2SecurityToken samlToken, string token return new ClaimsPrincipal(identity); } - private static async Task PopulateValidationParametersWithCurrentConfigurationAsync( - TokenValidationParameters validationParameters) - { - if(validationParameters.ConfigurationManager != null) { - var currentConfiguration = await validationParameters.ConfigurationManager.GetBaseConfigurationAsync(CancellationToken.None).ConfigureAwait(false); - validationParameters = validationParameters.Clone(); - var issuers = new[] { currentConfiguration.Issuer }; - validationParameters.ValidIssuers = (validationParameters.ValidIssuers == null ? issuers : validationParameters.ValidIssuers.Concat(issuers)); - validationParameters.IssuerSigningKeys = (validationParameters.IssuerSigningKeys == null ? currentConfiguration.SigningKeys : validationParameters.IssuerSigningKeys.Concat(currentConfiguration.SigningKeys)); + private static async Task PopulateValidationParametersWithCurrentConfigurationAsync(TokenValidationParameters validationParameters) + { + if (validationParameters.ConfigurationManager == null) + { + return validationParameters; } - return validationParameters; + + var currentConfiguration = await validationParameters.ConfigurationManager.GetBaseConfigurationAsync(CancellationToken.None).ConfigureAwait(false); + var validationParametersCloned = validationParameters.Clone(); + var issuers = new[] { currentConfiguration.Issuer }; + + validationParametersCloned.ValidIssuers = (validationParametersCloned.ValidIssuers == null ? issuers : validationParametersCloned.ValidIssuers.Concat(issuers)); + validationParametersCloned.IssuerSigningKeys = (validationParametersCloned.IssuerSigningKeys == null ? currentConfiguration.SigningKeys : validationParametersCloned.IssuerSigningKeys.Concat(currentConfiguration.SigningKeys)); + return validationParametersCloned; + } ///