Skip to content

Commit

Permalink
Remove linq from BaseConfigurationComparer
Browse files Browse the repository at this point in the history
Fixes #2464
  • Loading branch information
Keegan Caruso committed Jan 29, 2024
1 parent b6a5fec commit 085a4d1
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/Microsoft.IdentityModel.Tokens/BaseConfigurationComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Linq;

namespace Microsoft.IdentityModel.Tokens
{
Expand All @@ -17,22 +16,41 @@ public bool Equals(BaseConfiguration config1, BaseConfiguration config2)
return true;
else if (config1 == null || config2 == null)
return false;
else if (config1.Issuer == config2.Issuer && config1.SigningKeys.Count == config2.SigningKeys.Count
&& !config1.SigningKeys.Select(x => x.InternalId).Except(config2.SigningKeys.Select(x => x.InternalId)).Any())
return true;
else
return false;
{
if (config1.Issuer != config2.Issuer)
return false;

if (config1.SigningKeys.Count != config2.SigningKeys.Count)
return false;

foreach (var key in config1.SigningKeys)
{
if (!ContainsKeyWithInternalId(config2, key.InternalId))
return false;
}
}

return true;
}

private static bool ContainsKeyWithInternalId(BaseConfiguration config, string internalId)
{
foreach(var key in config.SigningKeys)
if (key.InternalId == internalId)
return true;

return false;
}

public int GetHashCode(BaseConfiguration config)
{
int defaultHash = string.Empty.GetHashCode();
int hashCode = defaultHash;
hashCode ^= string.IsNullOrEmpty(config.Issuer) ? defaultHash : config.Issuer.GetHashCode();
foreach(string internalId in config.SigningKeys.Select(x => x.InternalId))
{
hashCode ^= internalId.GetHashCode();
}

foreach (var key in config.SigningKeys)
hashCode ^= key.InternalId.GetHashCode();

return hashCode;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.IdentityModel.TestUtils;
using Xunit;

namespace Microsoft.IdentityModel.Tokens.Tests
{
public class BaseConfigurationComparerTests
{
[Theory, MemberData(nameof(ComparerTheoryData))]
public void Compare(BaseConfigurationComparerTheoryData theoryData)
{
var context = TestUtilities.WriteHeader($"{this}.Compare", theoryData);
var comparer = new BaseConfigurationComparer();
var result = comparer.Equals(theoryData.ConfigurationA, theoryData.ConfigurationB);
Assert.Equal(theoryData.ShouldBeEqual, result);
}

public static TheoryData<BaseConfigurationComparerTheoryData> ComparerTheoryData()
{
var jsonWebKey1 = new TestConfiguration
{
Issuer = "http://example.com/issuer/1",
};

jsonWebKey1.SigningKeys.Add(DataSets.JsonWebKey1);

var jsonWebKey2 = new TestConfiguration
{
Issuer = "http://example.com/issuer/1",
};

jsonWebKey2.SigningKeys.Add(DataSets.JsonWebKey2);

var bothJsonWebKeys = new TestConfiguration
{
Issuer = "http://example.com/issuer/1",
};

bothJsonWebKeys.SigningKeys.Add(DataSets.JsonWebKey1);
bothJsonWebKeys.SigningKeys.Add(DataSets.JsonWebKey2);

return new TheoryData<BaseConfigurationComparerTheoryData>()
{
new()
{
TestId = "Both null",
ConfigurationA = null,
ConfigurationB = null,
ShouldBeEqual = true,
},
new()
{
TestId = "B null",
ConfigurationA = new TestConfiguration(),
ConfigurationB = null,
ShouldBeEqual = false,
},
new()
{
TestId = "A null",
ConfigurationA = null,
ConfigurationB = new TestConfiguration(),
ShouldBeEqual = false,
},
new()
{
TestId = "Issuer mismatched",
ConfigurationA = new TestConfiguration
{
Issuer = "http://example.com/issuer/1"
},
ConfigurationB = new TestConfiguration
{
Issuer = "http://example.com/issuer/2"
},
ShouldBeEqual = false,
},
new()
{
TestId = "No Keys",
ConfigurationA = new TestConfiguration
{
Issuer = "http://example.com/issuer/1",
},
ConfigurationB = new TestConfiguration
{
Issuer = "http://example.com/issuer/1"
},
ShouldBeEqual = true,
},
new()
{
TestId = "different keys",
ConfigurationA = jsonWebKey1,
ConfigurationB = jsonWebKey2,
ShouldBeEqual = false,
},
new()
{
TestId = "same keys",
ConfigurationA = jsonWebKey1,
ConfigurationB = jsonWebKey1,
ShouldBeEqual = true,
},
new ()
{
TestId = "different number of keys",
ConfigurationA = jsonWebKey1,
ConfigurationB = bothJsonWebKeys,
ShouldBeEqual = false,
},
};
}
}

public class BaseConfigurationComparerTheoryData : TheoryDataBase
{
public BaseConfiguration ConfigurationA { get; set; }

public BaseConfiguration ConfigurationB { get; set; }

public bool ShouldBeEqual { get; set; }
}

public class TestConfiguration : BaseConfiguration
{
}
}

0 comments on commit 085a4d1

Please sign in to comment.