2026-05-12 09:39:23 +08:00
|
|
|
using System.Threading.Tasks;
|
2026-05-03 19:34:47 +08:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using SimApi.Communications;
|
|
|
|
|
using SimApi.Configurations;
|
|
|
|
|
using SimApi.Helpers;
|
|
|
|
|
|
2026-05-12 09:39:23 +08:00
|
|
|
namespace SimApi.AuthGate;
|
2026-05-03 19:34:47 +08:00
|
|
|
|
2026-05-12 09:39:23 +08:00
|
|
|
public class SimApiAuthGateMiddleware(RequestDelegate next, ILogger<SimApiAuthGateMiddleware> logger)
|
2026-05-03 19:34:47 +08:00
|
|
|
{
|
|
|
|
|
public Task Invoke(HttpContext httpContext, SimApiOptions simApiOptions)
|
|
|
|
|
{
|
|
|
|
|
if (httpContext.Request.Headers.TryGetValue("X-SimApi-Gate-Auth", out var auth) &&
|
|
|
|
|
httpContext.Request.Headers.TryGetValue("X-SimApi-Gate-Time", out var time) &&
|
|
|
|
|
httpContext.Request.Headers.TryGetValue("X-SimApi-Gate-Sign", out var sign))
|
|
|
|
|
{
|
|
|
|
|
var signStr =
|
2026-05-12 09:39:23 +08:00
|
|
|
$"appId={simApiOptions.SimApiAuthGateOptions.AppId}&auth={auth}&time={time}&appKey={simApiOptions.SimApiAuthGateOptions.AppKey}";
|
2026-05-03 19:34:47 +08:00
|
|
|
logger.LogDebug($"签名字符串 => {signStr}");
|
|
|
|
|
if (SimApiUtil.Md5(signStr) == sign && !string.IsNullOrEmpty(auth))
|
|
|
|
|
{
|
|
|
|
|
var login = SimApiUtil.Base64Decode<SimApiLoginItem>(auth!);
|
|
|
|
|
httpContext.Items.Add("LoginInfo", login);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.LogDebug("签名不匹配");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return next(httpContext);
|
|
|
|
|
}
|
|
|
|
|
}
|