实现Authenticator和TFA框架

This commit is contained in:
milimoe 2023-10-17 22:13:29 +08:00
parent 874e18f9b5
commit 77f9422e77
Signed by: milimoe
GPG Key ID: 05D280912DA6C69E
3 changed files with 84 additions and 2 deletions

View File

@ -1,9 +1,79 @@
using Milimoe.FunGame.Core.Api.Transmittal; using Milimoe.FunGame.Core.Api.Transmittal;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.SQLScript.Entity;
using Milimoe.FunGame.Server.Model;
using Milimoe.FunGame.Server.Others;
using Milimoe.FunGame.Server.Utility;
using TFA = Milimoe.FunGame.Server.Utility.TFA;
namespace Milimoe.FunGame.Server.Controllers namespace Milimoe.FunGame.Server.Controllers
{ {
public class Authenticator : Core.Library.Common.Architecture.Authenticator public class Authenticator : Core.Library.Common.Architecture.Authenticator
{ {
public Authenticator(SQLHelper SQLHelper) : base(SQLHelper) { } public TFA Login2FA = new();
private readonly ServerModel Server;
private readonly SQLHelper SQLHelper;
private readonly MailSender? MailSender;
public Authenticator(ServerModel Server, SQLHelper SQLHelper, MailSender? MailSender) : base(SQLHelper)
{
this.Server = Server;
this.SQLHelper = SQLHelper;
this.MailSender = MailSender;
}
public override bool AfterAuthenticator(AuthenticationType type, params object[] args)
{
if (type == AuthenticationType.Username)
{
// 添加2FA二次验证等
string username = (string)args[0];
string code = Login2FA.GetTFACode(username);
if (MailSender != null)
{
// 获取此账号的邮箱
string email = "";
SQLHelper.ExecuteDataSet(UserQuery.Select_IsExistUsername(username));
if (SQLHelper.Success && SQLHelper.DataSet.Tables[0].Rows.Count > 0)
{
email = Convert.ToString(SQLHelper.DataSet.Tables[0].Rows[0][UserQuery.Column_Email]) ?? "";
}
// 发送验证码
if (email != "")
{
string ServerName = Config.ServerName;
string Subject = $"[{ServerName}] FunGame 双重认证";
string Body = $"亲爱的 {username} <br/> 您正在登录[{ServerName}],为了保证安全性,需要进行邮箱验证,您的验证码是 {code} 10分钟内有效请及时输入<br/><br/>{ServerName}<br/>{DateTimeUtility.GetDateTimeToString(TimeType.DateOnly)}";
string[] To = new string[] { email };
if (MailSender.Send(MailSender.CreateMail(Subject, Body, System.Net.Mail.MailPriority.Normal, true, To)) == MailSendResult.Success)
{
ServerHelper.WriteLine(Server.GetClientName() + $" 已向{email}发送验证码:{code}");
}
else
{
ServerHelper.WriteLine(Server.GetClientName() + " 无法发送验证码");
ServerHelper.WriteLine(MailSender.ErrorMsg);
}
}
else
{
ServerHelper.WriteLine(Server.GetClientName() + $" 验证码为:{code},请服务器管理员告知此用户");
}
}
else // 不使用MailSender的情况
{
ServerHelper.WriteLine(Server.GetClientName() + $" 验证码为:{code},请服务器管理员告知此用户");
}
}
return true;
}
public override bool BeforeAuthenticator(AuthenticationType type, params object[] args)
{
// 添加人机验证或频繁验证等
return true;
}
} }
} }

View File

@ -29,7 +29,7 @@ namespace Milimoe.FunGame.Server.Controller
public DataRequestController(ServerModel server) public DataRequestController(ServerModel server)
{ {
Server = server; Server = server;
Authenticator = new(SQLHelper); Authenticator = new(Server, SQLHelper, MailSender);
} }
public Hashtable GetResultData(DataRequestType type, Hashtable data) public Hashtable GetResultData(DataRequestType type, Hashtable data)

View File

@ -0,0 +1,12 @@
using Milimoe.FunGame.Server.Model;
namespace Milimoe.FunGame.Server.Utility
{
public class TFA : Core.Api.Utility.TFA
{
public override bool IsAvailable(string username)
{
return true;
}
}
}