Skip to content

Commit 38e84b8

Browse files
committed
Update OpenIddictParameter to offer ImmutableArray<string> conversions instead of string[] and clone JsonNode objects to guarantee immutability
1 parent dd3937c commit 38e84b8

File tree

58 files changed

+834
-577
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+834
-577
lines changed

Directory.Build.targets

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
<PropertyGroup
101101
Condition=" '$(TargetFrameworkIdentifier)' == '.NETCoreApp' And $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '9.0')) ">
102102
<DefineConstants>$(DefineConstants);SUPPORTS_CERTIFICATE_LOADER</DefineConstants>
103+
<DefineConstants>$(DefineConstants);SUPPORTS_JSON_ELEMENT_DEEP_EQUALS</DefineConstants>
104+
<DefineConstants>$(DefineConstants);SUPPORTS_JSON_ELEMENT_PROPERTY_COUNT</DefineConstants>
103105
</PropertyGroup>
104106

105107
<PropertyGroup

src/OpenIddict.Abstractions/Primitives/OpenIddictExtensions.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -639,14 +639,14 @@ public static Claim SetDestinations(this Claim claim, IEnumerable<string>? desti
639639
/// </summary>
640640
/// <param name="identity">The identity.</param>
641641
/// <returns>The destinations, returned as a flattened dictionary.</returns>
642-
public static ImmutableDictionary<string, string[]> GetDestinations(this ClaimsIdentity identity)
642+
public static ImmutableDictionary<string, ImmutableArray<string>> GetDestinations(this ClaimsIdentity identity)
643643
{
644644
if (identity is null)
645645
{
646646
throw new ArgumentNullException(nameof(identity));
647647
}
648648

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

651651
foreach (var group in identity.Claims.GroupBy(claim => claim.Type))
652652
{
@@ -676,14 +676,14 @@ public static ImmutableDictionary<string, string[]> GetDestinations(this ClaimsI
676676
/// </summary>
677677
/// <param name="principal">The principal.</param>
678678
/// <returns>The destinations, returned as a flattened dictionary.</returns>
679-
public static ImmutableDictionary<string, string[]> GetDestinations(this ClaimsPrincipal principal)
679+
public static ImmutableDictionary<string, ImmutableArray<string>> GetDestinations(this ClaimsPrincipal principal)
680680
{
681681
if (principal is null)
682682
{
683683
throw new ArgumentNullException(nameof(principal));
684684
}
685685

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

688688
foreach (var group in principal.Claims.GroupBy(claim => claim.Type))
689689
{
@@ -714,7 +714,8 @@ public static ImmutableDictionary<string, string[]> GetDestinations(this ClaimsP
714714
/// <param name="identity">The identity.</param>
715715
/// <param name="destinations">The destinations, as a flattened dictionary.</param>
716716
/// <returns>The identity.</returns>
717-
public static ClaimsIdentity SetDestinations(this ClaimsIdentity identity, ImmutableDictionary<string, string[]> destinations)
717+
public static ClaimsIdentity SetDestinations(this ClaimsIdentity identity,
718+
ImmutableDictionary<string, ImmutableArray<string>> destinations)
718719
{
719720
if (identity is null)
720721
{
@@ -743,7 +744,8 @@ public static ClaimsIdentity SetDestinations(this ClaimsIdentity identity, Immut
743744
/// <param name="principal">The principal.</param>
744745
/// <param name="destinations">The destinations, as a flattened dictionary.</param>
745746
/// <returns>The principal.</returns>
746-
public static ClaimsPrincipal SetDestinations(this ClaimsPrincipal principal, ImmutableDictionary<string, string[]> destinations)
747+
public static ClaimsPrincipal SetDestinations(this ClaimsPrincipal principal,
748+
ImmutableDictionary<string, ImmutableArray<string>> destinations)
747749
{
748750
if (principal is null)
749751
{

src/OpenIddict.Abstractions/Primitives/OpenIddictMessage.cs

+19-21
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
* the license and the contributors participating to this project.
55
*/
66

7+
using System.Collections.Immutable;
78
using System.Collections.ObjectModel;
89
using System.Collections.Specialized;
910
using System.Diagnostics;
11+
using System.Runtime.InteropServices;
1012
using System.Text;
1113
using System.Text.Encodings.Web;
1214
using System.Text.Json;
@@ -141,17 +143,15 @@ public OpenIddictMessage(IEnumerable<KeyValuePair<string, string?>> parameters)
141143
continue;
142144
}
143145

144-
var values = parameter.Select(parameter => parameter.Value).ToArray();
145-
146146
// Note: the core OAuth 2.0 specification requires that request parameters
147147
// not be present more than once but derived specifications like the
148148
// token exchange specification deliberately allow specifying multiple
149149
// parameters with the same name to represent a multi-valued parameter.
150-
AddParameter(parameter.Key, values.Length switch
150+
AddParameter(parameter.Key, parameter.Select(parameter => parameter.Value).ToArray() switch
151151
{
152-
0 => default,
153-
1 => values[0],
154-
_ => values
152+
[] => default,
153+
[string value] => new OpenIddictParameter(value),
154+
[..] values => new(ImmutableCollectionsMarshal.AsImmutableArray(values))
155155
});
156156
}
157157
}
@@ -161,7 +161,7 @@ public OpenIddictMessage(IEnumerable<KeyValuePair<string, string?>> parameters)
161161
/// </summary>
162162
/// <param name="parameters">The message parameters.</param>
163163
/// <remarks>Parameters with a null or empty key are always ignored.</remarks>
164-
public OpenIddictMessage(IEnumerable<KeyValuePair<string, string?[]?>> parameters)
164+
public OpenIddictMessage(IEnumerable<KeyValuePair<string, ImmutableArray<string?>?>> parameters)
165165
{
166166
if (parameters is null)
167167
{
@@ -180,11 +180,11 @@ public OpenIddictMessage(IEnumerable<KeyValuePair<string, string?[]?>> parameter
180180
// not be present more than once but derived specifications like the
181181
// token exchange specification deliberately allow specifying multiple
182182
// parameters with the same name to represent a multi-valued parameter.
183-
AddParameter(parameter.Key, parameter.Value?.Length switch
183+
AddParameter(parameter.Key, parameter.Value switch
184184
{
185-
null or 0 => default,
186-
1 => parameter.Value[0],
187-
_ => parameter.Value
185+
null or [] => default,
186+
[string value] => new OpenIddictParameter(value),
187+
[..] values => new OpenIddictParameter(values)
188188
});
189189
}
190190
}
@@ -213,11 +213,11 @@ public OpenIddictMessage(IEnumerable<KeyValuePair<string, StringValues>> paramet
213213
// not be present more than once but derived specifications like the
214214
// token exchange specification deliberately allow specifying multiple
215215
// parameters with the same name to represent a multi-valued parameter.
216-
AddParameter(parameter.Key, parameter.Value.Count switch
216+
AddParameter(parameter.Key, parameter.Value switch
217217
{
218-
0 => default,
219-
1 => parameter.Value[0],
220-
_ => parameter.Value.ToArray()
218+
[] => default,
219+
[string value] => new OpenIddictParameter(value),
220+
[..] values => new(ImmutableCollectionsMarshal.AsImmutableArray(values.ToArray()))
221221
});
222222
}
223223
}
@@ -243,17 +243,15 @@ public OpenIddictMessage(NameValueCollection parameters)
243243
continue;
244244
}
245245

246-
var values = parameters.GetValues(name);
247-
248246
// Note: the core OAuth 2.0 specification requires that request parameters
249247
// not be present more than once but derived specifications like the
250248
// token exchange specification deliberately allow specifying multiple
251249
// parameters with the same name to represent a multi-valued parameter.
252-
AddParameter(name, values?.Length switch
250+
AddParameter(name, parameters.GetValues(name) switch
253251
{
254-
null or 0 => default,
255-
1 => values[0],
256-
_ => values
252+
null or [] => default,
253+
[string value] => new OpenIddictParameter(value),
254+
[..] values => new(ImmutableCollectionsMarshal.AsImmutableArray<string?>(values))
257255
});
258256
}
259257
}

0 commit comments

Comments
 (0)