From a5b47c8d6bf0b354a2b16b96f35654feb098fef2 Mon Sep 17 00:00:00 2001 From: nsnail Date: Mon, 22 Apr 2024 21:24:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E2=99=BB=EF=B8=8F=20=E6=B8=85?= =?UTF-8?q?=E7=90=86=E6=97=A0=E7=94=A8=E4=BB=A3=E7=A0=81=20(#101)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NetAdmin.Domain/Contexts/ContextApp.cs | 113 ------------------ 1 file changed, 113 deletions(-) delete mode 100644 src/backend/NetAdmin.Domain/Contexts/ContextApp.cs diff --git a/src/backend/NetAdmin.Domain/Contexts/ContextApp.cs b/src/backend/NetAdmin.Domain/Contexts/ContextApp.cs deleted file mode 100644 index f5b39e4d..00000000 --- a/src/backend/NetAdmin.Domain/Contexts/ContextApp.cs +++ /dev/null @@ -1,113 +0,0 @@ -namespace NetAdmin.Domain.Contexts; - -/// -/// 上下文应用信息 -/// -/// -/// 签名算法: $"${appId}{appSecret.ToLowerInvariant()}{timestamp}{reqBody}".Md5(Encoding.UTF8); -/// reqBody 需去除\r、\n、whitespace -/// -public sealed record ContextApp : DataAbstraction, IValidatableObject -{ - private const int _TS_OFFSET_SCOPE_SEC = 30; - - /// - /// Initializes a new instance of the class. - /// - public ContextApp(long appId, string appSecret, long timestamp) - { - AppId = appId; - AppSecret = appSecret; - Timestamp = timestamp; - } - - /// - /// Initializes a new instance of the class. - /// - private ContextApp() - { - AppId = App.HttpContext.Request.Headers[nameof(AppId)].FirstOrDefault().Int64Try(0); - AppSecret = App.HttpContext.Request.Headers[nameof(AppSecret)].FirstOrDefault(); - Sign = App.HttpContext.Request.Headers[nameof(Sign)].FirstOrDefault(); - Timestamp = App.HttpContext.Request.Headers[nameof(Timestamp)].FirstOrDefault().Int64Try(0); - } - - /// - /// AppId - /// - [Range(1, long.MaxValue)] - public long AppId { get; init; } - - /// - /// AppSecret - /// - public string AppSecret { get; init; } - - /// - /// 签名 - /// - public string Sign { get; set; } - - /// - /// 时间戳 - /// - public long Timestamp { get; set; } - - /// - /// 从HttpContext 创建上下文应用 - /// - public static async Task CreateAsync() - { - var ret = new ContextApp(); - if (!ret.TryValidate().IsValid) { - return null; - } - - // 具有secret的情况下,自动生成时间戳+sign,方便调试 - if (!ret.AppSecret.NullOrEmpty()) { - ret.Timestamp = DateTime.Now.TimeUnixUtc(); - ret.Sign = await ret.BuildSignFromHttpContextAsync().ConfigureAwait(false); - } - - return ret; - } - - /// - /// 构建签名 - /// - public string BuildSign(string reqBody) - { - // 去除\r\n和空格再计算签名,规避风格样式问题 - reqBody = reqBody.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace(" ", string.Empty); - return $"{AppId}{AppSecret.ToLowerInvariant()}{Timestamp}{reqBody}".Md5(Encoding.UTF8); - } - - /// - /// 构建签名(从http上下文) - /// - public async Task BuildSignFromHttpContextAsync() - { - var sr = new StreamReader(App.HttpContext.Request.Body); - var reqBody = await sr.ReadToEndAsync().ConfigureAwait(false); - - _ = App.HttpContext.Request.Body.Seek(0, SeekOrigin.Begin); - return BuildSign(reqBody); - } - - /// - public IEnumerable Validate(ValidationContext validationContext) - { - if (!AppSecret.NullOrEmpty()) { - yield break; - } - - // 没有密码, 就要签名+时间戳 - if (Sign.NullOrEmpty()) { - yield return new ValidationResult(Ln.签名缺失, new[] { nameof(Sign) }); - } - - if (Math.Abs(DateTime.Now.TimeUnixUtc() - Timestamp) > _TS_OFFSET_SCOPE_SEC) { - yield return new ValidationResult(Ln.时间戳缺失或误差过大, new[] { nameof(Timestamp) }); - } - } -} \ No newline at end of file