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

Update OpenIddictParameter to offer ImmutableArray<string> conversions instead of string[] and clone JsonNode objects to guarantee immutability #2271

Merged
merged 1 commit into from
Mar 17, 2025
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
2 changes: 2 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
<PropertyGroup
Condition=" '$(TargetFrameworkIdentifier)' == '.NETCoreApp' And $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '9.0')) ">
<DefineConstants>$(DefineConstants);SUPPORTS_CERTIFICATE_LOADER</DefineConstants>
<DefineConstants>$(DefineConstants);SUPPORTS_JSON_ELEMENT_DEEP_EQUALS</DefineConstants>
<DefineConstants>$(DefineConstants);SUPPORTS_JSON_ELEMENT_PROPERTY_COUNT</DefineConstants>
</PropertyGroup>

<PropertyGroup
Expand Down
14 changes: 8 additions & 6 deletions src/OpenIddict.Abstractions/Primitives/OpenIddictExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -639,14 +639,14 @@ public static Claim SetDestinations(this Claim claim, IEnumerable<string>? desti
/// </summary>
/// <param name="identity">The identity.</param>
/// <returns>The destinations, returned as a flattened dictionary.</returns>
public static ImmutableDictionary<string, string[]> GetDestinations(this ClaimsIdentity identity)
public static ImmutableDictionary<string, ImmutableArray<string>> GetDestinations(this ClaimsIdentity identity)
{
if (identity is null)
{
throw new ArgumentNullException(nameof(identity));
}

var builder = ImmutableDictionary.CreateBuilder<string, string[]>(StringComparer.Ordinal);
var builder = ImmutableDictionary.CreateBuilder<string, ImmutableArray<string>>(StringComparer.Ordinal);

foreach (var group in identity.Claims.GroupBy(claim => claim.Type))
{
Expand Down Expand Up @@ -676,14 +676,14 @@ public static ImmutableDictionary<string, string[]> GetDestinations(this ClaimsI
/// </summary>
/// <param name="principal">The principal.</param>
/// <returns>The destinations, returned as a flattened dictionary.</returns>
public static ImmutableDictionary<string, string[]> GetDestinations(this ClaimsPrincipal principal)
public static ImmutableDictionary<string, ImmutableArray<string>> GetDestinations(this ClaimsPrincipal principal)
{
if (principal is null)
{
throw new ArgumentNullException(nameof(principal));
}

var builder = ImmutableDictionary.CreateBuilder<string, string[]>(StringComparer.Ordinal);
var builder = ImmutableDictionary.CreateBuilder<string, ImmutableArray<string>>(StringComparer.Ordinal);

foreach (var group in principal.Claims.GroupBy(claim => claim.Type))
{
Expand Down Expand Up @@ -714,7 +714,8 @@ public static ImmutableDictionary<string, string[]> GetDestinations(this ClaimsP
/// <param name="identity">The identity.</param>
/// <param name="destinations">The destinations, as a flattened dictionary.</param>
/// <returns>The identity.</returns>
public static ClaimsIdentity SetDestinations(this ClaimsIdentity identity, ImmutableDictionary<string, string[]> destinations)
public static ClaimsIdentity SetDestinations(this ClaimsIdentity identity,
ImmutableDictionary<string, ImmutableArray<string>> destinations)
{
if (identity is null)
{
Expand Down Expand Up @@ -743,7 +744,8 @@ public static ClaimsIdentity SetDestinations(this ClaimsIdentity identity, Immut
/// <param name="principal">The principal.</param>
/// <param name="destinations">The destinations, as a flattened dictionary.</param>
/// <returns>The principal.</returns>
public static ClaimsPrincipal SetDestinations(this ClaimsPrincipal principal, ImmutableDictionary<string, string[]> destinations)
public static ClaimsPrincipal SetDestinations(this ClaimsPrincipal principal,
ImmutableDictionary<string, ImmutableArray<string>> destinations)
{
if (principal is null)
{
Expand Down
40 changes: 19 additions & 21 deletions src/OpenIddict.Abstractions/Primitives/OpenIddictMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
* the license and the contributors participating to this project.
*/

using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
Expand Down Expand Up @@ -141,17 +143,15 @@ public OpenIddictMessage(IEnumerable<KeyValuePair<string, string?>> parameters)
continue;
}

var values = parameter.Select(parameter => parameter.Value).ToArray();

// Note: the core OAuth 2.0 specification requires that request parameters
// not be present more than once but derived specifications like the
// token exchange specification deliberately allow specifying multiple
// parameters with the same name to represent a multi-valued parameter.
AddParameter(parameter.Key, values.Length switch
AddParameter(parameter.Key, parameter.Select(parameter => parameter.Value).ToArray() switch
{
0 => default,
1 => values[0],
_ => values
[] => default,
[string value] => new OpenIddictParameter(value),
[..] values => new(ImmutableCollectionsMarshal.AsImmutableArray(values))
});
}
}
Expand All @@ -161,7 +161,7 @@ public OpenIddictMessage(IEnumerable<KeyValuePair<string, string?>> parameters)
/// </summary>
/// <param name="parameters">The message parameters.</param>
/// <remarks>Parameters with a null or empty key are always ignored.</remarks>
public OpenIddictMessage(IEnumerable<KeyValuePair<string, string?[]?>> parameters)
public OpenIddictMessage(IEnumerable<KeyValuePair<string, ImmutableArray<string?>?>> parameters)
{
if (parameters is null)
{
Expand All @@ -180,11 +180,11 @@ public OpenIddictMessage(IEnumerable<KeyValuePair<string, string?[]?>> parameter
// not be present more than once but derived specifications like the
// token exchange specification deliberately allow specifying multiple
// parameters with the same name to represent a multi-valued parameter.
AddParameter(parameter.Key, parameter.Value?.Length switch
AddParameter(parameter.Key, parameter.Value switch
{
null or 0 => default,
1 => parameter.Value[0],
_ => parameter.Value
null or [] => default,
[string value] => new OpenIddictParameter(value),
[..] values => new OpenIddictParameter(values)
});
}
}
Expand Down Expand Up @@ -213,11 +213,11 @@ public OpenIddictMessage(IEnumerable<KeyValuePair<string, StringValues>> paramet
// not be present more than once but derived specifications like the
// token exchange specification deliberately allow specifying multiple
// parameters with the same name to represent a multi-valued parameter.
AddParameter(parameter.Key, parameter.Value.Count switch
AddParameter(parameter.Key, parameter.Value switch
{
0 => default,
1 => parameter.Value[0],
_ => parameter.Value.ToArray()
[] => default,
[string value] => new OpenIddictParameter(value),
[..] values => new(ImmutableCollectionsMarshal.AsImmutableArray(values.ToArray()))
});
}
}
Expand All @@ -243,17 +243,15 @@ public OpenIddictMessage(NameValueCollection parameters)
continue;
}

var values = parameters.GetValues(name);

// Note: the core OAuth 2.0 specification requires that request parameters
// not be present more than once but derived specifications like the
// token exchange specification deliberately allow specifying multiple
// parameters with the same name to represent a multi-valued parameter.
AddParameter(name, values?.Length switch
AddParameter(name, parameters.GetValues(name) switch
{
null or 0 => default,
1 => values[0],
_ => values
null or [] => default,
[string value] => new OpenIddictParameter(value),
[..] values => new(ImmutableCollectionsMarshal.AsImmutableArray<string?>(values))
});
}
}
Expand Down
Loading