From 8b25f5f669a270f8cbfe042f54420d0dee553e85 Mon Sep 17 00:00:00 2001 From: Markus Hartung Date: Fri, 26 Aug 2022 00:30:20 +0200 Subject: [PATCH 01/48] First take --- src/Directory.Build.targets | 8 +----- .../JWT.Extensions.AspNetCore.csproj | 4 +-- .../JWT.Extensions.DependencyInjection.csproj | 4 +-- .../ServiceCollectionExtensions.cs | 11 +++----- src/JWT/Builder/JwtBuilder.cs | 3 +++ src/JWT/Builder/JwtHeader.cs | 4 +-- src/JWT/JWT.csproj | 4 +-- .../DictionaryStringObjectJsonConverter.cs | 2 +- src/JWT/Serializers/JsonNetSerializer.cs | 4 +-- src/JWT/Serializers/JsonSerializerFactory.cs | 26 ++++++++++++++----- src/JWT/Serializers/SystemTextSerializer.cs | 2 +- .../Builder/JwtBuilderEncodeTests.cs | 3 ++- tests/JWT.Tests.Common/SerializerTests.cs | 17 ++++++++++++ 13 files changed, 56 insertions(+), 36 deletions(-) create mode 100644 tests/JWT.Tests.Common/SerializerTests.cs diff --git a/src/Directory.Build.targets b/src/Directory.Build.targets index 3d32dc3af..0930dcbca 100644 --- a/src/Directory.Build.targets +++ b/src/Directory.Build.targets @@ -4,14 +4,8 @@ - $(DefineConstants);SYSTEM_TEXT_JSON + $(DefineConstants);MODERN_DOTNET - - - $(DefineConstants);NEWTONSOFT_JSON - - - \ No newline at end of file diff --git a/src/JWT.Extensions.AspNetCore/JWT.Extensions.AspNetCore.csproj b/src/JWT.Extensions.AspNetCore/JWT.Extensions.AspNetCore.csproj index 2ddf10770..dc63ccd9c 100644 --- a/src/JWT.Extensions.AspNetCore/JWT.Extensions.AspNetCore.csproj +++ b/src/JWT.Extensions.AspNetCore/JWT.Extensions.AspNetCore.csproj @@ -30,8 +30,8 @@ - - + + diff --git a/src/JWT.Extensions.DependencyInjection/JWT.Extensions.DependencyInjection.csproj b/src/JWT.Extensions.DependencyInjection/JWT.Extensions.DependencyInjection.csproj index b8d721153..77809eca2 100644 --- a/src/JWT.Extensions.DependencyInjection/JWT.Extensions.DependencyInjection.csproj +++ b/src/JWT.Extensions.DependencyInjection/JWT.Extensions.DependencyInjection.csproj @@ -30,8 +30,8 @@ - - + + diff --git a/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs b/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs index 73fbbc037..e5935cc49 100644 --- a/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs @@ -1,15 +1,10 @@ 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 @@ -17,7 +12,7 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddJwtEncoder(this IServiceCollection services) { services.TryAddSingleton(); - services.TryAddSingleton(); + services.TryAddSingleton(JsonSerializerFactory.Serializer); services.TryAddSingleton(); return services; @@ -44,7 +39,7 @@ private static IServiceCollection AddJwtEncoder(this IServiceCollectio public static IServiceCollection AddJwtDecoder(this IServiceCollection services) { services.TryAddSingleton(); - services.TryAddSingleton(); + services.TryAddSingleton(JsonSerializerFactory.Serializer); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); diff --git a/src/JWT/Builder/JwtBuilder.cs b/src/JWT/Builder/JwtBuilder.cs index f682beaf3..02c45649b 100644 --- a/src/JWT/Builder/JwtBuilder.cs +++ b/src/JWT/Builder/JwtBuilder.cs @@ -78,6 +78,9 @@ public JwtBuilder AddClaim(string name, object value) public JwtBuilder WithSerializer(IJsonSerializer serializer) { _serializer = serializer; +#if MODERN_DOTNET + Serializer = serializer; +#endif return this; } diff --git a/src/JWT/Builder/JwtHeader.cs b/src/JWT/Builder/JwtHeader.cs index 8614fd499..08ad1ba72 100644 --- a/src/JWT/Builder/JwtHeader.cs +++ b/src/JWT/Builder/JwtHeader.cs @@ -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 diff --git a/src/JWT/JWT.csproj b/src/JWT/JWT.csproj index 3670d5ca0..0de1fc3ea 100644 --- a/src/JWT/JWT.csproj +++ b/src/JWT/JWT.csproj @@ -38,8 +38,8 @@ - - + + diff --git a/src/JWT/Serializers/Converters/DictionaryStringObjectJsonConverter.cs b/src/JWT/Serializers/Converters/DictionaryStringObjectJsonConverter.cs index 760624758..7fdbf2236 100644 --- a/src/JWT/Serializers/Converters/DictionaryStringObjectJsonConverter.cs +++ b/src/JWT/Serializers/Converters/DictionaryStringObjectJsonConverter.cs @@ -1,4 +1,4 @@ -#if SYSTEM_TEXT_JSON +#if MODERN_DOTNET using System; using System.Collections.Generic; using System.Linq; diff --git a/src/JWT/Serializers/JsonNetSerializer.cs b/src/JWT/Serializers/JsonNetSerializer.cs index 4b7864a07..4dbb02115 100644 --- a/src/JWT/Serializers/JsonNetSerializer.cs +++ b/src/JWT/Serializers/JsonNetSerializer.cs @@ -1,5 +1,4 @@ -#if NEWTONSOFT_JSON -using System; +using System; using System.IO; using System.Text; using Newtonsoft.Json; @@ -61,4 +60,3 @@ public object Deserialize(Type type, string json) } } } -#endif diff --git a/src/JWT/Serializers/JsonSerializerFactory.cs b/src/JWT/Serializers/JsonSerializerFactory.cs index 884861603..cb0ea96a4 100644 --- a/src/JWT/Serializers/JsonSerializerFactory.cs +++ b/src/JWT/Serializers/JsonSerializerFactory.cs @@ -1,16 +1,28 @@ namespace JWT.Serializers { - internal static class JsonSerializerFactory + public static class JsonSerializerFactory { - public static IJsonSerializer CreateSerializer() + private static IJsonSerializer _serializer; + + public static IJsonSerializer Serializer { -#if SYSTEM_TEXT_JSON - return new SystemTextSerializer(); -#elif NEWTONSOFT_JSON - return new JsonNetSerializer(); + get + { +#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; } } \ No newline at end of file diff --git a/src/JWT/Serializers/SystemTextSerializer.cs b/src/JWT/Serializers/SystemTextSerializer.cs index c66c4d16b..16f9dde0c 100644 --- a/src/JWT/Serializers/SystemTextSerializer.cs +++ b/src/JWT/Serializers/SystemTextSerializer.cs @@ -1,4 +1,4 @@ -#if SYSTEM_TEXT_JSON +#if MODERN_DOTNET using System; using System.Text.Json; using JWT.Serializers.Converters; diff --git a/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs b/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs index a3f3d72c6..9879ac923 100644 --- a/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs +++ b/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Globalization; using System.Linq; using System.Reflection; @@ -7,6 +7,7 @@ using FluentAssertions; using JWT.Algorithms; using JWT.Builder; +using JWT.Serializers; using JWT.Tests.Models; using Microsoft.VisualStudio.TestTools.UnitTesting; diff --git a/tests/JWT.Tests.Common/SerializerTests.cs b/tests/JWT.Tests.Common/SerializerTests.cs new file mode 100644 index 000000000..d86852501 --- /dev/null +++ b/tests/JWT.Tests.Common/SerializerTests.cs @@ -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); + } + } +} \ No newline at end of file From f94fc9004cda2364726adc7d0e26d891cddbf666 Mon Sep 17 00:00:00 2001 From: Markus Hartung Date: Fri, 26 Aug 2022 00:40:08 +0200 Subject: [PATCH 02/48] added some tests --- .../Builder/JwtBuilderEncodeTests.cs | 18 ++++++++++++++++++ tests/JWT.Tests.Common/JwtEncoderTests.cs | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs b/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs index 9879ac923..2de54e689 100644 --- a/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs +++ b/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs @@ -327,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(); + + 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) => diff --git a/tests/JWT.Tests.Common/JwtEncoderTests.cs b/tests/JWT.Tests.Common/JwtEncoderTests.cs index 640770624..bd928e196 100644 --- a/tests/JWT.Tests.Common/JwtEncoderTests.cs +++ b/tests/JWT.Tests.Common/JwtEncoderTests.cs @@ -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; @@ -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"); + } } } \ No newline at end of file From 81af0db938546f7003ec7b7e0149c0c5419c7c2b Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 25 Aug 2022 19:12:27 -0700 Subject: [PATCH 03/48] Update JwtBuilderEncodeTests.cs --- .../Builder/JwtBuilderEncodeTests.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs b/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs index 2de54e689..d7919ee0f 100644 --- a/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs +++ b/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs @@ -295,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] @@ -333,16 +333,16 @@ public void Encode_With_Secret_Should_Return_Valid_Token_Using_Json_Net() var secret = _fixture.Create(); var token = JwtBuilder.Create() - .WithAlgorithm(TestData.HMACSHA256Algorithm) - .WithSecret(secret) - .WithSerializer(new JsonNetSerializer()) - .Encode(); + .WithAlgorithm(TestData.HMACSHA256Algorithm) + .WithSecret(secret) + .WithSerializer(new JsonNetSerializer()) + .Encode(); token.Should() - .NotBeNullOrEmpty("because the token should contains some data"); + .NotBeNullOrEmpty("because the token should contains some data"); token.Split('.') - .Should() - .HaveCount(3, "because the token should consist of three parts"); + .Should() + .HaveCount(3, "because the token should consist of three parts"); } private sealed class CustomFactory : IAlgorithmFactory From 73fe125c2aa4f10c769a9b0e0f7b31a9940a7754 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 25 Aug 2022 19:12:51 -0700 Subject: [PATCH 04/48] Update JwtEncoderTests.cs --- tests/JWT.Tests.Common/JwtEncoderTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/JWT.Tests.Common/JwtEncoderTests.cs b/tests/JWT.Tests.Common/JwtEncoderTests.cs index bd928e196..8ac8bacf5 100644 --- a/tests/JWT.Tests.Common/JwtEncoderTests.cs +++ b/tests/JWT.Tests.Common/JwtEncoderTests.cs @@ -108,7 +108,7 @@ public void Encode_Should_Encode_To_Token_Using_Json_Net() 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"); + .Be(expected, "because the same data encoded with the same key must result in the same token"); } } -} \ No newline at end of file +} From 1ee6b46b345b436c40983d7f3d861b850132bea4 Mon Sep 17 00:00:00 2001 From: Markus Hartung Date: Sat, 27 Aug 2022 00:26:08 +0200 Subject: [PATCH 05/48] bumped version --- src/JWT.Extensions.AspNetCore/JWT.Extensions.AspNetCore.csproj | 2 +- .../JWT.Extensions.DependencyInjection.csproj | 2 +- src/JWT/JWT.csproj | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/JWT.Extensions.AspNetCore/JWT.Extensions.AspNetCore.csproj b/src/JWT.Extensions.AspNetCore/JWT.Extensions.AspNetCore.csproj index dc63ccd9c..b94df00d2 100644 --- a/src/JWT.Extensions.AspNetCore/JWT.Extensions.AspNetCore.csproj +++ b/src/JWT.Extensions.AspNetCore/JWT.Extensions.AspNetCore.csproj @@ -11,7 +11,7 @@ Alexander Batishchev jwt;json;asp.net;asp.net core;.net core;authorization MIT - 10.0.0-beta3 + 10.0.0-beta4 10.0.0.0 10.0.0.0 JWT.Extensions.AspNetCore diff --git a/src/JWT.Extensions.DependencyInjection/JWT.Extensions.DependencyInjection.csproj b/src/JWT.Extensions.DependencyInjection/JWT.Extensions.DependencyInjection.csproj index 77809eca2..65072da4b 100644 --- a/src/JWT.Extensions.DependencyInjection/JWT.Extensions.DependencyInjection.csproj +++ b/src/JWT.Extensions.DependencyInjection/JWT.Extensions.DependencyInjection.csproj @@ -11,7 +11,7 @@ Alexander Batishchev jwt;json;asp.net;asp.net core;.net core;authorization;dependenсy injection MIT - 2.2.0-beta3 + 2.2.0-beta4 2.0.0.0 2.0.0.0 JWT diff --git a/src/JWT/JWT.csproj b/src/JWT/JWT.csproj index 0de1fc3ea..7d891e154 100644 --- a/src/JWT/JWT.csproj +++ b/src/JWT/JWT.csproj @@ -20,7 +20,7 @@ Alexander Batishchev, John Sheehan, Michael Lehenbauer jwt;json;authorization CC0-1.0 - 10.0.0-beta6 + 10.0.0-beta7 10.0.0.0 10.0.0.0 JWT From c1a5b3c6020897623acb6ceceb2e2a0372cd5f5f Mon Sep 17 00:00:00 2001 From: Markus Hartung Date: Sat, 27 Aug 2022 00:52:37 +0200 Subject: [PATCH 06/48] Added assert to test and method to the JsonSerializerFactory --- .../ServiceCollectionExtensions.cs | 4 +- src/JWT/Builder/JwtBuilder.cs | 2 +- src/JWT/Serializers/JsonSerializerFactory.cs | 27 +++++-------- tests/JWT.Tests.Common/SerializerTests.cs | 40 ++++++++++++++++++- 4 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs b/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs index e5935cc49..655988937 100644 --- a/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs @@ -12,7 +12,7 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddJwtEncoder(this IServiceCollection services) { services.TryAddSingleton(); - services.TryAddSingleton(JsonSerializerFactory.Serializer); + services.TryAddSingleton(JsonSerializerFactory.CreateSerializer()); services.TryAddSingleton(); return services; @@ -39,7 +39,7 @@ private static IServiceCollection AddJwtEncoder(this IServiceCollectio public static IServiceCollection AddJwtDecoder(this IServiceCollection services) { services.TryAddSingleton(); - services.TryAddSingleton(JsonSerializerFactory.Serializer); + services.TryAddSingleton(JsonSerializerFactory.CreateSerializer()); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); diff --git a/src/JWT/Builder/JwtBuilder.cs b/src/JWT/Builder/JwtBuilder.cs index 02c45649b..47e007a50 100644 --- a/src/JWT/Builder/JwtBuilder.cs +++ b/src/JWT/Builder/JwtBuilder.cs @@ -79,7 +79,7 @@ public JwtBuilder WithSerializer(IJsonSerializer serializer) { _serializer = serializer; #if MODERN_DOTNET - Serializer = serializer; + SetSerializer(serializer); #endif return this; } diff --git a/src/JWT/Serializers/JsonSerializerFactory.cs b/src/JWT/Serializers/JsonSerializerFactory.cs index cb0ea96a4..613c8f179 100644 --- a/src/JWT/Serializers/JsonSerializerFactory.cs +++ b/src/JWT/Serializers/JsonSerializerFactory.cs @@ -2,27 +2,22 @@ { public static class JsonSerializerFactory { - private static IJsonSerializer _serializer; + private static IJsonSerializer _jsonSerializer; - public static IJsonSerializer Serializer - { - get - { #if MODERN_DOTNET - return _serializer ??= new SystemTextSerializer(); -#else - - return _serializer ??= new JsonNetSerializer(); + public static void SetSerializer(IJsonSerializer jsonSerializer) + { + _jsonSerializer = jsonSerializer; + } #endif - } + + public static IJsonSerializer CreateSerializer() + { #if MODERN_DOTNET - set - { - _serializer = value; - } + return _jsonSerializer ??= new SystemTextSerializer(); +#else + return _jsonSerializer ??= new JsonNetSerializer(); #endif } - - public static IJsonSerializer CreateSerializer() => Serializer; } } \ No newline at end of file diff --git a/tests/JWT.Tests.Common/SerializerTests.cs b/tests/JWT.Tests.Common/SerializerTests.cs index d86852501..74179550d 100644 --- a/tests/JWT.Tests.Common/SerializerTests.cs +++ b/tests/JWT.Tests.Common/SerializerTests.cs @@ -1,4 +1,6 @@ using System; +using System.Reflection; +using System.Runtime.Versioning; using JWT.Serializers; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -10,8 +12,42 @@ public class SerializerTests [TestMethod] public void Serializer_Should_Use_Correct_Default() { - var serializer = JsonSerializerFactory.Serializer; - Console.WriteLine(serializer.GetType().Name); + var serializer = JsonSerializerFactory.CreateSerializer(); + + var dotnetVersion = GetRunningDotnetVersion(); + Console.WriteLine($"Dotnet version: {dotnetVersion}"); + + var defaultSerializerClass = serializer.GetType().Name; + Console.WriteLine($"Default serializer class: {defaultSerializerClass}"); + + switch (dotnetVersion) + { + case ".NETFramework,Version=v4.6.2": + case ".NETCoreApp,Version=v3.0": + Assert.AreEqual(nameof(SystemTextSerializer), defaultSerializerClass); + break; + + default: + Assert.Fail($"Unrecognized dotnet version {dotnetVersion}"); + break; + } + } + + /// + /// Copied from: https://stackoverflow.com/a/49754978 + /// + /// The running dotnet version. + private string GetRunningDotnetVersion() + { + string version = Assembly.GetEntryAssembly()?.GetCustomAttribute()?.FrameworkName; + + if (!string.IsNullOrEmpty(version)) + { + return version; + } + + version = AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName; + return version; } } } \ No newline at end of file From 1c4421bf19be9c5e3c1abc4c4f9f7be361f92ce8 Mon Sep 17 00:00:00 2001 From: Markus Hartung Date: Sat, 27 Aug 2022 02:00:00 +0200 Subject: [PATCH 07/48] Lowered target framework for .net40 test so we get net461 so it uses JSON.net --- tests/JWT.Tests.Common/SerializerTests.cs | 20 +++++++++++--------- tests/JWT.Tests.Net40/JWT.Tests.Net40.csproj | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/tests/JWT.Tests.Common/SerializerTests.cs b/tests/JWT.Tests.Common/SerializerTests.cs index 74179550d..3b69912eb 100644 --- a/tests/JWT.Tests.Common/SerializerTests.cs +++ b/tests/JWT.Tests.Common/SerializerTests.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Reflection; using System.Runtime.Versioning; using JWT.Serializers; @@ -24,9 +25,13 @@ public void Serializer_Should_Use_Correct_Default() { case ".NETFramework,Version=v4.6.2": case ".NETCoreApp,Version=v3.0": - Assert.AreEqual(nameof(SystemTextSerializer), defaultSerializerClass); + case ".NETCoreApp,Version=v6.0": + Assert.AreEqual("SystemTextSerializer", defaultSerializerClass); break; + case ".NETFramework,Version=v4.6.1": + Assert.AreEqual("JsonNetSerializer", defaultSerializerClass); + break; default: Assert.Fail($"Unrecognized dotnet version {dotnetVersion}"); break; @@ -39,14 +44,11 @@ public void Serializer_Should_Use_Correct_Default() /// The running dotnet version. private string GetRunningDotnetVersion() { - string version = Assembly.GetEntryAssembly()?.GetCustomAttribute()?.FrameworkName; - - if (!string.IsNullOrEmpty(version)) - { - return version; - } - - version = AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName; + var version = Assembly.GetExecutingAssembly() + .GetCustomAttributes(typeof(TargetFrameworkAttribute), false) + .Cast() + .Single() + .FrameworkName; return version; } } diff --git a/tests/JWT.Tests.Net40/JWT.Tests.Net40.csproj b/tests/JWT.Tests.Net40/JWT.Tests.Net40.csproj index 805adb97f..9e6bffc48 100644 --- a/tests/JWT.Tests.Net40/JWT.Tests.Net40.csproj +++ b/tests/JWT.Tests.Net40/JWT.Tests.Net40.csproj @@ -1,7 +1,7 @@  - net462 + net461 From d4a51639be680491fde3bb137d94aa45bbd3ae3c Mon Sep 17 00:00:00 2001 From: Markus Hartung Date: Sat, 27 Aug 2022 02:15:20 +0200 Subject: [PATCH 08/48] Fix test running in mono (.net framework on linux) --- .../Builder/JwtBuilderEncodeTests.cs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs b/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs index d7919ee0f..9ab6f57d0 100644 --- a/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs +++ b/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs @@ -269,8 +269,26 @@ public void Encode_Should_Return_ThrowTargetException_Encode_TypesMatch() .AddClaim("ExtraClaim", new { NestedProperty1 = "Foo", NestedProperty2 = 3 }) .Encode(typeof(string), TestData.Customer); - action.Should() - .Throw("Object does not match target type."); + if (IsRunningOnMono()) + { + action.Should() + .Throw("Exception has been thrown by the target of an invocation."); + } + else + { + action.Should() + .Throw("Object does not match target type."); + } + } + + /// + /// Copied from: https://stackoverflow.com/a/7077620/2890855 + /// + /// + private static bool IsRunningOnMono() + { + Type type = Type.GetType("Mono.Runtime"); + return type != null; } [TestMethod] From 2b89f9cdd409317d3595510605cc13d15493555d Mon Sep 17 00:00:00 2001 From: Markus Hartung Date: Sun, 28 Aug 2022 23:21:53 +0200 Subject: [PATCH 09/48] made JsonSerializerFactory non-static --- .../ServiceCollectionExtensions.cs | 4 +- src/JWT/Builder/JwtBuilder.cs | 38 +++++++++---------- src/JWT/Serializers/JsonSerializerFactory.cs | 23 ++++++----- .../JwtAuthenticationHandlerTests.cs | 8 +++- tests/JWT.Tests.Common/JwtDecoderTests.cs | 8 +++- tests/JWT.Tests.Common/JwtEncoderTests.cs | 8 ++-- tests/JWT.Tests.Common/JwtSecurityTests.cs | 8 +++- tests/JWT.Tests.Common/JwtValidatorTests.cs | 7 +++- tests/JWT.Tests.Common/SerializerTests.cs | 26 ++++++------- 9 files changed, 74 insertions(+), 56 deletions(-) diff --git a/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs b/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs index 655988937..088b47f50 100644 --- a/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs @@ -12,7 +12,7 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddJwtEncoder(this IServiceCollection services) { services.TryAddSingleton(); - services.TryAddSingleton(JsonSerializerFactory.CreateSerializer()); + services.TryAddSingleton(new JsonSerializerFactory().CreateSerializer()); services.TryAddSingleton(); return services; @@ -39,7 +39,7 @@ private static IServiceCollection AddJwtEncoder(this IServiceCollectio public static IServiceCollection AddJwtDecoder(this IServiceCollection services) { services.TryAddSingleton(); - services.TryAddSingleton(JsonSerializerFactory.CreateSerializer()); + services.TryAddSingleton(new JsonSerializerFactory().CreateSerializer()); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); diff --git a/src/JWT/Builder/JwtBuilder.cs b/src/JWT/Builder/JwtBuilder.cs index 47e007a50..81aa49f84 100644 --- a/src/JWT/Builder/JwtBuilder.cs +++ b/src/JWT/Builder/JwtBuilder.cs @@ -2,9 +2,8 @@ using System.Linq; using System.Reflection; using JWT.Algorithms; - +using JWT.Serializers; using static JWT.Internal.EncodingHelper; -using static JWT.Serializers.JsonSerializerFactory; namespace JWT.Builder { @@ -19,7 +18,9 @@ public sealed class JwtBuilder private IJwtDecoder _decoder; private IJwtValidator _validator; - private IJsonSerializer _serializer = CreateSerializer(); + private JsonSerializerFactory _jsonSerializerFactory = new(); + private IJsonSerializer Serializer => _jsonSerializerFactory.CreateSerializer(); + private IBase64UrlEncoder _urlEncoder = new JwtBase64UrlEncoder(); private IDateTimeProvider _dateTimeProvider = new UtcDateTimeProvider(); private ValidationParameters _valParams = ValidationParameters.Default; @@ -77,10 +78,7 @@ public JwtBuilder AddClaim(string name, object value) /// Current builder instance public JwtBuilder WithSerializer(IJsonSerializer serializer) { - _serializer = serializer; -#if MODERN_DOTNET - SetSerializer(serializer); -#endif + _jsonSerializerFactory = new JsonSerializerFactory(serializer); return this; } @@ -324,42 +322,42 @@ private void TryCreateEncoder() { if (_algorithm is null && _algFactory is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtEncoder)}. Call {nameof(WithAlgorithm)}."); - if (_serializer is null) + if (this.Serializer is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtEncoder)}. Call {nameof(WithSerializer)}"); if (_urlEncoder is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtEncoder)}. Call {nameof(WithUrlEncoder)}."); if (_algorithm is object) - _encoder = new JwtEncoder(_algorithm, _serializer, _urlEncoder); + _encoder = new JwtEncoder(_algorithm, this.Serializer, _urlEncoder); else if (_algFactory is object) - _encoder = new JwtEncoder(_algFactory, _serializer, _urlEncoder); + _encoder = new JwtEncoder(_algFactory, this.Serializer, _urlEncoder); } private void TryCreateDecoder() { TryCreateValidator(); - if (_serializer is null) + if (this.Serializer is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithSerializer)}."); if (_urlEncoder is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithUrlEncoder)}."); if (_algorithm is object) - _decoder = new JwtDecoder(_serializer, _validator, _urlEncoder, _algorithm); + _decoder = new JwtDecoder(this.Serializer, _validator, _urlEncoder, _algorithm); else if (_algFactory is object) - _decoder = new JwtDecoder(_serializer, _validator, _urlEncoder, _algFactory); + _decoder = new JwtDecoder(this.Serializer, _validator, _urlEncoder, _algFactory); else if (!_valParams.ValidateSignature) - _decoder = new JwtDecoder(_serializer, _urlEncoder); + _decoder = new JwtDecoder(this.Serializer, _urlEncoder); } private void TryCreateDecoderForHeader() { - if (_serializer is null) + if (this.Serializer is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithSerializer)}."); if (_urlEncoder is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithUrlEncoder)}."); - _decoder = new JwtDecoder(_serializer, _urlEncoder); + _decoder = new JwtDecoder(this.Serializer, _urlEncoder); } private void TryCreateValidator() @@ -367,12 +365,12 @@ private void TryCreateValidator() if (_validator is object) return; - if (_serializer is null) + if (this.Serializer is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtValidator)}. Call {nameof(WithSerializer)}."); if (_dateTimeProvider is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtValidator)}. Call {nameof(WithDateTimeProvider)}."); - _validator = new JwtValidator(_serializer, _dateTimeProvider, _valParams); + _validator = new JwtValidator(this.Serializer, _dateTimeProvider, _valParams); } private void EnsureCanEncode() @@ -424,7 +422,7 @@ private void EnsureCanDecodeHeader() /// private bool CanEncode() => (_algorithm is object || _algFactory is object) && - _serializer is object && + this.Serializer is object && _urlEncoder is object && _jwt.Payload is object; @@ -447,7 +445,7 @@ private bool CanDecodeHeader() if (_urlEncoder is null) return false; - if (_serializer is null) + if (this.Serializer is null) return false; return true; diff --git a/src/JWT/Serializers/JsonSerializerFactory.cs b/src/JWT/Serializers/JsonSerializerFactory.cs index 613c8f179..0e77a4729 100644 --- a/src/JWT/Serializers/JsonSerializerFactory.cs +++ b/src/JWT/Serializers/JsonSerializerFactory.cs @@ -1,23 +1,26 @@ namespace JWT.Serializers { - public static class JsonSerializerFactory + public class JsonSerializerFactory { - private static IJsonSerializer _jsonSerializer; + private readonly IJsonSerializer _jsonSerializer; + public JsonSerializerFactory() + { #if MODERN_DOTNET - public static void SetSerializer(IJsonSerializer jsonSerializer) + _jsonSerializer = new SystemTextSerializer(); +#else + _jsonSerializer = new JsonNetSerializer(); +#endif + } + + public JsonSerializerFactory(IJsonSerializer jsonSerializer) { _jsonSerializer = jsonSerializer; } -#endif - public static IJsonSerializer CreateSerializer() + public IJsonSerializer CreateSerializer() { -#if MODERN_DOTNET - return _jsonSerializer ??= new SystemTextSerializer(); -#else - return _jsonSerializer ??= new JsonNetSerializer(); -#endif + return _jsonSerializer; } } } \ No newline at end of file diff --git a/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs b/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs index 6cb450e54..be03131b6 100644 --- a/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs +++ b/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs @@ -4,6 +4,7 @@ using AutoFixture; using FluentAssertions; using JWT.Extensions.AspNetCore.Factories; +using JWT.Serializers; using JWT.Tests.Models; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Http; @@ -13,8 +14,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; -using static JWT.Serializers.JsonSerializerFactory; - namespace JWT.Extensions.AspNetCore.Tests { [TestClass] @@ -22,6 +21,11 @@ public class JwtAuthenticationHandlerTests { private static readonly Fixture _fixture = new Fixture(); + private static IJsonSerializer CreateSerializer() + { + return new JsonSerializerFactory().CreateSerializer(); + } + [TestMethod] public async Task HandleAuthenticateAsync_Should_Return_Success_When_Token_Is_Valid() { diff --git a/tests/JWT.Tests.Common/JwtDecoderTests.cs b/tests/JWT.Tests.Common/JwtDecoderTests.cs index bd7871a15..933834201 100644 --- a/tests/JWT.Tests.Common/JwtDecoderTests.cs +++ b/tests/JWT.Tests.Common/JwtDecoderTests.cs @@ -5,12 +5,11 @@ using JWT.Algorithms; using JWT.Builder; using JWT.Exceptions; +using JWT.Serializers; using JWT.Tests.Models; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; -using static JWT.Serializers.JsonSerializerFactory; - namespace JWT.Tests { [TestClass] @@ -18,6 +17,11 @@ public class JwtDecoderTests { private static readonly Fixture _fixture = new Fixture(); + private static IJsonSerializer CreateSerializer() + { + return new JsonSerializerFactory().CreateSerializer(); + } + [TestMethod] public void DecodeHeader_Should_Return_Header() { diff --git a/tests/JWT.Tests.Common/JwtEncoderTests.cs b/tests/JWT.Tests.Common/JwtEncoderTests.cs index 8ac8bacf5..fb18dc208 100644 --- a/tests/JWT.Tests.Common/JwtEncoderTests.cs +++ b/tests/JWT.Tests.Common/JwtEncoderTests.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using FluentAssertions; using JWT.Algorithms; @@ -6,13 +5,16 @@ using JWT.Tests.Models; using Microsoft.VisualStudio.TestTools.UnitTesting; -using static JWT.Serializers.JsonSerializerFactory; - namespace JWT.Tests { [TestClass] public class JwtEncoderTests { + private static IJsonSerializer CreateSerializer() + { + return new JsonSerializerFactory().CreateSerializer(); + } + [TestMethod] public void Encode_Should_Encode_To_Token() { diff --git a/tests/JWT.Tests.Common/JwtSecurityTests.cs b/tests/JWT.Tests.Common/JwtSecurityTests.cs index 2f8e04a6a..b0ae8237b 100644 --- a/tests/JWT.Tests.Common/JwtSecurityTests.cs +++ b/tests/JWT.Tests.Common/JwtSecurityTests.cs @@ -2,11 +2,10 @@ using AutoFixture; using FluentAssertions; using JWT.Algorithms; +using JWT.Serializers; using JWT.Tests.Models; using Microsoft.VisualStudio.TestTools.UnitTesting; -using static JWT.Serializers.JsonSerializerFactory; - namespace JWT.Tests { [TestClass] @@ -14,6 +13,11 @@ public class JwtSecurityTests { private static readonly Fixture _fixture = new Fixture(); + private static IJsonSerializer CreateSerializer() + { + return new JsonSerializerFactory().CreateSerializer(); + } + [TestMethod] [TestCategory("Security")] public void Decode_Should_Throw_Exception_When_Jwt_Contains_No_Algorithm() diff --git a/tests/JWT.Tests.Common/JwtValidatorTests.cs b/tests/JWT.Tests.Common/JwtValidatorTests.cs index 0c326ec47..7348248bc 100644 --- a/tests/JWT.Tests.Common/JwtValidatorTests.cs +++ b/tests/JWT.Tests.Common/JwtValidatorTests.cs @@ -2,18 +2,23 @@ using FluentAssertions; using JWT.Algorithms; using JWT.Exceptions; +using JWT.Serializers; using JWT.Tests.Models; using JWT.Tests.Stubs; using Microsoft.VisualStudio.TestTools.UnitTesting; using static JWT.Internal.EncodingHelper; -using static JWT.Serializers.JsonSerializerFactory; namespace JWT.Tests { [TestClass] public class JwtValidatorTests { + private static IJsonSerializer CreateSerializer() + { + return new JsonSerializerFactory().CreateSerializer(); + } + [TestMethod] public void Ctor_Should_Throw_Exception_When_Serializer_Is_Null() { diff --git a/tests/JWT.Tests.Common/SerializerTests.cs b/tests/JWT.Tests.Common/SerializerTests.cs index 3b69912eb..a48b59e36 100644 --- a/tests/JWT.Tests.Common/SerializerTests.cs +++ b/tests/JWT.Tests.Common/SerializerTests.cs @@ -13,7 +13,7 @@ public class SerializerTests [TestMethod] public void Serializer_Should_Use_Correct_Default() { - var serializer = JsonSerializerFactory.CreateSerializer(); + var serializer = new JsonSerializerFactory().CreateSerializer(); var dotnetVersion = GetRunningDotnetVersion(); Console.WriteLine($"Dotnet version: {dotnetVersion}"); @@ -21,20 +21,18 @@ public void Serializer_Should_Use_Correct_Default() var defaultSerializerClass = serializer.GetType().Name; Console.WriteLine($"Default serializer class: {defaultSerializerClass}"); - switch (dotnetVersion) + if (dotnetVersion is ".NETFramework,Version=v4.6.2" || + dotnetVersion.StartsWith(".NETCoreApp")) { - case ".NETFramework,Version=v4.6.2": - case ".NETCoreApp,Version=v3.0": - case ".NETCoreApp,Version=v6.0": - Assert.AreEqual("SystemTextSerializer", defaultSerializerClass); - break; - - case ".NETFramework,Version=v4.6.1": - Assert.AreEqual("JsonNetSerializer", defaultSerializerClass); - break; - default: - Assert.Fail($"Unrecognized dotnet version {dotnetVersion}"); - break; + Assert.AreEqual("SystemTextSerializer", defaultSerializerClass); + } + else if (dotnetVersion == ".NETFramework,Version=v4.6.1") + { + Assert.AreEqual("JsonNetSerializer", defaultSerializerClass); + } + else + { + Assert.Fail($"Unrecognized dotnet version {dotnetVersion}"); } } From a6b8fa65e7ae3f29bf3f97f9356ce5935372787d Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 21:56:13 -0700 Subject: [PATCH 10/48] Update and rename JsonSerializerFactory.cs to DefaultJsonSerializerFactory.cs --- ...ializerFactory.cs => DefaultJsonSerializerFactory.cs} | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) rename src/JWT/Serializers/{JsonSerializerFactory.cs => DefaultJsonSerializerFactory.cs} (67%) diff --git a/src/JWT/Serializers/JsonSerializerFactory.cs b/src/JWT/Serializers/DefaultJsonSerializerFactory.cs similarity index 67% rename from src/JWT/Serializers/JsonSerializerFactory.cs rename to src/JWT/Serializers/DefaultJsonSerializerFactory.cs index 0e77a4729..1ecfc055e 100644 --- a/src/JWT/Serializers/JsonSerializerFactory.cs +++ b/src/JWT/Serializers/DefaultJsonSerializerFactory.cs @@ -1,6 +1,6 @@ namespace JWT.Serializers { - public class JsonSerializerFactory + internal sealed class DefaultJsonSerializerFactory : IJsonSerializerFactory { private readonly IJsonSerializer _jsonSerializer; @@ -13,14 +13,9 @@ public JsonSerializerFactory() #endif } - public JsonSerializerFactory(IJsonSerializer jsonSerializer) - { - _jsonSerializer = jsonSerializer; - } - public IJsonSerializer CreateSerializer() { return _jsonSerializer; } } -} \ No newline at end of file +} From b5b2e3c1c0704faf19d5a03de3abf18f01ecb06e Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 21:57:57 -0700 Subject: [PATCH 11/48] Create IJsonSerializerFactory.cs --- src/JWT/Serializers/IJsonSerializerFactory.cs | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/JWT/Serializers/IJsonSerializerFactory.cs diff --git a/src/JWT/Serializers/IJsonSerializerFactory.cs b/src/JWT/Serializers/IJsonSerializerFactory.cs new file mode 100644 index 000000000..157dca075 --- /dev/null +++ b/src/JWT/Serializers/IJsonSerializerFactory.cs @@ -0,0 +1,7 @@ +namespace JWT.Serializers +{ + public interface IJsonSerializerFactory + { + IJsonSerializer CreateSerializer(); + } +} From 6f57e14ab84f20e658efd7ce81cda18ce06c161d Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 21:58:52 -0700 Subject: [PATCH 12/48] Update IJsonSerializerFactory.cs --- src/JWT/Serializers/IJsonSerializerFactory.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/JWT/Serializers/IJsonSerializerFactory.cs b/src/JWT/Serializers/IJsonSerializerFactory.cs index 157dca075..e245e8c89 100644 --- a/src/JWT/Serializers/IJsonSerializerFactory.cs +++ b/src/JWT/Serializers/IJsonSerializerFactory.cs @@ -1,7 +1,7 @@ -namespace JWT.Serializers +namespace JWT.Serializers { public interface IJsonSerializerFactory { - IJsonSerializer CreateSerializer(); + IJsonSerializer Create(); } } From c1c817120111c2e1d2294f0976c2262b844d8145 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 21:59:39 -0700 Subject: [PATCH 13/48] Update DefaultJsonSerializerFactory.cs --- src/JWT/Serializers/DefaultJsonSerializerFactory.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/JWT/Serializers/DefaultJsonSerializerFactory.cs b/src/JWT/Serializers/DefaultJsonSerializerFactory.cs index 1ecfc055e..bcf99fc3e 100644 --- a/src/JWT/Serializers/DefaultJsonSerializerFactory.cs +++ b/src/JWT/Serializers/DefaultJsonSerializerFactory.cs @@ -13,9 +13,7 @@ public JsonSerializerFactory() #endif } - public IJsonSerializer CreateSerializer() - { - return _jsonSerializer; - } + public IJsonSerializer Create() => + _jsonSerializer; } } From 2f51d221f870895ecfe54c283ccb273fe5ecd661 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 22:11:37 -0700 Subject: [PATCH 14/48] Create DelegateJsonSerializerFactory.cs --- .../DelegateJsonSerializerFactory.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/JWT/Serializers/DelegateJsonSerializerFactory.cs diff --git a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs new file mode 100644 index 000000000..28a2bada5 --- /dev/null +++ b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs @@ -0,0 +1,29 @@ +using System; + +namespace JWT.Serializers +{ + internal sealed class DefaultJsonSerializerFactory : IJsonSerializerFactory + { + private readonly Func _factory; + + public DelegateJsonSerializerFactory(IJsonSerializer jsonSerializer) : + this(() => jsonSerializer) + { + if (jsonSerializer is null) + throw new ArgumentNullException(nameof(factory)) + } + + public DelegateJsonSerializerFactory(IJsonSerializerFactory factory) + this(() => factory.Create()) + { + if (factory is null) + throw new ArgumentNullException(nameof(factory)) + } + + public DelegateJsonSerializerFactory(Func factory) + _factory = factory ?? throw new ArgumentNullException(nameof(factory)); + + public IJsonSerializer CreateSerializer() => + _factory(); + } +} From 52a22c119ccecfe672ff6de873bd0cbc74d8fc35 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 22:12:16 -0700 Subject: [PATCH 15/48] Update DelegateJsonSerializerFactory.cs --- src/JWT/Serializers/DelegateJsonSerializerFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs index 28a2bada5..39cb802d3 100644 --- a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs +++ b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs @@ -13,7 +13,7 @@ public DelegateJsonSerializerFactory(IJsonSerializer jsonSerializer) : throw new ArgumentNullException(nameof(factory)) } - public DelegateJsonSerializerFactory(IJsonSerializerFactory factory) + public DelegateJsonSerializerFactory(IJsonSerializerFactory factory) : this(() => factory.Create()) { if (factory is null) From 2a994f78e6583bb678eaff8b8e7cfa47f08f21f5 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 22:19:53 -0700 Subject: [PATCH 16/48] Update ServiceCollectionExtensions.cs --- .../ServiceCollectionExtensions.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs b/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs index 088b47f50..a6b79beb4 100644 --- a/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs @@ -12,7 +12,7 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddJwtEncoder(this IServiceCollection services) { services.TryAddSingleton(); - services.TryAddSingleton(new JsonSerializerFactory().CreateSerializer()); + services.TryAddSingleton(); services.TryAddSingleton(); return services; @@ -39,7 +39,7 @@ private static IServiceCollection AddJwtEncoder(this IServiceCollectio public static IServiceCollection AddJwtDecoder(this IServiceCollection services) { services.TryAddSingleton(); - services.TryAddSingleton(new JsonSerializerFactory().CreateSerializer()); + services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); @@ -55,4 +55,4 @@ public static IServiceCollection AddJwtDecoder(this IServiceCollection return services.AddJwtDecoder(); } } -} \ No newline at end of file +} From 6d933cd310f4498a1cfa861cc4b7593086a680c7 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 22:28:20 -0700 Subject: [PATCH 17/48] Update JwtBuilder.cs --- src/JWT/Builder/JwtBuilder.cs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/JWT/Builder/JwtBuilder.cs b/src/JWT/Builder/JwtBuilder.cs index 81aa49f84..b430598ca 100644 --- a/src/JWT/Builder/JwtBuilder.cs +++ b/src/JWT/Builder/JwtBuilder.cs @@ -18,8 +18,7 @@ public sealed class JwtBuilder private IJwtDecoder _decoder; private IJwtValidator _validator; - private JsonSerializerFactory _jsonSerializerFactory = new(); - private IJsonSerializer Serializer => _jsonSerializerFactory.CreateSerializer(); + private IJsonSerializerFactory _jsonSerializerFactory = new DefaultSerializerFactory(); private IBase64UrlEncoder _urlEncoder = new JwtBase64UrlEncoder(); private IDateTimeProvider _dateTimeProvider = new UtcDateTimeProvider(); @@ -76,9 +75,23 @@ public JwtBuilder AddClaim(string name, object value) /// Sets JWT serializer. /// /// Current builder instance - public JwtBuilder WithSerializer(IJsonSerializer serializer) + public JwtBuilder WithSerializer(IJsonSerializer serializer) => + WithSerializerFactory(new DelegateSerializerFactory(serializer)); + + /// + /// Sets JWT serializer. + /// + /// Current builder instance + public JwtBuilder WithSerializer(Func factory) => + WithSerializerFactory(new DelegateSerializerFactory(factory)); + + /// + /// Sets JWT serializer factory. + /// + /// Current builder instance + public JwtBuilder WithSerializerFactory(IJsonSerializerFactory jsonSerializerFactory) { - _jsonSerializerFactory = new JsonSerializerFactory(serializer); + _jsonSerializerFactory = jsonSerializerFactory; return this; } From 15b69525bd2701364e6ad391f0c7bb8f7edc635f Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 22:30:18 -0700 Subject: [PATCH 18/48] Update DelegateJsonSerializerFactory.cs --- src/JWT/Serializers/DelegateJsonSerializerFactory.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs index 39cb802d3..a304cf3d3 100644 --- a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs +++ b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs @@ -10,14 +10,14 @@ public DelegateJsonSerializerFactory(IJsonSerializer jsonSerializer) : this(() => jsonSerializer) { if (jsonSerializer is null) - throw new ArgumentNullException(nameof(factory)) + throw new ArgumentNullException(nameof(factory)); } public DelegateJsonSerializerFactory(IJsonSerializerFactory factory) : this(() => factory.Create()) { if (factory is null) - throw new ArgumentNullException(nameof(factory)) + throw new ArgumentNullException(nameof(factory)); } public DelegateJsonSerializerFactory(Func factory) From 171ac0af6cf64cb5ee5cc2c22c51eb5afd0a3f23 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 22:49:51 -0700 Subject: [PATCH 19/48] Update DelegateJsonSerializerFactory.cs --- src/JWT/Serializers/DelegateJsonSerializerFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs index a304cf3d3..28b6fa7f9 100644 --- a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs +++ b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs @@ -20,7 +20,7 @@ public DelegateJsonSerializerFactory(IJsonSerializerFactory factory) : throw new ArgumentNullException(nameof(factory)); } - public DelegateJsonSerializerFactory(Func factory) + public DelegateJsonSerializerFactory(Func factory) : _factory = factory ?? throw new ArgumentNullException(nameof(factory)); public IJsonSerializer CreateSerializer() => From b21bb1f1d8a593275288b45a9dd1bec24c8eb884 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 22:53:39 -0700 Subject: [PATCH 20/48] Update DelegateAlgorithmFactory.cs --- src/JWT/Algorithms/DelegateAlgorithmFactory.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/JWT/Algorithms/DelegateAlgorithmFactory.cs b/src/JWT/Algorithms/DelegateAlgorithmFactory.cs index a8fa928d7..82f34aef1 100644 --- a/src/JWT/Algorithms/DelegateAlgorithmFactory.cs +++ b/src/JWT/Algorithms/DelegateAlgorithmFactory.cs @@ -20,16 +20,15 @@ public DelegateAlgorithmFactory(Func algFactory) => /// Creates an instance of with supplied algorithm. /// /// - public DelegateAlgorithmFactory(IJwtAlgorithm algorithm) + public DelegateAlgorithmFactory(IJwtAlgorithm algorithm) : + this(() => algorithm) { if (algorithm is null) throw new ArgumentNullException(nameof(algorithm)); - - _algFactory = () => algorithm; } /// public IJwtAlgorithm Create(JwtDecoderContext context) => _algFactory(); } -} \ No newline at end of file +} From c8001a7ffd536eb2774f7a37fccef3c799d855de Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 22:55:12 -0700 Subject: [PATCH 21/48] Update DelegateJsonSerializerFactory.cs --- src/JWT/Serializers/DelegateJsonSerializerFactory.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs index 28b6fa7f9..34c4b07e6 100644 --- a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs +++ b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs @@ -10,17 +10,17 @@ public DelegateJsonSerializerFactory(IJsonSerializer jsonSerializer) : this(() => jsonSerializer) { if (jsonSerializer is null) - throw new ArgumentNullException(nameof(factory)); + throw new ArgumentNullException(nameof(factory)); } public DelegateJsonSerializerFactory(IJsonSerializerFactory factory) : this(() => factory.Create()) { if (factory is null) - throw new ArgumentNullException(nameof(factory)); + throw new ArgumentNullException(nameof(factory)); } - public DelegateJsonSerializerFactory(Func factory) : + public DelegateJsonSerializerFactory(Func factory) => _factory = factory ?? throw new ArgumentNullException(nameof(factory)); public IJsonSerializer CreateSerializer() => From bd57102c52f6d2c980e2cb411ea6690d29131d9f Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 23:02:45 -0700 Subject: [PATCH 22/48] Update DelegateJsonSerializerFactory.cs --- src/JWT/Serializers/DelegateJsonSerializerFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs index 34c4b07e6..fec4694c0 100644 --- a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs +++ b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs @@ -2,7 +2,7 @@ namespace JWT.Serializers { - internal sealed class DefaultJsonSerializerFactory : IJsonSerializerFactory + internal sealed class DelegateJsonSerializerFactory : IJsonSerializerFactory { private readonly Func _factory; From f5db345fb1c8a9e58392ff4926d9c898c076d95f Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 23:26:18 -0700 Subject: [PATCH 23/48] Update DefaultJsonSerializerFactory.cs --- src/JWT/Serializers/DefaultJsonSerializerFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JWT/Serializers/DefaultJsonSerializerFactory.cs b/src/JWT/Serializers/DefaultJsonSerializerFactory.cs index bcf99fc3e..f258527b2 100644 --- a/src/JWT/Serializers/DefaultJsonSerializerFactory.cs +++ b/src/JWT/Serializers/DefaultJsonSerializerFactory.cs @@ -4,7 +4,7 @@ internal sealed class DefaultJsonSerializerFactory : IJsonSerializerFactory { private readonly IJsonSerializer _jsonSerializer; - public JsonSerializerFactory() + public DefaultJsonSerializerFactory() { #if MODERN_DOTNET _jsonSerializer = new SystemTextSerializer(); From ffc16cc92ccaff2d1ee87f1678421b5e1661624c Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 23:27:26 -0700 Subject: [PATCH 24/48] Update DelegateJsonSerializerFactory.cs --- src/JWT/Serializers/DelegateJsonSerializerFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs index fec4694c0..e2a1b7119 100644 --- a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs +++ b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs @@ -23,7 +23,7 @@ public DelegateJsonSerializerFactory(IJsonSerializerFactory factory) : public DelegateJsonSerializerFactory(Func factory) => _factory = factory ?? throw new ArgumentNullException(nameof(factory)); - public IJsonSerializer CreateSerializer() => + public IJsonSerializer Create() => _factory(); } } From b7713ce4b9442f9c0ffe706d2eb465bc8a97a66a Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 23:40:56 -0700 Subject: [PATCH 25/48] Update DelegateJsonSerializerFactory.cs --- src/JWT/Serializers/DelegateJsonSerializerFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs index e2a1b7119..754376ee6 100644 --- a/src/JWT/Serializers/DelegateJsonSerializerFactory.cs +++ b/src/JWT/Serializers/DelegateJsonSerializerFactory.cs @@ -10,7 +10,7 @@ public DelegateJsonSerializerFactory(IJsonSerializer jsonSerializer) : this(() => jsonSerializer) { if (jsonSerializer is null) - throw new ArgumentNullException(nameof(factory)); + throw new ArgumentNullException(nameof(jsonSerializer)); } public DelegateJsonSerializerFactory(IJsonSerializerFactory factory) : From 7f6fc4588891f3577f2b6c74f29229871725d637 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 23:43:45 -0700 Subject: [PATCH 26/48] Update JwtBuilder.cs --- src/JWT/Builder/JwtBuilder.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/JWT/Builder/JwtBuilder.cs b/src/JWT/Builder/JwtBuilder.cs index b430598ca..de3e201d4 100644 --- a/src/JWT/Builder/JwtBuilder.cs +++ b/src/JWT/Builder/JwtBuilder.cs @@ -18,7 +18,7 @@ public sealed class JwtBuilder private IJwtDecoder _decoder; private IJwtValidator _validator; - private IJsonSerializerFactory _jsonSerializerFactory = new DefaultSerializerFactory(); + private IJsonSerializerFactory _jsonSerializerFactory = new DefaultJsonSerializerFactory(); private IBase64UrlEncoder _urlEncoder = new JwtBase64UrlEncoder(); private IDateTimeProvider _dateTimeProvider = new UtcDateTimeProvider(); @@ -75,21 +75,21 @@ public JwtBuilder AddClaim(string name, object value) /// Sets JWT serializer. /// /// Current builder instance - public JwtBuilder WithSerializer(IJsonSerializer serializer) => - WithSerializerFactory(new DelegateSerializerFactory(serializer)); + public JwtBuilder WithJsonSerializer(IJsonSerializer serializer) => + WithJsonSerializerFactory(new DelegateJsonSerializerFactory(serializer)); /// /// Sets JWT serializer. /// /// Current builder instance - public JwtBuilder WithSerializer(Func factory) => - WithSerializerFactory(new DelegateSerializerFactory(factory)); + public JwtBuilder WithJsonSerializer(Func factory) => + WithJsonSerializerFactory(new DelegateJsonSerializerFactory(factory)); /// /// Sets JWT serializer factory. /// /// Current builder instance - public JwtBuilder WithSerializerFactory(IJsonSerializerFactory jsonSerializerFactory) + public JwtBuilder WithJsonSerializerFactory(IJsonSerializerFactory jsonSerializerFactory) { _jsonSerializerFactory = jsonSerializerFactory; return this; From 024265d92468178d5863dd58994694ee261a077a Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Wed, 7 Sep 2022 23:59:49 -0700 Subject: [PATCH 27/48] Update JwtBuilder.cs --- src/JWT/Builder/JwtBuilder.cs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/JWT/Builder/JwtBuilder.cs b/src/JWT/Builder/JwtBuilder.cs index de3e201d4..52ce534ff 100644 --- a/src/JWT/Builder/JwtBuilder.cs +++ b/src/JWT/Builder/JwtBuilder.cs @@ -335,32 +335,35 @@ private void TryCreateEncoder() { if (_algorithm is null && _algFactory is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtEncoder)}. Call {nameof(WithAlgorithm)}."); - if (this.Serializer is null) + + var jsonSerializer = _jsonSerializerFactory.Create(); + if (jsonSerializer is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtEncoder)}. Call {nameof(WithSerializer)}"); if (_urlEncoder is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtEncoder)}. Call {nameof(WithUrlEncoder)}."); if (_algorithm is object) - _encoder = new JwtEncoder(_algorithm, this.Serializer, _urlEncoder); + _encoder = new JwtEncoder(_algorithm, jsonSerializer, _urlEncoder); else if (_algFactory is object) - _encoder = new JwtEncoder(_algFactory, this.Serializer, _urlEncoder); + _encoder = new JwtEncoder(_algFactory, jsonSerializer, _urlEncoder); } private void TryCreateDecoder() { TryCreateValidator(); - if (this.Serializer is null) + var jsonSerializer = _jsonSerializerFactory.Create(); + if (jsonSerializer is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithSerializer)}."); if (_urlEncoder is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithUrlEncoder)}."); if (_algorithm is object) - _decoder = new JwtDecoder(this.Serializer, _validator, _urlEncoder, _algorithm); + _decoder = new JwtDecoder(jsonSerializer, _validator, _urlEncoder, _algorithm); else if (_algFactory is object) - _decoder = new JwtDecoder(this.Serializer, _validator, _urlEncoder, _algFactory); + _decoder = new JwtDecoder(jsonSerializer, _validator, _urlEncoder, _algFactory); else if (!_valParams.ValidateSignature) - _decoder = new JwtDecoder(this.Serializer, _urlEncoder); + _decoder = new JwtDecoder(jsonSerializer, _urlEncoder); } private void TryCreateDecoderForHeader() @@ -378,12 +381,13 @@ private void TryCreateValidator() if (_validator is object) return; - if (this.Serializer is null) + var jsonSerializer = _jsonSerializerFactory.Create(); + if (jsonSerializer is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtValidator)}. Call {nameof(WithSerializer)}."); if (_dateTimeProvider is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtValidator)}. Call {nameof(WithDateTimeProvider)}."); - _validator = new JwtValidator(this.Serializer, _dateTimeProvider, _valParams); + _validator = new JwtValidator(jsonSerializer, _dateTimeProvider, _valParams); } private void EnsureCanEncode() @@ -435,7 +439,7 @@ private void EnsureCanDecodeHeader() /// private bool CanEncode() => (_algorithm is object || _algFactory is object) && - this.Serializer is object && + _jsonSerializerFactory is object && _urlEncoder is object && _jwt.Payload is object; @@ -458,7 +462,7 @@ private bool CanDecodeHeader() if (_urlEncoder is null) return false; - if (this.Serializer is null) + if (_jsonSerializerFactory is null) return false; return true; From 994564275a504eaf7801d16b3c1e0b48d4dd5d74 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 00:01:11 -0700 Subject: [PATCH 28/48] Update JwtBuilderEncodeTests.cs --- .../JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs b/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs index 9ab6f57d0..ad2de7c79 100644 --- a/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs +++ b/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs @@ -272,12 +272,12 @@ public void Encode_Should_Return_ThrowTargetException_Encode_TypesMatch() if (IsRunningOnMono()) { action.Should() - .Throw("Exception has been thrown by the target of an invocation."); + .Throw("Exception has been thrown by the target of an invocation."); } else { action.Should() - .Throw("Object does not match target type."); + .Throw("Object does not match target type."); } } @@ -285,11 +285,8 @@ public void Encode_Should_Return_ThrowTargetException_Encode_TypesMatch() /// Copied from: https://stackoverflow.com/a/7077620/2890855 /// /// - private static bool IsRunningOnMono() - { - Type type = Type.GetType("Mono.Runtime"); - return type != null; - } + private static bool IsRunningOnMono() => + Type.GetType("Mono.Runtime") is not null; [TestMethod] public void Encode_Should_Return_Token_With_Custom_Extra_Headers_Full_Payload2() From cf59026fcfeb0f0d892e922b36431a1d5fd0b37b Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 00:02:08 -0700 Subject: [PATCH 29/48] Update JwtDecoderTests.cs --- tests/JWT.Tests.Common/JwtDecoderTests.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/JWT.Tests.Common/JwtDecoderTests.cs b/tests/JWT.Tests.Common/JwtDecoderTests.cs index 933834201..c7aec970a 100644 --- a/tests/JWT.Tests.Common/JwtDecoderTests.cs +++ b/tests/JWT.Tests.Common/JwtDecoderTests.cs @@ -17,11 +17,6 @@ public class JwtDecoderTests { private static readonly Fixture _fixture = new Fixture(); - private static IJsonSerializer CreateSerializer() - { - return new JsonSerializerFactory().CreateSerializer(); - } - [TestMethod] public void DecodeHeader_Should_Return_Header() { @@ -542,5 +537,8 @@ public void DecodeToObject_Should_Throw_Exception_On_Null_NotBefore_Claim() .Throw() .WithMessage("Claim 'nbf' must be a number.", "because the invalid 'nbf' must result in an exception on decoding"); } + + private static IJsonSerializer CreateSerializer() => + new DefaultJsonSerializerFactory().Create(); } -} \ No newline at end of file +} From b9e0d926ec3c821a4c977b33ef09a274ab35170d Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 00:05:35 -0700 Subject: [PATCH 30/48] Update SerializerTests.cs --- tests/JWT.Tests.Common/SerializerTests.cs | 28 ++++++++--------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/tests/JWT.Tests.Common/SerializerTests.cs b/tests/JWT.Tests.Common/SerializerTests.cs index a48b59e36..d078ca49a 100644 --- a/tests/JWT.Tests.Common/SerializerTests.cs +++ b/tests/JWT.Tests.Common/SerializerTests.cs @@ -13,20 +13,14 @@ public class SerializerTests [TestMethod] public void Serializer_Should_Use_Correct_Default() { - var serializer = new JsonSerializerFactory().CreateSerializer(); - + var serializer = new DefaultJsonSerializerFactory().Create(); var dotnetVersion = GetRunningDotnetVersion(); - Console.WriteLine($"Dotnet version: {dotnetVersion}"); - - var defaultSerializerClass = serializer.GetType().Name; - Console.WriteLine($"Default serializer class: {defaultSerializerClass}"); - if (dotnetVersion is ".NETFramework,Version=v4.6.2" || - dotnetVersion.StartsWith(".NETCoreApp")) + if (String.Equals(dotnetVersion, ".NETFramework,Version=v4.6.2", StringComparison.OrdinalIgnoreCase) || dotnetVersion.StartsWith(".NETCoreApp")) { Assert.AreEqual("SystemTextSerializer", defaultSerializerClass); } - else if (dotnetVersion == ".NETFramework,Version=v4.6.1") + else if (String.Equals(dotnetVersion, ".NETFramework,Version=v4.6.1", StringComparison.OrdinalIgnoreCase)) { Assert.AreEqual("JsonNetSerializer", defaultSerializerClass); } @@ -40,14 +34,10 @@ public void Serializer_Should_Use_Correct_Default() /// Copied from: https://stackoverflow.com/a/49754978 /// /// The running dotnet version. - private string GetRunningDotnetVersion() - { - var version = Assembly.GetExecutingAssembly() - .GetCustomAttributes(typeof(TargetFrameworkAttribute), false) - .Cast() - .Single() - .FrameworkName; - return version; - } + private string GetRunningDotnetVersion() => + Assembly.GetExecutingAssembly() + .GetCustomAttributes() + .SingleOrDefault() + ?.FrameworkName; } -} \ No newline at end of file +} From 5437103b3534e75159081dd8e76eb43354ce74bc Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 00:06:19 -0700 Subject: [PATCH 31/48] Update JwtEncoderTests.cs --- tests/JWT.Tests.Common/JwtEncoderTests.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/JWT.Tests.Common/JwtEncoderTests.cs b/tests/JWT.Tests.Common/JwtEncoderTests.cs index fb18dc208..8f67455ee 100644 --- a/tests/JWT.Tests.Common/JwtEncoderTests.cs +++ b/tests/JWT.Tests.Common/JwtEncoderTests.cs @@ -10,11 +10,6 @@ namespace JWT.Tests [TestClass] public class JwtEncoderTests { - private static IJsonSerializer CreateSerializer() - { - return new JsonSerializerFactory().CreateSerializer(); - } - [TestMethod] public void Encode_Should_Encode_To_Token() { @@ -112,5 +107,8 @@ public void Encode_Should_Encode_To_Token_Using_Json_Net() actual.Should() .Be(expected, "because the same data encoded with the same key must result in the same token"); } + + private static IJsonSerializer CreateSerializer() => + new DefaultJsonSerializerFactory().Create(); } } From b604fac14d4b0509f323371b44e301d92762c1bf Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 00:10:34 -0700 Subject: [PATCH 32/48] Update SerializerTests.cs --- tests/JWT.Tests.Common/SerializerTests.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/JWT.Tests.Common/SerializerTests.cs b/tests/JWT.Tests.Common/SerializerTests.cs index d078ca49a..ed513ab33 100644 --- a/tests/JWT.Tests.Common/SerializerTests.cs +++ b/tests/JWT.Tests.Common/SerializerTests.cs @@ -36,8 +36,9 @@ public void Serializer_Should_Use_Correct_Default() /// The running dotnet version. private string GetRunningDotnetVersion() => Assembly.GetExecutingAssembly() - .GetCustomAttributes() - .SingleOrDefault() + .GetCustomAttributes(typeof(TargetFrameworkAttribute), false) + .Cast() + .Single() ?.FrameworkName; } } From 82fe9c59fc88e54a864181d6d8c84d9cc42c480a Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 00:12:32 -0700 Subject: [PATCH 33/48] Update JwtBuilder.cs --- src/JWT/Builder/JwtBuilder.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/JWT/Builder/JwtBuilder.cs b/src/JWT/Builder/JwtBuilder.cs index 52ce534ff..9656a50b7 100644 --- a/src/JWT/Builder/JwtBuilder.cs +++ b/src/JWT/Builder/JwtBuilder.cs @@ -338,7 +338,7 @@ private void TryCreateEncoder() var jsonSerializer = _jsonSerializerFactory.Create(); if (jsonSerializer is null) - throw new InvalidOperationException($"Can't instantiate {nameof(JwtEncoder)}. Call {nameof(WithSerializer)}"); + throw new InvalidOperationException($"Can't instantiate {nameof(JwtEncoder)}. Call {nameof(WithJsonSerializer)}"); if (_urlEncoder is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtEncoder)}. Call {nameof(WithUrlEncoder)}."); @@ -354,7 +354,7 @@ private void TryCreateDecoder() var jsonSerializer = _jsonSerializerFactory.Create(); if (jsonSerializer is null) - throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithSerializer)}."); + throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithJsonSerializer)}."); if (_urlEncoder is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithUrlEncoder)}."); @@ -368,12 +368,13 @@ private void TryCreateDecoder() private void TryCreateDecoderForHeader() { - if (this.Serializer is null) - throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithSerializer)}."); + var jsonSerializer = _jsonSerializerFactory.Create(); + if (tjsonSerializer is null) + throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithJsonSerializer)}."); if (_urlEncoder is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithUrlEncoder)}."); - _decoder = new JwtDecoder(this.Serializer, _urlEncoder); + _decoder = new JwtDecoder(jsonSerializer, _urlEncoder); } private void TryCreateValidator() @@ -383,7 +384,7 @@ private void TryCreateValidator() var jsonSerializer = _jsonSerializerFactory.Create(); if (jsonSerializer is null) - throw new InvalidOperationException($"Can't instantiate {nameof(JwtValidator)}. Call {nameof(WithSerializer)}."); + throw new InvalidOperationException($"Can't instantiate {nameof(JwtValidator)}. Call {nameof(WithJsonSerializer)}."); if (_dateTimeProvider is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtValidator)}. Call {nameof(WithDateTimeProvider)}."); @@ -400,7 +401,7 @@ private void EnsureCanEncode() throw new InvalidOperationException( "Can't encode a token. Check if you have called all of the following methods:" + Environment.NewLine + $"-{nameof(WithAlgorithm)}" + Environment.NewLine + - $"-{nameof(WithSerializer)}" + Environment.NewLine + + $"-{nameof(WithJsonSerializer)}" + Environment.NewLine + $"-{nameof(WithUrlEncoder)}."); } } @@ -414,7 +415,7 @@ private void EnsureCanDecode() { throw new InvalidOperationException( "Can't decode a token. Check if you have called all of the following methods:" + Environment.NewLine + - $"-{nameof(WithSerializer)}" + Environment.NewLine + + $"-{nameof(WithJsonSerializer)}" + Environment.NewLine + $"-{nameof(WithValidator)}" + Environment.NewLine + $"-{nameof(WithUrlEncoder)}."); } @@ -429,7 +430,7 @@ private void EnsureCanDecodeHeader() { throw new InvalidOperationException( "Can't decode a token header. Check if you have called all of the following methods:" + Environment.NewLine + - $"-{nameof(WithSerializer)}" + Environment.NewLine + + $"-{nameof(WithJsonSerializer)}" + Environment.NewLine + $"-{nameof(WithUrlEncoder)}."); } } From adc1d660912eb62b036a564e25708b44f70d9946 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 08:33:25 -0700 Subject: [PATCH 34/48] Update JwtBuilder.cs --- src/JWT/Builder/JwtBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JWT/Builder/JwtBuilder.cs b/src/JWT/Builder/JwtBuilder.cs index 9656a50b7..36f6c61f4 100644 --- a/src/JWT/Builder/JwtBuilder.cs +++ b/src/JWT/Builder/JwtBuilder.cs @@ -369,7 +369,7 @@ private void TryCreateDecoder() private void TryCreateDecoderForHeader() { var jsonSerializer = _jsonSerializerFactory.Create(); - if (tjsonSerializer is null) + if (jsonSerializer is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithJsonSerializer)}."); if (_urlEncoder is null) throw new InvalidOperationException($"Can't instantiate {nameof(JwtDecoder)}. Call {nameof(WithUrlEncoder)}."); From dd65918e05f41ae2fed6b98408788af1b61e5ad0 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 08:43:29 -0700 Subject: [PATCH 35/48] Update and rename SerializerTests.cs to DefaultJsonSerializerFactoryTests.cs --- ...s => DefaultJsonSerializerFactoryTests.cs} | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) rename tests/JWT.Tests.Common/{SerializerTests.cs => DefaultJsonSerializerFactoryTests.cs} (56%) diff --git a/tests/JWT.Tests.Common/SerializerTests.cs b/tests/JWT.Tests.Common/DefaultJsonSerializerFactoryTests.cs similarity index 56% rename from tests/JWT.Tests.Common/SerializerTests.cs rename to tests/JWT.Tests.Common/DefaultJsonSerializerFactoryTests.cs index ed513ab33..2800b5c92 100644 --- a/tests/JWT.Tests.Common/SerializerTests.cs +++ b/tests/JWT.Tests.Common/DefaultJsonSerializerFactoryTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Reflection; using System.Runtime.Versioning; @@ -8,33 +8,33 @@ namespace JWT.Tests { [TestClass] - public class SerializerTests + public class DefaultJsonSerializerFactoryTests { [TestMethod] - public void Serializer_Should_Use_Correct_Default() + public void Create_Should_Return_Correct_Serializer_Per_Runtime_Version() { - var serializer = new DefaultJsonSerializerFactory().Create(); - var dotnetVersion = GetRunningDotnetVersion(); + var serializer = CreateSerializer(); + var defaultSerializerClass = serializer.GetType().Name; + var dotNetVersion = GetDotNetVersion(); - if (String.Equals(dotnetVersion, ".NETFramework,Version=v4.6.2", StringComparison.OrdinalIgnoreCase) || dotnetVersion.StartsWith(".NETCoreApp")) + if (String.Equals(dotNetVersion, ".NETFramework,Version=v4.6.2", StringComparison.OrdinalIgnoreCase) || dotnetVersion.StartsWith(".NETCoreApp")) { Assert.AreEqual("SystemTextSerializer", defaultSerializerClass); } - else if (String.Equals(dotnetVersion, ".NETFramework,Version=v4.6.1", StringComparison.OrdinalIgnoreCase)) + else if (String.Equals(dotNetVersion, ".NETFramework,Version=v4.6.1", StringComparison.OrdinalIgnoreCase)) { Assert.AreEqual("JsonNetSerializer", defaultSerializerClass); } else { - Assert.Fail($"Unrecognized dotnet version {dotnetVersion}"); + Assert.Fail($"Unrecognized dotnet version {dotNetVersion}"); } } + + private static IJsonSerializer CreateSerializer() => + new DefaultJsonSerializerFactory().Create(); - /// - /// Copied from: https://stackoverflow.com/a/49754978 - /// - /// The running dotnet version. - private string GetRunningDotnetVersion() => + private string GetDotNetVersion() => Assembly.GetExecutingAssembly() .GetCustomAttributes(typeof(TargetFrameworkAttribute), false) .Cast() From b7a9cea58a62842c9dff647652a78b055cb90228 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 09:05:57 -0700 Subject: [PATCH 36/48] Update JwtValidatorTests.cs --- tests/JWT.Tests.Common/JwtValidatorTests.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/JWT.Tests.Common/JwtValidatorTests.cs b/tests/JWT.Tests.Common/JwtValidatorTests.cs index 7348248bc..246f49d28 100644 --- a/tests/JWT.Tests.Common/JwtValidatorTests.cs +++ b/tests/JWT.Tests.Common/JwtValidatorTests.cs @@ -14,11 +14,6 @@ namespace JWT.Tests [TestClass] public class JwtValidatorTests { - private static IJsonSerializer CreateSerializer() - { - return new JsonSerializerFactory().CreateSerializer(); - } - [TestMethod] public void Ctor_Should_Throw_Exception_When_Serializer_Is_Null() { @@ -383,5 +378,8 @@ public void TryValidate_Should_Return_True_And_Exception_Null_When_Token_Is_Not_ .BeTrue("because token should be valid"); } + + private static IJsonSerializer CreateSerializer() => + new DefaultJsonSerializerFactory().CreateSerializer(); } } From 8d314cd4f2c9cbbaf1cc8897cc6da31d47df0669 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 09:11:51 -0700 Subject: [PATCH 37/48] Update DefaultJsonSerializerFactory.cs --- src/JWT/Serializers/DefaultJsonSerializerFactory.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/JWT/Serializers/DefaultJsonSerializerFactory.cs b/src/JWT/Serializers/DefaultJsonSerializerFactory.cs index f258527b2..86cc3e4d0 100644 --- a/src/JWT/Serializers/DefaultJsonSerializerFactory.cs +++ b/src/JWT/Serializers/DefaultJsonSerializerFactory.cs @@ -1,6 +1,6 @@ -namespace JWT.Serializers +namespace JWT.Serializers { - internal sealed class DefaultJsonSerializerFactory : IJsonSerializerFactory + public sealed class DefaultJsonSerializerFactory : IJsonSerializerFactory { private readonly IJsonSerializer _jsonSerializer; From 54299a5958de55db1a2207b7f75b4cc8e5e977d8 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 09:12:21 -0700 Subject: [PATCH 38/48] Update DefaultJsonSerializerFactoryTests.cs --- tests/JWT.Tests.Common/DefaultJsonSerializerFactoryTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/JWT.Tests.Common/DefaultJsonSerializerFactoryTests.cs b/tests/JWT.Tests.Common/DefaultJsonSerializerFactoryTests.cs index 2800b5c92..a7952406e 100644 --- a/tests/JWT.Tests.Common/DefaultJsonSerializerFactoryTests.cs +++ b/tests/JWT.Tests.Common/DefaultJsonSerializerFactoryTests.cs @@ -17,7 +17,7 @@ public void Create_Should_Return_Correct_Serializer_Per_Runtime_Version() var defaultSerializerClass = serializer.GetType().Name; var dotNetVersion = GetDotNetVersion(); - if (String.Equals(dotNetVersion, ".NETFramework,Version=v4.6.2", StringComparison.OrdinalIgnoreCase) || dotnetVersion.StartsWith(".NETCoreApp")) + if (String.Equals(dotNetVersion, ".NETFramework,Version=v4.6.2", StringComparison.OrdinalIgnoreCase) || dotNetVersion.StartsWith(".NETCoreApp")) { Assert.AreEqual("SystemTextSerializer", defaultSerializerClass); } From da82a6cc5fd5ff41dafce5a2e8038af907f13bc6 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 09:12:56 -0700 Subject: [PATCH 39/48] Update JwtBuilderEncodeTests.cs --- tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs b/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs index ad2de7c79..101776d73 100644 --- a/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs +++ b/tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Globalization; using System.Linq; using System.Reflection; @@ -350,7 +350,7 @@ public void Encode_With_Secret_Should_Return_Valid_Token_Using_Json_Net() var token = JwtBuilder.Create() .WithAlgorithm(TestData.HMACSHA256Algorithm) .WithSecret(secret) - .WithSerializer(new JsonNetSerializer()) + .WithJsonSerializer(new JsonNetSerializer()) .Encode(); token.Should() From 027cb83e480c671c45202bf75c6ad4895f5e4ace Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 09:21:37 -0700 Subject: [PATCH 40/48] Update JwtBuilderDecodeTests.cs --- tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs b/tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs index 267896f6d..f5e2cd6ad 100644 --- a/tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs +++ b/tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs @@ -188,7 +188,7 @@ public void Decode_Without_Serializer_Should_Throw_Exception() { Action action = () => JwtBuilder.Create() - .WithSerializer(null) + .WithJsonSerializer(null) .Decode(TestData.Token); action.Should() From bf981de456e8934718015154e9c14bf24fed2801 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 09:22:08 -0700 Subject: [PATCH 41/48] Update JwtValidatorTests.cs --- tests/JWT.Tests.Common/JwtValidatorTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/JWT.Tests.Common/JwtValidatorTests.cs b/tests/JWT.Tests.Common/JwtValidatorTests.cs index 246f49d28..515c3907f 100644 --- a/tests/JWT.Tests.Common/JwtValidatorTests.cs +++ b/tests/JWT.Tests.Common/JwtValidatorTests.cs @@ -380,6 +380,6 @@ public void TryValidate_Should_Return_True_And_Exception_Null_When_Token_Is_Not_ } private static IJsonSerializer CreateSerializer() => - new DefaultJsonSerializerFactory().CreateSerializer(); + new DefaultJsonSerializerFactory().Create(); } } From d7bf51edb11d21bc30e91eea6986d1104e0aa596 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 09:22:46 -0700 Subject: [PATCH 42/48] Update JwtSecurityTests.cs --- tests/JWT.Tests.Common/JwtSecurityTests.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/JWT.Tests.Common/JwtSecurityTests.cs b/tests/JWT.Tests.Common/JwtSecurityTests.cs index b0ae8237b..84111d651 100644 --- a/tests/JWT.Tests.Common/JwtSecurityTests.cs +++ b/tests/JWT.Tests.Common/JwtSecurityTests.cs @@ -13,11 +13,6 @@ public class JwtSecurityTests { private static readonly Fixture _fixture = new Fixture(); - private static IJsonSerializer CreateSerializer() - { - return new JsonSerializerFactory().CreateSerializer(); - } - [TestMethod] [TestCategory("Security")] public void Decode_Should_Throw_Exception_When_Jwt_Contains_No_Algorithm() @@ -100,5 +95,8 @@ public void Decode_Should_Throw_Exception_When_Jwt_Contains_HMA_Algorithm_But_RS action.Should() .Throw("because an encryption algorithm can't be changed on decoding"); } + + private static IJsonSerializer CreateSerializer() => + new DefaultJsonSerializerFactory().Create(); } -} \ No newline at end of file +} From 440da0891275ad80bc55dd9175d2ff7257862f8d Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 09:28:43 -0700 Subject: [PATCH 43/48] Update JwtBuilderDecodeTests.cs --- tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs b/tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs index f5e2cd6ad..7a1ebf8f7 100644 --- a/tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs +++ b/tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs @@ -188,7 +188,7 @@ public void Decode_Without_Serializer_Should_Throw_Exception() { Action action = () => JwtBuilder.Create() - .WithJsonSerializer(null) + .WithJsonSerializer((IJsonSerializer)null) .Decode(TestData.Token); action.Should() @@ -391,7 +391,7 @@ public void Decode_ToDictionary_Without_Serializer_Should_Throw_Exception() Action action = () => JwtBuilder.Create() .WithAlgorithm(TestData.RS256Algorithm) - .WithSerializer(null) + .WithJsonSerializer((IJsonSerializer)null) .WithSecret(TestData.Secret) .MustVerifySignature() .Decode>(TestData.Token); From 888c0c14f3cff39dd59a02f61075d7bd605c6147 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 09:33:20 -0700 Subject: [PATCH 44/48] Update JwtAuthenticationHandlerTests.cs --- .../JwtAuthenticationHandlerTests.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs b/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs index be03131b6..e34cd2d77 100644 --- a/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs +++ b/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs @@ -21,11 +21,6 @@ public class JwtAuthenticationHandlerTests { private static readonly Fixture _fixture = new Fixture(); - private static IJsonSerializer CreateSerializer() - { - return new JsonSerializerFactory().CreateSerializer(); - } - [TestMethod] public async Task HandleAuthenticateAsync_Should_Return_Success_When_Token_Is_Valid() { @@ -147,5 +142,9 @@ private static async Task CreateHandler(string header) await handler.InitializeAsync(scheme, context); return handler; } + + + private static IJsonSerializer CreateSerializer() => + new DefaultJsonSerializerFactory().Create(); } } From 42155c85bbd6116b64b77727b1d975de88e36593 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 09:39:01 -0700 Subject: [PATCH 45/48] Update JwtBuilderDecodeTests.cs --- tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs b/tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs index 7a1ebf8f7..904144b3d 100644 --- a/tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs +++ b/tests/JWT.Tests.Common/Builder/JwtBuilderDecodeTests.cs @@ -192,7 +192,7 @@ public void Decode_Without_Serializer_Should_Throw_Exception() .Decode(TestData.Token); action.Should() - .Throw("because token can't be decoded without valid serializer"); + .Throw(); } [TestMethod] @@ -397,7 +397,7 @@ public void Decode_ToDictionary_Without_Serializer_Should_Throw_Exception() .Decode>(TestData.Token); action.Should() - .Throw("because token can't be decoded without valid serializer"); + .Throw(); } } } From 27802094f6681e32a7b8c56042649a2d2dd6691d Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 09:45:33 -0700 Subject: [PATCH 46/48] Update JwtAuthenticationHandlerTests.cs --- .../JwtAuthenticationHandlerTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs b/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs index e34cd2d77..db4ab3134 100644 --- a/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs +++ b/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs @@ -143,7 +143,6 @@ private static async Task CreateHandler(string header) return handler; } - private static IJsonSerializer CreateSerializer() => new DefaultJsonSerializerFactory().Create(); } From fa7c10a369ecdc9c3773fc39260f2cc19e6c6a3b Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 09:47:24 -0700 Subject: [PATCH 47/48] Update ServiceCollectionExtensions.cs --- .../ServiceCollectionExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs b/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs index a6b79beb4..4129c8979 100644 --- a/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs @@ -39,7 +39,7 @@ private static IServiceCollection AddJwtEncoder(this IServiceCollectio public static IServiceCollection AddJwtDecoder(this IServiceCollection services) { services.TryAddSingleton(); - services.TryAddSingleton(); + services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); From 4f1883071d7fb11476153380851bb6614b32883a Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Thu, 8 Sep 2022 10:10:40 -0700 Subject: [PATCH 48/48] Update ServiceCollectionExtensions.cs --- .../ServiceCollectionExtensions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs b/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs index 4129c8979..d5d6ed572 100644 --- a/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs @@ -13,6 +13,7 @@ public static IServiceCollection AddJwtEncoder(this IServiceCollection services) { services.TryAddSingleton(); services.TryAddSingleton(); + services.TryAddSingleton(p => p.GetRequiredService().Create()); services.TryAddSingleton(); return services; @@ -40,6 +41,7 @@ public static IServiceCollection AddJwtDecoder(this IServiceCollection services) { services.TryAddSingleton(); services.TryAddSingleton(); + services.TryAddSingleton(p => p.GetRequiredService().Create()); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton();