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

Fix for #456 #462

Closed
Closed
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
6 changes: 6 additions & 0 deletions JWT.sln
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C86A941F-F65
src\Directory.Build.targets = src\Directory.Build.targets
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JWT.Tests.Net70", "tests\JWT.Tests.Net70\JWT.Tests.Net70.csproj", "{D7F24AC9-D178-4BAB-BF93-4BAD8028416D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -105,6 +107,10 @@ Global
{73167BAB-1F21-4DE9-83E0-5E0686AB7245}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73167BAB-1F21-4DE9-83E0-5E0686AB7245}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73167BAB-1F21-4DE9-83E0-5E0686AB7245}.Release|Any CPU.Build.0 = Release|Any CPU
{D7F24AC9-D178-4BAB-BF93-4BAD8028416D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D7F24AC9-D178-4BAB-BF93-4BAD8028416D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7F24AC9-D178-4BAB-BF93-4BAD8028416D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7F24AC9-D178-4BAB-BF93-4BAD8028416D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<Choose>
<When Condition="'$(TargetFramework)' == 'net462' OR '$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'net6.0'">
<When Condition="'$(TargetFramework)' == 'net462' OR '$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'net6.0' OR '$(TargetFramework)' == 'net7.0'">
<PropertyGroup>
<DefineConstants>$(DefineConstants);MODERN_DOTNET</DefineConstants>
</PropertyGroup>
Expand Down
34 changes: 33 additions & 1 deletion src/JWT/Builder/JwtBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using JWT.Algorithms;
using JWT.Serializers;
using Newtonsoft.Json;
using static JWT.Internal.EncodingHelper;

namespace JWT.Builder
Expand Down Expand Up @@ -276,7 +277,7 @@ public string Encode(Type payloadType, object payload)
EnsureCanEncode();

var dic = payloadType.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(prop => prop.Name, prop => prop.GetValue(payload, null));
.ToDictionary(info => GetPropName(info, _jsonSerializerFactory), prop => prop.GetValue(payload, null));

foreach (var pair in dic)
{
Expand All @@ -286,6 +287,37 @@ public string Encode(Type payloadType, object payload)
return Encode();
}

private static string GetPropName(MemberInfo prop, IJsonSerializerFactory jsonSerializerFactory)
{
var customAttributes = prop.GetCustomAttributes(true);
foreach (var attribute in customAttributes)
{
if (jsonSerializerFactory.Create() is JsonNetSerializer)
{
if (attribute is JsonPropertyAttribute jsonNetProperty)
{
return jsonNetProperty.PropertyName;
}
}
#if MODERN_DOTNET
else if (jsonSerializerFactory.Create() is SystemTextSerializer)
{
if (attribute is System.Text.Json.Serialization.JsonPropertyNameAttribute systemTextSerializerProperty)
{
return systemTextSerializerProperty.Name;
}
}
#endif
else
{
throw new NotSupportedException(
$"{jsonSerializerFactory.Create().GetType().Name} is not supported");
}
}

return prop.Name;
}

/// <summary>
/// Decodes a token using the supplied dependencies.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And please bump the version to 10.0.2 so I'll push a fix asap.

<TargetFrameworkIdentifier>.NETFramework,Version=6.0</TargetFrameworkIdentifier>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net7.0'">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add 7.0 (if we decide so) in a separate PR, see also #451.

<TargetFrameworkIdentifier>.NETFramework,Version=7.0</TargetFrameworkIdentifier>
</PropertyGroup>

<PropertyGroup>
<Copyright>Public Domain</Copyright>
Expand Down
1 change: 1 addition & 0 deletions src/JWT/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[assembly: InternalsVisibleTo("JWT.Tests.Net40, PublicKey=" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("JWT.Tests.Net46, PublicKey=" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("JWT.Tests.Net60, PublicKey=" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("JWT.Tests.Net70, PublicKey=" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("JWT.Extensions.AspNetCore.Tests, PublicKey=" + AssemblyInfo.PublicKey)]

namespace JWT
Expand Down
48 changes: 48 additions & 0 deletions tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,5 +433,53 @@ public void Decode_ToDictionary_Without_Serializer_Should_Throw_Exception()
action.Should()
.Throw<ArgumentNullException>();
}

[TestMethod]
public void Encode_Decode_ToJsonNetDecoratedType_Should_UseDecoratedName_Bug456()
{
var token = JwtBuilder.Create()
.WithAlgorithm(new NoneAlgorithm())
.WithJsonSerializer(new JsonNetSerializer());

var model = new TestData.TestDataJsonNetDecorated
{
AccessToken = "abc123",
};

var encoded = token.Encode(model);
Assert.IsNotNull(encoded);

token = JwtBuilder.Create()
.WithAlgorithm(new NoneAlgorithm())
.WithJsonSerializer(new JsonNetSerializer());

var payloadDecoded = token.Decode<TestData.TestDataJsonNetDecorated>(encoded);
Assert.AreEqual(model.AccessToken, payloadDecoded.AccessToken);
}
#if NETSTANDARD2_0 || NET6_0 || NET7_0

[TestMethod]
public void Encode_Decode_ToSystemTextSerializerDecoratedType_Should_UseDecoratedName_Bug456()
{
var token = JwtBuilder.Create()
.WithAlgorithm(new NoneAlgorithm())
.WithJsonSerializer(new SystemTextSerializer());

var model = new TestData.TestDataSystemTextSerializerDecorated
{
AccessToken = "abc123",
};

var encoded = token.Encode(model);
Assert.IsNotNull(encoded);

token = JwtBuilder.Create()
.WithAlgorithm(new NoneAlgorithm())
.WithJsonSerializer(new SystemTextSerializer());

var payloadDecoded = token.Decode<TestData.TestDataSystemTextSerializerDecorated>(encoded);
Assert.AreEqual(model.AccessToken, payloadDecoded.AccessToken);
}
#endif
}
}
2 changes: 1 addition & 1 deletion tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public void Encode_With_Secret_Should_Return_Valid_Token_Using_Json_Net()
.HaveCount(3, "because the token should consist of three parts");
}

#if NETSTANDARD2_0 || NET6_0
#if NETSTANDARD2_0 || NET6_0 || NET7_0
[TestMethod]
public void Encode_Test_Bug438()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/JWT.Tests.Common/JWT.Tests.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0;net462</TargetFrameworks>
<TargetFrameworks>net7.0;net6.0;netstandard2.0;net462</TargetFrameworks>
<IsTestProject>false</IsTestProject>
</PropertyGroup>

Expand Down
14 changes: 14 additions & 0 deletions tests/JWT.Tests.Common/Models/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ public static class TestData
Age = 33
};

public class TestDataJsonNetDecorated
{
[Newtonsoft.Json.JsonProperty("AT")]
public string AccessToken { get; set; }
}

#if NETSTANDARD2_0 || NET6_0 || NET7_0
public class TestDataSystemTextSerializerDecorated
{
[System.Text.Json.Serialization.JsonPropertyName("AT")]
public string AccessToken { get; set; }
}
#endif

public const string Secret = "GQDstcKsx0NHjPOuXOYg5MbeJ1XT0uFiwDVvVBrk";
public const string Secret2 = "QWORIJkmQWEDIHbjhOIHAUSDFOYnUGWEYT";

Expand Down
12 changes: 12 additions & 0 deletions tests/JWT.Tests.Net70/JWT.Tests.Net70.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\JWT\JWT.csproj" />
<ProjectReference Include="..\JWT.Tests.Common\JWT.Tests.Common.csproj" />
</ItemGroup>

</Project>