Skip to content

Commit

Permalink
Extend RSAlgorithmFactory to support all ctors of RS256Algorithm (#221)
Browse files Browse the repository at this point in the history
* Extend RSAlgorithmFactory to support all ctors of RS256Algorithm
* Update JWT.csproj to bump version
  • Loading branch information
flerka authored and abatishchev committed Oct 1, 2019
1 parent 8b42681 commit 2de2bd0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
32 changes: 20 additions & 12 deletions src/JWT/Algorithms/RSAlgorithmFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,40 @@ namespace JWT.Algorithms
/// <inheritdoc />
public sealed class RSAlgorithmFactory : HMACSHAAlgorithmFactory
{
private readonly Func<X509Certificate2> _certFactory;
private readonly Func<RS256Algorithm> _algFactory;

/// <summary>
/// Initializes a new instance of the <see cref="RSAlgorithmFactory"/> class
/// Creates an instance of the <see cref="RSAlgorithmFactory" /> class using the provided <see cref="X509Certificate2" />.
/// </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;
_algFactory = () => new RS256Algorithm(certFactory());

/// <summary>
/// Creates an instance of <see cref="RSAlgorithmFactory"/> using the provided public key only.
/// </summary>
/// <param name="publicKey">The public key for verifying the data.</param>
public RSAlgorithmFactory(RSA publicKey) =>
_algFactory = () => new RS256Algorithm(publicKey);

/// <summary>
/// Creates an instance of <see cref="RSAlgorithmFactory"/> using the provided pair of public and private keys.
/// </summary>
/// <param name="publicKey">The public key for verifying the data.</param>
/// <param name="privateKey">The private key for signing the data.</param>
public RSAlgorithmFactory(RSA publicKey, RSA privateKey) =>
_algFactory = () => new RS256Algorithm(publicKey, privateKey);

/// <inheritdoc />
public override IJwtAlgorithm Create(JwtHashAlgorithm algorithm)
{
switch (algorithm)
{
case JwtHashAlgorithm.RS256:
{
var certificate = _certFactory();
#if NETSTANDARD1_3
return new RS256Algorithm((RSACryptoServiceProvider)certificate.GetRSAPublicKey(), certificate.GetRSAPrivateKey());
#else
return new RS256Algorithm((RSACryptoServiceProvider)certificate.PublicKey.Key, (RSA)certificate.PrivateKey);
#endif
}
return _algFactory();
default:
throw new NotSupportedException($"For algorithm {Enum.GetName(typeof(JwtHashAlgorithm), algorithm)} please use the appropriate factory by implementing {nameof(IAlgorithmFactory)}");
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<!-- Include PDB in the built .nupkg -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>

<Version>5.3.0-beta1</Version>
<Version>5.3.0-beta2</Version>
<FileVersion>5.0.0.0</FileVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
</PropertyGroup>
Expand Down

0 comments on commit 2de2bd0

Please sign in to comment.