Skip to content

Commit

Permalink
Added so we can encode a whole object, so we have symmetry with the d…
Browse files Browse the repository at this point in the history
…ecode directly to object.
  • Loading branch information
hartmark committed Jun 10, 2022
1 parent 46eae50 commit 98e45b8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/JWT/Builder/JwtBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Reflection;
using JWT.Algorithms;
using static JWT.Internal.EncodingHelper;
using static JWT.Serializers.JsonSerializerFactory;
Expand Down Expand Up @@ -247,6 +248,22 @@ public string Encode()
return _encoder.Encode(_jwt.Header, _jwt.Payload, _secrets?[0]);
}

public string Encode<T>(T payload)
{
EnsureCanEncode();

var payloadAsDictionary = payload.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(prop => prop.Name, prop => prop.GetValue(payload, null));

foreach (var keyValuePair in payloadAsDictionary)
{
_jwt.Payload.Add(keyValuePair.Key, keyValuePair.Value);
}

return Encode();
}

/// <summary>
/// Decodes a token using the supplied dependencies.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#if SYSTEM_TEXT_JSON
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand Down Expand Up @@ -118,7 +120,15 @@ private static void HandleValue(Utf8JsonWriter writer, string key, object object
}
default:
{
writer.WriteNullValue();
var payloadPartAsDictionary = objectValue.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(prop => prop.Name, prop => prop.GetValue(objectValue, null));
writer.WriteStartObject();
foreach (var payloadPartKeyValue in payloadPartAsDictionary)
{
HandleValue(writer, payloadPartKeyValue.Key, payloadPartKeyValue.Value);
}
writer.WriteEndObject();
break;
}
}
Expand Down
30 changes: 30 additions & 0 deletions tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,36 @@ public void Encode_Should_Return_Token_With_Custom_Extra_Headers()
token.Should()
.Be(TestData.TokenWithCustomTypeHeader3, "because the same data encoded with the same key must result in the same token");
}

[TestMethod]
public void Encode_Should_Return_Token_With_Custom_Extra_Headers_Full_Payload()
{
const string key = TestData.Secret;

var token = JwtBuilder.Create()
.WithAlgorithm(new HMACSHA256Algorithm())
.WithSecret(key)
.AddHeader("version", 1)
.Encode(TestData.Customer);

token.Should()
.Be(TestData.TokenWithCustomTypeHeader3, "because the same data encoded with the same key must result in the same token");
}

[TestMethod]
public void Encode_Should_Return_Token_Nested_Data()
{
const string key = TestData.Secret;

var token = JwtBuilder.Create()
.WithAlgorithm(new HMACSHA256Algorithm())
.WithSecret(key)
.AddClaim<Customer>("Data", TestData.Customer)
.Encode();

token.Should()
.Be(TestData.TokenWithNestedData, "because the same data encoded with the same key must result in the same token");
}

[TestMethod]
public void Encode_With_Custom_Factory_Return_Token()
Expand Down
1 change: 1 addition & 0 deletions tests/JWT.Tests.Common/Models/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static class TestData
public const string TokenWithCustomTypeHeader = "eyJ0eXAiOiJmb28iLCJhbGciOiJIUzI1NiJ9.eyJGaXJzdE5hbWUiOiJKZXN1cyIsIkFnZSI6MzN9.vubwuLxx_7AWGvo-Y8XF_l7XP1WOv5obJulIk3RlVdk";
public const string TokenWithCustomTypeHeader2 = "eyJraWQiOiI0MiIsInR5cCI6IkpXVCIsImFsZyI6IkhTMjU2In0.eyJGaXJzdE5hbWUiOiJKZXN1cyIsIkFnZSI6MzN9.sPwGfDyhArZCmWRHTxm0xzNeG1gCf-qXhz21PdUxS4k";
public const string TokenWithCustomTypeHeader3 = "eyJ2ZXJzaW9uIjoxLCJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJGaXJzdE5hbWUiOiJKZXN1cyIsIkFnZSI6MzN9.TSQb2zVBJL9uY6mIdBKFaEooR-0OPjR-FPqY7hzwzwU";
public const string TokenWithNestedData = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJEYXRhIjp7IkZpcnN0TmFtZSI6Ikplc3VzIiwiQWdlIjozM319.IDx9z7phNsm-0MQLpZ1fREcUtkKtAVnSV6MSeP2R88U";
public const string TokenWithExp = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJGaXJzdE5hbWUiOiJKZXN1cyIsIkFnZSI6MzMsImV4cCI6MTYwNTgzNDI1NX0.dOkG1StO33Ae0qFQbHLslvSsCV6ThLofjc885egDnuY";
public const string TokenWithNbf = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJGaXJzdE5hbWUiOiJKZXN1cyIsIkFnZSI6MzMsIm5iZiI6MTYwNTgzNDI1NX0.iuxTYx6CMcNaxgvPn8pfnPFDhIZceKB0PrIZgkmHFbg";
public const long TokenTimestamp = 1605834255;
Expand Down

0 comments on commit 98e45b8

Please sign in to comment.