-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from thomas-tran/feature/add-jsonwebkey-converter
Feature: Add extension method to convert EdDsaSecurityKey to JsonWebKey
- Loading branch information
Showing
5 changed files
with
60 additions
and
6 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/ScottBrady.IdentityModel/Crypto/ExtendedJsonWebAlgorithmsKeyTypes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/ScottBrady.IdentityModel/Extensions/ExtendedJsonWebKeyConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
test/ScottBrady.IdentityModel.Tests/Tokens/ExtendedJsonWebKeyConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |