From c9bf80d0fb40decc307d95497bf5f9ca0690b59d Mon Sep 17 00:00:00 2001 From: Li Date: Mon, 9 Dec 2024 14:49:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(wxapi):=20=E6=9A=B4=E9=9C=B2=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=B9=B6=E8=A7=A3=E5=AF=86=E5=9B=9E=E8=B0=83=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E4=BA=8B=E4=BB=B6=E7=9A=84=E6=89=A9=E5=B1=95=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WechatApiClientEventExtensions.cs | 85 ++++++++++++++++--- 1 file changed, 75 insertions(+), 10 deletions(-) diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs index 5f42e049..0512fef7 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs @@ -12,6 +12,28 @@ namespace SKIT.FlurlHttpClient.Wechat.Api /// public static partial class WechatApiClientEventExtensions { + /// + /// 从 JSON 解析并解密得到明文回调通知事件内容。 + /// + /// + /// + /// + public static string DecryptEventFromJson(this WechatApiClient client, string webhookJson) + { + return InnerDecryptEventFromJson(client, webhookJson); + } + + /// + /// 从 XML 解析并解密明文回调通知事件内容。 + /// + /// + /// + /// + public static string DecryptEventFromXml(this WechatApiClient client, string webhookXml) + { + return InnerDecryptEventFromXml(client, webhookXml); + } + /// /// 从 JSON 反序列化得到 对象。 /// @@ -331,16 +353,9 @@ namespace SKIT.FlurlHttpClient.Wechat.Api try { - if (webhookJson.Contains("\"Encrypt\"")) - { - if (string.IsNullOrEmpty(client.Credentials.PushEncodingAESKey)) - throw new WechatApiException("Failed to decrypt event data, because the push no encoding AES key is not set."); + var webhookJsonString = InnerDecryptEventFromJson(client, webhookJson); - InnerEncryptedEvent encryptedEvent = client.JsonSerializer.Deserialize(webhookJson); - webhookJson = Utilities.WxMsgCryptor.AESDecrypt(cipherText: encryptedEvent.EncryptedData, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _); - } - - return client.JsonSerializer.Deserialize(webhookJson); + return client.JsonSerializer.Deserialize(webhookJsonString); } catch (WechatApiException) { @@ -354,6 +369,56 @@ namespace SKIT.FlurlHttpClient.Wechat.Api private static TEvent InnerDeserializeEventFromXml(WechatApiClient client, string webhookXml) where TEvent : WechatApiEvent + { + + try + { + var webhookXmlString = InnerDecryptEventFromXml(client, webhookXml); + return (TEvent)_XmlSimpleSerializer.Deserialize(webhookXmlString, typeof(TEvent)); + } + catch (ArgumentNullException) + { + throw; + } + catch (WechatApiException) + { + throw; + } + catch (Exception ex) + { + throw new WechatApiException("Failed to deserialize event data. Please see the inner exception for more details.", ex); + } + } + + private static string InnerDecryptEventFromJson(WechatApiClient client, string webhookJson) + { + if (client is null) throw new ArgumentNullException(nameof(client)); + if (webhookJson is null) throw new ArgumentNullException(webhookJson); + + try + { + if (webhookJson.Contains("\"Encrypt\"")) + { + if (string.IsNullOrEmpty(client.Credentials.PushEncodingAESKey)) + throw new WechatApiException("Failed to decrypt event data, because the push no encoding AES key is not set."); + + InnerEncryptedEvent encryptedEvent = client.JsonSerializer.Deserialize(webhookJson); + webhookJson = Utilities.WxMsgCryptor.AESDecrypt(cipherText: encryptedEvent.EncryptedData, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _); + } + + return webhookJson; + } + catch (WechatApiException) + { + throw; + } + catch (Exception ex) + { + throw new WechatApiException("Failed to deserialize event data. Please see the inner exception for more details.", ex); + } + } + + private static string InnerDecryptEventFromXml(WechatApiClient client, string webhookXml) { if (client is null) throw new ArgumentNullException(nameof(client)); if (webhookXml is null) throw new ArgumentNullException(webhookXml); @@ -368,7 +433,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api webhookXml = Utilities.WxMsgCryptor.AESDecrypt(cipherText: encryptedXml, encodingAESKey: client.Credentials.PushEncodingAESKey!, out _); } - return (TEvent)_XmlSimpleSerializer.Deserialize(webhookXml, typeof(TEvent)); + return webhookXml; } catch (WechatApiException) {