Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonWebKeySet stores the String it was created with #2755

Merged
merged 16 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Microsoft.IdentityModel.Tokens/JsonWebKeySet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class JsonWebKeySet
{
internal const string ClassName = "Microsoft.IdentityModel.Tokens.JsonWebKeySet";
private Dictionary<string, object> _additionalData;
private string _jsonData = string.Empty;

/// <summary>
/// Returns a new instance of <see cref="JsonWebKeySet"/>.
Expand Down Expand Up @@ -54,6 +55,8 @@ public JsonWebKeySet(string json)
if (string.IsNullOrEmpty(json))
throw LogHelper.LogArgumentNullException(nameof(json));

_jsonData = json;
westin-m marked this conversation as resolved.
Show resolved Hide resolved

try
{
if (LogHelper.IsEnabled(EventLogLevel.Verbose))
Expand Down Expand Up @@ -97,6 +100,22 @@ public JsonWebKeySet(string json)
[JsonIgnore]
public bool SkipUnresolvedJsonWebKeys { get; set; } = DefaultSkipUnresolvedJsonWebKeys;

/// <summary>
/// The original string used to create this instance if a string was provided.
/// </summary>
[JsonIgnore]
internal string JsonData
{
get
{
return _jsonData;
}
set
{
_jsonData = value;
}
}

/// <summary>
/// Returns the JsonWebKeys as a <see cref="IList{SecurityKey}"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.IdentityModel.TestUtils;
using Microsoft.IdentityModel.Tokens;
using Xunit;

namespace Microsoft.IdentityModel.Protocols.OpenIdConnect.Tests
Expand All @@ -28,7 +30,15 @@ public async Task FromNetwork()
[Fact]
public async Task FromFile()
{
var context = new CompareContext();
var context = new CompareContext
{
PropertiesToIgnoreWhenComparing = new Dictionary<Type, List<string>>
westin-m marked this conversation as resolved.
Show resolved Hide resolved
{
// If the objects being compared are created from the same string and they are equal, the string itself can be ignored.
westin-m marked this conversation as resolved.
Show resolved Hide resolved
// The strings may not be equal because of whitespace, but the json they represent is semantically identical.
{ typeof(JsonWebKeySet), [ "JsonData" ] },
}
};
var configuration = await GetConfigurationAsync(
OpenIdConfigData.JsonFile,
ExpectedException.NoExceptionExpected,
Expand All @@ -52,7 +62,15 @@ public async Task FromFile()
[Fact]
public async Task FromJson()
{
var context = new CompareContext();
var context = new CompareContext
{
PropertiesToIgnoreWhenComparing = new Dictionary<Type, List<string>>
{
// If the objects being compared are created from the same string and they are equal, the string itself can be ignored.
// The strings may not be equal because of whitespace, but the json they represent is semantically identical.
{ typeof(JsonWebKeySet), [ "JsonData" ] },
}
};
var configuration = await GetConfigurationFromMixedAsync(
OpenIdConfigData.OpenIdConnectMetadataPingString,
expectedException: ExpectedException.NoExceptionExpected);
Expand Down
7 changes: 4 additions & 3 deletions test/Microsoft.IdentityModel.Tokens.Tests/Json/DataSets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -729,9 +729,7 @@ public static JsonWebKeySet JsonWebKeySet1
{
get
{
JsonWebKeySet jsonWebKeySet = new JsonWebKeySet();
jsonWebKeySet.Keys.Add(JsonWebKey1);
jsonWebKeySet.Keys.Add(JsonWebKey2);
JsonWebKeySet jsonWebKeySet = new JsonWebKeySet(JsonWebKeySetString1);

return jsonWebKeySet;
}
Expand All @@ -753,6 +751,7 @@ public static JsonWebKeySet JsonWebKeySetX509Data

JsonWebKeySet jsonWebKeySet = new JsonWebKeySet();
jsonWebKeySet.Keys.Add(jsonWebKey);
jsonWebKeySet.JsonData = JsonWebKeySetX509DataString;

return jsonWebKeySet;
}
Expand All @@ -766,6 +765,7 @@ public static JsonWebKeySet JsonWebKeySetEC
jsonWebKeySet.Keys.Add(JsonWebKeyES256);
jsonWebKeySet.Keys.Add(JsonWebKeyES384);
jsonWebKeySet.Keys.Add(JsonWebKeyES512);
jsonWebKeySet.JsonData = JsonWebKeySetECCString;

return jsonWebKeySet;
}
Expand All @@ -785,6 +785,7 @@ public static JsonWebKeySet JsonWebKeySetOnlyX5t

JsonWebKeySet jsonWebKeySet = new JsonWebKeySet();
jsonWebKeySet.Keys.Add(jsonWebKey);
jsonWebKeySet.JsonData = JsonWebKeySetOnlyX5tString;

return jsonWebKeySet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using Microsoft.IdentityModel.TestUtils;
using Xunit;

Expand All @@ -17,6 +18,12 @@ public class JsonWebKeySetSerializationTests
public void Serialize(JsonWebKeySetTheoryData theoryData)
{
var context = TestUtilities.WriteHeader($"{this}.Serialize", theoryData);
context.PropertiesToIgnoreWhenComparing = new Dictionary<Type, List<string>>
{
// If the objects being compared are created from the same string and they are equal, the string itself can be ignored.
// The strings may not be equal because of whitespace, but the json they represent is semantically identical.
{ typeof(JsonWebKeySet), [ "JsonData" ] },
};

try
{
Expand All @@ -29,7 +36,7 @@ public void Serialize(JsonWebKeySetTheoryData theoryData)
// compare our utf8Reader with expected value
if (!IdentityComparer.AreEqual(jsonWebKeySetUtf8Reader, theoryData.JsonWebKeySet, context))
{
context.Diffs.Add("jsonWebKeySetUtf8Reader != theoryData.JsonWebKeySet1");
context.Diffs.Add("jsonWebKeySetUtf8Reader != theoryData.JsonWebKeySet");
context.Diffs.Add("=========================================");
}
}
Expand Down Expand Up @@ -60,6 +67,12 @@ public static TheoryData<JsonWebKeySetTheoryData> SerializeDataSet
public void Deserialize(JsonWebKeySetTheoryData theoryData)
{
var context = TestUtilities.WriteHeader($"{this}.Deserialize", theoryData);
context.PropertiesToIgnoreWhenComparing = new Dictionary<Type, List<string>>
{
// If the objects being compared are created from the same string and they are equal, the string itself can be ignored.
// The strings may not be equal because of whitespace, but the json they represent is semantically identical.
{ typeof(JsonWebKeySet), [ "JsonData" ] },
};

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public void Constructors(JsonWebKeySetTheoryData theoryData)
{
var jsonWebKeys = new JsonWebKeySet(theoryData.Json);
var keys = jsonWebKeys.GetSigningKeys();
var originalString = jsonWebKeys.JsonData;
theoryData.ExpectedException.ProcessNoException(context);

IdentityComparer.AreStringsEqual(originalString, theoryData.Json, context);

if (theoryData.JsonWebKeySet != null)
IdentityComparer.AreEqual(jsonWebKeys, theoryData.JsonWebKeySet, context);

Expand Down
Loading