Browse Source
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
12 changed files with 195 additions and 11 deletions
@ -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!; |
|||
} |
|||
@ -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!; |
|||
} |
|||
@ -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>(); |
|||
} |
|||
} |
|||
@ -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, |
|||
}; |
|||
} |
|||
} |
|||
@ -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); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue