Skip to content

Commit

Permalink
Redo
Browse files Browse the repository at this point in the history
  • Loading branch information
abatishchev committed Apr 11, 2022
1 parent 7b71f7e commit 9d9476f
Show file tree
Hide file tree
Showing 25 changed files with 269 additions and 128 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>
16 changes: 14 additions & 2 deletions src/JWT.Extensions.AspNetCore/JWT.Extensions.AspNetCore.csproj
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</PackageTags>
<PackageLicense>MIT</PackageLicense>
<Version>9.0.0-beta2</Version>
<Version>9.0.0-beta3</Version>
<FileVersion>9.0.0.0</FileVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<RootNamespace>JWT.Extensions.AspNetCore</RootNamespace>
Expand All @@ -31,9 +31,21 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup>

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

<ItemGroup>
<ProjectReference Include="..\JWT.Extensions.DependencyInjection\JWT.Extensions.DependencyInjection.csproj" />
<ProjectReference Include="..\JWT\JWT.csproj" />
Expand Down
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>
<PackageLicense>MIT</PackageLicense>
<Version>2.0.0-beta1</Version>
<Version>2.0.0-beta2</Version>
<FileVersion>2.0.0.0</FileVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<RootNamespace>JWT</RootNamespace>
Expand All @@ -33,6 +33,19 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.22" />
</ItemGroup>

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

<ItemGroup>
<ProjectReference Include="..\JWT\JWT.csproj" />
</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
16 changes: 14 additions & 2 deletions src/JWT/Builder/JwtBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using JWT.Serializers;

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

namespace JWT.Builder
{
Expand All @@ -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;
Expand Down Expand Up @@ -281,6 +282,17 @@ public T Decode<T>(string token)
return _decoder.DecodeToObject<T>(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)
Expand Down Expand Up @@ -411,4 +423,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
117 changes: 63 additions & 54 deletions src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
@@ -1,55 +1,64 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net6.0;net35;net40;net46;</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net35'">
<FrameworkPathOverride>$(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client</FrameworkPathOverride>
<AutomaticallyUseReferenceAssemblyPackages>false</AutomaticallyUseReferenceAssemblyPackages>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net35' OR '$(TargetFramework)' == 'net40' OR '$(TargetFramework)' == 'net46'">
<TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard1.3' OR '$(TargetFramework)' == 'netstandard2.0'">
<TargetFrameworkIdentifier>.NETStandard</TargetFrameworkIdentifier>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<TargetFrameworkIdentifier>.NETFramework,Version=6.0</TargetFrameworkIdentifier>
</PropertyGroup>

<PropertyGroup>
<Copyright>Public Domain</Copyright>
<PackageDescription>Jwt.Net, a JWT (JSON Web Token) implementation for .NET</PackageDescription>
<Authors>Alexander Batishchev, John Sheehan, Michael Lehenbauer</Authors>
<PackageTags>jwt;json;authorization</PackageTags>
<PackageLicense>CC0-1.0</PackageLicense>
<Version>9.0.0-beta5</Version>
<FileVersion>9.0.0.0</FileVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<RootNamespace>JWT</RootNamespace>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>

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

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

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3' OR '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.ComponentModel.Primitives" Version="4.3.0" />
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.7.0" />
<PackageReference Include="System.Security.Cryptography.Csp" Version="4.3.0" />
</ItemGroup>

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0;netstandard1.3;net462;net40;net35</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net35'">
<FrameworkPathOverride>$(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client</FrameworkPathOverride>
<AutomaticallyUseReferenceAssemblyPackages>false</AutomaticallyUseReferenceAssemblyPackages>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net35' OR '$(TargetFramework)' == 'net40' OR '$(TargetFramework)' == 'net462'">
<TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard1.3' OR '$(TargetFramework)' == 'netstandard2.0'">
<TargetFrameworkIdentifier>.NETStandard</TargetFrameworkIdentifier>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<TargetFrameworkIdentifier>.NETFramework,Version=6.0</TargetFrameworkIdentifier>
</PropertyGroup>

<PropertyGroup>
<Copyright>Public Domain</Copyright>
<PackageDescription>Jwt.Net, a JWT (JSON Web Token) implementation for .NET</PackageDescription>
<Authors>Alexander Batishchev, John Sheehan, Michael Lehenbauer</Authors>
<PackageTags>jwt;json;authorization</PackageTags>
<PackageLicense>CC0-1.0</PackageLicense>
<Version>9.0.0-beta5</Version>
<FileVersion>9.0.0.0</FileVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<RootNamespace>JWT</RootNamespace>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>

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

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

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

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3' OR '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.ComponentModel.Primitives" Version="4.3.0" />
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.7.0" />
<PackageReference Include="System.Security.Cryptography.Csp" Version="4.3.0" />
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion src/JWT/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
6 changes: 4 additions & 2 deletions src/JWT/Serializers/JsonNetSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#if NEWTONSOFT_JSON
using System;
using System.IO;
using System.Text;
using Newtonsoft.Json;
Expand Down Expand Up @@ -59,4 +60,5 @@ public object Deserialize(Type type, string json)
return _serializer.Deserialize(jsonReader, type);
}
}
}
}
#endif
16 changes: 16 additions & 0 deletions src/JWT/Serializers/JsonSerializerFactory.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Loading

0 comments on commit 9d9476f

Please sign in to comment.