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

176 lines
6.0 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;
using Newtonsoft.Json;
namespace JNPF.Extras.CollectiveOAuth.Request;
public class MicrosoftAuthRequest : DefaultAuthRequest
{
public MicrosoftAuthRequest(ClientConfig config) : base(config, new MicrosoftAuthSource())
{
}
public MicrosoftAuthRequest(ClientConfig config, IAuthStateCache authStateCache)
: base(config, new MicrosoftAuthSource(), authStateCache)
{
}
protected override AuthToken getAccessToken(AuthCallback authCallback)
{
return getToken(accessTokenUrl(authCallback.code));
}
/**
* 获取token适用于获取access_token和刷新token
*
* @param accessTokenUrl 实际请求token的地址
* @return token对象
*/
private AuthToken getToken(string accessTokenUrl)
{
var reqParams = new Dictionary<string, object>
{
{ "Host", "https://login.microsoftonline.com" },
{ "Content-Type", "application/x-www-form-urlencoded" },
};
var reqParamDic = GlobalAuthUtil.parseUrlObject(accessTokenUrl);
var response = HttpUtils.RequestPost(accessTokenUrl, JsonConvert.SerializeObject(reqParamDic), reqParams);
var accessTokenObject = response.parseObject();
this.checkResponse(accessTokenObject);
var authToken = new AuthToken();
authToken.accessToken = accessTokenObject.getString("access_token");
authToken.tokenType = accessTokenObject.getString("token_type");
authToken.expireIn = accessTokenObject.getInt32("expires_in");
authToken.refreshToken = accessTokenObject.getString("refresh_token");
authToken.scope = accessTokenObject.getString("scope");
return authToken;
}
protected override AuthUser getUserInfo(AuthToken authToken)
{
var token = authToken.accessToken;
var tokenType = authToken.tokenType;
var jwt = tokenType + " " + token;
var reqParams = new Dictionary<string, object>
{
{ "Authorization", jwt },
};
var response = HttpUtils.RequestGet(userInfoUrl(authToken), reqParams);
var userObj = response.parseObject();
this.checkResponse(userObj);
var authUser = new AuthUser();
authUser.uuid = userObj.getString("id");
authUser.username = userObj.getString("userPrincipalName");
authUser.nickname = userObj.getString("displayName");
authUser.location = userObj.getString("officeLocation");
authUser.email = userObj.getString("mail");
authUser.gender = AuthUserGender.UNKNOWN;
authUser.token = authToken;
authUser.source = source.getName();
authUser.originalUser = userObj;
authUser.originalUserStr = response;
return authUser;
}
/**
* 刷新access token (续期)
*
* @param authToken 登录成功后返回的Token信息
* @return AuthResponse
*/
public override AuthResponse refresh(AuthToken authToken)
{
var token = getToken(refreshTokenUrl(authToken.refreshToken));
return new AuthResponse(AuthResponseStatus.SUCCESS.GetCode(), AuthResponseStatus.SUCCESS.GetDesc(), token);
}
/**
* 返回带{@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_id", config.clientId)
.queryParam("redirect_uri", config.redirectUri)
.queryParam("response_mode", "query")
.queryParam("scope", "offline_access%20" + (config.scope.IsNullOrWhiteSpace() ? "user.read%20mail.read" : config.scope))
.queryParam("state", getRealState(state))
.build();
}
/**
* 返回获取accessToken的url
*
* @param code 授权code
* @return 返回获取accessToken的url
*/
protected override string accessTokenUrl(string code)
{
return UrlBuilder.fromBaseUrl(source.accessToken())
.queryParam("code", code)
.queryParam("client_id", config.clientId)
.queryParam("client_secret", config.clientSecret)
.queryParam("grant_type", "authorization_code")
.queryParam("scope", config.scope.IsNullOrWhiteSpace() ? "user.read%20mail.read" : config.scope)
.queryParam("redirect_uri", config.redirectUri)
.build();
}
/**
* 返回获取userInfo的url
*
* @param authToken 用户授权后的token
* @return 返回获取userInfo的url
*/
protected override string userInfoUrl(AuthToken authToken)
{
return UrlBuilder.fromBaseUrl(source.userInfo()).build();
}
/**
* 返回获取accessToken的url
*
* @param refreshToken 用户授权后的token
* @return 返回获取accessToken的url
*/
protected override string refreshTokenUrl(string refreshToken)
{
return UrlBuilder.fromBaseUrl(source.refresh())
.queryParam("client_id", config.clientId)
.queryParam("client_secret", config.clientSecret)
.queryParam("refresh_token", refreshToken)
.queryParam("grant_type", "refresh_token")
.queryParam("scope", config.scope.IsNullOrWhiteSpace() ? "user.read%20mail.read" : config.scope)
.queryParam("redirect_uri", config.redirectUri)
.build();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private void checkResponse(Dictionary<string, object> dic)
{
if (dic.ContainsKey("error"))
{
throw new Exception(dic.getString("error_description"));
}
}
}