Skip to content

Commit

Permalink
Replaced NewtonSoft.Json with System.Text.Json (#401)
Browse files Browse the repository at this point in the history
* Added SystemTextSerializer, JsonSerializerFactory
* References System.Text.Json
* Bumped version of JWT to 10.0.0-beta3
* Bumped version of JWT.Extensions.AspNetCore to 10.0.0-beta1
* Bumped version of JWT.Extensions.DependecyInjection to 2.2.0-beta2

Co-authored-by: Alexander Batishchev <[email protected]>
  • Loading branch information
hartmark and abatishchev authored Jun 18, 2022
1 parent e606e88 commit 1c4e365
Show file tree
Hide file tree
Showing 28 changed files with 379 additions and 102 deletions.
4 changes: 3 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<LangVersion>latest</LangVersion>

Expand All @@ -25,7 +26,7 @@
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>

<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.csproj'">
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand All @@ -35,4 +36,5 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions JWT.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
17 changes: 17 additions & 0 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<Choose>
<When Condition="'$(TargetFramework)' == 'net462' OR '$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'net6.0'">
<PropertyGroup>
<DefineConstants>$(DefineConstants);SYSTEM_TEXT_JSON</DefineConstants>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<DefineConstants>$(DefineConstants);NEWTONSOFT_JSON</DefineConstants>
</PropertyGroup>
</Otherwise>
</Choose>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ IIdentity IIdentityFactory.CreateIdentity(Type type, object payload)
if (payload is null)
throw new ArgumentException(nameof(payload));

Type targetType = typeof(IDictionary<string, string>);
Type targetType = typeof(IDictionary<string, object>);
if (!targetType.IsAssignableFrom(type))
throw new ArgumentOutOfRangeException(nameof(type), $"Type {type} is not assignable to {targetType}");

return CreateIdentity((IDictionary<string, string>)payload);
return CreateIdentity((IDictionary<string, object>)payload);
}

/// <summary>
/// Creates user's identity from user's claims
/// </summary>
/// <param name="payload"><see cref="IDictionary{String,String}" /> of user's claims</param>
/// <returns><see cref="ClaimsIdentity" /></returns>
public IIdentity CreateIdentity(IDictionary<string, string> payload)
public IIdentity CreateIdentity(IDictionary<string, object> payload)
{
var claims = payload.Select(p => new Claim(p.Key, p.Value));
var claims = payload.Select(p => new Claim(p.Key, p.Value.ToString()));
return _options.CurrentValue.IncludeAuthenticationScheme ?
new ClaimsIdentity(claims, JwtAuthenticationDefaults.AuthenticationScheme) :
new ClaimsIdentity(claims);
Expand Down
10 changes: 5 additions & 5 deletions src/JWT.Extensions.AspNetCore/JWT.Extensions.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<Authors>Alexander Batishchev</Authors>
<PackageTags>jwt;json;asp.net;asp.net core;.net core;authorization</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>9.1.0</Version>
<FileVersion>9.1.0.0</FileVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<Version>10.0.0-beta1</Version>
<FileVersion>10.0.0.0</FileVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
<RootNamespace>JWT.Extensions.AspNetCore</RootNamespace>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
Expand All @@ -22,7 +22,6 @@
<DefineConstants>TRACE;DEBUG</DefineConstants>
<OutputPath>bin\Debug\</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>TRACE</DefineConstants>
<OutputPath>bin\Release\</OutputPath>
Expand All @@ -31,7 +30,8 @@

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

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/JWT.Extensions.AspNetCore/JwtAuthenticationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public class JwtAuthenticationOptions : AuthenticationSchemeOptions
/// <remarks>
/// The default value is <see cref="Dictionary{String, String}" />.
/// </remarks>
public Type PayloadType { get; set; } = typeof(Dictionary<string, string>);
public Type PayloadType { get; set; } = typeof(Dictionary<string, object>);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Authors>Alexander Batishchev</Authors>
<PackageTags>jwt;json;asp.net;asp.net core;.net core;authorization;dependenсy injection</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>2.1.0</Version>
<Version>2.2.0-beta2</Version>
<FileVersion>2.0.0.0</FileVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<RootNamespace>JWT</RootNamespace>
Expand All @@ -22,7 +22,6 @@
<DefineConstants>TRACE;DEBUG</DefineConstants>
<OutputPath>bin\Debug\</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>TRACE</DefineConstants>
<OutputPath>bin\Release\</OutputPath>
Expand All @@ -31,6 +30,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.25" />
<PackageReference Condition="$(DefineConstants.Contains(SYSTEM_TEXT_JSON))" Include="System.Text.Json" Version="6.0.4" />
<PackageReference Condition="$(DefineConstants.Contains(NEWTONSOFT_JSON))" Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup>

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

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

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

return services;
Expand All @@ -39,7 +44,7 @@ private static IServiceCollection AddJwtEncoder<TFactory>(this IServiceCollectio
public static IServiceCollection AddJwtDecoder(this IServiceCollection services)
{
services.TryAddSingleton<IJwtDecoder, JwtDecoder>();
services.TryAddSingleton<IJsonSerializer, JsonNetSerializer>();
services.TryAddSingleton<IJsonSerializer, JsonSerializer>();
services.TryAddSingleton<IJwtValidator, JwtValidator>();
services.TryAddSingleton<IBase64UrlEncoder, JwtBase64UrlEncoder>();
services.TryAddSingleton<IDateTimeProvider, UtcDatetimeProvider>();
Expand Down
4 changes: 2 additions & 2 deletions src/JWT/Algorithms/RSAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
10 changes: 3 additions & 7 deletions src/JWT/Builder/JwtBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Linq;
using JWT.Algorithms;
using JWT.Serializers;

using static JWT.Internal.EncodingHelper;
using static JWT.Serializers.JsonSerializerFactory;

namespace JWT.Builder
{
Expand All @@ -18,7 +17,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;
Expand Down Expand Up @@ -73,9 +72,6 @@ public JwtBuilder AddClaim(string name, object value)
/// <summary>
/// Sets JWT serializer.
/// </summary>
/// <remarks>
/// If not set then default <see cref="JsonNetSerializer" /> will be used.
/// </remarks>
/// <returns>Current builder instance</returns>
public JwtBuilder WithSerializer(IJsonSerializer serializer)
{
Expand Down Expand Up @@ -429,4 +425,4 @@ private bool CanDecodeHeader()
return true;
}
}
}
}
4 changes: 4 additions & 0 deletions src/JWT/Builder/JwtHeader.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
10 changes: 5 additions & 5 deletions src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@
<Authors>Alexander Batishchev, John Sheehan, Michael Lehenbauer</Authors>
<PackageTags>jwt;json;authorization</PackageTags>
<PackageLicenseExpression>CC0-1.0</PackageLicenseExpression>
<Version>10.0.0-beta2</Version>
<Version>10.0.0-beta3</Version>
<FileVersion>10.0.0.0</FileVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
<RootNamespace>JWT</RootNamespace>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE;DEBUG</DefineConstants>
<DefineConstants>$(DefineConstants);TRACE;DEBUG</DefineConstants>
<OutputPath>bin\Debug\</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>$(DefineConstants);TRACE</DefineConstants>
<OutputPath>bin\Release\</OutputPath>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

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

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3' OR '$(TargetFramework)' == 'netstandard2.0'">
Expand Down
2 changes: 1 addition & 1 deletion src/JWT/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
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)]
[assembly: InternalsVisibleTo("JWT.Tests.Net46, PublicKey=" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("JWT.Tests.Net60, PublicKey=" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("JWT.Extensions.AspNetCore.Tests, PublicKey=" + AssemblyInfo.PublicKey)]

namespace JWT
{
Expand Down
Loading

0 comments on commit 1c4e365

Please sign in to comment.