Files
tnb.server/common/Tnb.CollectiveOAuth/Request/AuthRequests/WeChatOpenAuthRequest.cs
2023-03-13 15:00:34 +08:00

168 lines
5.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using JNPF.Extras.CollectiveOAuth.Config;
using JNPF.Extras.CollectiveOAuth.Models;
using JNPF.Extras.CollectiveOAuth.Utils;
using JNPF.Extras.CollectiveOAuth.Enums;
using JNPF.Extras.CollectiveOAuth.Cache;
namespace JNPF.Extras.CollectiveOAuth.Request;
public partial class WeChatOpenAuthRequest : DefaultAuthRequest
{
public WeChatOpenAuthRequest(ClientConfig config) : base(config, new WechatOpenAuthSource())
{
}
public WeChatOpenAuthRequest(ClientConfig config, IAuthStateCache authStateCache)
: base(config, new WechatOpenAuthSource(), authStateCache)
{
}
/**
* 微信的特殊性,此时返回的信息同时包含 openid 和 access_token
*
* @param authCallback 回调返回的参数
* @return 所有信息
*/
protected override AuthToken getAccessToken(AuthCallback authCallback)
{
return this.getToken(accessTokenUrl(authCallback.code));
}
protected override AuthUser getUserInfo(AuthToken authToken)
{
string openId = authToken.openId;
string response = doGetUserInfo(authToken);
var jsonObj = response.parseObject();
this.checkResponse(jsonObj);
//string location = String.format("%s-%s-%s", object.getString("country"), object.getString("province"), object.getString("city"));
string location = $"{jsonObj.getString("country")}-{jsonObj.getString("province")}-{jsonObj.getString("city")}";
if (jsonObj.ContainsKey("unionid"))
{
authToken.unionId = jsonObj.getString("unionid");
}
var authUser = new AuthUser();
authUser.username = jsonObj.getString("nickname");
authUser.nickname = jsonObj.getString("nickname");
authUser.avatar = jsonObj.getString("headimgurl");
authUser.location = location;
authUser.uuid = openId;
authUser.gender = GlobalAuthUtil.getWechatRealGender(jsonObj.getString("sex"));
authUser.token = authToken;
authUser.source = source.getName();
authUser.originalUser = jsonObj;
authUser.originalUserStr = response;
return authUser;
}
public override AuthResponse refresh(AuthToken oldToken)
{
return new AuthResponse(Convert.ToInt32(AuthResponseStatus.SUCCESS), null, this.getToken(refreshTokenUrl(oldToken.refreshToken)));
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private void checkResponse(Dictionary<string, object> dic)
{
if (dic.ContainsKey("errcode"))
{
throw new Exception($"errcode: {dic.getString("errcode")}, errmsg: {dic.getString("errmsg")}");
}
}
/**
* 获取token适用于获取access_token和刷新token
*
* @param accessTokenUrl 实际请求token的地址
* @return token对象
*/
private AuthToken getToken(string accessTokenUrl)
{
string response = HttpUtils.RequestGet(accessTokenUrl);
var accessTokenObject = response.parseObject();
this.checkResponse(accessTokenObject);
var authToken = new AuthToken();
authToken.accessToken = accessTokenObject.getString("access_token");
authToken.refreshToken = accessTokenObject.getString("refresh_token");
authToken.expireIn = accessTokenObject.getInt32("expires_in");
authToken.openId = accessTokenObject.getString("openid");
return authToken;
}
/**
* 返回带{@code state}参数的授权url授权回调时会带上这个{@code state}
*
* @param state state 验证授权流程的参数可以防止csrf
* @return 返回授权地址
* @since 1.9.3
*/
public override string authorize(string state)
{
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("appid", config.clientId)
.queryParam("redirect_uri", GlobalAuthUtil.urlEncode(config.redirectUri))
.queryParam("response_type", "code")
.queryParam("scope", config.scope.IsNullOrWhiteSpace() ? "snsapi_login" : config.scope)
.queryParam("state", getRealState(state))
.build();
}
/**
* 返回获取accessToken的url
*
* @param code 授权码
* @return 返回获取accessToken的url
*/
protected override string accessTokenUrl(string code)
{
return UrlBuilder.fromBaseUrl(source.accessToken())
.queryParam("appid", config.clientId)
.queryParam("secret", config.clientSecret)
.queryParam("code", code)
.queryParam("grant_type", "authorization_code")
.build();
}
/**
* 返回获取userInfo的url
*
* @param authToken 用户授权后的token
* @return 返回获取userInfo的url
*/
protected override string userInfoUrl(AuthToken authToken)
{
return UrlBuilder.fromBaseUrl(source.userInfo())
.queryParam("access_token", authToken.accessToken)
.queryParam("openid", authToken.openId)
.queryParam("lang", "zh_CN")
.build();
}
/**
* 返回获取userInfo的url
*
* @param refreshToken getAccessToken方法返回的refreshToken
* @return 返回获取userInfo的url
*/
protected override string refreshTokenUrl(string refreshToken)
{
return UrlBuilder.fromBaseUrl(source.refresh())
.queryParam("appid", config.clientId)
.queryParam("grant_type", "refresh_token")
.queryParam("refresh_token", refreshToken)
.build();
}
}