-
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.
Expanded and tested ExtendedJsonWebKeyConverter, cleaned up formattin…
…g, and removed unsupported EdDSA factory method.
- Loading branch information
1 parent
960e5c5
commit f38a273
Showing
12 changed files
with
316 additions
and
136 deletions.
There are no files selected for viewing
10 changes: 4 additions & 6 deletions
10
samples/ScottBrady.IdentityModel.Samples.AspNetCore/SampleOptions.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 |
---|---|---|
@@ -1,14 +1,12 @@ | ||
using System; | ||
using ScottBrady.IdentityModel.Crypto; | ||
using ScottBrady.IdentityModel.Tokens; | ||
|
||
namespace ScottBrady.IdentityModel.Samples.AspNetCore; | ||
|
||
public class SampleOptions | ||
{ | ||
public readonly EdDsaSecurityKey EdDsaPublicKey = new EdDsaSecurityKey( | ||
EdDsa.Create(new EdDsaParameters(ExtendedSecurityAlgorithms.Curves.Ed25519) {X =Convert.FromBase64String("doaS7QILHBdnPULlgs1fX0MWpd1wak14r1yT6ae/b4M=")})); | ||
|
||
public readonly EdDsaSecurityKey EdDsaPrivateKey= new EdDsaSecurityKey( | ||
EdDsa.Create(new EdDsaParameters(ExtendedSecurityAlgorithms.Curves.Ed25519) {D =Convert.FromBase64String("TYXei5+8Qd2ZqKIlEuJJ3S50WYuocFTrqK+3/gHVH9B2hpLtAgscF2c9QuWCzV9fQxal3XBqTXivXJPpp79vgw==")})); | ||
private static readonly EdDsa _key = EdDsa.Create(ExtendedSecurityAlgorithms.Curves.Ed25519); | ||
|
||
public readonly EdDsaSecurityKey EdDsaPublicKey = new EdDsaSecurityKey(_key); | ||
public readonly EdDsaSecurityKey EdDsaPrivateKey= new EdDsaSecurityKey(_key); | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
62 changes: 48 additions & 14 deletions
62
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 |
---|---|---|
@@ -1,23 +1,57 @@ | ||
using Microsoft.IdentityModel.Tokens; | ||
using System; | ||
using Microsoft.IdentityModel.Logging; | ||
using Microsoft.IdentityModel.Tokens; | ||
using ScottBrady.IdentityModel.Crypto; | ||
using ScottBrady.IdentityModel.Tokens; | ||
|
||
namespace ScottBrady.IdentityModel.Extensions | ||
namespace ScottBrady.IdentityModel; | ||
|
||
public static class ExtendedJsonWebKeyConverter | ||
{ | ||
public static class ExtendedJsonWebKeyConverter | ||
public static JsonWebKey ConvertFromEdDsaSecurityKey(EdDsaSecurityKey key) | ||
{ | ||
|
||
var parameters = key.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 = ExtendedSecurityAlgorithms.KeyTypes.Ecdh, | ||
Alg = ExtendedSecurityAlgorithms.EdDsa, | ||
CryptoProviderFactory = key.CryptoProviderFactory, | ||
}; | ||
} | ||
|
||
public static bool TryConvertToEdDsaSecurityKey(JsonWebKey webKey, out EdDsaSecurityKey key) | ||
{ | ||
public static JsonWebKey ConvertFromEdDsaSecurityKey(EdDsaSecurityKey securityKey) | ||
key = null; | ||
|
||
if (webKey != null | ||
&& webKey.Kty == ExtendedSecurityAlgorithms.KeyTypes.Ecdh | ||
&& webKey.Alg == ExtendedSecurityAlgorithms.EdDsa) | ||
{ | ||
var parameters = securityKey.EdDsa.Parameters; | ||
return new JsonWebKey | ||
if (webKey.Crv == ExtendedSecurityAlgorithms.Curves.Ed25519 | ||
|| webKey.Crv == ExtendedSecurityAlgorithms.Curves.Ed448) | ||
{ | ||
Crv = parameters.Curve, | ||
X = parameters.X != null ? Base64UrlEncoder.Encode(parameters.X) : null, | ||
D = parameters.D != null ? Base64UrlEncoder.Encode(parameters.D) : null, | ||
Kty = ExtendedSecurityAlgorithms.KeyTypes.Ecdh, | ||
Alg = ExtendedSecurityAlgorithms.EdDsa, | ||
CryptoProviderFactory = securityKey.CryptoProviderFactory, | ||
}; | ||
try | ||
{ | ||
key = new EdDsaSecurityKey(EdDsa.Create(new EdDsaParameters(webKey.Crv) | ||
{ | ||
X = webKey.X != null ? Base64UrlEncoder.DecodeBytes(webKey.X) : null, | ||
D = webKey.D != null ? Base64UrlEncoder.DecodeBytes(webKey.D) : null | ||
})); | ||
|
||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
LogHelper.LogWarning(LogHelper.FormatInvariant("Unable to create an EdDsaSecurityKey from the properties found in the JsonWebKey: '{0}', Exception '{1}'.", webKey, ex)); | ||
} | ||
|
||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.