Skip to content

Commit

Permalink
Fix for when encoding Json.NET decorated payload jwt-dotnet#456
Browse files Browse the repository at this point in the history
  • Loading branch information
hartmark committed Jan 24, 2023
1 parent 03ad1ef commit e4ffac5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
17 changes: 16 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(GetPropName, prop => prop.GetValue(payload, null));

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

private static string GetPropName(MemberInfo prop)
{
var customAttributes = prop.GetCustomAttributes(true);
foreach (var attribute in customAttributes)
{
if (attribute is JsonPropertyAttribute jsonNetProperty)
{
return jsonNetProperty.PropertyName;
}
}

return prop.Name;
}

/// <summary>
/// Decodes a token using the supplied dependencies.
/// </summary>
Expand Down
23 changes: 23 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,28 @@ public void Decode_ToDictionary_Without_Serializer_Should_Throw_Exception()
action.Should()
.Throw<ArgumentNullException>();
}

[TestMethod]
public void Encode_Decode_ToJsonNetDecoratedType_Should_UseDecoratedName()
{
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);
}
}
}
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
6 changes: 6 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,12 @@ public static class TestData
Age = 33
};

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

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

Expand Down

0 comments on commit e4ffac5

Please sign in to comment.