Skip to content

Commit

Permalink
Added RFC test vectors for JWK parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbrady91 committed Mar 31, 2024
1 parent ef02980 commit 2936014
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public static bool TryConvertToEdDsaSecurityKey(JsonWebKey webKey, out EdDsaSecu
{
key = null;

if (webKey != null
&& webKey.Kty == ExtendedSecurityAlgorithms.KeyTypes.Ecdh
&& webKey.Alg == ExtendedSecurityAlgorithms.EdDsa)
if (webKey != null && webKey.Kty == ExtendedSecurityAlgorithms.KeyTypes.Ecdh)
{
if (webKey.Crv == ExtendedSecurityAlgorithms.Curves.Ed25519
|| webKey.Crv == ExtendedSecurityAlgorithms.Curves.Ed448)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using ScottBrady.IdentityModel.Tokens;
using Xunit;

namespace ScottBrady.IdentityModel.Tests.Tokens;
namespace ScottBrady.IdentityModel.Tests.Extensions;

public class ExtendedJsonWebKeyConverterTests
{
Expand Down Expand Up @@ -157,7 +157,7 @@ public void TryConvertToEdDsaSecurityKey_WhenJwkIsEd448PublicKey_ReturnsTrueWith
[Fact]
public void ConvertFromEdDsaSecurityKey_TryConvertToEdDsaSecurityKey_ExpectConvertableKey()
{
const string jwk = "{\n \"kty\": \"OKP\",\n \"alg\": \"EdDSA\",\n \"crv\": \"Ed25519\",\n \"x\": \"60mR98SQlHUSeLeIu7TeJBTLRG10qlcDLU4AJjQdqMQ\"\n}";
const string jwk = "{\"kty\":\"OKP\",\"crv\": \"Ed25519\",\"alg\":\"EdDSA\",\"x\":\"60mR98SQlHUSeLeIu7TeJBTLRG10qlcDLU4AJjQdqMQ\"}";
var jsonWebKey = new JsonWebKey(jwk);

ExtendedJsonWebKeyConverter.TryConvertToEdDsaSecurityKey(jsonWebKey, out var edDsaKey).Should().BeTrue();
Expand All @@ -166,6 +166,19 @@ public void ConvertFromEdDsaSecurityKey_TryConvertToEdDsaSecurityKey_ExpectConve
jsonWebKey.Should().BeEquivalentTo(convertedJsonWebKey);
}

[Fact]
public void ConvertFromEdDsaSecurityKey_WithRfc8037Jwk_ExpectConvertableKey()
{
const string jwk = "{\"kty\":\"OKP\",\"crv\":\"Ed25519\",\"d\":\"nWGxne_9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A\",\"x\":\"11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo\"}";
var jsonWebKey = new JsonWebKey(jwk);

ExtendedJsonWebKeyConverter.TryConvertToEdDsaSecurityKey(jsonWebKey, out var edDsaKey).Should().BeTrue();
var convertedJsonWebKey = ExtendedJsonWebKeyConverter.ConvertFromEdDsaSecurityKey(edDsaKey);

// RFC8037 test vectors do not include the alg parameter
jsonWebKey.Should().BeEquivalentTo(convertedJsonWebKey, options => options.Excluding(x => x.Alg));
}

private static void TestAndAssertFailure(JsonWebKey jwk)
{
var isSuccess = ExtendedJsonWebKeyConverter.TryConvertToEdDsaSecurityKey(jwk, out var key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace ScottBrady.IdentityModel.Tests.Tokens.EdDSA.AsymmetricAlgorithm;

public class EdDsaBaseClassTests : EdDsaTestBase
public class AsymmetricAlgorithmTests : EdDsaTestBase
{
public static TheoryData<EdDsa, int> Keys
=> new TheoryData<EdDsa, int> { { _ed25519Key, 32 }, { _ed448Key, 57 } };
Expand Down Expand Up @@ -154,6 +154,4 @@ public void Clear_WhenDisposed_ExpectNoException(EdDsa key, int _)
[Theory, MemberData(nameof(Keys))]
public void Dispose_WhenDisposed_ExpectNoException(EdDsa key, int _)
=> key.Dispose();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ScottBrady.IdentityModel.Tests.Tokens;

public class JwtBearerHandlerTests
{

}

0 comments on commit 2936014

Please sign in to comment.