From c46b968562f95868790c747dafddf1e42bcb4eb4 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Wed, 14 May 2025 15:02:52 +0200 Subject: [PATCH] passthrow HttpListenerContext --- Pilz.Net/Api/ApiAuthCheckEventArgs.cs | 9 +++++---- Pilz.Net/Api/ApiServer.cs | 12 ++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Pilz.Net/Api/ApiAuthCheckEventArgs.cs b/Pilz.Net/Api/ApiAuthCheckEventArgs.cs index 5d5c4ad..1b2db59 100644 --- a/Pilz.Net/Api/ApiAuthCheckEventArgs.cs +++ b/Pilz.Net/Api/ApiAuthCheckEventArgs.cs @@ -1,13 +1,14 @@ -namespace Pilz.Net.Api; +using System.Net; -public class ApiAuthCheckEventArgs(string authKey, Delegate? handler) : EventArgs +namespace Pilz.Net.Api; + +public class ApiAuthCheckEventArgs(string authKey, Delegate? handler, HttpListenerContext context) : EventArgs { private bool hasDenyed; + public HttpListenerContext Context { get; } = context; public Delegate? Handler { get; } = handler; - public string AuthKey { get; } = authKey; - public bool Valid { get; set; } public void Deny() diff --git a/Pilz.Net/Api/ApiServer.cs b/Pilz.Net/Api/ApiServer.cs index b2e2b7c..71f15a2 100644 --- a/Pilz.Net/Api/ApiServer.cs +++ b/Pilz.Net/Api/ApiServer.cs @@ -437,7 +437,7 @@ public class ApiServer : IApiServer // Handle message Log.Debug("Handle mssage"); - if (HandleMessage(context, path, query, context.Request.HttpMethod, handler, contentJson, authKey) is not PrivateApiResult result) + if (HandleMessage(context, path, query, handler, contentJson, authKey) is not PrivateApiResult result) { Log.Warn("Request couldn't be handled"); close(); @@ -470,13 +470,13 @@ public class ApiServer : IApiServer close(); } - protected virtual PrivateApiResult? HandleMessage(HttpListenerContext context, string url, string? query, string method, PrivateMessageHandler handler, string? json, string? authKey) + protected virtual PrivateApiResult? HandleMessage(HttpListenerContext context, string url, string? query, PrivateMessageHandler handler, string? json, string? authKey) { // Check authentication Log.Debug("Check authentication"); var isAuthenticated = false; if (!string.IsNullOrWhiteSpace(authKey) && DecodeAuthKey(authKey) is string authKeyDecoded) - isAuthenticated = CheckAuthentication(authKeyDecoded, handler.Handler); + isAuthenticated = CheckAuthentication(authKeyDecoded, handler.Handler, context); else authKeyDecoded = null!; if (handler.Attribute.RequiesAuth && !isAuthenticated) @@ -497,7 +497,7 @@ public class ApiServer : IApiServer // Invoke handler Log.Debug("Invoke handler"); - var parameters = BuildParameters(url, query, handler, () => message, () => new(message, isAuthenticated, authKeyDecoded, url, method, context)); + var parameters = BuildParameters(url, query, handler, () => message, () => new(message, isAuthenticated, authKeyDecoded, url, context.Request.HttpMethod, context)); if (handler.Handler.DynamicInvoke(parameters) is not ApiResult result) return new(ApiResult.InternalServerError(), null); @@ -573,11 +573,11 @@ public class ApiServer : IApiServer return Serializer; } - protected virtual bool CheckAuthentication(string authKey, Delegate? handler) + protected virtual bool CheckAuthentication(string authKey, Delegate? handler, HttpListenerContext context) { if (OnCheckAuthentication != null) { - var args = new ApiAuthCheckEventArgs(authKey, handler); + var args = new ApiAuthCheckEventArgs(authKey, handler, context); OnCheckAuthentication?.Invoke(this, args); return args.Valid; }