milimoe 18854781a6
完善 WebSocket 支持;防止 SQL 注入;修改 Json 自定义转换器基类 (#104)
* 更换任务计划实现方式;修改自定义 Json 转换器基类

* RunTimeController 添加 HTTPClient(WebSocket)支持;SQL 查询修改为参数化,防止注入
2025-01-11 01:09:49 +08:00

74 lines
2.7 KiB
C#

using System.Data;
using Milimoe.FunGame.Core.Api.Transmittal;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.SQLScript.Entity;
namespace Milimoe.FunGame.Core.Library.Common.Architecture
{
public abstract class Authenticator
{
private readonly SQLHelper SQLHelper;
public Authenticator(SQLHelper SQLHelper)
{
this.SQLHelper = SQLHelper;
}
public abstract bool BeforeAuthenticator(AuthenticationType type, params object[] args);
public abstract bool AfterAuthenticator(AuthenticationType type, params object[] args);
public bool Authenticate(string script)
{
if (!BeforeAuthenticator(AuthenticationType.ScriptOnly, script)) return false;
SQLHelper.ExecuteDataSet(script);
if (SQLHelper.Success)
{
DataSet ds = SQLHelper.DataSet;
if (ds.Tables.Count > 0 &&
ds.Tables[0].Rows.Count > 0)
{
if (!AfterAuthenticator(AuthenticationType.ScriptOnly, script)) return false;
return true;
}
}
return false;
}
public bool Authenticate<T>(string script, string column, T keyword)
{
if (!BeforeAuthenticator(AuthenticationType.Column, script, column, keyword?.ToString() ?? "")) return false;
SQLHelper.ExecuteDataSet(script);
if (SQLHelper.Success)
{
DataSet ds = SQLHelper.DataSet;
if (ds.Tables.Count > 0 &&
ds.Tables[0].Columns.Contains(column) &&
ds.Tables[0].Rows.Count > 0 &&
ds.Tables[0].AsEnumerable().Where(row => row.Field<T>(column)?.Equals(keyword) ?? false).Any())
{
if (!AfterAuthenticator(AuthenticationType.Column, script, column, keyword?.ToString() ?? "")) return false;
return true;
}
}
return false;
}
public bool Authenticate(string username, string password)
{
if (!BeforeAuthenticator(AuthenticationType.Username, username, password)) return false;
SQLHelper.ExecuteDataSet(UserQuery.Select_Users_LoginQuery(SQLHelper, username, password));
if (SQLHelper.Success)
{
DataSet ds = SQLHelper.DataSet;
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
if (!AfterAuthenticator(AuthenticationType.Username, username, password)) return false;
return true;
}
}
return false;
}
}
}