From fdb2d2a982dbe0726fc0dda7c4b0e4c7f0b7b481 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Thu, 18 Jul 2024 15:56:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SKIT.FlurlHttpClient.Wechat.Api.csproj | 2 +- .../Utilities/AESUtility.cs | 8 ++++++++ .../WechatTenpayClientSigningExtensions.cs | 4 ++-- .../SKIT.FlurlHttpClient.Wechat.TenpayV3.csproj | 2 +- .../Settings/CertificateEntry.cs | 12 ++++++++---- .../Utilities/AESUtility.cs | 6 +++++- .../Finance/InteropServices/MarshalerHelper.cs | 8 ++++---- .../ExtendedSDK/Finance/WechatWorkFinanceClient.cs | 4 ++-- .../ExtendedSDK/Finance/WechatWorkFinanceResponse.cs | 10 +++++----- .../SKIT.FlurlHttpClient.Wechat.Api.UnitTests.csproj | 2 +- ....FlurlHttpClient.Wechat.TenpayV3.UnitTests.csproj | 2 +- 11 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/SKIT.FlurlHttpClient.Wechat.Api.csproj b/src/SKIT.FlurlHttpClient.Wechat.Api/SKIT.FlurlHttpClient.Wechat.Api.csproj index 503029e9..ab4a1d7e 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/SKIT.FlurlHttpClient.Wechat.Api.csproj +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/SKIT.FlurlHttpClient.Wechat.Api.csproj @@ -1,7 +1,7 @@ - net462; netstandard2.0; net6.0 + net462; netstandard2.0; net6.0; net8.0 10.0 enable true diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/AESUtility.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/AESUtility.cs index 941d92c1..352ec144 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/AESUtility.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Utilities/AESUtility.cs @@ -93,7 +93,11 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities if (!string.Equals(paddingMode, PADDING_MODE_NOPADDING, StringComparison.OrdinalIgnoreCase)) throw new NotSupportedException(); +#if NET8_0_OR_GREATER + using (AesGcm aes = new AesGcm(keyBytes, TAG_LENGTH_BYTE)) +#else using (AesGcm aes = new AesGcm(keyBytes)) +#endif { byte[] cipherWithoutTagBytes = new byte[cipherBytes.Length - TAG_LENGTH_BYTE]; byte[] tagBytes = new byte[TAG_LENGTH_BYTE]; @@ -216,7 +220,11 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities if (!string.Equals(paddingMode, PADDING_MODE_NOPADDING, StringComparison.OrdinalIgnoreCase)) throw new NotSupportedException(); +#if NET8_0_OR_GREATER + using (AesGcm aes = new AesGcm(keyBytes, TAG_LENGTH_BYTE)) +#else using (AesGcm aes = new AesGcm(keyBytes)) +#endif { byte[] cipherBytes = new byte[plainBytes.Length]; byte[] tagBytes = new byte[TAG_LENGTH_BYTE]; diff --git a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Extensions/__Internal/WechatTenpayClientSigningExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Extensions/__Internal/WechatTenpayClientSigningExtensions.cs index 3b0d09d5..284fab0f 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Extensions/__Internal/WechatTenpayClientSigningExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Extensions/__Internal/WechatTenpayClientSigningExtensions.cs @@ -17,7 +17,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3 CertificateEntry? entry = client.PlatformCertificateManager.GetEntry(strSerialNumber); if (!entry.HasValue) { - return ErroredResult.Fail(new Exception($"The platform certificate manager does not contain a certificate with serial number \"{strSerialNumber}\".")); + return ErroredResult.Fail(new Exception($"The platform certificate manager does not contain a certificate matched the serial number \"{strSerialNumber}\".")); } return GenerateSignatureResultByCertificate( @@ -41,7 +41,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3 CertificateEntry? entry = await ((ICertificateManagerAsync)client.PlatformCertificateManager).GetEntryAsync(strSerialNumber, cancellationToken).ConfigureAwait(false); if (!entry.HasValue) { - return ErroredResult.Fail(new Exception($"The platform certificate manager does not contain a certificate with serial number \"{strSerialNumber}\".")); + return ErroredResult.Fail(new Exception($"The platform certificate manager does not contain a certificate matched the serial number \"{strSerialNumber}\".")); } return GenerateSignatureResultByCertificate( diff --git a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/SKIT.FlurlHttpClient.Wechat.TenpayV3.csproj b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/SKIT.FlurlHttpClient.Wechat.TenpayV3.csproj index ce237dd9..a2bb0773 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/SKIT.FlurlHttpClient.Wechat.TenpayV3.csproj +++ b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/SKIT.FlurlHttpClient.Wechat.TenpayV3.csproj @@ -1,7 +1,7 @@ - net462; netstandard2.0; net6.0 + net462; netstandard2.0; net6.0; net8.0 10.0 enable true diff --git a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Settings/CertificateEntry.cs b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Settings/CertificateEntry.cs index 8f98e8ea..efcae55f 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Settings/CertificateEntry.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Settings/CertificateEntry.cs @@ -55,6 +55,8 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings [System.Text.Json.Serialization.JsonConstructor] public CertificateEntry(string algorithmType, string serialNumber, string certificate, DateTimeOffset effectiveTime, DateTimeOffset expireTime) { + certificate = certificate?.Trim()!; + if (string.IsNullOrEmpty(algorithmType)) throw new ArgumentException("The value of `algorithmType` can not be empty.", nameof(algorithmType)); if (string.IsNullOrEmpty(certificate)) @@ -63,8 +65,8 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings throw new ArgumentException("The value of `serialNumber` can not be empty.", nameof(serialNumber)); if (!ALGORITHM_TYPE_RSA.Equals(algorithmType) && !ALGORITHM_TYPE_SM2.Equals(algorithmType)) throw new ArgumentException("The value of `algorithmType` an invalid value.", nameof(algorithmType)); - if (!certificate.Trim().StartsWith("-----BEGIN CERTIFICATE-----") || !certificate.Trim().EndsWith("-----END CERTIFICATE-----")) - throw new ArgumentException("The value of `certificate` is an invalid certificate file content.", nameof(certificate)); + if (!(certificate.StartsWith("-----BEGIN CERTIFICATE-----") && certificate.EndsWith("-----END CERTIFICATE-----"))) + throw new ArgumentException("The value of `certificate` is an invalid certificate file content. Maybe you forgot to decrypt?", nameof(certificate)); AlgorithmType = algorithmType; SerialNumber = serialNumber.ToUpper(); @@ -80,12 +82,14 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings /// public CertificateEntry(string algorithmType, string certificate) { + certificate = certificate?.Trim()!; + if (string.IsNullOrEmpty(algorithmType)) throw new ArgumentException("The value of `algorithmType` can not be empty.", nameof(algorithmType)); if (string.IsNullOrEmpty(certificate)) throw new ArgumentException("The value of `certificate` can not be empty.", nameof(certificate)); - if (!certificate.Trim().StartsWith("-----BEGIN CERTIFICATE-----") || !certificate.Trim().EndsWith("-----END CERTIFICATE-----")) - throw new ArgumentException("The value of `certificate` is an invalid certificate file content.", nameof(certificate)); + if (!(certificate.StartsWith("-----BEGIN CERTIFICATE-----") && certificate.EndsWith("-----END CERTIFICATE-----"))) + throw new ArgumentException("The value of `certificate` is an invalid certificate file content. Maybe you forgot to decrypt?", nameof(certificate)); AlgorithmType = algorithmType; Certificate = certificate; diff --git a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Utilities/AESUtility.cs b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Utilities/AESUtility.cs index f5f5d443..f0bee13a 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Utilities/AESUtility.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Utilities/AESUtility.cs @@ -45,8 +45,12 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities { if (!string.Equals(paddingMode, PADDING_MODE_NOPADDING, StringComparison.OrdinalIgnoreCase)) throw new NotSupportedException(); - + +#if NET8_0_OR_GREATER + using (AesGcm aes = new AesGcm(keyBytes, TAG_LENGTH_BYTE)) +#else using (AesGcm aes = new AesGcm(keyBytes)) +#endif { byte[] cipherWithoutTagBytes = new byte[cipherBytes.Length - TAG_LENGTH_BYTE]; byte[] tagBytes = new byte[TAG_LENGTH_BYTE]; diff --git a/src/SKIT.FlurlHttpClient.Wechat.Work/ExtendedSDK/Finance/InteropServices/MarshalerHelper.cs b/src/SKIT.FlurlHttpClient.Wechat.Work/ExtendedSDK/Finance/InteropServices/MarshalerHelper.cs index 7d235472..c45c994c 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Work/ExtendedSDK/Finance/InteropServices/MarshalerHelper.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Work/ExtendedSDK/Finance/InteropServices/MarshalerHelper.cs @@ -6,18 +6,18 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance.InteropServices { internal static class MarshalerHelper { - public static string PtrToStringAnsi(IntPtr ptr) + public static string? PtrToStringAnsi(IntPtr ptr) { if (ptr == IntPtr.Zero) - return default!; + return null; return Marshal.PtrToStringAnsi(ptr)!; } - public static string PtrToStringUTF8(IntPtr ptr) + public static string? PtrToStringUTF8(IntPtr ptr) { if (ptr == IntPtr.Zero) - return default!; + return null; #if NETCOREAPP || NET5_0_OR_GREATER return Marshal.PtrToStringUTF8(ptr)!; diff --git a/src/SKIT.FlurlHttpClient.Wechat.Work/ExtendedSDK/Finance/WechatWorkFinanceClient.cs b/src/SKIT.FlurlHttpClient.Wechat.Work/ExtendedSDK/Finance/WechatWorkFinanceClient.cs index d1dac7b4..ba41aa36 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Work/ExtendedSDK/Finance/WechatWorkFinanceClient.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Work/ExtendedSDK/Finance/WechatWorkFinanceClient.cs @@ -159,7 +159,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance IsRunOnWindows() ? FinanceDllWindowsPInvoker.GetContentFromSlice(dataPtr) : IsRunOnLinux() ? FinanceDllLinuxPInvoker.GetContentFromSlice(dataPtr) : throw new PlatformNotSupportedException(); - string dataContent = MarshalerHelper.PtrToStringUTF8(dataContentPtr); + string dataContent = MarshalerHelper.PtrToStringUTF8(dataContentPtr)!; response = JsonSerializer.Deserialize(dataContent); response._InternalRawBytes = Encoding.UTF8.GetBytes(dataContent); @@ -253,7 +253,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance IsRunOnWindows() ? FinanceDllWindowsPInvoker.GetContentFromSlice(dataPtr) : IsRunOnLinux() ? FinanceDllLinuxPInvoker.GetContentFromSlice(dataPtr) : throw new PlatformNotSupportedException(); - string dataContent = MarshalerHelper.PtrToStringUTF8(dataContentPtr); + string dataContent = MarshalerHelper.PtrToStringUTF8(dataContentPtr)!; try { diff --git a/src/SKIT.FlurlHttpClient.Wechat.Work/ExtendedSDK/Finance/WechatWorkFinanceResponse.cs b/src/SKIT.FlurlHttpClient.Wechat.Work/ExtendedSDK/Finance/WechatWorkFinanceResponse.cs index 3f77c792..5e373dcb 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Work/ExtendedSDK/Finance/WechatWorkFinanceResponse.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Work/ExtendedSDK/Finance/WechatWorkFinanceResponse.cs @@ -7,11 +7,6 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance /// public abstract class WechatWorkFinanceResponse : ICommonResponse { - internal protected WechatWorkFinanceResponse() - { - _InternalRawBytes = Array.Empty(); - } - [Newtonsoft.Json.JsonIgnore] [System.Text.Json.Serialization.JsonIgnore] internal byte[] _InternalRawBytes; @@ -34,6 +29,11 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance return _InternalRawBytes; } + internal protected WechatWorkFinanceResponse() + { + _InternalRawBytes = Array.Empty(); + } + /// /// 获取企业微信会话内容存档 API 返回的返回值。 /// diff --git a/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/SKIT.FlurlHttpClient.Wechat.Api.UnitTests.csproj b/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/SKIT.FlurlHttpClient.Wechat.Api.UnitTests.csproj index e38d62ec..028b08f6 100644 --- a/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/SKIT.FlurlHttpClient.Wechat.Api.UnitTests.csproj +++ b/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/SKIT.FlurlHttpClient.Wechat.Api.UnitTests.csproj @@ -1,7 +1,7 @@ - net472; net6.0 + net472; net6.0; net8.0 latest enable true diff --git a/test/SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests/SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests.csproj b/test/SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests/SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests.csproj index 0e4624c9..a548b603 100644 --- a/test/SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests/SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests.csproj +++ b/test/SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests/SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests.csproj @@ -1,7 +1,7 @@ - net472; net6.0 + net472; net6.0; net8.0 latest enable true