feat(wxapi): 新增 Credentials 类型,存储初始化客户端时的凭证副本

This commit is contained in:
Fu Diwei 2021-07-27 00:49:38 +08:00
parent 3744c2fea6
commit ee8fb3824c
16 changed files with 96 additions and 81 deletions

View File

@ -21,7 +21,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (client == null) throw new ArgumentNullException(nameof(client));
if (string.IsNullOrEmpty(callbackJson)) throw new ArgumentNullException(callbackJson);
return client.FlurlJsonSerializer.Deserialize<TEvent>(callbackJson);
return client.JsonSerializer.Deserialize<TEvent>(callbackJson);
}
/// <summary>

View File

@ -173,7 +173,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
if (request.CardExtra != null && string.IsNullOrEmpty(request.CardExtra.Nonce))
request.CardExtra.Nonce = Guid.NewGuid().ToString("N");

View File

@ -25,10 +25,10 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.ComponentAppId))
request.ComponentAppId = client.WechatAppId;
request.ComponentAppId = client.Credentials.AppId;
if (string.IsNullOrEmpty(request.ComponentSecret))
request.ComponentSecret = client.WechatAppSecret;
request.ComponentSecret = client.Credentials.AppSecret;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_start_push_ticket");
@ -50,10 +50,10 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.ComponentAppId))
request.ComponentAppId = client.WechatAppId;
request.ComponentAppId = client.Credentials.AppId;
if (string.IsNullOrEmpty(request.ComponentSecret))
request.ComponentSecret = client.WechatAppSecret;
request.ComponentSecret = client.Credentials.AppSecret;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_component_token");
@ -75,7 +75,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.ComponentAppId))
request.ComponentAppId = client.WechatAppId;
request.ComponentAppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_create_preauthcode")
@ -98,7 +98,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.ComponentAppId))
request.ComponentAppId = client.WechatAppId;
request.ComponentAppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_query_auth")
@ -121,7 +121,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.ComponentAppId))
request.ComponentAppId = client.WechatAppId;
request.ComponentAppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_authorizer_token")
@ -144,7 +144,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.ComponentAppId))
request.ComponentAppId = client.WechatAppId;
request.ComponentAppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_get_authorizer_info")
@ -167,7 +167,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.ComponentAppId))
request.ComponentAppId = client.WechatAppId;
request.ComponentAppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_get_authorizer_list")
@ -190,7 +190,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.ComponentAppId))
request.ComponentAppId = client.WechatAppId;
request.ComponentAppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_get_authorizer_option")
@ -213,7 +213,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.ComponentAppId))
request.ComponentAppId = client.WechatAppId;
request.ComponentAppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "component", "api_set_authorizer_option")

View File

@ -19,12 +19,12 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (string.IsNullOrEmpty(request.ShopId))
{
request.ShopId = client.WechatImmeDeliveryAppKey;
request.ShopId = client.Credentials.ImmeDeliveryAppKey;
}
if (string.IsNullOrEmpty(request.DeliverySignature))
{
string plainText = $"{request.ShopId}{request.ShopOrderId}{client.WechatImmeDeliveryAppSecret}";
string plainText = $"{request.ShopId}{request.ShopOrderId}{client.Credentials.ImmeDeliveryAppSecret}";
request.DeliverySignature = Security.SHA1Utility.Hash(plainText).ToLower();
}

View File

@ -29,8 +29,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Get, "cgi-bin", "token")
.SetQueryParam("grant_type", request.GrantType)
.SetQueryParam("appid", client.WechatAppId)
.SetQueryParam("secret", client.WechatAppSecret);
.SetQueryParam("appid", client.Credentials.AppId)
.SetQueryParam("secret", client.Credentials.AppSecret);
return await client.SendRequestWithJsonAsync<Models.CgibinTokenResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
}
@ -112,7 +112,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "clear_quota")

View File

@ -85,7 +85,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
using var fileContent = new ByteArrayContent(request.FileBytes ?? new byte[0]);
using var descContent = new ByteArrayContent(Encoding.UTF8.GetBytes(client.FlurlJsonSerializer.Serialize(request)));
using var descContent = new ByteArrayContent(Encoding.UTF8.GetBytes(client.JsonSerializer.Serialize(request)));
using var httpContent = new MultipartFormDataContent(boundary);
httpContent.Add(fileContent, "\"media\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);

View File

@ -22,7 +22,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (string.IsNullOrEmpty(request.AppId))
{
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
}
if (!request.Timestamp.HasValue)
@ -46,8 +46,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
string plainText = string.Join("&", paramMap.Select(e => $"{e.Key}={e.Value}"))
+ $"&org_loc={reqLoc}"
+ $"&method={method.ToUpper()}"
+ $"&secret={client.WechatMidasAppKey}";
request.Signature = Security.HMACSHA256Utility.Hash(client.WechatMidasAppKey ?? string.Empty, plainText).ToLower();
+ $"&secret={client.Credentials.MidasAppKey}";
request.Signature = Security.HMACSHA256Utility.Hash(client.Credentials.MidasAppKey ?? string.Empty, plainText).ToLower();
}
return request;

View File

@ -70,7 +70,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "nontax", "insertbill")
@ -94,7 +94,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "nontax", "queryfee")
@ -117,7 +117,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "nontax", "unifiedorder")
@ -140,7 +140,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "nontax", "getorder")
@ -163,7 +163,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "nontax", "refund")
@ -186,7 +186,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "nontax", "downloadbill")
@ -209,7 +209,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "nontax", "notifyinconsistentorder")
@ -232,7 +232,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "nontax", "mocknotification")
@ -255,7 +255,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "nontax", "mockqueryfee")
@ -278,7 +278,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "nontax", "micropay")
@ -301,7 +301,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "nontax", "getorderlist")

View File

@ -111,7 +111,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "product", "register", "submit_merchantinfo")
@ -134,7 +134,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "product", "register", "submit_basicinfo")

View File

@ -28,7 +28,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "semantic", "semproxy", "search")

View File

@ -29,8 +29,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Get, "sns", "oauth2", "access_token")
.SetQueryParam("grant_type", request.GrantType)
.SetQueryParam("appid", client.WechatAppId)
.SetQueryParam("secret", client.WechatAppSecret)
.SetQueryParam("appid", client.Credentials.AppId)
.SetQueryParam("secret", client.Credentials.AppSecret)
.SetQueryParam("code", request.Code);
return await client.SendRequestWithJsonAsync<Models.SnsOAuth2AccessTokenResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
@ -54,7 +54,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Get, "sns", "oauth2", "refresh_token")
.SetQueryParam("grant_type", request.GrantType)
.SetQueryParam("appid", client.WechatAppId)
.SetQueryParam("appid", client.Credentials.AppId)
.SetQueryParam("refresh_token", request.RefreshToken);
return await client.SendRequestWithJsonAsync<Models.SnsOAuth2RefreshTokenResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
@ -124,8 +124,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Get, "sns", "jscode2session")
.SetQueryParam("grant_type", request.GrantType)
.SetQueryParam("appid", client.WechatAppId)
.SetQueryParam("secret", client.WechatAppSecret)
.SetQueryParam("appid", client.Credentials.AppId)
.SetQueryParam("secret", client.Credentials.AppSecret)
.SetQueryParam("js_code", request.JsCode);
return await client.SendRequestWithJsonAsync<Models.SnsJsCode2SessionResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
@ -146,7 +146,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.ComponentAppId))
request.ComponentAppId = client.WechatAppId;
request.ComponentAppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Get, "sns", "component", "jscode2session")

View File

@ -26,7 +26,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "wxa", "business", "getpayfororder")

View File

@ -400,7 +400,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
if (request is null) throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.AppId))
request.AppId = client.WechatAppId;
request.AppId = client.Credentials.AppId;
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "wxa", "getuserriskrank")

View File

@ -37,7 +37,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
return new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()
{
{ "appId", client.WechatAppId },
{ "appId", client.Credentials.AppId },
{ "timestamp", timestamp },
{ "nonceStr", nonce },
{ "signature", sign }
@ -63,7 +63,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
SortedSet<string> sortedParams = new SortedSet<string>();
sortedParams.Add(cardType);
sortedParams.Add(timestamp);
sortedParams.Add(client.WechatAppId);
sortedParams.Add(client.Credentials.AppId);
sortedParams.Add(nonce);
sortedParams.Add(wxcardTicket);
string cardSign = Security.SHA1Utility.Hash(string.Join(string.Empty, sortedParams)).ToLower();

View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SKIT.FlurlHttpClient.Wechat.Api.Settings
{
public class Credentials
{
/// <summary>
/// 初始化客户端时 <see cref="WechatApiClientOptions.AppId"/> 的副本。
/// </summary>
public string AppId { get; }
/// <summary>
/// 初始化客户端时 <see cref="WechatApiClientOptions.AppSecret"/> 的副本。
/// </summary>
public string AppSecret { get; }
/// <summary>
/// 初始化客户端时 <see cref="WechatApiClientOptions.ImmeDeliveryAppKey"/> 的副本。
/// </summary>
public string? ImmeDeliveryAppKey { get; }
/// <summary>
/// 初始化客户端时 <see cref="WechatApiClientOptions.ImmeDeliveryAppSecret"/> 的副本。
/// </summary>
public string? ImmeDeliveryAppSecret { get; }
/// <summary>
/// 初始化客户端时 <see cref="WechatApiClientOptions.MidasAppKey"/> 的副本。
/// </summary>
public string? MidasAppKey { get; }
internal Credentials(WechatApiClientOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));
AppId = options.AppId;
AppSecret = options.AppSecret;
ImmeDeliveryAppKey = options.ImmeDeliveryAppKey;
ImmeDeliveryAppSecret = options.ImmeDeliveryAppSecret;
MidasAppKey = options.MidasAppKey;
}
}
}

View File

@ -18,37 +18,9 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
public class WechatApiClient : WechatClientBase
{
/// <summary>
/// 获取当前客户端使用的微信 AppId
/// 获取当前客户端使用的微信公众平台凭证
/// </summary>
public string WechatAppId { get; }
/// <summary>
/// 获取当前客户端使用的微信 AppSecret。
/// </summary>
internal string WechatAppSecret { get; }
/// <summary>
/// 获取当前客户端使用的即时配送公司帐号 AppKey用于即使配送相关接口的请求签名
/// </summary>
internal string? WechatImmeDeliveryAppKey { get; }
/// <summary>
/// 获取当前客户端使用的即时配送公司帐号 AppSecret用于即使配送相关接口的请求签名
/// </summary>
internal string? WechatImmeDeliveryAppSecret { get; }
/// <summary>
/// 获取或设置米大师平台 AppKey用于虚拟支付相关接口的请求签名
/// </summary>
internal string? WechatMidasAppKey { get; }
/// <summary>
/// 获取当前客户端使用的 JSON 序列化器。
/// </summary>
internal ISerializer FlurlJsonSerializer
{
get { return FlurlClient.Settings?.JsonSerializer ?? new FlurlNewtonsoftJsonSerializer(); }
}
public Settings.Credentials Credentials { get; }
/// <summary>
/// 用指定的配置项初始化 <see cref="WechatApiClient"/> 类的新实例。
@ -59,11 +31,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
{
if (options == null) throw new ArgumentNullException(nameof(options));
WechatAppId = options.AppId;
WechatAppSecret = options.AppSecret;
WechatImmeDeliveryAppKey = options.ImmeDeliveryAppKey;
WechatImmeDeliveryAppSecret = options.ImmeDeliveryAppSecret;
WechatMidasAppKey = options.MidasAppKey;
Credentials = new Settings.Credentials(options);
FlurlClient.BaseUrl = options.Endpoints ?? WechatApiEndpoints.DEFAULT;
FlurlClient.WithTimeout(TimeSpan.FromMilliseconds(options.Timeout));