Skip to content

Commit

Permalink
Added AddJwtEncoder() overload accepting IJwtAlgorithm (#385)
Browse files Browse the repository at this point in the history
* Added overload accepting IJwtAlgorithm
* Made overload accepting IAlgorithmFactory private
* Added GenericAlgorithmFactory
* Bumped version of JWT to 9.0.0-beta5
* Bumped version of JWT.Extensions.DI to 2.0.0-beta1
* Bumped version of JWT.Extensions.AspNetCore to 9.0.0-beta2
  • Loading branch information
abatishchev authored Apr 10, 2022
1 parent 3254c0d commit e15f629
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 14 deletions.
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-beta1</Version>
<Version>9.0.0-beta2</Version>
<FileVersion>9.0.0.0</FileVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<RootNamespace>JWT.Extensions.AspNetCore</RootNamespace>
Expand Down
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;dependenсy injection</PackageTags>
<PackageLicense>MIT</PackageLicense>
<Version>1.1.0</Version>
<FileVersion>1.0.0.0</FileVersion>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Version>2.0.0-beta1</Version>
<FileVersion>2.0.0.0</FileVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<RootNamespace>JWT</RootNamespace>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JWT.Algorithms;
using System;
using JWT.Algorithms;
using JWT.Internal;
using JWT.Serializers;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -17,10 +18,20 @@ public static IServiceCollection AddJwtEncoder(this IServiceCollection services)
return services;
}

public static IServiceCollection AddJwtEncoder<TFactory>(this IServiceCollection services)
public static IServiceCollection AddJwtEncoder<TAlgo>(this IServiceCollection services)
where TAlgo : IJwtAlgorithm, new() =>
services.AddJwtEncoder(new GenericAlgorithmFactory<TAlgo>());

public static IServiceCollection AddJwtEncoder(this IServiceCollection services, Func<IJwtAlgorithm> algFactory) =>
services.AddJwtEncoder(new DelegateAlgorithmFactory(algFactory));

public static IServiceCollection AddJwtEncoder(this IServiceCollection services, IJwtAlgorithm algorithm) =>
services.AddJwtEncoder(() => algorithm);

private static IServiceCollection AddJwtEncoder<TFactory>(this IServiceCollection services, TFactory algFactory)
where TFactory : class, IAlgorithmFactory
{
services.TryAddSingleton<IAlgorithmFactory, TFactory>();
services.TryAddSingleton<IAlgorithmFactory>(_ => algFactory);

return services.AddJwtEncoder();
}
Expand Down
10 changes: 10 additions & 0 deletions src/JWT/Algorithms/GenericAlgorithmFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace JWT.Algorithms
{
public sealed class GenericAlgorithmFactory<TAlgo> : IAlgorithmFactory
where TAlgo : IJwtAlgorithm, new()
{
/// <inheritdoc />
public IJwtAlgorithm Create(JwtDecoderContext context) =>
new TAlgo();
}
}
2 changes: 1 addition & 1 deletion src/JWT/Algorithms/HMACSHAAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace JWT.Algorithms
[Obsolete(ObsoleteMessage, error: false)]
public abstract class HMACSHAAlgorithm : IJwtAlgorithm
{
internal const string ObsoleteMessage = "HMAC SHA based algorithms are not secure to protect modern web applications. Consider switch to RSASSA or ECDSA.";
internal const string ObsoleteMessage = "HMAC SHA based algorithms are not secure to protect modern web applications. Consider switching to RSASSA or ECDSA.";

/// <inheritdoc />
public abstract string Name { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<Authors>Alexander Batishchev, John Sheehan, Michael Lehenbauer</Authors>
<PackageTags>jwt;json;authorization</PackageTags>
<PackageLicense>CC0-1.0</PackageLicense>
<Version>9.0.0-beta4</Version>
<Version>9.0.0-beta5/Version>
<FileVersion>9.0.0.0</FileVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<RootNamespace>JWT</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,64 @@ public void AddJwtEncoder_And_Register_AlgorithmFactory_Should_Register_JwtEncod
{
var services = new ServiceCollection();
services.AddJwtEncoder()
.AddSingleton<IAlgorithmFactory, HMACSHAAlgorithmFactory>();
.AddSingleton<IAlgorithmFactory>(p => new DelegateAlgorithmFactory(TestData.HMACSHA256Algorithm));

var provider = services.BuildServiceProvider();
var encoder = provider.GetRequiredService<IJwtEncoder>();
encoder.Should().NotBeNull();

var payload = TestData.DictionaryPayload;
const string key = TestData.Secret;
var jwt = encoder.Encode(payload, key);
jwt.Should().NotBeNull();
}

[TestMethod]
public void AddJwtEncoder_With_Algorithm_Generic_Type_Should_Register_JwtEncoder()
{
var services = new ServiceCollection();
services.AddJwtEncoder<HMACSHA256Algorithm>();

var provider = services.BuildServiceProvider();
var encoder = provider.GetRequiredService<IJwtEncoder>();
encoder.Should().NotBeNull();

var payload = TestData.DictionaryPayload;
const string key = TestData.Secret;
var jwt = encoder.Encode(payload, key);
jwt.Should().NotBeNull();
}

[TestMethod]
public void AddJwtEncoder_With_AlgorithmFactory_Should_Register_JwtEncoder()
public void AddJwtEncoder_With_Algorithm_Should_Register_JwtEncoder()
{
var services = new ServiceCollection();
services.AddJwtEncoder<HMACSHAAlgorithmFactory>();
services.AddJwtEncoder(TestData.HMACSHA256Algorithm);

var provider = services.BuildServiceProvider();
var encoder = provider.GetRequiredService<IJwtEncoder>();
encoder.Should().NotBeNull();

var payload = TestData.DictionaryPayload;
const string key = TestData.Secret;
var jwt = encoder.Encode(payload, key);
jwt.Should().NotBeNull();
}

[TestMethod]
public void AddJwtEncoder_With_Algorithm_Func_Should_Register_JwtEncoder()
{
var services = new ServiceCollection();
services.AddJwtEncoder(() => TestData.HMACSHA256Algorithm);

var provider = services.BuildServiceProvider();
var encoder = provider.GetRequiredService<IJwtEncoder>();
encoder.Should().NotBeNull();

var payload = TestData.DictionaryPayload;
const string key = TestData.Secret;
var jwt = encoder.Encode(payload, key);
jwt.Should().NotBeNull();
}

[TestMethod]
Expand All @@ -70,8 +110,11 @@ public void AddJwtDecoder_And_Register_AlgorithmFactory_Should_Register_JwtDecod

var provider = services.BuildServiceProvider();
var decoder = provider.GetRequiredService<IJwtDecoder>();

decoder.Should().NotBeNull();

const string token = TestData.Token;
var payload = decoder.Decode(token);
payload.Should().NotBeNull();
}

[TestMethod]
Expand All @@ -82,8 +125,11 @@ public void AddJwtDecoder_With_AlgorithmFactory_Should_Register_JwtDecoder()

var provider = services.BuildServiceProvider();
var decoder = provider.GetRequiredService<IJwtDecoder>();

decoder.Should().NotBeNull();

const string token = TestData.Token;
var payload = decoder.Decode(token);
payload.Should().NotBeNull();
}
}
}

0 comments on commit e15f629

Please sign in to comment.