Skip to content

Commit

Permalink
Adding IAlgFactory to master (v3) branch (#102)
Browse files Browse the repository at this point in the history
* Adding IAlgorithmFactory by default implemented by AlgorithmFactory. Using IAlgorithmFactory in JwtDecoder
* Renaming AlgorithmFactory to HMACSHAAlgorithmFactory
* Adding RSAlgorithmFactory inheriting HMACSHAAlgorithmFactory
* Adding DotSettings with abbreviations
* Fixing build, adding missed using
* Moving IAlgorithmFactory to the Algorithms namespace/folder
* Adding missing xml docs
* Bumping nuget version
  • Loading branch information
abatishchev authored May 13, 2017
1 parent 977cb10 commit 8137c59
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 28 deletions.
4 changes: 4 additions & 0 deletions JWT.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=HMACSHA/@EntryIndexedValue">HMACSHA</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RS/@EntryIndexedValue">RS</s:String>
</wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
using System;
using JWT.Algorithms;

namespace JWT
namespace JWT.Algorithms
{
/// <summary>
/// Provides IJwtAlgorithms.
/// </summary>
public sealed class AlgorithmFactory
/// <inheritdoc />
public class HMACSHAAlgorithmFactory : IAlgorithmFactory
{
/// <summary>
/// Creates an AlgorithmFactory using the provided
/// algorithm name.
/// </summary>
/// <param name="algorithmName">The name of the algorithm.</param>
/// <returns></returns>
/// <inheritdoc />
public IJwtAlgorithm Create(string algorithmName)
{
return Create((JwtHashAlgorithm)Enum.Parse(typeof(JwtHashAlgorithm), algorithmName));
}

/// <summary>
/// Creates an AlgorithmFactory using the provided
/// algorithm name.
/// </summary>
/// <param name="algorithm">The name of the algorithm.</param>
public IJwtAlgorithm Create(JwtHashAlgorithm algorithm)
/// <inheritdoc />
public virtual IJwtAlgorithm Create(JwtHashAlgorithm algorithm)
{
switch (algorithm)
{
Expand All @@ -34,6 +22,8 @@ public IJwtAlgorithm Create(JwtHashAlgorithm algorithm)
return new HMACSHA384Algorithm();
case JwtHashAlgorithm.HS512:
return new HMACSHA512Algorithm();
case JwtHashAlgorithm.RS256:
throw new NotSupportedException($"For algorithm {nameof(JwtHashAlgorithm.RS256)} please create custom factory by implementing {nameof(IAlgorithmFactory)}");
default:
throw new InvalidOperationException($"Algorithm {algorithm} is not supported.");
}
Expand Down
20 changes: 20 additions & 0 deletions src/JWT/Algorithms/IAlgorithmFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace JWT.Algorithms
{
/// <summary>
/// Provides IJwtAlgorithms.
/// </summary>
public interface IAlgorithmFactory
{
/// <summary>
/// Creates an AlgorithmFactory using the provided algorithm name.
/// </summary>
/// <param name="algorithmName">The name of the algorithm.</param>
IJwtAlgorithm Create(string algorithmName);

/// <summary>
/// Creates an AlgorithmFactory using the provided algorithm enum.
/// </summary>
/// <param name="algorithm">The enum value of the algorithm.</param>
IJwtAlgorithm Create(JwtHashAlgorithm algorithm);
}
}
28 changes: 28 additions & 0 deletions src/JWT/Algorithms/RSAlgorithmFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Security.Cryptography.X509Certificates;

namespace JWT.Algorithms
{
/// <inheritdoc />
public sealed class RSAlgorithmFactory : HMACSHAAlgorithmFactory
{
private readonly Func<X509Certificate2> _certFactory;

/// <summary>
/// Initializes a new instance of the <see cref="RSAlgorithmFactory"/> class
/// </summary>
/// <param name="certFactory">Func that returns <see cref="X509Certificate2" /> which will be used to instantiate <see cref="RS256Algorithm" /></param>
public RSAlgorithmFactory(Func<X509Certificate2> certFactory)
{
_certFactory = certFactory;
}

/// <inheritdoc />
public override IJwtAlgorithm Create(JwtHashAlgorithm algorithm)
{
return algorithm == JwtHashAlgorithm.RS256 ?
new RS256Algorithm(_certFactory()) :
base.Create(algorithm);
}
}
}
3 changes: 2 additions & 1 deletion src/JWT/IJwtDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace JWT
{
Expand Down
4 changes: 2 additions & 2 deletions src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
<PropertyGroup>
<Company>Public Domain</Company>
<Copyright>Public Domain</Copyright>
<Description>JWT.NET, a JWT (JSON Web Token) implementation for .NET</Description>
<Description>Jwt.Net, a JWT (JSON Web Token) implementation for .NET</Description>
<RepositoryUrl>https://github.com/jwt-dotnet/jwt</RepositoryUrl>
<PackageProjectUrl>https://github.com/jwt-dotnet/jwt</PackageProjectUrl>
<Authors>John Sheehan, Michael Lehenbauer, Alexander Batishchev</Authors>
<PackageLicenseUrl>https://creativecommons.org/publicdomain/zero/1.0/</PackageLicenseUrl>
<Version>3.0.0-beta1</Version>
<Version>3.0.0-beta2</Version>
<PackageTags>jwt json</PackageTags>
</PropertyGroup>

Expand Down
6 changes: 3 additions & 3 deletions src/JWT/JsonWebToken.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;

using JWT.Algorithms;
using JWT.Serializers;

namespace JWT
Expand All @@ -21,7 +21,7 @@ public static class JsonWebToken

private static readonly IJwtValidator _jwtValidator = new JwtValidator(JsonSerializer, new UtcDateTimeProvider());

private static readonly AlgorithmFactory _algorithmFactory = new AlgorithmFactory();
private static readonly IAlgorithmFactory _hmacshaAlgorithmFactory = new HMACSHAAlgorithmFactory();

private static readonly IBase64UrlEncoder _urlEncoder = new JwtBase64UrlEncoder();

Expand Down Expand Up @@ -73,7 +73,7 @@ public static string Encode(IDictionary<string, object> extraHeaders, object pay
public static string Encode(IDictionary<string, object> extraHeaders, object payload, byte[] key, JwtHashAlgorithm algorithm)
{
return new JwtEncoder(
_algorithmFactory.Create(algorithm),
_hmacshaAlgorithmFactory.Create(algorithm),
JsonSerializer,
_urlEncoder)
.Encode(extraHeaders, payload, key);
Expand Down
22 changes: 20 additions & 2 deletions src/JWT/JwtDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using JWT.Algorithms;

namespace JWT
{
Expand All @@ -9,25 +10,42 @@ namespace JWT
/// </summary>
public sealed class JwtDecoder : IJwtDecoder
{
private static readonly AlgorithmFactory _algFactory = new AlgorithmFactory();
private static readonly IAlgorithmFactory _defaultAlgorithmFactory = new HMACSHAAlgorithmFactory();

private readonly IJsonSerializer _jsonSerializer;
private readonly IJwtValidator _jwtValidator;
private readonly IBase64UrlEncoder _urlEncoder;
private readonly IAlgorithmFactory _algFactory;

/// <summary>
/// Creates an instance of the decoder.
/// Creates an instance of <see cref="JwtDecoder" />.
/// </summary>
/// <param name="jsonSerializer">The Json Serializer.</param>
/// <param name="jwtValidator">The Jwt Validator.</param>
/// <param name="urlEncoder">The Base64 URL Encoder.</param>
public JwtDecoder(IJsonSerializer jsonSerializer, IJwtValidator jwtValidator, IBase64UrlEncoder urlEncoder)
: this(jsonSerializer, jwtValidator, urlEncoder, _defaultAlgorithmFactory)
{
_jsonSerializer = jsonSerializer;
_jwtValidator = jwtValidator;
_urlEncoder = urlEncoder;
}

/// <summary>
/// Creates an instance of <see cref="JwtDecoder" />.
/// </summary>
/// <param name="jsonSerializer">The Json Serializer.</param>
/// <param name="jwtValidator">The Jwt Validator.</param>
/// <param name="urlEncoder">The Base64 URL Encoder.</param>
/// <param name="algFactory">The Algorithm Factory.</param>
public JwtDecoder(IJsonSerializer jsonSerializer, IJwtValidator jwtValidator, IBase64UrlEncoder urlEncoder, IAlgorithmFactory algFactory)
{
_jsonSerializer = jsonSerializer;
_jwtValidator = jwtValidator;
_urlEncoder = urlEncoder;
_algFactory = algFactory;
}

/// <inheritdoc />
public string Decode(string token, string key, bool verify)
{
Expand Down
2 changes: 1 addition & 1 deletion src/JWT/JwtEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class JwtEncoder : IJwtEncoder
private readonly IBase64UrlEncoder _urlEncoder;

/// <summary>
/// Creates an instance of encoder.
/// Creates an instance of <see cref="JwtEncoder" />.
/// </summary>
/// <param name="jsonSerializer">The Json Serializer.</param>
/// <param name="algorithm">The Jwt Algorithm.</param>
Expand Down
6 changes: 5 additions & 1 deletion src/JWT/JwtValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ namespace JWT
/// </summary>
public sealed class JwtValidator : IJwtValidator
{
/// <summary>
/// Describes instants in time, defined as the number of seconds that have elapsed since 00:00:00 UTC, Thursday, 1 January 1970, not counting leap seconds.
/// See https://en.wikipedia.org/wiki/Unix_time />
/// </summary>
public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

private readonly IJsonSerializer _jsonSerializer;
private readonly IDateTimeProvider _dateTimeProvider;

/// <summary>
/// Creates an instance of the Jwt Validator.
/// Creates an instance of <see cref="JwtValidator" />.
/// </summary>
/// <param name="jsonSerializer">The Json Serializer.</param>
/// <param name="dateTimeProvider">The DateTime Provider.</param>
Expand Down

0 comments on commit 8137c59

Please sign in to comment.