Browse Source

Merge pull request #1618 from colinin/openiddict-wechat-work

feat(openiddict): Split the WeChat Work token grant into OAuth and mini program flows
pull/1626/head
i2nets 1 month ago
committed by GitHub
parent
commit
8bdb641abb
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  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> /// </summary>
public static string ProviderName { get; set; } = "WorkWeixin"; public static string ProviderName { get; set; } = "WorkWeixin";
/// <summary> /// <summary>
/// 企业微信授权类型 /// 企业微信OAuth授权类型
/// </summary> /// </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>
/// 企业微信个人信息标识 /// 企业微信个人信息标识
/// </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, string code,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default);
Task<WeChatWorkCode2Session> GetCode2SessionAsync(
string code,
CancellationToken cancellationToken = default);
Task<WeChatWorkUserDetail> GetUserDetailAsync( Task<WeChatWorkUserDetail> GetUserDetailAsync(
string userTicket, string userTicket,
CancellationToken cancellationToken = default); 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( public static WeChatWorkUserDetail ToUserDetail(
this WeChatWorkUserDetailResponse response) 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(); 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( public async virtual Task<WeChatWorkUserDetail> GetUserDetailAsync(
string userTicket, string userTicket,
CancellationToken cancellationToken = default) 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 => Configure<AbpOpenIddictExtensionGrantsOptions>(options =>
{ {
options.Grants.TryAdd( options.Grants.TryAdd(
AbpWeChatWorkGlobalConsts.GrantType, AbpWeChatWorkGlobalConsts.OAuthGrantType,
new WeChatWorkTokenExtensionGrant()); new WeChatWorkOAuthTokenExtensionGrant());
options.Grants.TryAdd(
AbpWeChatWorkGlobalConsts.MiniProgramGrantType,
new WeChatWorkMiniProgramTokenExtensionGrant());
}); });
Configure<AbpVirtualFileSystemOptions>(options => 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;
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.Security.Claims;
using LINGYUN.Abp.WeChat.Work.Settings; using LINGYUN.Abp.WeChat.Work.Settings;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
@ -28,9 +28,11 @@ using SignInResult = Microsoft.AspNetCore.Mvc.SignInResult;
namespace LINGYUN.Abp.OpenIddict.WeChat.Work; 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) public async virtual Task<IActionResult> HandleAsync(ExtensionGrantContext context)
{ {
@ -58,11 +60,9 @@ public class WeChatWorkTokenExtensionGrant : ITokenExtensionGrant
return Forbid(properties, OpenIddictServerAspNetCoreDefaults.AuthenticationScheme); return Forbid(properties, OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
} }
var userFinder = GetRequiredService<IWeChatWorkUserFinder>(context);
try try
{ {
var userInfo = await userFinder.GetUserInfoAsync(code); var userInfo = await FindUserInfoAsync(context, code);
var userManager = GetRequiredService<IdentityUserManager>(context); var userManager = GetRequiredService<IdentityUserManager>(context);
var currentUser = await userManager.FindByLoginAsync(AbpWeChatWorkGlobalConsts.ProviderName, userInfo.UserId); 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) public static OpenIddictServerBuilder AllowWeChatWorkFlow(this OpenIddictServerBuilder builder)
{ {
return builder return builder
.AllowCustomFlow(AbpWeChatWorkGlobalConsts.GrantType); .AllowCustomFlow(AbpWeChatWorkGlobalConsts.OAuthGrantType)
.AllowCustomFlow(AbpWeChatWorkGlobalConsts.MiniProgramGrantType);
} }
public static OpenIddictServerBuilder RegisterWeChatWorkScopes(this OpenIddictServerBuilder builder) public static OpenIddictServerBuilder RegisterWeChatWorkScopes(this OpenIddictServerBuilder builder)

Loading…
Cancel
Save