From 5c3af6b2175f56019decf32a071f4f3be4f313fd Mon Sep 17 00:00:00 2001 From: Jan Trejbal Date: Wed, 26 Oct 2022 17:44:30 +0200 Subject: [PATCH] Fix OpenIddictExtensions.IsEmptyJsonElement() to return true for JsonValueKind.Undefined/Null --- OpenIddict.sln | 7 ++ .../Primitives/OpenIddictExtensions.cs | 3 + .../Primitives/OpenIddictExtensionsTests.cs | 109 +++++++++++++++++- ...nIddict.Server.DataProtection.Tests.csproj | 22 ++++ .../OpenIddictServerBuilderTests.cs | 30 +++++ 5 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 test/OpenIddict.Server.DataProtection.Tests/OpenIddict.Server.DataProtection.Tests.csproj create mode 100644 test/OpenIddict.Server.DataProtection.Tests/OpenIddictServerBuilderTests.cs diff --git a/OpenIddict.sln b/OpenIddict.sln index e51df046..ead2b113 100644 --- a/OpenIddict.sln +++ b/OpenIddict.sln @@ -144,6 +144,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenIddict.Client.AspNetCor EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenIddict.Client.Owin.IntegrationTests", "test\OpenIddict.Client.Owin.IntegrationTests\OpenIddict.Client.Owin.IntegrationTests.csproj", "{2F3E9EED-446B-46C3-BC52-ED66C280E0A3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenIddict.Server.DataProtection.Tests", "test\OpenIddict.Server.DataProtection.Tests\OpenIddict.Server.DataProtection.Tests.csproj", "{C92838AB-3923-49A1-B23E-FA01306CAC9D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -350,6 +352,10 @@ Global {2F3E9EED-446B-46C3-BC52-ED66C280E0A3}.Debug|Any CPU.Build.0 = Debug|Any CPU {2F3E9EED-446B-46C3-BC52-ED66C280E0A3}.Release|Any CPU.ActiveCfg = Release|Any CPU {2F3E9EED-446B-46C3-BC52-ED66C280E0A3}.Release|Any CPU.Build.0 = Release|Any CPU + {C92838AB-3923-49A1-B23E-FA01306CAC9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C92838AB-3923-49A1-B23E-FA01306CAC9D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C92838AB-3923-49A1-B23E-FA01306CAC9D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C92838AB-3923-49A1-B23E-FA01306CAC9D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -405,6 +411,7 @@ Global {16BDABB5-387F-421E-95C6-0E3A2311B7E0} = {5FC71D6A-A994-4F62-977F-88A7D25379D7} {CC731B63-4D5C-4587-8F28-B40F4EEAC735} = {5FC71D6A-A994-4F62-977F-88A7D25379D7} {2F3E9EED-446B-46C3-BC52-ED66C280E0A3} = {5FC71D6A-A994-4F62-977F-88A7D25379D7} + {C92838AB-3923-49A1-B23E-FA01306CAC9D} = {5FC71D6A-A994-4F62-977F-88A7D25379D7} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A710059F-0466-4D48-9B3A-0EF4F840B616} diff --git a/src/OpenIddict.Abstractions/Primitives/OpenIddictExtensions.cs b/src/OpenIddict.Abstractions/Primitives/OpenIddictExtensions.cs index 8ebb94f1..8b6ab97a 100644 --- a/src/OpenIddict.Abstractions/Primitives/OpenIddictExtensions.cs +++ b/src/OpenIddict.Abstractions/Primitives/OpenIddictExtensions.cs @@ -3097,6 +3097,9 @@ public static class OpenIddictExtensions { switch (element.ValueKind) { + case JsonValueKind.Undefined or JsonValueKind.Null: + return true; + case JsonValueKind.String: return string.IsNullOrEmpty(element.GetString()); diff --git a/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictExtensionsTests.cs b/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictExtensionsTests.cs index 11b8a721..b21404f7 100644 --- a/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictExtensionsTests.cs +++ b/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictExtensionsTests.cs @@ -5,7 +5,6 @@ */ using System.Collections.Immutable; -using System.Globalization; using System.Security.Claims; using System.Text.Json; using Xunit; @@ -3010,6 +3009,60 @@ public class OpenIddictExtensionsTests Assert.Null(principal.GetClaim("type")); } + [Fact] + public void ClaimsIdentity_SetClaimWithJsonElement_Undefined() + { + // Arrange + var identity = new ClaimsIdentity(); + + // Act + identity.SetClaim("type", default(JsonElement)); + + // Assert + Assert.Null(identity.GetClaim("type")); + } + + [Fact] + public void ClaimsPrincipal_SetClaimWithJsonElement_Undefined() + { + // Arrange + var principal = new ClaimsPrincipal(new ClaimsIdentity()); + principal.AddClaim("type", "value"); + + // Act + principal.SetClaim("type", default(JsonElement)); + + // Assert + Assert.Null(principal.GetClaim("type")); + } + + [Fact] + public void ClaimsIdentity_SetClaimWithJsonElement_Null() + { + // Arrange + var identity = new ClaimsIdentity(); + + // Act + identity.SetClaim("type", JsonSerializer.Deserialize("null")); + + // Assert + Assert.Null(identity.GetClaim("type")); + } + + [Fact] + public void ClaimsPrincipal_SetClaimWithJsonElement_Null() + { + // Arrange + var principal = new ClaimsPrincipal(new ClaimsIdentity()); + principal.AddClaim("type", "value"); + + // Act + principal.SetClaim("type", JsonSerializer.Deserialize("null")); + + // Assert + Assert.Null(principal.GetClaim("type")); + } + [Fact] public void ClaimsIdentity_SetClaimsWithArray_ThrowsAnExceptionForNullIdentity() { @@ -3331,6 +3384,60 @@ public class OpenIddictExtensionsTests Assert.Empty(principal.GetClaims("type")); } + [Fact] + public void ClaimsIdentity_SetClaimsWithJsonElement_Undefined() + { + // Arrange + var identity = new ClaimsIdentity(); + + // Act + identity.SetClaims("type", default(JsonElement)); + + // Assert + Assert.Null(identity.GetClaim("type")); + } + + [Fact] + public void ClaimsPrincipal_SetClaimsWithJsonElement_Undefined() + { + // Arrange + var principal = new ClaimsPrincipal(new ClaimsIdentity()); + principal.AddClaim("type", "value"); + + // Act + principal.SetClaims("type", default(JsonElement)); + + // Assert + Assert.Null(principal.GetClaim("type")); + } + + [Fact] + public void ClaimsIdentity_SetClaimsWithJsonElement_Null() + { + // Arrange + var identity = new ClaimsIdentity(); + + // Act + identity.SetClaims("type", JsonSerializer.Deserialize("null")); + + // Assert + Assert.Null(identity.GetClaim("type")); + } + + [Fact] + public void ClaimsPrincipal_SetClaimsWithJsonElement_Null() + { + // Arrange + var principal = new ClaimsPrincipal(new ClaimsIdentity()); + principal.AddClaim("type", "value"); + + // Act + principal.SetClaims("type", JsonSerializer.Deserialize("null")); + + // Assert + Assert.Null(principal.GetClaim("type")); + } + [Fact] public void ClaimsIdentity_GetCreationDate_ThrowsAnExceptionForNullIdentity() { diff --git a/test/OpenIddict.Server.DataProtection.Tests/OpenIddict.Server.DataProtection.Tests.csproj b/test/OpenIddict.Server.DataProtection.Tests/OpenIddict.Server.DataProtection.Tests.csproj new file mode 100644 index 00000000..d78a2d69 --- /dev/null +++ b/test/OpenIddict.Server.DataProtection.Tests/OpenIddict.Server.DataProtection.Tests.csproj @@ -0,0 +1,22 @@ + + + + net461;net472;netcoreapp3.1;net6.0 + + + + + + + + + + + + + + + + + + diff --git a/test/OpenIddict.Server.DataProtection.Tests/OpenIddictServerBuilderTests.cs b/test/OpenIddict.Server.DataProtection.Tests/OpenIddictServerBuilderTests.cs new file mode 100644 index 00000000..1f4c3f71 --- /dev/null +++ b/test/OpenIddict.Server.DataProtection.Tests/OpenIddictServerBuilderTests.cs @@ -0,0 +1,30 @@ +using System.Security.Claims; +using Xunit; + +namespace OpenIddict.Server.DataProtection.Tests; + +public class OpenIddictServerDataProtectionFormatterTests +{ + [Fact] + public void WriteToken_ReadToken_WithEmptyClaimsPrincipal() + { + // Arrange + var services = new OpenIddictServerDataProtectionFormatter(); + + using var buffer = new MemoryStream(); + using var writer = new BinaryWriter(buffer); + + var principal = new ClaimsPrincipal(); + + // Act and assert + services.WriteToken(writer, principal); + + buffer.Seek(0, SeekOrigin.Begin); + + using var reader = new BinaryReader(buffer); + + var deserializedClaimsPrincipal = services.ReadToken(reader); + + Assert.NotNull(deserializedClaimsPrincipal); + } +}