passthrow HttpListenerContext
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user