Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Third party platform services and tokens management #36

Merged
merged 1 commit into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>1.11.0</Version>
<Version>1.12.0-preview.1</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis</RootNamespace>
<Description>ABP vNext微信模块,基于StackExchangeRedis提供多服务间共享access_token等缓存数据的功能扩展。</Description>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
<LangVersion>8</LangVersion>
</PropertyGroup>

Expand All @@ -19,4 +20,8 @@
<Content Remove="Localization\StackExchangeRedis\*.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="6.0.*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ public interface IWeChatMiniProgramOptions
string OpenAppId { get; set; }

/// <summary>
/// 微信公众号的 AppId。
/// 微信小程序的 AppId。
/// </summary>
string AppId { get; set; }

/// <summary>
/// 微信公众号的 API Secret。
/// 微信小程序的 API Secret。
/// 如果安装了微信第三方平台模块,此值留空时,接口调用的 access_token 将由微信第三方平台接管。
/// </summary>
string AppSecret { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface IWeChatOfficialOptions

/// <summary>
/// 微信公众号的 API Secret。
/// 如果安装了微信第三方平台模块,此值留空时,接口调用的 access_token 将由微信第三方平台接管。
/// </summary>
string AppSecret { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
using EasyAbp.Abp.WeChat.Common.Infrastructure.Encryption;
using EasyAbp.Abp.WeChat.Common.Models;
using EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.Models.ThirdPartyPlatform;
using EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.OptionsResolve;
using EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.ThirdPartyPlatform;
using EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.ThirdPartyPlatform.OptionsResolve;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
Expand All @@ -23,11 +23,11 @@ namespace EasyAbp.Abp.WeChat.OpenPlatform.Controllers;
public class WeChatOpenPlatformController : AbpControllerBase
{
private readonly IWeChatNotificationEncryptor _weChatNotificationEncryptor;
private readonly IWeChatOpenPlatformOptionsResolver _optionsResolver;
private readonly IWeChatThirdPartyPlatformOptionsResolver _optionsResolver;

public WeChatOpenPlatformController(
IWeChatNotificationEncryptor weChatNotificationEncryptor,
IWeChatOpenPlatformOptionsResolver optionsResolver)
IWeChatThirdPartyPlatformOptionsResolver optionsResolver)
{
_weChatNotificationEncryptor = weChatNotificationEncryptor;
_optionsResolver = optionsResolver;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.ThirdPartyPlatform;
using EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.ThirdPartyPlatform.VerifyTicket;

namespace EasyAbp.Abp.WeChat.OpenPlatform.Handlers;

public class TicketRecordingWeChatThirdPartyPlatformAuthEventHandler : IWeChatThirdPartyPlatformAuthEventHandler
{
public string InfoType => WeChatThirdPartyPlatformAuthEventInfoTypes.ComponentVerifyTicket;

private readonly IComponentVerifyTicketStore _componentVerifyTicketStore;

public TicketRecordingWeChatThirdPartyPlatformAuthEventHandler(
IComponentVerifyTicketStore componentVerifyTicketStore)
{
_componentVerifyTicketStore = componentVerifyTicketStore;
}

public virtual async Task HandleAsync(WeChatThirdPartyPlatformAuthEventHandlerContext context)
{
if (context.Model.ComponentVerifyTicket.IsNullOrWhiteSpace())
{
return;
}

await _componentVerifyTicketStore.SetAsync(context.Model.AppId, context.Model.ComponentVerifyTicket);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using EasyAbp.Abp.WeChat.Common;
using EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.OptionsResolve;
using EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.OptionsResolve.Contributors;
using EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.ThirdPartyPlatform.OptionsResolve;
using EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.ThirdPartyPlatform.OptionsResolve.Contributors;
using Volo.Abp.Modularity;

namespace EasyAbp.Abp.WeChat.OpenPlatform;
Expand All @@ -10,11 +10,11 @@ public class AbpWeChatOpenPlatformModule : AbpModule
{
public override void PostConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpWeChatOpenPlatformResolveOptions>(options =>
Configure<AbpWeChatThirdPartyPlatformResolveOptions>(options =>
{
if (!options.WeChatOpenPlatformOptionsResolveContributors.Exists(x => x.Name == AsyncLocalOptionsResolveContributor.ContributorName))
if (!options.WeChatThirdPartyPlatformOptionsResolveContributors.Exists(x => x.Name == AsyncLocalOptionsResolveContributor.ContributorName))
{
options.WeChatOpenPlatformOptionsResolveContributors.Insert(0, new AsyncLocalOptionsResolveContributor());
options.WeChatThirdPartyPlatformOptionsResolveContributors.Insert(0, new AsyncLocalOptionsResolveContributor());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.OptionsResolve;
using EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.ThirdPartyPlatform.OptionsResolve;

namespace EasyAbp.Abp.WeChat.OpenPlatform;

public class AbpWeChatOpenPlatformOptions : IWeChatOpenPlatformOptions
public class AbpWeChatThirdPartyPlatformOptions : IWeChatThirdPartyPlatformOptions
{
public string Token { get; set; }

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,64 +14,42 @@ namespace EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure;
public class DefaultWeChatOpenPlatformApiRequester : IWeChatOpenPlatformApiRequester, ITransientDependency
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly IAccessTokenAccessor _accessTokenAccessor;

public DefaultWeChatOpenPlatformApiRequester(IHttpClientFactory httpClientFactory,
IAccessTokenAccessor accessTokenAccessor)
public DefaultWeChatOpenPlatformApiRequester(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
_accessTokenAccessor = accessTokenAccessor;
}

#region > Public Methods <

public virtual async Task<string> RequestAsync(string targetUrl,
HttpMethod method,
IOpenPlatformRequest openPlatformRequest = null,
bool withAccessToken = true)
public virtual async Task<string> RequestAsync(
string targetUrl, HttpMethod method, IOpenPlatformRequest openPlatformRequest = null)
{
var client = _httpClientFactory.CreateClient();

targetUrl = targetUrl.EnsureEndsWith('?');

if (withAccessToken)
{
targetUrl += $"access_token={await _accessTokenAccessor.GetAccessTokenAsync()}";
}

var requestMsg = method == HttpMethod.Get
? BuildHttpGetRequestMessage(targetUrl, openPlatformRequest)
: BuildHttpPostRequestMessage(targetUrl, openPlatformRequest);

return await (await client.SendAsync(requestMsg)).Content.ReadAsStringAsync();
}

public virtual async Task<TResponse> RequestAsync<TResponse>(string targetUrl,
HttpMethod method,
IOpenPlatformRequest openPlatformRequest = null,
bool withAccessToken = true)
public virtual async Task<TResponse> RequestAsync<TResponse>(
string targetUrl, HttpMethod method, IOpenPlatformRequest openPlatformRequest = null)
{
var resultStr = await RequestAsync(targetUrl,
method,
openPlatformRequest,
withAccessToken);
var resultStr = await RequestAsync(targetUrl, method, openPlatformRequest);

return JsonConvert.DeserializeObject<TResponse>(resultStr);
}

public virtual async Task<TResponse> RequestFromDataAsync<TResponse>(string targetUrl,
MultipartFormDataContent formDataContent,
IOpenPlatformRequest openPlatformRequest = null,
bool withAccessToken = true)
public virtual async Task<TResponse> RequestFromDataAsync<TResponse>(
string targetUrl, MultipartFormDataContent formDataContent, IOpenPlatformRequest openPlatformRequest = null)
{
var client = _httpClientFactory.CreateClient();
targetUrl = targetUrl.EnsureEndsWith('?');

if (withAccessToken)
{
targetUrl += $"access_token={await _accessTokenAccessor.GetAccessTokenAsync()}";
}

var requestMsg = BuildHttpGetRequestMessage(targetUrl, openPlatformRequest);
requestMsg.Method = HttpMethod.Post;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ public interface IWeChatOpenPlatformApiRequester
{
Task<string> RequestAsync(string targetUrl,
HttpMethod method,
IOpenPlatformRequest openPlatformRequest = null,
bool withAccessToken = true);
IOpenPlatformRequest openPlatformRequest = null);

Task<TResponse> RequestAsync<TResponse>(string targetUrl,
HttpMethod method,
IOpenPlatformRequest openPlatformRequest = null,
bool withAccessToken = true);
IOpenPlatformRequest openPlatformRequest = null);

Task<TResponse> RequestFromDataAsync<TResponse>(string targetUrl,
MultipartFormDataContent formDataContent,
IOpenPlatformRequest openPlatformRequest = null,
bool withAccessToken = true);
IOpenPlatformRequest openPlatformRequest = null);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Newtonsoft.Json;

namespace EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.Models.ThirdPartyPlatform;

public class ApiComponentTokenRequest : OpenPlatformCommonRequest
{
[JsonProperty("component_appid")]
public string ComponentAppId { get; set; }

[JsonProperty("component_appsecret")]
public string ComponentAppSecret { get; set; }

[JsonProperty("component_verify_ticket")]
public string ComponentVerifyTicket { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;

namespace EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.Models.ThirdPartyPlatform;

public class ApiComponentTokenResponse : OpenPlatformCommonResponse
{
[JsonProperty("component_access_token")]
public string ComponentAccessToken { get; set; }

[JsonProperty("expires_in")]
public int ExpiresIn { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ public class AuthNotificationModel : ExtensibleObject

public string InfoType => this.GetProperty<string>("InfoType");

public string AuthorizerAppid => this.GetProperty<string>("AuthorizerAppid");
public string AuthorizerAppId => this.GetProperty<string>("AuthorizerAppid");

public string AuthorizationCode => this.GetProperty<string>("AuthorizationCode");

public int AuthorizationCodeExpiredTime => this.GetProperty<int>("AuthorizationCodeExpiredTime");

public string PreAuthCode => this.GetProperty<string>("PreAuthCode");

public string ComponentVerifyTicket => this.GetProperty<string>("ComponentVerifyTicket");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Newtonsoft.Json;

namespace EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.Models.ThirdPartyPlatform;

public class AuthorizerTokenRequest : OpenPlatformCommonRequest
{
[JsonProperty("component_appid")]
public string ComponentAppId { get; set; }

[JsonProperty("authorizer_appid")]
public string AuthorizerAppId { get; set; }

[JsonProperty("authorizer_refresh_token")]
public string AuthorizerRefreshToken { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Newtonsoft.Json;

namespace EasyAbp.Abp.WeChat.OpenPlatform.Infrastructure.Models.ThirdPartyPlatform;

public class AuthorizerTokenResponse : OpenPlatformCommonResponse
{
[JsonProperty("authorizer_access_token")]
public string AuthorizerAccessToken { get; set; }

[JsonProperty("expires_in")]
public int ExpiresIn { get; set; }

[JsonProperty("authorizer_refresh_token")]
public string AuthorizerRefreshToken { get; set; }
}

This file was deleted.