From e411bbf2fbd20f12270a985ee74b136170e216f8 Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 15 Oct 2026 16:09:19 +0800 Subject: [PATCH] feat(openiddict): Split the WeChat Work token grant into OAuth and mini program flows Give the OAuth login and the mini program login their own WeChat Work grant types instead of sharing one, and add the code2session exchange the mini program flow needs. - AbpWeChatWorkGlobalConsts.GrantType becomes OAuthGrantType with the value "wecom_oauth", and MiniProgramGrantType ("wecom_mp") is added - WeChatWorkTokenExtensionGrant becomes abstract and exposes a FindUserInfoAsync hook; WeChatWorkOAuthTokenExtensionGrant keeps the existing user info exchange and the new WeChatWorkMiniProgramTokenExtensionGrant exchanges the js code through IWeChatWorkUserFinder.GetCode2SessionAsync - add the code2session model, response and mapping, plus the /cgi-bin/miniprogram/jscode2session client call - register both grant types in AbpOpenIddictWeChatWorkModule and allow both custom flows in AllowWeChatWorkFlow, and migrate the remaining GrantType call sites BREAKING CHANGE: AbpWeChatWorkGlobalConsts.GrantType was removed and its value changed, so clients that authenticate with grant_type "wecom" must switch to "wecom_oauth"; the public WeChatWorkTokenExtensionGrant is now abstract and must be subclassed. --- .../WeChat/Work/AbpWeChatWorkGlobalConsts.cs | 8 +++- .../Work/Authorize/IWeChatWorkUserFinder.cs | 4 ++ .../Models/WeChatWorkCode2Session.cs | 34 +++++++++++++++++ .../WeChatWorkCode2SessionResponse.cs | 38 +++++++++++++++++++ .../Response/WeChatWorkResponseExtensions.cs | 14 +++++++ .../Work/Authorize/WeChatWorkUserFinder.cs | 12 ++++++ ...WeChatWorkRequestExtensions.MiniProgram.cs | 29 ++++++++++++++ .../Work/AbpOpenIddictWeChatWorkModule.cs | 8 +++- ...eChatWorkMiniProgramTokenExtensionGrant.cs | 25 ++++++++++++ .../WeChatWorkOAuthTokenExtensionGrant.cs | 19 ++++++++++ .../Work/WeChatWorkTokenExtensionGrant.cs | 12 +++--- ...atWorkOpenIddictServerBuilderExtensions.cs | 3 +- 12 files changed, 195 insertions(+), 11 deletions(-) create mode 100644 aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Models/WeChatWorkCode2Session.cs create mode 100644 aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Response/WeChatWorkCode2SessionResponse.cs create mode 100644 aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/System/Net/Http/HttpClientWeChatWorkRequestExtensions.MiniProgram.cs create mode 100644 aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkMiniProgramTokenExtensionGrant.cs create mode 100644 aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkOAuthTokenExtensionGrant.cs diff --git a/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/AbpWeChatWorkGlobalConsts.cs b/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/AbpWeChatWorkGlobalConsts.cs index 21f8856b5..c6e77c1b0 100644 --- a/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/AbpWeChatWorkGlobalConsts.cs +++ b/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/AbpWeChatWorkGlobalConsts.cs @@ -7,9 +7,13 @@ public class AbpWeChatWorkGlobalConsts /// public static string ProviderName { get; set; } = "WorkWeixin"; /// - /// 企业微信授权类型 + /// 企业微信OAuth授权类型 /// - public static string GrantType { get; set; } = "wecom"; + public static string OAuthGrantType { get; set; } = "wecom_oauth"; + /// + /// 企业微信小程序授权类型 + /// + public static string MiniProgramGrantType { get; set; } = "wecom_mp"; /// /// 企业微信个人信息标识 /// diff --git a/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/IWeChatWorkUserFinder.cs b/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/IWeChatWorkUserFinder.cs index cb81fefdc..d7c820206 100644 --- a/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/IWeChatWorkUserFinder.cs +++ b/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/IWeChatWorkUserFinder.cs @@ -12,6 +12,10 @@ public interface IWeChatWorkUserFinder string code, CancellationToken cancellationToken = default); + Task GetCode2SessionAsync( + string code, + CancellationToken cancellationToken = default); + Task GetUserDetailAsync( string userTicket, CancellationToken cancellationToken = default); diff --git a/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Models/WeChatWorkCode2Session.cs b/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Models/WeChatWorkCode2Session.cs new file mode 100644 index 000000000..4535a3da9 --- /dev/null +++ b/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Models/WeChatWorkCode2Session.cs @@ -0,0 +1,34 @@ +using JetBrains.Annotations; +using Newtonsoft.Json; +using System.Text.Json.Serialization; + +namespace LINGYUN.Abp.WeChat.Work.Authorize.Models; + +public class WeChatWorkCode2Session +{ + /// + /// 用户所属企业的corpid + /// + [NotNull] + [JsonProperty("corpid")] + [JsonPropertyName("corpid")] + public string CorpId { get; set; } = default!; + /// + /// 用户在企业内的UserID,对应管理端的账号,企业内唯一。注意:第三方小程序此处返回加密的userid + /// + [NotNull] + [JsonProperty("userid")] + [JsonPropertyName("userid")] + public string UserId { get; set; } = default!; + /// + /// 会话密钥 + /// + [NotNull] + [JsonProperty("session_key")] + [JsonPropertyName("session_key")] + public string SessionKey { get; set; } = default!; + [CanBeNull] + [JsonProperty("deviceid")] + [JsonPropertyName("deviceid")] + public string? DeviceId { get; set; } = default!; +} diff --git a/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Response/WeChatWorkCode2SessionResponse.cs b/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Response/WeChatWorkCode2SessionResponse.cs new file mode 100644 index 000000000..3435d77ed --- /dev/null +++ b/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Response/WeChatWorkCode2SessionResponse.cs @@ -0,0 +1,38 @@ +using JetBrains.Annotations; +using Newtonsoft.Json; +using System; +using System.Text.Json.Serialization; + +namespace LINGYUN.Abp.WeChat.Work.Authorize.Response; +/// +/// 企业微信临时登录凭证校验响应 +/// +[Serializable] +public class WeChatWorkCode2SessionResponse : WeChatWorkResponse +{ + /// + /// 用户所属企业的corpid + /// + [NotNull] + [JsonProperty("corpid")] + [JsonPropertyName("corpid")] + public string CorpId { get; set; } = default!; + /// + /// 用户在企业内的UserID,对应管理端的账号,企业内唯一。注意:第三方小程序此处返回加密的userid + /// + [NotNull] + [JsonProperty("userid")] + [JsonPropertyName("userid")] + public string UserId { get; set; } = default!; + /// + /// 会话密钥 + /// + [NotNull] + [JsonProperty("session_key")] + [JsonPropertyName("session_key")] + public string SessionKey { get; set; } = default!; + [CanBeNull] + [JsonProperty("deviceid")] + [JsonPropertyName("deviceid")] + public string? DeviceId { get; set; } = default!; +} diff --git a/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Response/WeChatWorkResponseExtensions.cs b/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Response/WeChatWorkResponseExtensions.cs index 9c5284b3a..e57427c4f 100644 --- a/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Response/WeChatWorkResponseExtensions.cs +++ b/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Response/WeChatWorkResponseExtensions.cs @@ -15,6 +15,20 @@ internal static class WeChatWorkResponseExtensions }; } + public static WeChatWorkCode2Session ToCode2Session( + this WeChatWorkCode2SessionResponse response) + { + response.ThrowIfNotSuccess(); + + return new WeChatWorkCode2Session + { + CorpId = response.CorpId, + UserId = response.UserId, + DeviceId = response.DeviceId, + SessionKey = response.SessionKey, + }; + } + public static WeChatWorkUserDetail ToUserDetail( this WeChatWorkUserDetailResponse response) { diff --git a/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/WeChatWorkUserFinder.cs b/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/WeChatWorkUserFinder.cs index 924b646f3..d47daab6a 100644 --- a/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/WeChatWorkUserFinder.cs +++ b/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/WeChatWorkUserFinder.cs @@ -38,6 +38,18 @@ public class WeChatWorkUserFinder : IWeChatWorkUserFinder, ISingletonDependency return wechatWorkResponse.ToUserInfo(); } + public async virtual Task GetCode2SessionAsync( + string code, + CancellationToken cancellationToken = default) + { + var token = await WeChatWorkTokenProvider.GetTokenAsync(cancellationToken); + var client = HttpClientFactory.CreateWeChatWorkApiClient(); + + var wechatWorkResponse = await client.Code2SessionAsync(token.AccessToken, code, cancellationToken: cancellationToken); + + return wechatWorkResponse.ToCode2Session(); + } + public async virtual Task GetUserDetailAsync( string userTicket, CancellationToken cancellationToken = default) diff --git a/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/System/Net/Http/HttpClientWeChatWorkRequestExtensions.MiniProgram.cs b/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/System/Net/Http/HttpClientWeChatWorkRequestExtensions.MiniProgram.cs new file mode 100644 index 000000000..2eac3b39f --- /dev/null +++ b/aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/System/Net/Http/HttpClientWeChatWorkRequestExtensions.MiniProgram.cs @@ -0,0 +1,29 @@ +using LINGYUN.Abp.WeChat.Work.Authorize.Response; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace System.Net.Http; + +internal static partial class HttpClientWeChatWorkRequestExtensions +{ + public async static Task Code2SessionAsync( + this HttpMessageInvoker client, + string accessToken, + string jsCode, + string grantType = "authorization_code", + CancellationToken cancellationToken = default) + { + var urlBuilder = new StringBuilder(); + urlBuilder.Append("/cgi-bin/miniprogram/jscode2session"); + urlBuilder.AppendFormat("?access_token={0}", accessToken); + urlBuilder.AppendFormat("&js_code={0}", jsCode); + urlBuilder.AppendFormat("&grant_type={0}", grantType); + + using var httpRequest = new HttpRequestMessage(HttpMethod.Get, urlBuilder.ToString()); + + using var httpResponse = await client.SendAsync(httpRequest, cancellationToken); + + return await httpResponse.DeserializeObjectAsync(); + } +} diff --git a/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/AbpOpenIddictWeChatWorkModule.cs b/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/AbpOpenIddictWeChatWorkModule.cs index 2447af85c..9cf098e62 100644 --- a/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/AbpOpenIddictWeChatWorkModule.cs +++ b/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/AbpOpenIddictWeChatWorkModule.cs @@ -30,8 +30,12 @@ public class AbpOpenIddictWeChatWorkModule : AbpModule Configure(options => { options.Grants.TryAdd( - AbpWeChatWorkGlobalConsts.GrantType, - new WeChatWorkTokenExtensionGrant()); + AbpWeChatWorkGlobalConsts.OAuthGrantType, + new WeChatWorkOAuthTokenExtensionGrant()); + + options.Grants.TryAdd( + AbpWeChatWorkGlobalConsts.MiniProgramGrantType, + new WeChatWorkMiniProgramTokenExtensionGrant()); }); Configure(options => diff --git a/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkMiniProgramTokenExtensionGrant.cs b/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkMiniProgramTokenExtensionGrant.cs new file mode 100644 index 000000000..95539855e --- /dev/null +++ b/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkMiniProgramTokenExtensionGrant.cs @@ -0,0 +1,25 @@ +using LINGYUN.Abp.WeChat.Work; +using LINGYUN.Abp.WeChat.Work.Authorize; +using LINGYUN.Abp.WeChat.Work.Authorize.Models; +using System.Threading.Tasks; +using Volo.Abp.OpenIddict.ExtensionGrantTypes; + +namespace LINGYUN.Abp.OpenIddict.WeChat.Work; + +public class WeChatWorkMiniProgramTokenExtensionGrant : WeChatWorkTokenExtensionGrant +{ + public override string Name => AbpWeChatWorkGlobalConsts.MiniProgramGrantType; + + protected async override Task FindUserInfoAsync(ExtensionGrantContext context, string code) + { + var userFinder = GetRequiredService(context); + + var code2Session = await userFinder.GetCode2SessionAsync(code); + + return new WeChatWorkUserInfo + { + UserId = code2Session.UserId, + UserTicket = code2Session.SessionKey, + }; + } +} diff --git a/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkOAuthTokenExtensionGrant.cs b/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkOAuthTokenExtensionGrant.cs new file mode 100644 index 000000000..f7806a9c0 --- /dev/null +++ b/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkOAuthTokenExtensionGrant.cs @@ -0,0 +1,19 @@ +using LINGYUN.Abp.WeChat.Work; +using LINGYUN.Abp.WeChat.Work.Authorize; +using LINGYUN.Abp.WeChat.Work.Authorize.Models; +using System.Threading.Tasks; +using Volo.Abp.OpenIddict.ExtensionGrantTypes; + +namespace LINGYUN.Abp.OpenIddict.WeChat.Work; + +public class WeChatWorkOAuthTokenExtensionGrant : WeChatWorkTokenExtensionGrant +{ + public override string Name => AbpWeChatWorkGlobalConsts.OAuthGrantType; + + protected async override Task FindUserInfoAsync(ExtensionGrantContext context, string code) + { + var userFinder = GetRequiredService(context); + + return await userFinder.GetUserInfoAsync(code); + } +} diff --git a/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkTokenExtensionGrant.cs b/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkTokenExtensionGrant.cs index 4c9b1e49e..270a20c90 100644 --- a/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkTokenExtensionGrant.cs +++ b/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkTokenExtensionGrant.cs @@ -1,5 +1,5 @@ using LINGYUN.Abp.WeChat.Work; -using LINGYUN.Abp.WeChat.Work.Authorize; +using LINGYUN.Abp.WeChat.Work.Authorize.Models; using LINGYUN.Abp.WeChat.Work.Security.Claims; using LINGYUN.Abp.WeChat.Work.Settings; using Microsoft.AspNetCore.Authentication; @@ -28,9 +28,11 @@ using SignInResult = Microsoft.AspNetCore.Mvc.SignInResult; namespace LINGYUN.Abp.OpenIddict.WeChat.Work; -public class WeChatWorkTokenExtensionGrant : ITokenExtensionGrant +public abstract class WeChatWorkTokenExtensionGrant : ITokenExtensionGrant { - public string Name => AbpWeChatWorkGlobalConsts.GrantType; + public abstract string Name { get; } + + protected abstract Task FindUserInfoAsync(ExtensionGrantContext context, string code); public async virtual Task HandleAsync(ExtensionGrantContext context) { @@ -58,11 +60,9 @@ public class WeChatWorkTokenExtensionGrant : ITokenExtensionGrant return Forbid(properties, OpenIddictServerAspNetCoreDefaults.AuthenticationScheme); } - var userFinder = GetRequiredService(context); - try { - var userInfo = await userFinder.GetUserInfoAsync(code); + var userInfo = await FindUserInfoAsync(context, code); var userManager = GetRequiredService(context); var currentUser = await userManager.FindByLoginAsync(AbpWeChatWorkGlobalConsts.ProviderName, userInfo.UserId); diff --git a/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/Microsoft/Extensions/DependencyInjection/WeChatWorkOpenIddictServerBuilderExtensions.cs b/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/Microsoft/Extensions/DependencyInjection/WeChatWorkOpenIddictServerBuilderExtensions.cs index 8b024a7b4..bb28230fb 100644 --- a/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/Microsoft/Extensions/DependencyInjection/WeChatWorkOpenIddictServerBuilderExtensions.cs +++ b/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/Microsoft/Extensions/DependencyInjection/WeChatWorkOpenIddictServerBuilderExtensions.cs @@ -8,7 +8,8 @@ public static class WeChatWorkOpenIddictServerBuilderExtensions public static OpenIddictServerBuilder AllowWeChatWorkFlow(this OpenIddictServerBuilder builder) { return builder - .AllowCustomFlow(AbpWeChatWorkGlobalConsts.GrantType); + .AllowCustomFlow(AbpWeChatWorkGlobalConsts.OAuthGrantType) + .AllowCustomFlow(AbpWeChatWorkGlobalConsts.MiniProgramGrantType); } public static OpenIddictServerBuilder RegisterWeChatWorkScopes(this OpenIddictServerBuilder builder)