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

Added option to select serializer #433

Merged
merged 48 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
8b25f5f
First take
Aug 25, 2022
f94fc90
added some tests
Aug 25, 2022
81af0db
Update JwtBuilderEncodeTests.cs
abatishchev Aug 26, 2022
73fe125
Update JwtEncoderTests.cs
abatishchev Aug 26, 2022
1ee6b46
bumped version
Aug 26, 2022
c1a5b3c
Added assert to test and method to the JsonSerializerFactory
Aug 26, 2022
1c4421b
Lowered target framework for .net40 test so we get net461 so it uses …
hartmark Aug 27, 2022
d4a5163
Fix test running in mono (.net framework on linux)
hartmark Aug 27, 2022
2b89f9c
made JsonSerializerFactory non-static
Aug 28, 2022
a6b8fa6
Update and rename JsonSerializerFactory.cs to DefaultJsonSerializerFa…
abatishchev Sep 8, 2022
b5b2e3c
Create IJsonSerializerFactory.cs
abatishchev Sep 8, 2022
6f57e14
Update IJsonSerializerFactory.cs
abatishchev Sep 8, 2022
c1c8171
Update DefaultJsonSerializerFactory.cs
abatishchev Sep 8, 2022
2f51d22
Create DelegateJsonSerializerFactory.cs
abatishchev Sep 8, 2022
52a22c1
Update DelegateJsonSerializerFactory.cs
abatishchev Sep 8, 2022
2a994f7
Update ServiceCollectionExtensions.cs
abatishchev Sep 8, 2022
6d933cd
Update JwtBuilder.cs
abatishchev Sep 8, 2022
15b6952
Update DelegateJsonSerializerFactory.cs
abatishchev Sep 8, 2022
171ac0a
Update DelegateJsonSerializerFactory.cs
abatishchev Sep 8, 2022
b21bb1f
Update DelegateAlgorithmFactory.cs
abatishchev Sep 8, 2022
c8001a7
Update DelegateJsonSerializerFactory.cs
abatishchev Sep 8, 2022
bd57102
Update DelegateJsonSerializerFactory.cs
abatishchev Sep 8, 2022
f5db345
Update DefaultJsonSerializerFactory.cs
abatishchev Sep 8, 2022
ffc16cc
Update DelegateJsonSerializerFactory.cs
abatishchev Sep 8, 2022
b7713ce
Update DelegateJsonSerializerFactory.cs
abatishchev Sep 8, 2022
7f6fc45
Update JwtBuilder.cs
abatishchev Sep 8, 2022
024265d
Update JwtBuilder.cs
abatishchev Sep 8, 2022
9945642
Update JwtBuilderEncodeTests.cs
abatishchev Sep 8, 2022
cf59026
Update JwtDecoderTests.cs
abatishchev Sep 8, 2022
b9e0d92
Update SerializerTests.cs
abatishchev Sep 8, 2022
5437103
Update JwtEncoderTests.cs
abatishchev Sep 8, 2022
b604fac
Update SerializerTests.cs
abatishchev Sep 8, 2022
82fe9c5
Update JwtBuilder.cs
abatishchev Sep 8, 2022
adc1d66
Update JwtBuilder.cs
abatishchev Sep 8, 2022
dd65918
Update and rename SerializerTests.cs to DefaultJsonSerializerFactoryT…
abatishchev Sep 8, 2022
b7a9cea
Update JwtValidatorTests.cs
abatishchev Sep 8, 2022
8d314cd
Update DefaultJsonSerializerFactory.cs
abatishchev Sep 8, 2022
54299a5
Update DefaultJsonSerializerFactoryTests.cs
abatishchev Sep 8, 2022
da82a6c
Update JwtBuilderEncodeTests.cs
abatishchev Sep 8, 2022
027cb83
Update JwtBuilderDecodeTests.cs
abatishchev Sep 8, 2022
bf981de
Update JwtValidatorTests.cs
abatishchev Sep 8, 2022
d7bf51e
Update JwtSecurityTests.cs
abatishchev Sep 8, 2022
440da08
Update JwtBuilderDecodeTests.cs
abatishchev Sep 8, 2022
888c0c1
Update JwtAuthenticationHandlerTests.cs
abatishchev Sep 8, 2022
42155c8
Update JwtBuilderDecodeTests.cs
abatishchev Sep 8, 2022
2780209
Update JwtAuthenticationHandlerTests.cs
abatishchev Sep 8, 2022
fa7c10a
Update ServiceCollectionExtensions.cs
abatishchev Sep 8, 2022
4f18830
Update ServiceCollectionExtensions.cs
abatishchev Sep 8, 2022
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
8 changes: 1 addition & 7 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@
<Choose>
<When Condition="'$(TargetFramework)' == 'net462' OR '$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'net6.0'">
<PropertyGroup>
<DefineConstants>$(DefineConstants);SYSTEM_TEXT_JSON</DefineConstants>
<DefineConstants>$(DefineConstants);MODERN_DOTNET</DefineConstants>
abatishchev marked this conversation as resolved.
Show resolved Hide resolved
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<DefineConstants>$(DefineConstants);NEWTONSOFT_JSON</DefineConstants>
</PropertyGroup>
</Otherwise>
</Choose>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
<PackageReference Condition="$(DefineConstants.Contains(SYSTEM_TEXT_JSON))" Include="System.Text.Json" Version="6.0.4" />
<PackageReference Condition="$(DefineConstants.Contains(NEWTONSOFT_JSON))" Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Condition="$(DefineConstants.Contains(MODERN_DOTNET))" Include="System.Text.Json" Version="6.0.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.25" />
<PackageReference Condition="$(DefineConstants.Contains(SYSTEM_TEXT_JSON))" Include="System.Text.Json" Version="6.0.4" />
<PackageReference Condition="$(DefineConstants.Contains(NEWTONSOFT_JSON))" Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Condition="$(DefineConstants.Contains(MODERN_DOTNET))" Include="System.Text.Json" Version="6.0.4" />
hartmark marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
using System;
using JWT.Algorithms;
using JWT.Internal;
using JWT.Serializers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

#if SYSTEM_TEXT_JSON
using JsonSerializer = JWT.Serializers.SystemTextSerializer;
#elif NEWTONSOFT_JSON
using JsonSerializer = JWT.Serializers.JsonNetSerializer;
#endif

namespace JWT
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddJwtEncoder(this IServiceCollection services)
{
services.TryAddSingleton<IJwtEncoder, JwtEncoder>();
services.TryAddSingleton<IJsonSerializer, JsonSerializer>();
services.TryAddSingleton(JsonSerializerFactory.Serializer);
services.TryAddSingleton<IBase64UrlEncoder, JwtBase64UrlEncoder>();

return services;
Expand All @@ -44,7 +39,7 @@ private static IServiceCollection AddJwtEncoder<TFactory>(this IServiceCollectio
public static IServiceCollection AddJwtDecoder(this IServiceCollection services)
{
services.TryAddSingleton<IJwtDecoder, JwtDecoder>();
services.TryAddSingleton<IJsonSerializer, JsonSerializer>();
services.TryAddSingleton(JsonSerializerFactory.Serializer);
services.TryAddSingleton<IJwtValidator, JwtValidator>();
services.TryAddSingleton<IBase64UrlEncoder, JwtBase64UrlEncoder>();
services.TryAddSingleton<IDateTimeProvider, UtcDatetimeProvider>();
Expand Down
3 changes: 3 additions & 0 deletions src/JWT/Builder/JwtBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public JwtBuilder AddClaim(string name, object value)
public JwtBuilder WithSerializer(IJsonSerializer serializer)
{
_serializer = serializer;
#if MODERN_DOTNET
hartmark marked this conversation as resolved.
Show resolved Hide resolved
Serializer = serializer;
#endif
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions src/JWT/Builder/JwtHeader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#if SYSTEM_TEXT_JSON
#if MODERN_DOTNET
using JsonProperty = System.Text.Json.Serialization.JsonPropertyNameAttribute;
#elif NEWTONSOFT_JSON
#else
using Newtonsoft.Json;
#endif

Expand Down
4 changes: 2 additions & 2 deletions src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Condition="$(DefineConstants.Contains(SYSTEM_TEXT_JSON))" Include="System.Text.Json" Version="6.0.4" />
<PackageReference Condition="$(DefineConstants.Contains(NEWTONSOFT_JSON))" Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Condition="$(DefineConstants.Contains(MODERN_DOTNET))" Include="System.Text.Json" Version="6.0.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3' OR '$(TargetFramework)' == 'netstandard2.0'">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if SYSTEM_TEXT_JSON
#if MODERN_DOTNET
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
4 changes: 1 addition & 3 deletions src/JWT/Serializers/JsonNetSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if NEWTONSOFT_JSON
using System;
using System;
using System.IO;
using System.Text;
using Newtonsoft.Json;
Expand Down Expand Up @@ -61,4 +60,3 @@ public object Deserialize(Type type, string json)
}
}
}
#endif
26 changes: 19 additions & 7 deletions src/JWT/Serializers/JsonSerializerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
namespace JWT.Serializers
{
internal static class JsonSerializerFactory
public static class JsonSerializerFactory
hartmark marked this conversation as resolved.
Show resolved Hide resolved
{
public static IJsonSerializer CreateSerializer()
private static IJsonSerializer _serializer;

public static IJsonSerializer Serializer
hartmark marked this conversation as resolved.
Show resolved Hide resolved
{
#if SYSTEM_TEXT_JSON
return new SystemTextSerializer();
#elif NEWTONSOFT_JSON
return new JsonNetSerializer();
get
{
hartmark marked this conversation as resolved.
Show resolved Hide resolved
#if MODERN_DOTNET
return _serializer ??= new SystemTextSerializer();
#else
throw new System.NotSupportedException();

return _serializer ??= new JsonNetSerializer();
#endif
}
#if MODERN_DOTNET
set
{
_serializer = value;
}
#endif
}

public static IJsonSerializer CreateSerializer() => Serializer;
}
}
2 changes: 1 addition & 1 deletion src/JWT/Serializers/SystemTextSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if SYSTEM_TEXT_JSON
#if MODERN_DOTNET
using System;
using System.Text.Json;
using JWT.Serializers.Converters;
Expand Down
23 changes: 21 additions & 2 deletions tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
Expand All @@ -7,6 +7,7 @@
using FluentAssertions;
using JWT.Algorithms;
using JWT.Builder;
using JWT.Serializers;
using JWT.Tests.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -294,7 +295,7 @@ public void Encode_Should_Return_Token_With_Custom_Extra_Headers_Full_Payload2()
});

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

[TestMethod]
Expand Down Expand Up @@ -326,6 +327,24 @@ public void Encode_With_Custom_Factory_Return_Token()
.NotBeNullOrEmpty("because the token should contains some data");
}

[TestMethod]
public void Encode_With_Secret_Should_Return_Valid_Token_Using_Json_Net()
{
var secret = _fixture.Create<string>();

var token = JwtBuilder.Create()
.WithAlgorithm(TestData.HMACSHA256Algorithm)
.WithSecret(secret)
.WithSerializer(new JsonNetSerializer())
.Encode();

token.Should()
.NotBeNullOrEmpty("because the token should contains some data");
token.Split('.')
.Should()
.HaveCount(3, "because the token should consist of three parts");
}

private sealed class CustomFactory : IAlgorithmFactory
{
public IJwtAlgorithm Create(JwtDecoderContext context) =>
Expand Down
21 changes: 20 additions & 1 deletion tests/JWT.Tests.Common/JwtEncoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using FluentAssertions;
using JWT.Algorithms;
using JWT.Serializers;
using JWT.Tests.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -91,5 +92,23 @@ public void Encode_With_NoAlgorithm_Should_Encode_To_Token()
actual.Should()
.Be(expected, "because the same data encoded with the same key must result in the same token");
}

[TestMethod]
public void Encode_Should_Encode_To_Token_Using_Json_Net()
{
const string key = TestData.Secret;
var toEncode = TestData.Customer;
const string expected = TestData.Token;

var algorithm = new HMACSHA256Algorithm();
var urlEncoder = new JwtBase64UrlEncoder();
var serializer = new JsonNetSerializer();
var encoder = new JwtEncoder(algorithm, serializer, urlEncoder);

var actual = encoder.Encode(toEncode, key);

actual.Should()
.Be(expected, "because the same data encoded with the same key must result in the same token");
}
}
}
}
17 changes: 17 additions & 0 deletions tests/JWT.Tests.Common/SerializerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using JWT.Serializers;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JWT.Tests
{
[TestClass]
public class SerializerTests
{
[TestMethod]
public void Serializer_Should_Use_Correct_Default()
{
var serializer = JsonSerializerFactory.Serializer;
Console.WriteLine(serializer.GetType().Name);
hartmark marked this conversation as resolved.
Show resolved Hide resolved
}
}
}