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

Additional Write API take a Stream #2698

Merged
merged 6 commits into from
Jul 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Text.Json.Serialization;
using System.Threading;
using Microsoft.IdentityModel.Abstractions;
Expand Down Expand Up @@ -81,7 +82,7 @@ public static OpenIdConnectConfiguration Create(string json)
/// </summary>
/// <param name="configuration"><see cref="OpenIdConnectConfiguration"/> object to serialize.</param>
/// <returns>json string representing the configuration object.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="configuration"/> is null.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="configuration"/> is <see langword="null"/>.</exception>
public static string Write(OpenIdConnectConfiguration configuration)
{
if (configuration == null)
Expand All @@ -93,6 +94,27 @@ public static string Write(OpenIdConnectConfiguration configuration)
return OpenIdConnectConfigurationSerializer.Write(configuration);
}

/// <summary>
/// Writes an <see cref="OpenIdConnectConfiguration"/> as JSON to the <paramref name="stream"/>.
/// </summary>
/// <param name="configuration">The <see cref="OpenIdConnectConfiguration"/> to serialize.</param>
/// <param name="stream">The <see cref="Stream"/> to write to.</param>
/// <remarks>Because a <see cref="Stream"/> is provided, this method does not return a value.</remarks>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="configuration"/> or <paramref name="stream"/> is <see langword="null"/>.</exception>
public static void Write(OpenIdConnectConfiguration configuration, Stream stream)
{
if (configuration == null)
throw LogHelper.LogArgumentNullException(nameof(configuration));

if (stream == null)
throw LogHelper.LogArgumentNullException(nameof(stream));

if (LogHelper.IsEnabled(EventLogLevel.Verbose))
LogHelper.LogVerbose(LogMessages.IDX21809);

OpenIdConnectConfigurationSerializer.Write(configuration, stream);
}

/// <summary>
/// Initializes an new instance of <see cref="OpenIdConnectConfiguration"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,20 @@ public static string Write(OpenIdConnectConfiguration OpenIdConnectConfiguration
}
}

public static void Write(OpenIdConnectConfiguration OpenIdConnectConfiguration, Stream stream)
westin-m marked this conversation as resolved.
Show resolved Hide resolved
{
Utf8JsonWriter writer = null;
try
{
writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping });
Write(ref writer, OpenIdConnectConfiguration);
}
finally
{
writer?.Dispose();
}
}

public static void Write(ref Utf8JsonWriter writer, OpenIdConnectConfiguration config)
{
writer.WriteStartObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using Microsoft.IdentityModel.TestUtils;
using Microsoft.IdentityModel.Tokens;
Expand Down Expand Up @@ -276,6 +279,32 @@ public void RoundTripFromJson()
TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void RoundTripFromJsonWithStream()
{
using MemoryStream stream = new();

var context = new CompareContext { Title = "RoundTripFromJson" };
var oidcConfig1 = OpenIdConnectConfiguration.Create(OpenIdConfigData.JsonAllValues);
var oidcConfig2 = new OpenIdConnectConfiguration(OpenIdConfigData.JsonAllValues);

OpenIdConnectConfiguration.Write(oidcConfig1, stream);
var oidcJson1 = Encoding.UTF8.GetString(stream.ToArray());
var oidcConfig3 = OpenIdConnectConfiguration.Create(oidcJson1);
stream.SetLength(0);

OpenIdConnectConfiguration.Write(oidcConfig2, stream);
var oidcJson2 = Encoding.UTF8.GetString(stream.ToArray());
var oidcConfig4 = new OpenIdConnectConfiguration(oidcJson2);

IdentityComparer.AreEqual(oidcConfig1, oidcConfig2, context);
IdentityComparer.AreEqual(oidcConfig1, oidcConfig3, context);
IdentityComparer.AreEqual(oidcConfig1, oidcConfig4, context);
IdentityComparer.AreEqual(oidcJson1, oidcJson2, context);

TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void EmptyCollectionSerialization()
{
Expand All @@ -289,6 +318,22 @@ public void EmptyCollectionSerialization()
TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void EmptyCollectionSerializationWithStream()
{
using MemoryStream stream = new();

var context = new CompareContext {Title = "EmptyCollectionSerialization"};
// Initialize an OpenIdConnectConfiguration object with all collections empty.
var oidcWithEmptyCollections = new OpenIdConnectConfiguration();
OpenIdConnectConfiguration.Write(oidcWithEmptyCollections, stream);
var emptyCollectionBytes = Encoding.UTF8.GetBytes("{}");

IdentityComparer.AreEqual(stream.ToArray(), emptyCollectionBytes, context);

TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void NonemptyCollectionSerialization()
{
Expand Down Expand Up @@ -345,5 +390,23 @@ public void NonemptyCollectionSerialization()
}
TestUtilities.AssertFailIfErrors(context);
}

[Fact]
public void NonemptyCollectionSerializationWithStream()
keegan-caruso marked this conversation as resolved.
Show resolved Hide resolved
{
using MemoryStream stream = new();

var context = new CompareContext { Title = "NonemptyCollectionSerialization" };
// Initialize an OpenIdConnectConfiguration object that has at least one element in each Collection.
var oidcWithAllCollections = OpenIdConnectConfiguration.Create(OpenIdConfigData.JsonAllValues);
var oidcWithAllCollectionsJson = OpenIdConnectConfiguration.Write(oidcWithAllCollections);
var oidcWithAllCollectionsBytes = Encoding.UTF8.GetBytes(oidcWithAllCollectionsJson);

OpenIdConnectConfiguration.Write(oidcWithAllCollections, stream);

IdentityComparer.AreBytesEqual(oidcWithAllCollectionsBytes, stream.GetBuffer(), context);

TestUtilities.AssertFailIfErrors(context);
}
}
}