Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add extension method to convert EdDsaSecurityKey to JsonWebKey #13

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ScottBrady.IdentityModel.Crypto
{
public static class ExtendedJsonWebAlgorithmsKeyTypes
{
// https://datatracker.ietf.org/doc/html/draft-ietf-jose-cfrg-curves-06#section-2
public const string ECDH = "OKP";
scottbrady91 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.IdentityModel.Tokens;
using ScottBrady.IdentityModel.Crypto;
using ScottBrady.IdentityModel.Tokens;

namespace ScottBrady.IdentityModel.Extensions
{
public static class ExtendedJsonWebKeyConverter
{
public static JsonWebKey ConvertFromEdDsaSecurityKey(EdDsaSecurityKey securityKey)
{
var parameters = securityKey.EdDsa.Parameters;
return new JsonWebKey
{
Crv = parameters.Curve,
X = parameters.X != null ? Base64UrlEncoder.Encode(parameters.X) : null,
D = parameters.D != null ? Base64UrlEncoder.Encode(parameters.D) : null,
Kty = ExtendedJsonWebAlgorithmsKeyTypes.ECDH,
Alg = ExtendedSecurityAlgorithms.EdDsa,
CryptoProviderFactory = securityKey.CryptoProviderFactory,
};
}
}
}
6 changes: 3 additions & 3 deletions src/ScottBrady.IdentityModel/Tokens/EdDsa.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Security.Cryptography;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
Expand All @@ -9,7 +10,7 @@

namespace ScottBrady.IdentityModel.Tokens;

public class EdDsa
public class EdDsa: AsymmetricAlgorithm
scottbrady91 marked this conversation as resolved.
Show resolved Hide resolved
{
internal EdDsaParameters Parameters { get; private init; }

Expand All @@ -27,7 +28,7 @@
/// Create new key for EdDSA.
/// </summary>
/// <param name="curve">Create key for curve Ed25519 or Ed448.</param>
public static EdDsa Create(string curve)

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (macos-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (macos-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (macos-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (macos-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (macos-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (macos-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.

Check warning on line 31 in src/ScottBrady.IdentityModel/Tokens/EdDsa.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest)

'EdDsa.Create(string)' hides inherited member 'AsymmetricAlgorithm.Create(string)'. Use the new keyword if hiding was intended.
{
if (string.IsNullOrWhiteSpace(curve)) throw new ArgumentNullException(nameof(curve));

Expand All @@ -36,7 +37,6 @@
var generator = new Ed25519KeyPairGenerator();
generator.Init(new Ed25519KeyGenerationParameters(new SecureRandom()));
var keyPair = generator.GenerateKeyPair();

return new EdDsa {Parameters = new EdDsaParameters(keyPair, curve)};
}

Expand All @@ -60,7 +60,7 @@
{
throw new NotImplementedException();
}

public byte[] Sign(byte[] input)
{
if (input == null) throw new ArgumentNullException(nameof(input));
Expand Down
6 changes: 3 additions & 3 deletions src/ScottBrady.IdentityModel/Tokens/EdDsaSecurityKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public EdDsaSecurityKey(Ed25519PublicKeyParameters keyParameters) : this()
if (keyParameters == null) throw new ArgumentNullException(nameof(keyParameters));
EdDsa = EdDsa.Create(new EdDsaParameters(ExtendedSecurityAlgorithms.Curves.Ed25519) {X = keyParameters.GetEncoded()});
}
public override int KeySize => throw new NotImplementedException();

public override int KeySize => EdDsa.KeySize;

[Obsolete("HasPrivateKey method is deprecated, please use PrivateKeyStatus.")]
public override bool HasPrivateKey => EdDsa.Parameters.D != null;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using ScottBrady.IdentityModel.Crypto;
using ScottBrady.IdentityModel.Extensions;
using ScottBrady.IdentityModel.Tokens;
using Xunit;

namespace ScottBrady.IdentityModel.Tests.Tokens
{
public class ExtendedJsonWebKeyConverterTests
{
[Fact]
public void JsonWebKeyConverter_ConvertFromEdDsaSecurityKey()
{
var originKey = new EdDsaSecurityKey(EdDsa.Create(ExtendedSecurityAlgorithms.Curves.Ed25519));
var jwk = ExtendedJsonWebKeyConverter.ConvertFromEdDsaSecurityKey(originKey);
Assert.NotNull(jwk);
Assert.Equal(ExtendedSecurityAlgorithms.Curves.Ed25519, jwk.Crv);
Assert.Equal(ExtendedJsonWebAlgorithmsKeyTypes.ECDH, jwk.Kty);
Assert.Equal(ExtendedSecurityAlgorithms.EdDsa, jwk.Alg);
Assert.NotNull(jwk.D);
Assert.NotNull(jwk.X);
}
}
}