From d3ccdf5b4fd486ea0eb3c10f7f4d3cc4b383c6de Mon Sep 17 00:00:00 2001 From: colin Date: Mon, 19 Oct 2026 10:26:21 +0800 Subject: [PATCH] refactor(identityserver): Split the WeChat Work grant validator into OAuth and mini program flows Apply the same split the OpenIddict grant already got to the legacy IdentityServer module, so the OAuth login and the mini program login stop sharing a single validator, and drop the removed AbpWeChatWorkGlobalConsts.GrantType constant that broke this project's build. - WeChatWorkGrantValidator becomes abstract: GrantType becomes abstract and the user lookup moves behind a FindUserInfoAsync hook; the constructor becomes protected - add WeChatWorkOAuthGrantValidator (grant type wecom_oauth, user info exchange) and WeChatWorkMiniProgramGrantValidator (grant type wecom_mp, code2session exchange mapped to the user id plus the session key as user ticket) - register both validators in AbpIdentityServerWeChatWorkModule Fixes the CS0117 build error introduced when AbpWeChatWorkGlobalConsts.GrantType was renamed. --- .../Work/AbpIdentityServerWeChatWorkModule.cs | 3 +- .../WeChat/Work/WeChatWorkGrantValidator.cs | 13 +++-- .../WeChatWorkMiniProgramGrantValidator.cs | 55 +++++++++++++++++++ .../Work/WeChatWorkOAuthGrantValidator.cs | 49 +++++++++++++++++ 4 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/WeChatWorkMiniProgramGrantValidator.cs create mode 100644 aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/WeChatWorkOAuthGrantValidator.cs diff --git a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/AbpIdentityServerWeChatWorkModule.cs b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/AbpIdentityServerWeChatWorkModule.cs index 0f702528b..d2685efa4 100644 --- a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/AbpIdentityServerWeChatWorkModule.cs +++ b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/AbpIdentityServerWeChatWorkModule.cs @@ -18,7 +18,8 @@ public class AbpIdentityServerWeChatWorkModule : AbpModule { PreConfigure(builder => { - builder.AddExtensionGrantValidator(); + builder.AddExtensionGrantValidator(); + builder.AddExtensionGrantValidator(); }); } diff --git a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/WeChatWorkGrantValidator.cs b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/WeChatWorkGrantValidator.cs index 45652b2f9..1ee52b03b 100644 --- a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/WeChatWorkGrantValidator.cs +++ b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/WeChatWorkGrantValidator.cs @@ -5,6 +5,7 @@ using IdentityServer4.Services; using IdentityServer4.Validation; using LINGYUN.Abp.WeChat.Work; using LINGYUN.Abp.WeChat.Work.Authorize; +using LINGYUN.Abp.WeChat.Work.Authorize.Models; using LINGYUN.Abp.WeChat.Work.Localization; using LINGYUN.Abp.WeChat.Work.Settings; using Microsoft.AspNetCore.Identity; @@ -24,10 +25,8 @@ using Volo.Abp.Uow; using IdentityUser = Volo.Abp.Identity.IdentityUser; namespace LINGYUN.Abp.IdentityServer.WeChat.Work; -public class WeChatWorkGrantValidator : IExtensionGrantValidator +public abstract class WeChatWorkGrantValidator : IExtensionGrantValidator { - public string GrantType => AbpWeChatWorkGlobalConsts.GrantType; - protected ILogger Logger { get; } protected IEventService EventService { get; } protected UserManager UserManager { get; } @@ -38,7 +37,7 @@ public class WeChatWorkGrantValidator : IExtensionGrantValidator protected IStringLocalizer WeChatWorkLocalizer { get; } protected IWeChatWorkUserFinder WeChatWorkUserFinder { get; } - public WeChatWorkGrantValidator( + protected WeChatWorkGrantValidator( IEventService eventService, ICurrentTenant currentTenant, IGuidGenerator guidGenerator, @@ -60,6 +59,10 @@ public class WeChatWorkGrantValidator : IExtensionGrantValidator WeChatWorkUserFinder = weChatWorkUserFinder; } + public abstract string GrantType { get; } + + protected abstract Task FindUserInfoAsync(ExtensionGrantValidationContext context, string code); + [UnitOfWork] public async virtual Task ValidateAsync(ExtensionGrantValidationContext context) { @@ -83,7 +86,7 @@ public class WeChatWorkGrantValidator : IExtensionGrantValidator try { - var userInfo = await WeChatWorkUserFinder.GetUserInfoAsync(code); + var userInfo = await FindUserInfoAsync(context, code); var currentUser = await UserManager.FindByLoginAsync(AbpWeChatWorkGlobalConsts.ProviderName, userInfo.UserId); if (currentUser == null) diff --git a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/WeChatWorkMiniProgramGrantValidator.cs b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/WeChatWorkMiniProgramGrantValidator.cs new file mode 100644 index 000000000..5d2f7a17c --- /dev/null +++ b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/WeChatWorkMiniProgramGrantValidator.cs @@ -0,0 +1,55 @@ +using IdentityServer4.Services; +using IdentityServer4.Validation; +using LINGYUN.Abp.WeChat.Work; +using LINGYUN.Abp.WeChat.Work.Authorize; +using LINGYUN.Abp.WeChat.Work.Authorize.Models; +using LINGYUN.Abp.WeChat.Work.Localization; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.Localization; +using Microsoft.Extensions.Logging; +using System.Threading.Tasks; +using Volo.Abp.Guids; +using Volo.Abp.Identity; +using Volo.Abp.MultiTenancy; +using Volo.Abp.Settings; + +namespace LINGYUN.Abp.IdentityServer.WeChat.Work; + +public class WeChatWorkMiniProgramGrantValidator : WeChatWorkGrantValidator +{ + public WeChatWorkMiniProgramGrantValidator( + IEventService eventService, + ICurrentTenant currentTenant, + IGuidGenerator guidGenerator, + ISettingProvider settingProvider, + UserManager userManager, + IdentitySecurityLogManager identitySecurityLogManager, + IStringLocalizer weChatWorkLocalizer, + IWeChatWorkUserFinder weChatWorkUserFinder, + ILogger logger) : + base( + eventService, + currentTenant, + guidGenerator, + settingProvider, + userManager, + identitySecurityLogManager, + weChatWorkLocalizer, + weChatWorkUserFinder, + logger) + { + } + + public override string GrantType => AbpWeChatWorkGlobalConsts.MiniProgramGrantType; + + protected async override Task FindUserInfoAsync(ExtensionGrantValidationContext context, string code) + { + var code2Session = await WeChatWorkUserFinder.GetCode2SessionAsync(code); + + return new WeChatWorkUserInfo + { + UserId = code2Session.UserId, + UserTicket = code2Session.SessionKey, + }; + } +} diff --git a/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/WeChatWorkOAuthGrantValidator.cs b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/WeChatWorkOAuthGrantValidator.cs new file mode 100644 index 000000000..555f3cc4e --- /dev/null +++ b/aspnet-core/modules/identityServer/LINGYUN.Abp.IdentityServer.WeChat.Work/LINGYUN/Abp/IdentityServer/WeChat/Work/WeChatWorkOAuthGrantValidator.cs @@ -0,0 +1,49 @@ +using IdentityServer4.Services; +using IdentityServer4.Validation; +using LINGYUN.Abp.WeChat.Work; +using LINGYUN.Abp.WeChat.Work.Authorize; +using LINGYUN.Abp.WeChat.Work.Authorize.Models; +using LINGYUN.Abp.WeChat.Work.Localization; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.Localization; +using Microsoft.Extensions.Logging; +using System.Threading.Tasks; +using Volo.Abp.Guids; +using Volo.Abp.Identity; +using Volo.Abp.MultiTenancy; +using Volo.Abp.Settings; + +namespace LINGYUN.Abp.IdentityServer.WeChat.Work; + +public class WeChatWorkOAuthGrantValidator : WeChatWorkGrantValidator +{ + public WeChatWorkOAuthGrantValidator( + IEventService eventService, + ICurrentTenant currentTenant, + IGuidGenerator guidGenerator, + ISettingProvider settingProvider, + UserManager userManager, + IdentitySecurityLogManager identitySecurityLogManager, + IStringLocalizer weChatWorkLocalizer, + IWeChatWorkUserFinder weChatWorkUserFinder, + ILogger logger) : + base( + eventService, + currentTenant, + guidGenerator, + settingProvider, + userManager, + identitySecurityLogManager, + weChatWorkLocalizer, + weChatWorkUserFinder, + logger) + { + } + + public override string GrantType => AbpWeChatWorkGlobalConsts.OAuthGrantType; + + protected async override Task FindUserInfoAsync(ExtensionGrantValidationContext context, string code) + { + return await WeChatWorkUserFinder.GetUserInfoAsync(code); + } +}