add handler to authentication check

This commit is contained in:
Pilzinsel64
2024-10-28 09:20:50 +01:00
parent 24482ccb85
commit 11c0711726
2 changed files with 6 additions and 4 deletions

View File

@@ -1,9 +1,11 @@
namespace Pilz.Net.Api; namespace Pilz.Net.Api;
public class ApiAuthCheckEventArgs(string authKey) : EventArgs public class ApiAuthCheckEventArgs(string authKey, Delegate? handler) : EventArgs
{ {
private bool hasDenyed; private bool hasDenyed;
public Delegate? Handler { get; }
public string AuthKey { get; } = authKey; public string AuthKey { get; } = authKey;
public bool Valid { get; set; } public bool Valid { get; set; }

View File

@@ -205,7 +205,7 @@ public class ApiServer(string apiUrl) : IApiServer
Log.Debug("Check authentication"); Log.Debug("Check authentication");
var isAuthenticated = false; var isAuthenticated = false;
if (!string.IsNullOrWhiteSpace(authKey) && DecodeAuthKey(authKey) is string authKeyDecoded) if (!string.IsNullOrWhiteSpace(authKey) && DecodeAuthKey(authKey) is string authKeyDecoded)
isAuthenticated = CheckAuthentication(authKeyDecoded); isAuthenticated = CheckAuthentication(authKeyDecoded, handler);
else else
authKeyDecoded = null!; authKeyDecoded = null!;
if (attribute.RequiesAuth && !isAuthenticated) if (attribute.RequiesAuth && !isAuthenticated)
@@ -278,11 +278,11 @@ public class ApiServer(string apiUrl) : IApiServer
return Serializer; return Serializer;
} }
protected virtual bool CheckAuthentication(string authKey) protected virtual bool CheckAuthentication(string authKey, Delegate? handler)
{ {
if (OnCheckAuthentication != null) if (OnCheckAuthentication != null)
{ {
var args = new ApiAuthCheckEventArgs(authKey); var args = new ApiAuthCheckEventArgs(authKey, handler);
OnCheckAuthentication?.Invoke(this, args); OnCheckAuthentication?.Invoke(this, args);
return args.Valid; return args.Valid;
} }