Browse Source

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.
pull/1618/head
colin 1 month ago
parent
commit
e411bbf2fb
  1. 8
      aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/AbpWeChatWorkGlobalConsts.cs
  2. 4
      aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/IWeChatWorkUserFinder.cs
  3. 34
      aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Models/WeChatWorkCode2Session.cs
  4. 38
      aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Response/WeChatWorkCode2SessionResponse.cs
  5. 14
      aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/Response/WeChatWorkResponseExtensions.cs
  6. 12
      aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/Authorize/WeChatWorkUserFinder.cs
  7. 29
      aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/System/Net/Http/HttpClientWeChatWorkRequestExtensions.MiniProgram.cs
  8. 8
      aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/AbpOpenIddictWeChatWorkModule.cs
  9. 25
      aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkMiniProgramTokenExtensionGrant.cs
  10. 19
      aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkOAuthTokenExtensionGrant.cs
  11. 12
      aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/LINGYUN/Abp/OpenIddict/WeChat/Work/WeChatWorkTokenExtensionGrant.cs
  12. 3
      aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.WeChat.Work/Microsoft/Extensions/DependencyInjection/WeChatWorkOpenIddictServerBuilderExtensions.cs

8
aspnet-core/framework/wechat/LINGYUN.Abp.WeChat.Work/LINGYUN/Abp/WeChat/Work/AbpWeChatWorkGlobalConsts.cs

@ -7,9 +7,13 @@ public class AbpWeChatWorkGlobalConsts
/// </summary>
public static string ProviderName { get; set; } = "WorkWeixin";
/// <summary>
/// 企业微信授权类型
/// 企业微信OAuth授权类型
/// </summary>
public static string GrantType { get; set; } = "wecom";
public static string OAuthGrantType { get; set; } = "wecom_oauth";
/// <summary>
/// 企业微信小程序授权类型
/// </summary>
public static string MiniProgramGrantType { get; set; } = "wecom_mp";
/// <summary>
/// 企业微信个人信息标识
/// </summary>

4
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<WeChatWorkCode2Session> GetCode2SessionAsync(
string code,
CancellationToken cancellationToken = default);
Task<WeChatWorkUserDetail> GetUserDetailAsync(
string userTicket,
CancellationToken cancellationToken = default);

34
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
{
/// <summary>
/// 用户所属企业的corpid
/// </summary>
[NotNull]
[JsonProperty("corpid")]
[JsonPropertyName("corpid")]
public string CorpId { get; set; } = default!;
/// <summary>
/// 用户在企业内的UserID,对应管理端的账号,企业内唯一。注意:第三方小程序此处返回加密的userid
/// </summary>
[NotNull]
[JsonProperty("userid")]
[JsonPropertyName("userid")]
public string UserId { get; set; } = default!;
/// <summary>
/// 会话密钥
/// </summary>
[NotNull]
[JsonProperty("session_key")]
[JsonPropertyName("session_key")]
public string SessionKey { get; set; } = default!;
[CanBeNull]
[JsonProperty("deviceid")]
[JsonPropertyName("deviceid")]
public string? DeviceId { get; set; } = default!;
}

38
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;
/// <summary>
/// 企业微信临时登录凭证校验响应
/// </summary>
[Serializable]
public class WeChatWorkCode2SessionResponse : WeChatWorkResponse
{
/// <summary>
/// 用户所属企业的corpid
/// </summary>
[NotNull]
[JsonProperty("corpid")]
[JsonPropertyName("corpid")]
public string CorpId { get; set; } = default!;
/// <summary>
/// 用户在企业内的UserID,对应管理端的账号,企业内唯一。注意:第三方小程序此处返回加密的userid
/// </summary>
[NotNull]
[JsonProperty("userid")]
[JsonPropertyName("userid")]
public string UserId { get; set; } = default!;
/// <summary>
/// 会话密钥
/// </summary>
[NotNull]
[JsonProperty("session_key")]
[JsonPropertyName("session_key")]
public string SessionKey { get; set; } = default!;
[CanBeNull]
[JsonProperty("deviceid")]
[JsonPropertyName("deviceid")]
public string? DeviceId { get; set; } = default!;
}

14
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)
{

12
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<WeChatWorkCode2Session> 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<WeChatWorkUserDetail> GetUserDetailAsync(
string userTicket,
CancellationToken cancellationToken = default)

29
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<WeChatWorkCode2SessionResponse> 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<WeChatWorkCode2SessionResponse>();
}
}

8
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<AbpOpenIddictExtensionGrantsOptions>(options =>
{
options.Grants.TryAdd(
AbpWeChatWorkGlobalConsts.GrantType,
new WeChatWorkTokenExtensionGrant());
AbpWeChatWorkGlobalConsts.OAuthGrantType,
new WeChatWorkOAuthTokenExtensionGrant());
options.Grants.TryAdd(
AbpWeChatWorkGlobalConsts.MiniProgramGrantType,
new WeChatWorkMiniProgramTokenExtensionGrant());
});
Configure<AbpVirtualFileSystemOptions>(options =>

25
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<WeChatWorkUserInfo> FindUserInfoAsync(ExtensionGrantContext context, string code)
{
var userFinder = GetRequiredService<IWeChatWorkUserFinder>(context);
var code2Session = await userFinder.GetCode2SessionAsync(code);
return new WeChatWorkUserInfo
{
UserId = code2Session.UserId,
UserTicket = code2Session.SessionKey,
};
}
}

19
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<WeChatWorkUserInfo> FindUserInfoAsync(ExtensionGrantContext context, string code)
{
var userFinder = GetRequiredService<IWeChatWorkUserFinder>(context);
return await userFinder.GetUserInfoAsync(code);
}
}

12
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<WeChatWorkUserInfo> FindUserInfoAsync(ExtensionGrantContext context, string code);
public async virtual Task<IActionResult> HandleAsync(ExtensionGrantContext context)
{
@ -58,11 +60,9 @@ public class WeChatWorkTokenExtensionGrant : ITokenExtensionGrant
return Forbid(properties, OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
}
var userFinder = GetRequiredService<IWeChatWorkUserFinder>(context);
try
{
var userInfo = await userFinder.GetUserInfoAsync(code);
var userInfo = await FindUserInfoAsync(context, code);
var userManager = GetRequiredService<IdentityUserManager>(context);
var currentUser = await userManager.FindByLoginAsync(AbpWeChatWorkGlobalConsts.ProviderName, userInfo.UserId);

3
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)

Loading…
Cancel
Save