From 07429761cb3bd711b333f23e11f7e5fcc7b4d019 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Mon, 21 Nov 2022 13:51:33 +0800 Subject: [PATCH] =?UTF-8?q?feat(openai):=20=E7=A7=BB=E9=99=A4=20JWT=20?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SKIT.FlurlHttpClient.Wechat.OpenAI.csproj | 1 - .../Utilities/Internal/JwtUtility.cs | 27 -------------- .../WechatOpenAIClient.cs | 37 ------------------- .../WechatOpenAIRequest.cs | 4 -- .../TestCase_JwtUtilityTests.cs | 26 ------------- 5 files changed, 95 deletions(-) delete mode 100644 src/SKIT.FlurlHttpClient.Wechat.OpenAI/Utilities/Internal/JwtUtility.cs delete mode 100644 test/SKIT.FlurlHttpClient.Wechat.OpenAI.UnitTests/TestCase_JwtUtilityTests.cs diff --git a/src/SKIT.FlurlHttpClient.Wechat.OpenAI/SKIT.FlurlHttpClient.Wechat.OpenAI.csproj b/src/SKIT.FlurlHttpClient.Wechat.OpenAI/SKIT.FlurlHttpClient.Wechat.OpenAI.csproj index f2871f29..302f6a76 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.OpenAI/SKIT.FlurlHttpClient.Wechat.OpenAI.csproj +++ b/src/SKIT.FlurlHttpClient.Wechat.OpenAI/SKIT.FlurlHttpClient.Wechat.OpenAI.csproj @@ -40,7 +40,6 @@ - diff --git a/src/SKIT.FlurlHttpClient.Wechat.OpenAI/Utilities/Internal/JwtUtility.cs b/src/SKIT.FlurlHttpClient.Wechat.OpenAI/Utilities/Internal/JwtUtility.cs deleted file mode 100644 index 801eea45..00000000 --- a/src/SKIT.FlurlHttpClient.Wechat.OpenAI/Utilities/Internal/JwtUtility.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using JWT; -using JWT.Algorithms; -using JWT.Serializers; -using Newtonsoft.Json; - -namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Utilities -{ - internal static class JwtUtility - { - private static readonly Lazy _encoder = new Lazy(() => - { -#pragma warning disable CS0618 - IJwtAlgorithm algorithm = new HMACSHA256Algorithm(); -#pragma warning restore CS0618 - IJsonSerializer serializer = new JsonNetSerializer(new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore }); - IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); - IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder); - return encoder; - }, isThreadSafe: true); - - public static string EncodeWithHS256(object payload, string secret) - { - return _encoder.Value.Encode(payload, secret); - } - } -} diff --git a/src/SKIT.FlurlHttpClient.Wechat.OpenAI/WechatOpenAIClient.cs b/src/SKIT.FlurlHttpClient.Wechat.OpenAI/WechatOpenAIClient.cs index 6b064fe5..ddedf0d7 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.OpenAI/WechatOpenAIClient.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.OpenAI/WechatOpenAIClient.cs @@ -125,42 +125,5 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI throw new WechatOpenAIException(ex.Message, ex); } } - - /// - /// 异步发起请求。 - /// - /// - /// - /// - /// - /// - public async Task SendRequestWithUrlEncodedAsync(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default) - where T : WechatOpenAIResponse, new() - { - if (flurlRequest == null) throw new ArgumentNullException(nameof(flurlRequest)); - - try - { - if (data is WechatOpenAIRequest.Serialization.IEncryptedUrlEncoded) - { - string jwt = Utilities.JwtUtility.EncodeWithHS256(payload: data, secret: Credentials.EncodingAESKey!); - data = new { query = jwt }; - } - - using IFlurlResponse flurlResponse = await flurlRequest - .WithClient(FlurlClient) - .AllowAnyHttpStatus() - .SendUrlEncodedAsync(flurlRequest.Verb, data, cancellationToken); - return await WrapResponseWithJsonAsync(flurlResponse, cancellationToken); - } - catch (FlurlHttpTimeoutException ex) - { - throw new Exceptions.WechatOpenAIRequestTimeoutException(ex.Message, ex); - } - catch (FlurlHttpException ex) - { - throw new WechatOpenAIException(ex.Message, ex); - } - } } } diff --git a/src/SKIT.FlurlHttpClient.Wechat.OpenAI/WechatOpenAIRequest.cs b/src/SKIT.FlurlHttpClient.Wechat.OpenAI/WechatOpenAIRequest.cs index d8db83b2..ed3342cb 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.OpenAI/WechatOpenAIRequest.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.OpenAI/WechatOpenAIRequest.cs @@ -10,10 +10,6 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI public interface IEncryptedXmlable { } - - public interface IEncryptedUrlEncoded - { - } } /// diff --git a/test/SKIT.FlurlHttpClient.Wechat.OpenAI.UnitTests/TestCase_JwtUtilityTests.cs b/test/SKIT.FlurlHttpClient.Wechat.OpenAI.UnitTests/TestCase_JwtUtilityTests.cs deleted file mode 100644 index 84bb29d7..00000000 --- a/test/SKIT.FlurlHttpClient.Wechat.OpenAI.UnitTests/TestCase_JwtUtilityTests.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Xunit; - -namespace SKIT.FlurlHttpClient.Wechat.OpenAI.UnitTests -{ - public class TestCase_JwtUtilityTests - { - [Fact(DisplayName = "测试用例:JWT HS256 编码")] - public void TestJwtEncodeWithHS256() - { - object payload = new - { - uid = "xjlsj33lasfaf", - data = new - { - q = "在微信智言与微信智聆两大技术的支持下,微信AI团队推出了“微信对话开放平台”和“腾讯小微”智能硬件两大核心产品。微信支付团队最新发布的“微信青蛙Pro”在现场设置了体验区,让大家感受AI认脸的本事。" - } - }; - string secret = "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C"; - - string actualJwt = Utilities.JwtUtility.EncodeWithHS256(payload: payload, secret: secret); - string expectedJwt = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiJ4amxzajMzbGFzZmFmIiwiZGF0YSI6eyJxIjoi5Zyo5b6u5L-h5pm66KiA5LiO5b6u5L-h5pm66IGG5Lik5aSn5oqA5pyv55qE5pSv5oyB5LiL77yM5b6u5L-hQUnlm6LpmJ_mjqjlh7rkuobigJzlvq7kv6Hlr7nor53lvIDmlL7lubPlj7DigJ3lkozigJzohb7orq_lsI_lvq7igJ3mmbrog73noazku7bkuKTlpKfmoLjlv4Pkuqflk4HjgILlvq7kv6HmlK_ku5jlm6LpmJ_mnIDmlrDlj5HluIPnmoTigJzlvq7kv6HpnZLom5lQcm_igJ3lnKjnjrDlnLrorr7nva7kuobkvZPpqozljLrvvIzorqnlpKflrrbmhJ_lj5dBSeiupOiEuOeahOacrOS6i-OAgiJ9fQ.8FeSvxKlIrbI6MCAaWGekB4sHGA8DeUxgVXiHa8ulJk"; - - Assert.Equal(expectedJwt, actualJwt, ignoreCase: true); - } - } -}