From 97be2c0effecc9ee59dcb17f3aad845101e763b9 Mon Sep 17 00:00:00 2001 From: Argo Date: Wed, 19 Feb 2020 12:12:48 +0800 Subject: [PATCH] =?UTF-8?q?!70=20=E5=A2=9E=E5=8A=A0=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0=E6=94=AF=E4=BB=98=E5=AE=9D=E7=99=BB?= =?UTF-8?q?=E5=BD=95=20*=20build:=20Alipay=20=E4=BD=BF=E7=94=A8=E6=AD=A3?= =?UTF-8?q?=E5=BC=8F=E7=89=88=E7=BB=84=E4=BB=B6=20*=20Revert=20"revert:=20?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=20Alipay=20=E7=9B=B8=E5=85=B3=E4=BB=A3?= =?UTF-8?q?=E7=A0=81"=20*=20revert:=20=E7=A7=BB=E9=99=A4=20Alipay=20?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81=20*=20feat:=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20Alipay=20=E5=8A=9F=E8=83=BD=20*=20fix:=20=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E5=BE=AE=E4=BF=A1=E7=99=BB=E5=BD=95=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D=E7=A7=B0=E9=94=99=E8=AF=AF=20*=20re?= =?UTF-8?q?factor:=20=E6=9B=B4=E6=96=B0=E6=94=AF=E4=BB=98=E5=AE=9D?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=8C=89=E9=92=AE=E6=8F=90=E7=A4=BA=E6=96=87?= =?UTF-8?q?=E5=AD=97=20*=20feat:=20=E5=A2=9E=E5=8A=A0=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E5=AE=9D=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/AccountController.cs | 12 +++ src/admin/Bootstrap.Admin/Startup.cs | 6 +- .../Views/Account/Login.cshtml | 4 +- .../appsettings.Development.json | 11 +++ src/admin/Bootstrap.Admin/appsettings.json | 11 +++ .../Bootstrap.DataAccess.MongoDB.csproj | 2 +- .../Bootstrap.DataAccess.csproj | 9 ++- .../Helper/OAuthHelper.cs | 49 +++-------- .../Helper/WeChatHelper.cs | 81 ------------------- ...Bootstrap.Client.DataAccess.MongoDB.csproj | 2 +- .../Bootstrap.Client.DataAccess.csproj | 4 +- test/UnitTest/UnitTest.csproj | 2 +- 12 files changed, 61 insertions(+), 132 deletions(-) delete mode 100644 src/admin/Bootstrap.DataAccess/Helper/WeChatHelper.cs diff --git a/src/admin/Bootstrap.Admin/Controllers/AccountController.cs b/src/admin/Bootstrap.Admin/Controllers/AccountController.cs index a35c6d0f..739c784f 100644 --- a/src/admin/Bootstrap.Admin/Controllers/AccountController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/AccountController.cs @@ -1,6 +1,7 @@ using Bootstrap.Admin.Models; using Bootstrap.DataAccess; using Bootstrap.Security.Mvc; +using Longbow.AlipayAuth; using Longbow.GiteeAuth; using Longbow.GitHubAuth; using Longbow.TencentAuth; @@ -219,6 +220,17 @@ namespace Bootstrap.Admin.Controllers return Challenge(enabled ? TencentDefaults.AuthenticationScheme : CookieAuthenticationDefaults.AuthenticationScheme); } + /// + /// Alipay 认证 + /// + /// + [HttpGet] + public IActionResult Alipay([FromServices]IConfiguration config) + { + var enabled = config.GetValue($"{nameof(AlipayOptions)}:Enabled", false); + return Challenge(enabled ? AlipayDefaults.AuthenticationScheme : CookieAuthenticationDefaults.AuthenticationScheme); + } + /// /// WeChat 认证 /// diff --git a/src/admin/Bootstrap.Admin/Startup.cs b/src/admin/Bootstrap.Admin/Startup.cs index ea31d335..63c60422 100644 --- a/src/admin/Bootstrap.Admin/Startup.cs +++ b/src/admin/Bootstrap.Admin/Startup.cs @@ -59,7 +59,11 @@ namespace Bootstrap.Admin services.AddOnlineUsers(); services.AddSignalR().AddJsonProtocol(op => op.PayloadSerializerOptions.AddDefaultConverters()); services.AddSignalRExceptionFilterHandler(async (client, ex) => await client.SendMessageBody(ex).ConfigureAwait(false)); - services.AddBootstrapAdminAuthentication(Configuration).AddGitee(OAuthHelper.Configure).AddGitHub(OAuthHelper.Configure).AddTencent(OAuthHelper.Configure); + services.AddBootstrapAdminAuthentication(Configuration) + .AddGitee(OAuthHelper.Configure) + .AddGitHub(OAuthHelper.Configure) + .AddTencent(OAuthHelper.Configure) + .AddAlipay(OAuthHelper.Configure); services.AddAuthorization(options => options.DefaultPolicy = new AuthorizationPolicyBuilder().RequireBootstrapAdminAuthorizate().Build()); services.AddButtonAuthorization(MenuHelper.AuthorizateButtons); services.AddBootstrapAdminBackgroundTask(); diff --git a/src/admin/Bootstrap.Admin/Views/Account/Login.cshtml b/src/admin/Bootstrap.Admin/Views/Account/Login.cshtml index ec70fbc7..ddfff204 100644 --- a/src/admin/Bootstrap.Admin/Views/Account/Login.cshtml +++ b/src/admin/Bootstrap.Admin/Views/Account/Login.cshtml @@ -135,8 +135,8 @@ diff --git a/src/admin/Bootstrap.Admin/appsettings.Development.json b/src/admin/Bootstrap.Admin/appsettings.Development.json index 4c2a11c3..bb425b49 100644 --- a/src/admin/Bootstrap.Admin/appsettings.Development.json +++ b/src/admin/Bootstrap.Admin/appsettings.Development.json @@ -128,6 +128,17 @@ "HomePath": "/Admin/Profiles", "App": "Demo" }, + "AlipayOptions": { + "Enabled": true, + "ClientId": "", + "ClientSecret": "", + "CallbackPath": "/signin-alipay", + "HomePath": "/Admin/Profiles", + "SignKey": "", + "Scope": [ "auth_user" ], + "Roles": [ "Administrators" ], + "App": "Demo" + }, "TencentSMSOptions": { "AppId": "", "AppKey": "", diff --git a/src/admin/Bootstrap.Admin/appsettings.json b/src/admin/Bootstrap.Admin/appsettings.json index 28a24d44..26a79b33 100644 --- a/src/admin/Bootstrap.Admin/appsettings.json +++ b/src/admin/Bootstrap.Admin/appsettings.json @@ -117,6 +117,17 @@ "Roles": [ "Administrators" ], "App": "Demo" }, + "AlipayOptions": { + "Enabled": true, + "ClientId": "", + "ClientSecret": "", + "CallbackPath": "/signin-alipay", + "HomePath": "/Admin/Profiles", + "SignKey": "", + "Scope": [ "auth_user" ], + "Roles": [ "Administrators" ], + "App": "Demo" + }, "SMSOptions": { "CompanyCode": "", "MD5Key": "MD5Key", diff --git a/src/admin/Bootstrap.DataAccess.MongoDB/Bootstrap.DataAccess.MongoDB.csproj b/src/admin/Bootstrap.DataAccess.MongoDB/Bootstrap.DataAccess.MongoDB.csproj index d42e75ad..335a1718 100644 --- a/src/admin/Bootstrap.DataAccess.MongoDB/Bootstrap.DataAccess.MongoDB.csproj +++ b/src/admin/Bootstrap.DataAccess.MongoDB/Bootstrap.DataAccess.MongoDB.csproj @@ -6,7 +6,7 @@ - + diff --git a/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj b/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj index b7bed318..3f5cc3ff 100644 --- a/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj +++ b/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj @@ -8,20 +8,21 @@ + - + - + - + - + diff --git a/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs b/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs index 53525334..d393c4d6 100644 --- a/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs +++ b/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs @@ -1,6 +1,8 @@ -using Longbow.OAuth; +using Longbow.AlipayAuth; +using Longbow.OAuth; using Longbow.Security.Cryptography; using Longbow.TencentAuth; +using Longbow.WeChatAuth; using Microsoft.AspNetCore.Authentication.OAuth; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Configuration; @@ -51,43 +53,6 @@ namespace Bootstrap.DataAccess }; } - private static T? ToObject(this System.Text.Json.JsonElement element) where T : OAuthUser - { - var user = new OAuthUser(); - var target = element.EnumerateObject(); - user.Id = target.TryGetValue("Id"); - user.Login = target.TryGetValue("Login"); - user.Name = target.TryGetValue("Name"); - user.Avatar_Url = target.TryGetValue("Avatar_Url"); - return user as T; - } - - private static OAuthUser? ToTencentUser(this System.Text.Json.JsonElement element) - { - var target = element.EnumerateObject(); - var ret = target.TryGetValue("ret"); - OAuthUser? user = null; - if (ret == "0") - { - user = new OAuthUser - { - Id = target.TryGetValue("Id"), - Login = target.TryGetValue("nickname"), - Name = target.TryGetValue("nickname"), - Avatar_Url = target.TryGetValue("figureurl_qq_2") - }; - } - return user; - } - - private static string TryGetValue(this System.Text.Json.JsonElement.ObjectEnumerator target, string propertyName) - { - var ret = string.Empty; - var property = target.FirstOrDefault(t => t.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase)); - ret = property.Value.ToString(); - return ret; - } - /// /// 插入 Gitee 授权用户到数据库中 /// @@ -95,7 +60,13 @@ namespace Bootstrap.DataAccess /// private static User ParseUser(OAuthCreatingTicketContext context) { - var user = context.Scheme.DisplayName == TencentDefaults.DisplayName ? context.User.ToTencentUser() : context.User.ToObject(); + var user = context.Scheme.DisplayName switch + { + WeChatDefaults.DisplayName => context.User.ToWeChatUser(), + TencentDefaults.DisplayName => context.User.ToTencentUser(), + AlipayDefaults.DisplayName => context.User.ToAlipayUser(), + _ => context.User.ToAuthUser() + }; return new User() { ApprovedBy = "OAuth", diff --git a/src/admin/Bootstrap.DataAccess/Helper/WeChatHelper.cs b/src/admin/Bootstrap.DataAccess/Helper/WeChatHelper.cs deleted file mode 100644 index fac236da..00000000 --- a/src/admin/Bootstrap.DataAccess/Helper/WeChatHelper.cs +++ /dev/null @@ -1,81 +0,0 @@ -using Longbow.OAuth; -using Longbow.Security.Cryptography; -using Longbow.WeChatAuth; -using Microsoft.AspNetCore.Authentication.OAuth; -using System; -using System.Linq; -using System.Text.Json; - -namespace Bootstrap.DataAccess -{ - /// - /// 微信登陆帮助类 - /// - public static class WeChatHelper - { - /// - /// 微信登陆配置方法 - /// - /// - /// - public static void Configure(TOptions option) where TOptions : LgbOAuthOptions - { - option.Events.OnCreatingTicket = context => - { - // 生成用户 - var user = ParseUser(context); - user.App = option.App; - OAuthHelper.SaveUser(user, option.Roles); - - // 记录登陆日志 - context.HttpContext.Log(user.DisplayName, true); - return System.Threading.Tasks.Task.CompletedTask; - }; - } - - /// - /// 插入 Gitee 授权用户到数据库中 - /// - /// - /// - private static User ParseUser(OAuthCreatingTicketContext context) - { - var user = context.User.ToObject(); - return new User() - { - ApprovedBy = "OAuth", - ApprovedTime = DateTime.Now, - DisplayName = user?.NickName ?? "", - UserName = user?.UnionId ?? "", - Password = LgbCryptography.GenerateSalt(), - Icon = user?.HeadImgUrl ?? "", - Description = $"{context.Scheme.Name}" - }; - } - - private static T? ToObject(this JsonElement element) where T : WeChatUser - { - var user = new WeChatUser(); - var target = element.EnumerateObject(); - user.OpenId = target.TryGetValue("OpenId"); - user.UnionId = target.TryGetValue("UnionId"); - user.NickName = target.TryGetValue("NickName"); - user.Privilege = target.TryGetValue("Privilege"); - user.Sex = target.TryGetValue("Sex"); - user.Province = target.TryGetValue("Province"); - user.City = target.TryGetValue("City"); - user.Country = target.TryGetValue("Country"); - user.HeadImgUrl = target.TryGetValue("HeadImgUrl"); - user.Privilege = target.TryGetValue("Privilege"); - return user as T; - } - - private static string TryGetValue(this JsonElement.ObjectEnumerator target, string propertyName) - { - var ret = string.Empty; - var property = target.FirstOrDefault(t => t.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase)); - ret = property.Value.ToString(); - return ret; - } - } -} diff --git a/src/client/Bootstrap.Client.DataAccess.MongoDB/Bootstrap.Client.DataAccess.MongoDB.csproj b/src/client/Bootstrap.Client.DataAccess.MongoDB/Bootstrap.Client.DataAccess.MongoDB.csproj index 7a36c5b3..1a41d2a9 100644 --- a/src/client/Bootstrap.Client.DataAccess.MongoDB/Bootstrap.Client.DataAccess.MongoDB.csproj +++ b/src/client/Bootstrap.Client.DataAccess.MongoDB/Bootstrap.Client.DataAccess.MongoDB.csproj @@ -5,7 +5,7 @@ - + diff --git a/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj b/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj index f3f01068..f56ceae6 100644 --- a/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj +++ b/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj @@ -8,10 +8,10 @@ - + - + diff --git a/test/UnitTest/UnitTest.csproj b/test/UnitTest/UnitTest.csproj index a0f3d562..bc75770c 100644 --- a/test/UnitTest/UnitTest.csproj +++ b/test/UnitTest/UnitTest.csproj @@ -11,7 +11,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - +