diff --git a/Directory.Build.props b/Directory.Build.props index c231047f2..a3dbd8867 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,6 @@ + latest @@ -25,7 +26,7 @@ $(NoWarn);CS1591 - + all runtime; build; native; contentfiles; analyzers @@ -35,4 +36,5 @@ runtime; build; native; contentfiles; analyzers + \ No newline at end of file diff --git a/JWT.sln b/JWT.sln index 17d5fe398..5b2f952d2 100644 --- a/JWT.sln +++ b/JWT.sln @@ -49,6 +49,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{44284230 tests\Directory.Build.props = tests\Directory.Build.props EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C86A941F-F655-4EF5-A6A1-47BD4A7FC748}" + ProjectSection(SolutionItems) = preProject + src\Directory.Build.targets = src\Directory.Build.targets + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -110,6 +115,7 @@ Global {5805A1D3-60B7-4F73-BA9F-F49B0BBFBB94} = {E7F3756F-ABDA-436D-8C42-FADAF79012ED} {73167BAB-1F21-4DE9-83E0-5E0686AB7245} = {E7F3756F-ABDA-436D-8C42-FADAF79012ED} {44284230-53D0-4589-AFE8-52F003F6CB05} = {513CE2B5-E0D6-43BC-998A-A02CB2875479} + {C86A941F-F655-4EF5-A6A1-47BD4A7FC748} = {513CE2B5-E0D6-43BC-998A-A02CB2875479} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {E1410453-72A1-44D5-AE80-32835754BEDA} diff --git a/src/Directory.Build.targets b/src/Directory.Build.targets new file mode 100644 index 000000000..3d32dc3af --- /dev/null +++ b/src/Directory.Build.targets @@ -0,0 +1,17 @@ + + + + + + + $(DefineConstants);SYSTEM_TEXT_JSON + + + + + $(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 3fc1fd752..0cc09f8ea 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 - 9.0.0-beta2 + 9.0.0-beta3 9.0.0.0 9.0.0.0 JWT.Extensions.AspNetCore @@ -31,9 +31,21 @@ - + + + + + + + + + + + + + diff --git a/src/JWT.Extensions.DependencyInjection/JWT.Extensions.DependencyInjection.csproj b/src/JWT.Extensions.DependencyInjection/JWT.Extensions.DependencyInjection.csproj index ca01ec793..10850e3c3 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.0.0-beta1 + 2.0.0-beta2 2.0.0.0 2.0.0.0 JWT @@ -33,6 +33,19 @@ + + + + + + + + + + + + + diff --git a/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs b/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs index 10d0e76fc..73fbbc037 100644 --- a/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/JWT.Extensions.DependencyInjection/ServiceCollectionExtensions.cs @@ -1,10 +1,15 @@ 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 @@ -12,7 +17,7 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddJwtEncoder(this IServiceCollection services) { services.TryAddSingleton(); - services.TryAddSingleton(); + services.TryAddSingleton(); services.TryAddSingleton(); return services; @@ -39,7 +44,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(); diff --git a/src/JWT/Algorithms/RSAlgorithm.cs b/src/JWT/Algorithms/RSAlgorithm.cs index 14c9cfcf0..2bd64be20 100644 --- a/src/JWT/Algorithms/RSAlgorithm.cs +++ b/src/JWT/Algorithms/RSAlgorithm.cs @@ -86,7 +86,7 @@ private static RSA GetPrivateKey(X509Certificate2 cert) if (cert is null) throw new ArgumentNullException(nameof(cert)); -#if NETSTANDARD || NET || NETCOREAPP || NETFRAMEWORK && (NET46 || NET461 || NET462 || NET47 || NET471 || NET472 || NET48) +#if NETSTANDARD || NETCOREAPP || NET462 return cert.GetRSAPrivateKey(); #else return (RSA)cert.PrivateKey; @@ -98,7 +98,7 @@ private static RSA GetPublicKey(X509Certificate2 cert) if (cert is null) throw new ArgumentNullException(nameof(cert)); -#if NETSTANDARD || NET || NETCOREAPP || NETFRAMEWORK && (NET46 || NET461 || NET462 || NET47 || NET471 || NET472 || NET48) +#if NETSTANDARD || NETCOREAPP || NET462 return cert.GetRSAPublicKey(); #else return (RSA)cert.PublicKey.Key; diff --git a/src/JWT/Builder/JwtBuilder.cs b/src/JWT/Builder/JwtBuilder.cs index 9b751377e..31cecbdfb 100644 --- a/src/JWT/Builder/JwtBuilder.cs +++ b/src/JWT/Builder/JwtBuilder.cs @@ -4,6 +4,7 @@ using JWT.Serializers; using static JWT.Internal.EncodingHelper; +using static JWT.Serializers.JsonSerializerFactory; namespace JWT.Builder { @@ -18,7 +19,7 @@ public sealed class JwtBuilder private IJwtDecoder _decoder; private IJwtValidator _validator; - private IJsonSerializer _serializer = new JsonNetSerializer(); + private IJsonSerializer _serializer = CreateSerializer(); private IBase64UrlEncoder _urlEncoder = new JwtBase64UrlEncoder(); private IDateTimeProvider _dateTimeProvider = new UtcDateTimeProvider(); private ValidationParameters _valParams = ValidationParameters.Default; @@ -281,6 +282,17 @@ public T Decode(string token) return _decoder.DecodeToObject(token, _secrets, _valParams.ValidateSignature); } + private static IJsonSerializer CreateSerializer() + { +#if SYSTEM_TEXT_JSON + return new SystemTextSerializer(); +#elif NEWTONSOFT_JSON + return new JsonNetSerializer(); +#else + throw new NotSupportedException(); +#endif + } + private void TryCreateEncoder() { if (_algorithm is null && _algFactory is null) @@ -411,4 +423,4 @@ private bool CanDecodeHeader() return true; } } -} +} \ No newline at end of file diff --git a/src/JWT/Builder/JwtHeader.cs b/src/JWT/Builder/JwtHeader.cs index bdbe33a1b..8614fd499 100644 --- a/src/JWT/Builder/JwtHeader.cs +++ b/src/JWT/Builder/JwtHeader.cs @@ -1,4 +1,8 @@ +#if SYSTEM_TEXT_JSON +using JsonProperty = System.Text.Json.Serialization.JsonPropertyNameAttribute; +#elif NEWTONSOFT_JSON using Newtonsoft.Json; +#endif namespace JWT.Builder { diff --git a/src/JWT/JWT.csproj b/src/JWT/JWT.csproj index b05d91034..76d7b1799 100644 --- a/src/JWT/JWT.csproj +++ b/src/JWT/JWT.csproj @@ -1,55 +1,64 @@ - - - - netstandard1.3;netstandard2.0;net6.0;net35;net40;net46; - - - - $(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client - false - - - .NETFramework - - - .NETStandard - - - .NETFramework,Version=6.0 - - - - Public Domain - Jwt.Net, a JWT (JSON Web Token) implementation for .NET - Alexander Batishchev, John Sheehan, Michael Lehenbauer - jwt;json;authorization - CC0-1.0 - 9.0.0-beta5 - 9.0.0.0 - 9.0.0.0 - JWT - true - - - - TRACE;DEBUG - bin\Debug\ - - - - TRACE - bin\Release\ - true - - - - - - - - - - - - + + + + net6.0;netstandard2.0;netstandard1.3;net462;net40;net35 + + + + $(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client + false + + + .NETFramework + + + .NETStandard + + + .NETFramework,Version=6.0 + + + + Public Domain + Jwt.Net, a JWT (JSON Web Token) implementation for .NET + Alexander Batishchev, John Sheehan, Michael Lehenbauer + jwt;json;authorization + CC0-1.0 + 9.0.0-beta5 + 9.0.0.0 + 9.0.0.0 + JWT + true + + + + $(DefineConstants);TRACE;DEBUG + bin\Debug\ + + + + $(DefineConstants);TRACE + bin\Release\ + true + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/JWT/Properties/AssemblyInfo.cs b/src/JWT/Properties/AssemblyInfo.cs index 369a697ae..5d13524ab 100644 --- a/src/JWT/Properties/AssemblyInfo.cs +++ b/src/JWT/Properties/AssemblyInfo.cs @@ -2,7 +2,6 @@ using JWT; [assembly: InternalsVisibleTo("JWT.Tests.Common, PublicKey=" + AssemblyInfo.PublicKey)] -[assembly: InternalsVisibleTo("JWT.Tests.NetCore2, PublicKey=" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("JWT.Tests.NetCore3, PublicKey=" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("JWT.Tests.Net35, PublicKey=" + AssemblyInfo.PublicKey)] [assembly: InternalsVisibleTo("JWT.Tests.Net40, PublicKey=" + AssemblyInfo.PublicKey)] diff --git a/src/JWT/Serializers/JsonNetSerializer.cs b/src/JWT/Serializers/JsonNetSerializer.cs index 204b1d3ed..48bb3601a 100644 --- a/src/JWT/Serializers/JsonNetSerializer.cs +++ b/src/JWT/Serializers/JsonNetSerializer.cs @@ -1,4 +1,5 @@ -using System; +#if NEWTONSOFT_JSON +using System; using System.IO; using System.Text; using Newtonsoft.Json; @@ -59,4 +60,5 @@ public object Deserialize(Type type, string json) return _serializer.Deserialize(jsonReader, type); } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/JWT/Serializers/JsonSerializerFactory.cs b/src/JWT/Serializers/JsonSerializerFactory.cs new file mode 100644 index 000000000..884861603 --- /dev/null +++ b/src/JWT/Serializers/JsonSerializerFactory.cs @@ -0,0 +1,16 @@ +namespace JWT.Serializers +{ + internal static class JsonSerializerFactory + { + public static IJsonSerializer CreateSerializer() + { +#if SYSTEM_TEXT_JSON + return new SystemTextSerializer(); +#elif NEWTONSOFT_JSON + return new JsonNetSerializer(); +#else + throw new System.NotSupportedException(); +#endif + } + } +} \ No newline at end of file diff --git a/src/JWT/Serializers/SystemTextSerializer.cs b/src/JWT/Serializers/SystemTextSerializer.cs new file mode 100644 index 000000000..7ce4a126d --- /dev/null +++ b/src/JWT/Serializers/SystemTextSerializer.cs @@ -0,0 +1,36 @@ +#if SYSTEM_TEXT_JSON +using System; +using System.Text.Json; + +namespace JWT.Serializers +{ + /// + /// JSON serializer using Newtonsoft.Json implementation. + /// + public sealed class SystemTextSerializer : IJsonSerializer + { + /// + /// + public string Serialize(object obj) + { + if (obj is null) + throw new ArgumentNullException(nameof(obj)); + + return JsonSerializer.Serialize(obj); + } + + /// + /// + /// + public object Deserialize(Type type, string json) + { + if (type is null) + throw new ArgumentNullException(nameof(type)); + if (String.IsNullOrEmpty(json)) + throw new ArgumentException(nameof(json)); + + return JsonSerializer.Deserialize(json, type); + } + } +} +#endif \ No newline at end of file diff --git a/tests/JWT.Extensions.AspNetCore.Tests/JWT.Extensions.AspNetCore.Tests.csproj b/tests/JWT.Extensions.AspNetCore.Tests/JWT.Extensions.AspNetCore.Tests.csproj index bf21eb688..f474bef31 100644 --- a/tests/JWT.Extensions.AspNetCore.Tests/JWT.Extensions.AspNetCore.Tests.csproj +++ b/tests/JWT.Extensions.AspNetCore.Tests/JWT.Extensions.AspNetCore.Tests.csproj @@ -11,6 +11,7 @@ + diff --git a/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs b/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs index df5685d7f..068b75252 100644 --- a/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs +++ b/tests/JWT.Extensions.AspNetCore.Tests/JwtAuthenticationHandlerTests.cs @@ -4,7 +4,6 @@ using AutoFixture; using FluentAssertions; using JWT.Extensions.AspNetCore.Factories; -using JWT.Serializers; using JWT.Tests.Models; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Http; @@ -14,6 +13,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; +using static JWT.Serializers.JsonSerializerFactory; + namespace JWT.Extensions.AspNetCore.Tests { [TestClass] @@ -99,7 +100,7 @@ public async Task HandleAuthenticateAsync_Should_Return_None_When_Authentication private static async Task CreateHandler(string header) { - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var urlEncoder = new JwtBase64UrlEncoder(); var jwtValidator = new JwtValidator(serializer, dateTimeProvider); diff --git a/tests/JWT.Extensions.DependencyInjection.Tests/JWT.Extensions.DependencyInjection.Tests.csproj b/tests/JWT.Extensions.DependencyInjection.Tests/JWT.Extensions.DependencyInjection.Tests.csproj index 4e75e58c3..ce189eafa 100644 --- a/tests/JWT.Extensions.DependencyInjection.Tests/JWT.Extensions.DependencyInjection.Tests.csproj +++ b/tests/JWT.Extensions.DependencyInjection.Tests/JWT.Extensions.DependencyInjection.Tests.csproj @@ -10,6 +10,7 @@ + diff --git a/tests/JWT.Tests.Common/JWT.Tests.Common.csproj b/tests/JWT.Tests.Common/JWT.Tests.Common.csproj index 9f5972bec..262634e9c 100644 --- a/tests/JWT.Tests.Common/JWT.Tests.Common.csproj +++ b/tests/JWT.Tests.Common/JWT.Tests.Common.csproj @@ -1,7 +1,7 @@  - net46;netstandard2.0;net6.0 + net46;netstandard2.0;netcoreapp3.1;net6.0 diff --git a/tests/JWT.Tests.Common/JwtDecoderTests.cs b/tests/JWT.Tests.Common/JwtDecoderTests.cs index 32483b5b5..26a69b8a4 100644 --- a/tests/JWT.Tests.Common/JwtDecoderTests.cs +++ b/tests/JWT.Tests.Common/JwtDecoderTests.cs @@ -5,11 +5,12 @@ 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] @@ -22,7 +23,7 @@ public void DecodeHeader_Should_Return_Header() { const string token = TestData.TokenByAsymmetricAlgorithm; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var urlEncoder = new JwtBase64UrlEncoder(); var decoder = new JwtDecoder(serializer, urlEncoder); @@ -37,7 +38,7 @@ public void DecodeHeaderToDictionary_Should_Return_Header() { const string token = TestData.TokenByAsymmetricAlgorithm; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var urlEncoder = new JwtBase64UrlEncoder(); var decoder = new JwtDecoder(serializer, urlEncoder); @@ -59,7 +60,7 @@ public void DecodeHeader_To_JwtHeader_Should_Return_Header() { const string token = TestData.TokenByAsymmetricAlgorithm; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var urlEncoder = new JwtBase64UrlEncoder(); var decoder = new JwtDecoder(serializer, urlEncoder); @@ -84,7 +85,7 @@ public void DecodeHeader_To_Dictionary_Should_Return_Header() { const string token = TestData.TokenByAsymmetricAlgorithm; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var urlEncoder = new JwtBase64UrlEncoder(); var decoder = new JwtDecoder(serializer, urlEncoder); @@ -106,7 +107,7 @@ public void Decode_Should_Decode_Token_To_Json_String() const string token = TestData.Token; var payload = TestData.Customer; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var validator = new JwtValidator(serializer, dateTimeProvider); var urlEncoder = new JwtBase64UrlEncoder(); @@ -126,7 +127,7 @@ public void Decode_With_Multiple_Secrets_Should_Return_Token() const string token = TestData.Token; var payload = TestData.Customer; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var validator = new JwtValidator(serializer, dateTimeProvider); var urlEncoder = new JwtBase64UrlEncoder(); @@ -149,7 +150,7 @@ public void Decode_Should_Call_Custom_AlgorithmFactory() .Returns(TestData.RS256Algorithm) .Verifiable("because custom algorithm factory must be called"); - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var validator = new JwtValidator(serializer, dateTimeProvider); var urlEncoder = new JwtBase64UrlEncoder(); @@ -166,7 +167,7 @@ public void Decode_Without_VerifySignature_And_Without_Algorithm_Should_Return_T const string token = TestData.Token; var payload = TestData.Customer; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var urlEncoder = new JwtBase64UrlEncoder(); var decoder = new JwtDecoder(serializer, urlEncoder); @@ -177,14 +178,14 @@ public void Decode_Without_VerifySignature_And_Without_Algorithm_Should_Return_T actual.Should() .Be(expected, "because the provided object should be correctly serialized in the token"); } - + [TestMethod] public void Decode_Token_Missing_Signature_Without_VerifySignature_And_Without_Algorithm_Should_Return_Token() { const string token = TestData.TokenWithoutSignature; var payload = TestData.Customer; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var urlEncoder = new JwtBase64UrlEncoder(); var decoder = new JwtDecoder(serializer, urlEncoder); @@ -203,7 +204,7 @@ public void DecodeToObject_Should_Decode_Token_To_Dictionary() const string key = TestData.Secret; const string token = TestData.Token; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var validator = new JwtValidator(serializer, dateTimeProvider); var urlEncoder = new JwtBase64UrlEncoder(); @@ -222,7 +223,7 @@ public void DecodeToObject_Should_Decode_Token_To_Dictionary_Multiple_Secrets() const string key = TestData.Secret; const string token = TestData.Token; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var validator = new JwtValidator(serializer, dateTimeProvider); var urlEncoder = new JwtBase64UrlEncoder(); @@ -241,7 +242,7 @@ public void DecodeToObject_Should_Decode_Token_To_Generic_Type() const string key = TestData.Secret; const string token = TestData.Token; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var validator = new JwtValidator(serializer, dateTimeProvider); var urlEncoder = new JwtBase64UrlEncoder(); @@ -259,7 +260,7 @@ public void DecodeToObject_Should_Decode_Token_To_Generic_Type_With_Multiple_Sec const string key = TestData.Secret; const string token = TestData.Token; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var validator = new JwtValidator(serializer, dateTimeProvider); var urlEncoder = new JwtBase64UrlEncoder(); @@ -277,7 +278,7 @@ public void DecodeToObject_Should_Throw_Exception_On_Malformed_Token() const string badToken = TestData.TokenWithoutHeader; const string key = TestData.Secret; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var validator = new JwtValidator(serializer, dateTimeProvider); var urlEncoder = new JwtBase64UrlEncoder(); @@ -296,7 +297,7 @@ public void DecodeToObject_Should_Throw_Exception_On_Malformed_Token_With_Multip const string badToken = TestData.TokenWithoutHeader; var keys = new[] { TestData.Secret, TestData.Secret2 }; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var validator = new JwtValidator(serializer, dateTimeProvider); var urlEncoder = new JwtBase64UrlEncoder(); @@ -315,7 +316,7 @@ public void DecodeToObject_Should_Throw_Exception_On_Invalid_Key() const string token = TestData.Token; var key = _fixture.Create(); - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); var urlEncoder = new JwtBase64UrlEncoder(); var decoder = new JwtDecoder(serializer, validator, urlEncoder, TestData.HMACSHA256Algorithm); @@ -333,7 +334,7 @@ public void DecodeToObject_Should_Throw_Exception_On_Invalid_Key_Multiple_Secret const string token = TestData.Token; var keys = _fixture.Create(); - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); var urlEncoder = new JwtBase64UrlEncoder(); var decoder = new JwtDecoder(serializer, validator, urlEncoder, TestData.HMACSHA256Algorithm); @@ -350,7 +351,7 @@ public void DecodeToObject_Should_Throw_Exception_On_Invalid_Expiration_Claim() { const string key = TestData.Secret; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); var urlEncoder = new JwtBase64UrlEncoder(); @@ -371,7 +372,7 @@ public void DecodeToObject_Should_Throw_Exception_On_Invalid_Expiration_Claim_Mu { const string key = TestData.Secret; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); var urlEncoder = new JwtBase64UrlEncoder(); @@ -392,7 +393,7 @@ public void DecodeToObject_Should_Throw_Exception_On_Null_Expiration_Claim() { const string key = TestData.Secret; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); var urlEncoder = new JwtBase64UrlEncoder(); @@ -414,7 +415,7 @@ public void DecodeToObject_Should_Throw_Exception_On_Null_Expiration_Claim_Multi { const string key = TestData.Secret; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); var urlEncoder = new JwtBase64UrlEncoder(); @@ -438,7 +439,7 @@ public void DecodeToObject_Should_Throw_Exception_On_Expired_Claim() const int timeDelta = -1; var dateTimeProvider = new UtcDateTimeProvider(); - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var validator = new JwtValidator(serializer, dateTimeProvider); var urlEncoder = new JwtBase64UrlEncoder(); @@ -462,7 +463,7 @@ public void DecodeToObject_Should_Decode_Token_On_Exp_Claim_After_Year2038() { const string key = TestData.Secret; var dateTimeProvider = new UtcDateTimeProvider(); - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var validator = new JwtValidator(serializer, dateTimeProvider); var urlEncoder = new JwtBase64UrlEncoder(); var decoder = new JwtDecoder(serializer, validator, urlEncoder, TestData.HMACSHA256Algorithm); @@ -483,7 +484,7 @@ public void DecodeToObject_Should_Decode_Token_On_Exp_Claim_After_Year2038() [TestMethod] public void DecodeToObject_Should_Throw_Exception_Before_NotBefore_Becomes_Valid() { - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var validator = new JwtValidator(serializer, dateTimeProvider); var urlEncoder = new JwtBase64UrlEncoder(); @@ -508,7 +509,7 @@ public void DecodeToObject_Should_Decode_Token_After_NotBefore_Becomes_Valid() var dateTimeProvider = new UtcDateTimeProvider(); const string key = TestData.Secret; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); var urlEncoder = new JwtBase64UrlEncoder(); @@ -531,7 +532,7 @@ public void DecodeToObject_Should_Throw_Exception_On_Null_NotBefore_Claim() { const string key = TestData.Secret; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); var urlEncoder = new JwtBase64UrlEncoder(); diff --git a/tests/JWT.Tests.Common/JwtEncoderTests.cs b/tests/JWT.Tests.Common/JwtEncoderTests.cs index b201c1d0a..a3ee9d892 100644 --- a/tests/JWT.Tests.Common/JwtEncoderTests.cs +++ b/tests/JWT.Tests.Common/JwtEncoderTests.cs @@ -1,10 +1,11 @@ using System.Collections.Generic; 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] @@ -19,7 +20,7 @@ public void Encode_Should_Encode_To_Token() var algorithm = new HMACSHA256Algorithm(); var urlEncoder = new JwtBase64UrlEncoder(); - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var encoder = new JwtEncoder(algorithm, serializer, urlEncoder); var actual = encoder.Encode(toEncode, key); @@ -41,7 +42,7 @@ public void Encode_Should_Encode_To_Token_With_Extra_Headers() var algorithm = new HMACSHA256Algorithm(); var urlEncoder = new JwtBase64UrlEncoder(); - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var encoder = new JwtEncoder(algorithm, serializer, urlEncoder); var actual = encoder.Encode(extraHeaders, toEncode, key); @@ -63,7 +64,7 @@ public void Encode_Should_Encode_To_Token_With_Custom_Type_Headers() var algorithm = new HMACSHA256Algorithm(); var urlEncoder = new JwtBase64UrlEncoder(); - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var encoder = new JwtEncoder(algorithm, serializer, urlEncoder); var actual = encoder.Encode(extraHeaders, toEncode, key); @@ -81,7 +82,7 @@ public void Encode_With_NoAlgorithm_Should_Encode_To_Token() var algorithm = new NoneAlgorithm(); var urlEncoder = new JwtBase64UrlEncoder(); - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var encoder = new JwtEncoder(algorithm, serializer, urlEncoder); var actual = encoder.Encode(toEncode, key); diff --git a/tests/JWT.Tests.Common/JwtSecurityTests.cs b/tests/JWT.Tests.Common/JwtSecurityTests.cs index 275d5c089..e793ba5d9 100644 --- a/tests/JWT.Tests.Common/JwtSecurityTests.cs +++ b/tests/JWT.Tests.Common/JwtSecurityTests.cs @@ -3,10 +3,11 @@ 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] @@ -21,7 +22,7 @@ public void Decode_Should_Throw_Exception_When_Jwt_Contains_No_Algorithm() var key = _fixture.Create(); const string token = TestData.TokenWithoutAlgorithm; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); var urlEncoder = new JwtBase64UrlEncoder(); var decoder = new JwtDecoder(serializer, validator, urlEncoder, new HMACSHAAlgorithmFactory()); @@ -40,7 +41,7 @@ public void Decode_Should_Throw_Exception_When_Jwt_Contains_Multiple_Keys() var keys = _fixture.Create(); const string token = TestData.TokenWithoutAlgorithm; - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var validator = new JwtValidator(serializer, new UtcDateTimeProvider()); var urlEncoder = new JwtBase64UrlEncoder(); var decoder = new JwtDecoder(serializer, validator, urlEncoder, new HMACSHAAlgorithmFactory()); @@ -56,7 +57,7 @@ public void Decode_Should_Throw_Exception_When_Jwt_Contains_Multiple_Keys() [TestCategory("Security")] public void Decode_Should_Throw_Exception_When_Jwt_Contains_HMA_Algorithm_But_RSA_Was_Expected() { - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var urlEncoder = new JwtBase64UrlEncoder(); var encoder = new JwtEncoder(TestData.HMACSHA256Algorithm, serializer, urlEncoder); @@ -78,7 +79,7 @@ public void Decode_Should_Throw_Exception_When_Jwt_Contains_HMA_Algorithm_But_RS [TestCategory("Security")] public void Decode_Should_Throw_Exception_When_Jwt_Contains_HMA_Algorithm_But_RSA_Was_Expected_With_Multiple_Keys() { - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var urlEncoder = new JwtBase64UrlEncoder(); var encoder = new JwtEncoder(TestData.HMACSHA256Algorithm, serializer, urlEncoder); diff --git a/tests/JWT.Tests.Common/JwtValidatorTests.cs b/tests/JWT.Tests.Common/JwtValidatorTests.cs index 5508ab1e4..48843fb84 100644 --- a/tests/JWT.Tests.Common/JwtValidatorTests.cs +++ b/tests/JWT.Tests.Common/JwtValidatorTests.cs @@ -2,11 +2,12 @@ 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 { @@ -27,7 +28,7 @@ public void Ctor_Should_Throw_Exception_When_Serializer_Is_Null() [TestMethod] public void Ctor_Should_Throw_Exception_When_DateTimeProvider_Is_Null() { - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); Action action = () => new JwtValidator(serializer, null); @@ -38,7 +39,7 @@ public void Ctor_Should_Throw_Exception_When_DateTimeProvider_Is_Null() [TestMethod] public void Ctor_Should_Throw_Exception_When_ValidationParameters_Are_Null() { - var serializer = new JsonNetSerializer(); + var serializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); Action action = () => new JwtValidator(serializer, dateTimeProvider, null); @@ -56,7 +57,7 @@ public void Ctor_Should_Throw_Exception_When_ValidationParameters_Are_Null() [DataRow("{}", TestData.Token, "")] public void Validate_Should_Throw_Exception_When_Argument_Is_Null_Or_Empty(string payloadJson, string decodedCrypto, string decodedSignature) { - var jsonSerializer = new JsonNetSerializer(); + var jsonSerializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var jwtValidator = new JwtValidator(jsonSerializer, dateTimeProvider); @@ -71,7 +72,7 @@ public void Validate_Should_Throw_Exception_When_Signature_Is_Invalid() { const string token = TestData.Token; var urlEncoder = new JwtBase64UrlEncoder(); - var jsonSerializer = new JsonNetSerializer(); + var jsonSerializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var jwt = new JwtParts(token); @@ -98,7 +99,7 @@ public void Validate_Should_Throw_Exception_When_Signature_Is_Invalid() public void Validate_Should_Not_Throw_Exception_When_Crypto_Matches_Signature() { var urlEncoder = new JwtBase64UrlEncoder(); - var jsonSerializer = new JsonNetSerializer(); + var jsonSerializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var jwt = new JwtParts(TestData.Token); @@ -125,7 +126,7 @@ public void Validate_Should_Not_Throw_Exception_When_Crypto_Matches_Signature() [DataRow("{}", TestData.Token, "")] public void TryValidate_Should_Return_False_And_Exception_Not_Null_When_Argument_Is_Null_Or_Empty(string payloadJson, string decodedCrypto, string decodedSignature) { - var jsonSerializer = new JsonNetSerializer(); + var jsonSerializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var jwtValidator = new JwtValidator(jsonSerializer, dateTimeProvider); @@ -142,7 +143,7 @@ public void TryValidate_Should_Return_False_And_Exception_Not_Null_When_Argument public void TryValidate_Should_Return_False_And_Exception_Not_Null_When_Signature_Is_Not_Valid() { var urlEncoder = new JwtBase64UrlEncoder(); - var jsonSerializer = new JsonNetSerializer(); + var jsonSerializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var jwt = new JwtParts(TestData.Token); @@ -172,7 +173,7 @@ public void TryValidate_Should_Return_False_And_Exception_Not_Null_When_Signatur public void TryValidate_Should_Return_True_And_Exception_Null_When_Crypto_Signature_Is_Valid() { var urlEncoder = new JwtBase64UrlEncoder(); - var jsonSerializer = new JsonNetSerializer(); + var jsonSerializer = CreateSerializer(); var dateTimeProvider = new UtcDateTimeProvider(); var jwt = new JwtParts(TestData.Token); @@ -201,7 +202,7 @@ public void TryValidate_Should_Return_True_And_Exception_Null_When_Crypto_Signat public void TryValidate_Should_Return_False_And_Exception_Not_Null_When_Token_Is_Expired() { var urlEncoder = new JwtBase64UrlEncoder(); - var jsonSerializer = new JsonNetSerializer(); + var jsonSerializer = CreateSerializer(); var dateTimeProvider = new StaticDateTimeProvider(DateTimeOffset.FromUnixTimeSeconds(TestData.TokenTimestamp)); var jwt = new JwtParts(TestData.TokenWithExp); @@ -233,7 +234,7 @@ public void TryValidate_Should_Return_False_And_Exception_Not_Null_When_Token_Is public void TryValidate_Should_Return_True_And_Exception_Null_When_Token_Is_Not_Expired() { var urlEncoder = new JwtBase64UrlEncoder(); - var jsonSerializer = new JsonNetSerializer(); + var jsonSerializer = CreateSerializer(); var dateTimeProvider = new StaticDateTimeProvider(DateTimeOffset.FromUnixTimeSeconds(TestData.TokenTimestamp - 1)); var jwt = new JwtParts(TestData.TokenWithExp); @@ -262,7 +263,7 @@ public void TryValidate_Should_Return_True_And_Exception_Null_When_Token_Is_Not_ public void TryValidate_Should_Return_True_And_Exception_Null_When_Token_Is_Expired_But_Validator_Has_Time_Margin() { var urlEncoder = new JwtBase64UrlEncoder(); - var jsonSerializer = new JsonNetSerializer(); + var jsonSerializer = CreateSerializer(); var dateTimeProvider = new StaticDateTimeProvider(DateTimeOffset.FromUnixTimeSeconds(TestData.TokenTimestamp)); var valParams = ValidationParameters.Default.With(p => p.TimeMargin = 1); @@ -292,7 +293,7 @@ public void TryValidate_Should_Return_True_And_Exception_Null_When_Token_Is_Expi public void TryValidate_Should_Return_False_And_Exception_Not_Null_When_Token_Is_Not_Yet_Usable() { var urlEncoder = new JwtBase64UrlEncoder(); - var jsonSerializer = new JsonNetSerializer(); + var jsonSerializer = CreateSerializer(); var dateTimeProvider = new StaticDateTimeProvider(DateTimeOffset.FromUnixTimeSeconds(TestData.TokenTimestamp - 1)); var jwt = new JwtParts(TestData.TokenWithNbf); @@ -322,7 +323,7 @@ public void TryValidate_Should_Return_False_And_Exception_Not_Null_When_Token_Is public void TryValidate_Should_Return_True_And_Exception_Null_When_Token_Is_Usable() { var urlEncoder = new JwtBase64UrlEncoder(); - var jsonSerializer = new JsonNetSerializer(); + var jsonSerializer = CreateSerializer(); var dateTimeProvider = new StaticDateTimeProvider(DateTimeOffset.FromUnixTimeSeconds(TestData.TokenTimestamp)); var jwt = new JwtParts(TestData.TokenWithNbf); @@ -348,10 +349,10 @@ public void TryValidate_Should_Return_True_And_Exception_Null_When_Token_Is_Usab } [TestMethod] - public void TryValidate_Should_Return_True_And_Exception_Null_When_Token_Is_Not_Yet_Usable_But_Validator_Has_Time_Margin() + public void TryValidate_Should_Return_True_And_Exception_Null_When_Token_Is_Not_Yet_Valid_But_Validator_Has_Time_Margin() { var urlEncoder = new JwtBase64UrlEncoder(); - var jsonSerializer = new JsonNetSerializer(); + var jsonSerializer = CreateSerializer(); var dateTimeProvider = new StaticDateTimeProvider(DateTimeOffset.FromUnixTimeSeconds(TestData.TokenTimestamp - 1)); var valParams = ValidationParameters.Default.With(p => p.TimeMargin = 1); diff --git a/tests/JWT.Tests.Net46/JWT.Tests.Net46.csproj b/tests/JWT.Tests.Net46/JWT.Tests.Net46.csproj index 0adff8034..7aa8b5fd8 100644 --- a/tests/JWT.Tests.Net46/JWT.Tests.Net46.csproj +++ b/tests/JWT.Tests.Net46/JWT.Tests.Net46.csproj @@ -1,7 +1,7 @@  - net46 + net462 diff --git a/tests/JWT.Tests.Net60/JWT.Tests.Net60.csproj b/tests/JWT.Tests.Net60/JWT.Tests.Net60.csproj index c966f71d6..1caf4dc0a 100644 --- a/tests/JWT.Tests.Net60/JWT.Tests.Net60.csproj +++ b/tests/JWT.Tests.Net60/JWT.Tests.Net60.csproj @@ -1,8 +1,7 @@ - + net6.0 - .NETFramework,Version=6.0 @@ -12,6 +11,7 @@ + diff --git a/tests/JWT.Tests.NetCore3/JWT.Tests.NetCore3.csproj b/tests/JWT.Tests.NetCore3/JWT.Tests.NetCore3.csproj index f80988ae1..37d8eaaa8 100644 --- a/tests/JWT.Tests.NetCore3/JWT.Tests.NetCore3.csproj +++ b/tests/JWT.Tests.NetCore3/JWT.Tests.NetCore3.csproj @@ -11,6 +11,7 @@ +