feat: 优化第三方登录结构

This commit is contained in:
陈淳
2024-01-08 11:17:05 +08:00
parent ecb4240d8d
commit 8d0deaebba
12 changed files with 69 additions and 95 deletions

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.AspNetCore.Authentication.OAuth
{
public class AuthenticationConstants
{
public const string OpenId = "urn:openid";
public const string AccessToken = "urn:access_token";
public const string Name = "urn:name";
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.OAuth;
namespace Yi.Framework.AspNetCore.Authentication.OAuth
{
public class AuthenticationOAuthOptions:OAuthOptions
{
public string RedirectUri { get; set; }
}
}

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.AspNetCore.Authentication.OAuth.Gitee;
namespace Yi.Framework.AspNetCore.Authentication.OAuth
{
public class AuthticationErrCodeModel
{
public string error { get; set; }
public string error_description { get; set; }
public static void VerifyErrResponse(string content)
{
var model = Newtonsoft.Json.JsonConvert.DeserializeObject<AuthticationErrCodeModel>(content);
if (model.error != null)
{
throw new Exception($"第三方授权返回错误,错误码:【{model.error}】,错误详情:【{model.error_description}】");
}
}
}
}

View File

@@ -15,9 +15,5 @@ public static class GiteeAuthenticationConstants
{ {
public const string Url = "urn:gitee:url"; public const string Url = "urn:gitee:url";
public const string AvatarUrl = "urn:gitee:avatarUrl"; public const string AvatarUrl = "urn:gitee:avatarUrl";
public const string OpenId = "urn:openid";
public const string AccessToken = "urn:access_token";
public const string Name = "urn:name";
} }
} }

View File

@@ -2,7 +2,6 @@
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Yi.Framework.AspNetCore.Authentication.OAuth.QQ;
using static Yi.Framework.AspNetCore.Authentication.OAuth.Gitee.GiteeAuthenticationConstants; using static Yi.Framework.AspNetCore.Authentication.OAuth.Gitee.GiteeAuthenticationConstants;
namespace Yi.Framework.AspNetCore.Authentication.OAuth.Gitee namespace Yi.Framework.AspNetCore.Authentication.OAuth.Gitee
@@ -40,16 +39,11 @@ namespace Yi.Framework.AspNetCore.Authentication.OAuth.Gitee
new Claim(Claims.AvatarUrl, userInfoMdoel.avatar_url), new Claim(Claims.AvatarUrl, userInfoMdoel.avatar_url),
new Claim(Claims.Url, userInfoMdoel.url), new Claim(Claims.Url, userInfoMdoel.url),
new Claim(Claims.OpenId,userInfoMdoel.id.ToString()), new Claim(AuthenticationConstants.OpenId,userInfoMdoel.id.ToString()),
new Claim(Claims.Name, userInfoMdoel.name), new Claim(AuthenticationConstants.Name, userInfoMdoel.name),
new Claim(Claims.AccessToken, tokenModel.access_token) new Claim(AuthenticationConstants.AccessToken, tokenModel.access_token)
}; };
return claims; return claims;
} }
protected override void VerifyErrResponse(string content)
{
GiteeAuthticationErrCodeModel.VerifyErrResponse(content);
}
} }
} }

View File

@@ -6,7 +6,6 @@
using System.Security.Claims; using System.Security.Claims;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OAuth;
using static Yi.Framework.AspNetCore.Authentication.OAuth.Gitee.GiteeAuthenticationConstants; using static Yi.Framework.AspNetCore.Authentication.OAuth.Gitee.GiteeAuthenticationConstants;
namespace Yi.Framework.AspNetCore.Authentication.OAuth.Gitee; namespace Yi.Framework.AspNetCore.Authentication.OAuth.Gitee;
@@ -14,7 +13,7 @@ namespace Yi.Framework.AspNetCore.Authentication.OAuth.Gitee;
/// <summary> /// <summary>
/// Defines a set of options used by <see cref="GiteeAuthenticationHandler"/>. /// Defines a set of options used by <see cref="GiteeAuthenticationHandler"/>.
/// </summary> /// </summary>
public class GiteeAuthenticationOptions : OAuthOptions public class GiteeAuthenticationOptions : AuthenticationOAuthOptions
{ {
public GiteeAuthenticationOptions() public GiteeAuthenticationOptions()
{ {
@@ -33,7 +32,7 @@ public class GiteeAuthenticationOptions : OAuthOptions
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id"); ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
ClaimActions.MapJsonKey(ClaimTypes.Name, "login"); ClaimActions.MapJsonKey(ClaimTypes.Name, "login");
ClaimActions.MapJsonKey(ClaimTypes.Email, "email"); ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
ClaimActions.MapJsonKey(Claims.Name, "name"); ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
ClaimActions.MapJsonKey(Claims.Url, "url"); ClaimActions.MapJsonKey(Claims.Url, "url");
} }
@@ -42,6 +41,4 @@ public class GiteeAuthenticationOptions : OAuthOptions
/// the email addresses associated with the logged in user. /// the email addresses associated with the logged in user.
/// </summary> /// </summary>
public string UserEmailsEndpoint { get; set; } public string UserEmailsEndpoint { get; set; }
}
public string RedirectUri { get; set; }
}

View File

@@ -1,30 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace Yi.Framework.AspNetCore.Authentication.OAuth.Gitee
{
public class GiteeAuthticationErrCodeModel
{
public string error { get; set; }
public string error_description { get; set; }
public static void VerifyErrResponse(string content)
{
var model =Newtonsoft.Json.JsonConvert.DeserializeObject <GiteeAuthticationErrCodeModel>(content);
if (model.error != null)
{
throw new Exception($"第三方授权返回错误,错误码:【{model.error}】,错误详情:【{model.error_description}】");
}
}
}
}

View File

@@ -63,7 +63,7 @@ namespace Yi.Framework.AspNetCore.Authentication.OAuth
protected virtual void VerifyErrResponse(string content) protected virtual void VerifyErrResponse(string content)
{ {
return; AuthticationErrCodeModel.VerifyErrResponse(content);
} }
protected abstract Task<List<Claim>> GetAuthTicketAsync(string code); protected abstract Task<List<Claim>> GetAuthTicketAsync(string code);

View File

@@ -19,9 +19,5 @@ public static class QQAuthenticationConstants
public const string PictureMediumUrl = "urn:qq:picture_medium"; public const string PictureMediumUrl = "urn:qq:picture_medium";
public const string PictureUrl = "urn:qq:picture"; public const string PictureUrl = "urn:qq:picture";
public const string UnionId = "urn:qq:unionid"; public const string UnionId = "urn:qq:unionid";
public const string OpenId = "urn:openid";
public const string AccessToken = "urn:access_token";
public const string Name = "urn:name";
} }
} }

View File

@@ -14,11 +14,6 @@ namespace Yi.Framework.AspNetCore.Authentication.OAuth.QQ
public override string AuthenticationSchemeNmae => QQAuthenticationDefaults.AuthenticationScheme; public override string AuthenticationSchemeNmae => QQAuthenticationDefaults.AuthenticationScheme;
protected override void VerifyErrResponse(string content)
{
QQAuthticationErrCodeModel.VerifyErrResponse(content);
}
protected override async Task<List<Claim>> GetAuthTicketAsync(string code) protected override async Task<List<Claim>> GetAuthTicketAsync(string code)
{ {
@@ -58,9 +53,9 @@ namespace Yi.Framework.AspNetCore.Authentication.OAuth.QQ
new Claim(Claims.PictureMediumUrl, userInfoMdoel.figureurl_qq_1), new Claim(Claims.PictureMediumUrl, userInfoMdoel.figureurl_qq_1),
new Claim(Claims.PictureUrl, userInfoMdoel.figureurl), new Claim(Claims.PictureUrl, userInfoMdoel.figureurl),
new Claim(Claims.OpenId, tokenModel.openid), new Claim(AuthenticationConstants.OpenId, tokenModel.openid),
new Claim(Claims.Name, userInfoMdoel.nickname), new Claim(AuthenticationConstants.Name, userInfoMdoel.nickname),
new Claim(Claims.AccessToken, tokenModel.access_token), new Claim(AuthenticationConstants.AccessToken, tokenModel.access_token),
}; };
return claims; return claims;

View File

@@ -6,7 +6,6 @@
using System.Security.Claims; using System.Security.Claims;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OAuth;
using static Yi.Framework.AspNetCore.Authentication.OAuth.QQ.QQAuthenticationConstants; using static Yi.Framework.AspNetCore.Authentication.OAuth.QQ.QQAuthenticationConstants;
namespace Yi.Framework.AspNetCore.Authentication.OAuth.QQ; namespace Yi.Framework.AspNetCore.Authentication.OAuth.QQ;
@@ -14,7 +13,7 @@ namespace Yi.Framework.AspNetCore.Authentication.OAuth.QQ;
/// <summary> /// <summary>
/// Defines a set of options used by <see cref="QQAuthenticationHandler"/>. /// Defines a set of options used by <see cref="QQAuthenticationHandler"/>.
/// </summary> /// </summary>
public class QQAuthenticationOptions : OAuthOptions public class QQAuthenticationOptions : AuthenticationOAuthOptions
{ {
public QQAuthenticationOptions() public QQAuthenticationOptions()
{ {
@@ -47,5 +46,4 @@ public class QQAuthenticationOptions : OAuthOptions
/// </summary> /// </summary>
public string UserIdentificationEndpoint { get; set; } public string UserIdentificationEndpoint { get; set; }
public string RedirectUri { get; set; }
} }

View File

@@ -1,30 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace Yi.Framework.AspNetCore.Authentication.OAuth.QQ
{
public class QQAuthticationErrCodeModel
{
public string error { get; set; }
public string error_description { get; set; }
public static void VerifyErrResponse(string content)
{
var model =Newtonsoft.Json.JsonConvert.DeserializeObject <QQAuthticationErrCodeModel>(content);
if (model.error != null)
{
throw new Exception($"第三方授权返回错误,错误码:【{model.error}】,错误详情:【{model.error_description}】");
}
}
}
}