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

136 lines
4.6 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.Cache;
using JNPF.Extras.CollectiveOAuth.Config;
using JNPF.Extras.CollectiveOAuth.Models;
using JNPF.Extras.CollectiveOAuth.Utils;
using JNPF.Extras.CollectiveOAuth.Enums;
namespace JNPF.Extras.CollectiveOAuth.Request;
public class ToutiaoAuthRequest : DefaultAuthRequest
{
public ToutiaoAuthRequest(ClientConfig config) : base(config, new ToutiaoAuthSource())
{
}
public ToutiaoAuthRequest(ClientConfig config, IAuthStateCache authStateCache)
: base(config, new ToutiaoAuthSource(), authStateCache)
{
}
protected override AuthToken getAccessToken(AuthCallback authCallback)
{
var response = doGetAuthorizationCode(authCallback.code);
var accessTokenObject = response.parseObject();
this.checkResponse(accessTokenObject);
var authToken = new AuthToken();
authToken.accessToken = accessTokenObject.getString("access_token");
authToken.expireIn = accessTokenObject.getInt32("expires_in");
authToken.openId = accessTokenObject.getString("open_id");
authToken.code = authCallback.code;
return authToken;
}
protected override AuthUser getUserInfo(AuthToken authToken)
{
string userResponse = doGetUserInfo(authToken);
var userProfile = userResponse.parseObject();
this.checkResponse(userProfile);
var userObj = userProfile.getString("data").parseObject();
bool isAnonymousUser = userObj.getInt32("uid_type") == 14;
string anonymousUserName = "匿名用户";
var authUser = new AuthUser();
authUser.uuid = userObj.getString("uid");
authUser.username = isAnonymousUser ? anonymousUserName : userObj.getString("screen_name");
authUser.nickname = isAnonymousUser ? anonymousUserName : userObj.getString("screen_name");
authUser.avatar = userObj.getString("avatar_url");
authUser.remark = userObj.getString("description");
authUser.gender = GlobalAuthUtil.getRealGender(userObj.getString("gender"));
authUser.token = authToken;
authUser.source = source.getName();
authUser.originalUser = userProfile;
authUser.originalUserStr = userResponse;
return authUser;
}
/**
* 返回带{@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("response_type", "code")
.queryParam("client_key", config.clientId)
.queryParam("redirect_uri", config.redirectUri)
.queryParam("auth_only", 1)
.queryParam("display", 0)
.queryParam("state", getRealState(state))
.build();
}
/**
* 返回获取accessToken的url
*
* @param code 授权码
* @return 返回获取accessToken的url
*/
protected override string accessTokenUrl(string code)
{
return UrlBuilder.fromBaseUrl(source.accessToken())
.queryParam("code", code)
.queryParam("client_key", config.clientId)
.queryParam("client_secret", config.clientSecret)
.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("client_key", config.clientId)
.queryParam("access_token", authToken.accessToken)
.build();
}
/**
* 校验请求结果
*
* @param response 请求结果
* @return 如果请求结果正常则返回Exception
*/
private void checkResponse(Dictionary<string, object> dic)
{
if (dic.ContainsKey("error_code"))
{
throw new Exception(getToutiaoErrorCode(dic.getInt32("error_code")).GetDesc());
}
}
private AuthToutiaoErrorCode getToutiaoErrorCode(int errorCode)
{
var enumObjects = typeof(AuthToutiaoErrorCode).ToList();
var codeEnum = enumObjects.Where(a => a.ID == errorCode).ToList();
if (codeEnum.Count > 0)
{
return GlobalAuthUtil.enumFromString<AuthToutiaoErrorCode>(codeEnum[0].Name);
}
else
{
return AuthToutiaoErrorCode.EC999;
}
}
}