From 514de350cca73a0cd8bd454c988868745f75c540 Mon Sep 17 00:00:00 2001 From: Scott Brady Date: Sun, 31 Mar 2024 09:56:22 +0100 Subject: [PATCH] Initial update to Microsoft.IdentityModel v7 --- ...rady.IdentityModel.Samples.AspNetCore.csproj | 2 +- .../ScottBrady.IdentityModel.csproj | 6 +++--- .../Tokens/EdDsaSignatureProvider.cs | 17 +++++++++++++++++ .../ScottBrady.IdentityModel.Tests.csproj | 6 +++--- .../AsymmetricAlgorithmTests.cs | 8 +++----- .../Tokens/JsonWebTokenHandlerTests.cs | 13 +++++++------ 6 files changed, 34 insertions(+), 18 deletions(-) diff --git a/samples/ScottBrady.IdentityModel.Samples.AspNetCore/ScottBrady.IdentityModel.Samples.AspNetCore.csproj b/samples/ScottBrady.IdentityModel.Samples.AspNetCore/ScottBrady.IdentityModel.Samples.AspNetCore.csproj index ef444f9..45f9fd6 100644 --- a/samples/ScottBrady.IdentityModel.Samples.AspNetCore/ScottBrady.IdentityModel.Samples.AspNetCore.csproj +++ b/samples/ScottBrady.IdentityModel.Samples.AspNetCore/ScottBrady.IdentityModel.Samples.AspNetCore.csproj @@ -5,7 +5,7 @@ - + diff --git a/src/ScottBrady.IdentityModel/ScottBrady.IdentityModel.csproj b/src/ScottBrady.IdentityModel/ScottBrady.IdentityModel.csproj index 360c967..e29fd2e 100644 --- a/src/ScottBrady.IdentityModel/ScottBrady.IdentityModel.csproj +++ b/src/ScottBrady.IdentityModel/ScottBrady.IdentityModel.csproj @@ -1,7 +1,7 @@ - net6.0;net8.0 + net8.0 Scott Brady Extensions for Microsoft.IdentityModel, including support for EdDSA signed JWTs. icon.png @@ -18,8 +18,8 @@ - - + + diff --git a/src/ScottBrady.IdentityModel/Tokens/EdDsaSignatureProvider.cs b/src/ScottBrady.IdentityModel/Tokens/EdDsaSignatureProvider.cs index a720e1c..27dc509 100644 --- a/src/ScottBrady.IdentityModel/Tokens/EdDsaSignatureProvider.cs +++ b/src/ScottBrady.IdentityModel/Tokens/EdDsaSignatureProvider.cs @@ -1,3 +1,4 @@ +using System; using Microsoft.IdentityModel.Tokens; namespace ScottBrady.IdentityModel.Tokens; @@ -14,6 +15,22 @@ public EdDsaSignatureProvider(EdDsaSecurityKey key, string algorithm) protected override void Dispose(bool disposing) { } public override byte[] Sign(byte[] input) => edDsaKey.EdDsa.Sign(input); + + public override bool Sign(ReadOnlySpan data, Span destination, out int bytesWritten) + { + var signature = edDsaKey.EdDsa.Sign(data.ToArray()); + signature.CopyTo(destination); + bytesWritten = signature.Length; + return true; + } + + public override byte[] Sign(byte[] input, int offset, int count) + { + var data = new byte[count]; + Buffer.BlockCopy(input, offset, data, 0, count); + return edDsaKey.EdDsa.Sign(data); + } + public override bool Verify(byte[] input, byte[] signature) => edDsaKey.EdDsa.Verify(input, signature); public override bool Verify(byte[] input, int inputOffset, int inputLength, byte[] signature, int signatureOffset, int signatureLength) => edDsaKey.EdDsa.Verify(input, inputOffset, inputLength, signature, signatureOffset, signatureLength); diff --git a/test/ScottBrady.IdentityModel.Tests/ScottBrady.IdentityModel.Tests.csproj b/test/ScottBrady.IdentityModel.Tests/ScottBrady.IdentityModel.Tests.csproj index 0cdfa05..a115d58 100644 --- a/test/ScottBrady.IdentityModel.Tests/ScottBrady.IdentityModel.Tests.csproj +++ b/test/ScottBrady.IdentityModel.Tests/ScottBrady.IdentityModel.Tests.csproj @@ -1,17 +1,17 @@ - net6.0;net8.0 + net8.0 latest - + - + all diff --git a/test/ScottBrady.IdentityModel.Tests/Tokens/EdDSA/AsymmetricAlgorithm/AsymmetricAlgorithmTests.cs b/test/ScottBrady.IdentityModel.Tests/Tokens/EdDSA/AsymmetricAlgorithm/AsymmetricAlgorithmTests.cs index fd1130d..3158f50 100644 --- a/test/ScottBrady.IdentityModel.Tests/Tokens/EdDSA/AsymmetricAlgorithm/AsymmetricAlgorithmTests.cs +++ b/test/ScottBrady.IdentityModel.Tests/Tokens/EdDSA/AsymmetricAlgorithm/AsymmetricAlgorithmTests.cs @@ -113,7 +113,6 @@ public void ImportFromEncryptedPem_WithPasswordBytes_ExpectNotImplementedExcepti public void ImportFromPem_WithPasswordBytes_ExpectNotImplementedException(EdDsa key, int _) => Assert.Throws(() => key.ImportFromPem(Array.Empty())); -#if NET8 [Theory, MemberData(nameof(Keys))] public void ExportPkcs8PrivateKeyPem_ExpectNotImplementedException(EdDsa key, int _) => Assert.Throws(() => key.ExportPkcs8PrivateKeyPem()); @@ -128,7 +127,7 @@ public void ExportEncryptedPkcs8PrivateKeyPem_WithPasswordBytes_ExpectNotImpleme [Theory, MemberData(nameof(Keys))] public void ExportSubjectPublicKeyInfoPem_ExpectNotImplementedException(EdDsa key, int _) - => Assert.Throws(() => key.ExportSubjectPublicKeyInfoPem(); + => Assert.Throws(key.ExportSubjectPublicKeyInfoPem); [Theory, MemberData(nameof(Keys))] public void TryExportSubjectPublicKeyInfoPem_ExpectNotImplementedException(EdDsa key, int _) @@ -140,12 +139,11 @@ public void TryExportPkcs8PrivateKeyPem_ExpectNotImplementedException(EdDsa key, [Theory, MemberData(nameof(Keys))] public void TryExportEncryptedPkcs8PrivateKeyPem_WithPasswordString_ExpectNotImplementedException(EdDsa key, int _) - => Assert.Throws(() => key.TryExportEncryptedPkcs8PrivateKeyPem(Array.Empty(), _fixture.Create()), Array.Empty(), out var _)); + => Assert.Throws(() => key.TryExportEncryptedPkcs8PrivateKeyPem(Array.Empty(), _fixture.Create(), Array.Empty(), out var _)); [Theory, MemberData(nameof(Keys))] public void TryExportEncryptedPkcs8PrivateKeyPem_WithPasswordBytes_ExpectNotImplementedException(EdDsa key, int _) - => Assert.Throws(() => key.TryExportEncryptedPkcs8PrivateKeyPem(Array.Empty(), _fixture.Create()), Array.Empty(), out var _)); -#endif + => Assert.Throws(() => key.TryExportEncryptedPkcs8PrivateKeyPem(Array.Empty(), _fixture.Create(), Array.Empty(), out var _)); [Theory, MemberData(nameof(Keys))] public void Clear_WhenDisposed_ExpectNoException(EdDsa key, int _) diff --git a/test/ScottBrady.IdentityModel.Tests/Tokens/JsonWebTokenHandlerTests.cs b/test/ScottBrady.IdentityModel.Tests/Tokens/JsonWebTokenHandlerTests.cs index 351daa1..e3026d9 100644 --- a/test/ScottBrady.IdentityModel.Tests/Tokens/JsonWebTokenHandlerTests.cs +++ b/test/ScottBrady.IdentityModel.Tests/Tokens/JsonWebTokenHandlerTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.IdentityModel.JsonWebTokens; using Microsoft.IdentityModel.Tokens; @@ -32,7 +33,7 @@ public class JsonWebTokenHandlerTests }; [Fact] - public void WhenEd25519TokenGenerated_ExpectEdDsaTokenVerifiable() + public async Task WhenEd25519TokenGenerated_ExpectEdDsaTokenVerifiable() { var key = EdDsa.Create(ExtendedSecurityAlgorithms.Curves.Ed25519); @@ -42,14 +43,14 @@ public void WhenEd25519TokenGenerated_ExpectEdDsaTokenVerifiable() var jwt = handler.CreateToken(securityTokenDescriptor); - var validationResult = handler.ValidateToken(jwt, tokenValidationParameters); + var validationResult = await handler.ValidateTokenAsync(jwt, tokenValidationParameters); validationResult.IsValid.Should().BeTrue(); validationResult.ClaimsIdentity.Claims.Should().Contain(x => x.Type == "sub" && x.Value == Subject); } [Fact] - public void WhenEd448TokenGenerated_ExpectEdDsaTokenVerifiable() + public async Task WhenEd448TokenGenerated_ExpectEdDsaTokenVerifiable() { var key = EdDsa.Create(ExtendedSecurityAlgorithms.Curves.Ed448); @@ -59,14 +60,14 @@ public void WhenEd448TokenGenerated_ExpectEdDsaTokenVerifiable() var jwt = handler.CreateToken(securityTokenDescriptor); - var validationResult = handler.ValidateToken(jwt, tokenValidationParameters); + var validationResult = await handler.ValidateTokenAsync(jwt, tokenValidationParameters); validationResult.IsValid.Should().BeTrue(); validationResult.ClaimsIdentity.Claims.Should().Contain(x => x.Type == "sub" && x.Value == Subject); } [Fact] - public void WhenEd25519SignatureValidatedUsingEs448_ExpectInvalidToken() + public async Task WhenEd25519SignatureValidatedUsingEs448_ExpectInvalidToken() { var signingKey = EdDsa.Create(ExtendedSecurityAlgorithms.Curves.Ed25519); var validationKey = EdDsa.Create(ExtendedSecurityAlgorithms.Curves.Ed448); @@ -77,7 +78,7 @@ public void WhenEd25519SignatureValidatedUsingEs448_ExpectInvalidToken() var jwt = handler.CreateToken(securityTokenDescriptor); - var validationResult = handler.ValidateToken(jwt, tokenValidationParameters); + var validationResult = await handler.ValidateTokenAsync(jwt, tokenValidationParameters); validationResult.IsValid.Should().BeFalse(); }