This commit is contained in:
2025-02-21 16:09:11 +01:00
parent 1f03123f2e
commit 11aef4b249
9 changed files with 117 additions and 16 deletions

View File

@@ -13,8 +13,10 @@ using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.Core;
using BeWo.Service.ServiceContracts;
using BeWo.Service.ServiceUtils;
using BS.Shared.Attributes;
using BS.Shared.Extensions;
using NHibernate.Hql.Ast.ANTLR.Tree;
namespace BeWo.Service.Security
{
@@ -101,7 +103,7 @@ namespace BeWo.Service.Security
if (lUser != null && DAOFactory.UserDAO.CheckPassword(lUser, lPassword))
{
if (!HasMethodAuthorization(lUser, lMethodName, lServiceInterface))
if (!HasMethodAuthorization(lUser, lMethodName, pMessage))
{
throw new SecurityException("Methode nicht autorisiert: " + lMethodName);
}
@@ -120,7 +122,7 @@ namespace BeWo.Service.Security
}
}
private bool HasMethodAuthorization(ApplicationUser lUser, string methodName, string serviceInterface)
private bool HasMethodAuthorization(ApplicationUser lUser, string methodName, Message pMessage)
{
if (ALLOW_ONLY_WITH_LOGIN.Contains(methodName))
{
@@ -135,10 +137,7 @@ namespace BeWo.Service.Security
try
{
var name = $"BeWo.Service.ServiceContracts.{serviceInterface}";
var t = Type.GetType(name);
var methodInfo = t.GetMethod(methodName);
var attribute = methodInfo.GetCustomAttribute<RequirePermissionAttribute>();
var attribute = pMessage.GetCustomAttribute<RequirePermissionAttribute>();
if (attribute != null)
{
return attribute.HasPermission(rights);

View File

@@ -529,6 +529,7 @@
<Compile Include="ServiceUtils\DistanceCalculator\IDistanceAPI.cs" />
<Compile Include="ServiceUtils\DistanceCalculator\OpenrouteServiceAPI.cs" />
<Compile Include="ServiceUtils\ServiceTranslator.cs" />
<Compile Include="ServiceUtils\ServiceHelper.cs" />
<Compile Include="UnitOfWork\HibernateSessionEndpointBehavior.cs" />
<Compile Include="UnitOfWork\HibernateSessionBehaviorExtension.cs" />
<Compile Include="UnitOfWork\HibernateSessionContextInitializer.cs" />

View File

@@ -14,12 +14,36 @@ namespace BeWo.Service.ServiceBehavior
{
public void AfterInvoke(object pCorrelationState)
{
if (pCorrelationState != null)
{
WCFHibernateSessionManager.CloseSession();
OperationContext.Current.Extensions.Remove((SessionOperationContextExt)pCorrelationState);
}
}
public object BeforeInvoke(InstanceContext pInstanceContext, IClientChannel pChannel, Message pMessage)
{
return null;
if (LoggedInUserOperationContextExt.Current?.User is null)
return null;
try
{
//System.Threading.Monitor.Enter
SessionOperationContextExt lExt = new SessionOperationContextExt();
//int c = OperationContext.Current.Extensions.Count;
//if (c > 0)
//{
// int te st = 0;
//}
OperationContext.Current.Extensions.Add(lExt);
WCFHibernateSessionManager.OpenSession();
return lExt;
}
catch (Exception e)
{
throw new FaultException(e.ToString());
return null;
}
}
}
}

View File

@@ -10,6 +10,10 @@ using System.Web;
using System.Xml;
using System.ServiceModel.Dispatcher;
using BeWo.Service.Core;
using BS.Shared.Extensions;
using BeWo.Service.ServiceUtils;
using BS.Shared.Attributes;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
namespace BeWo.Service.ServiceBehavior
{
@@ -29,6 +33,14 @@ namespace BeWo.Service.ServiceBehavior
try
{
// RequireNoTenant
var lTemp = pMessage.GetCustomAttribute<RequireNoTenantAttribute>();
if (lTemp is object)
{
return null;
}
// Hole tenant aus Query
if (pMessage.Properties?.Via is Uri uri)
{
var query = HttpUtility.ParseQueryString(uri.Query);
@@ -37,14 +49,15 @@ namespace BeWo.Service.ServiceBehavior
lTenant = tenant;
}
if (!string.IsNullOrEmpty(lTenant))
// Wenn kein Tenant gefunden wird,
if (string.IsNullOrWhiteSpace(lTenant))
{
var lExt = new MultitenancyOperationContextExt { Tenant = lTenant };
OperationContext.Current.Extensions.Add(lExt);
return lExt;
throw new ArgumentException($"Zugriff verweigert. Tenant (\'{lTenant}\') darf nicht leer sein.", nameof(lTenant));
}
return null;
var lExt = new MultitenancyOperationContextExt { Tenant = lTenant };
OperationContext.Current.Extensions.Add(lExt);
return lExt;
}
catch (Exception e)
{

View File

@@ -30,11 +30,11 @@ namespace BeWo.Service.ServiceBehavior
}
// Hole erlaubten API Key aus den appSettings
string allowedApikey = ConfigurationManager.AppSettings["AdminServiceIPAddress"];
string allowedApikey = ConfigurationManager.AppSettings["AdminServiceAPIKey"];
// Extrahiert den API Key von Remote
var prop = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
var remoteApikey = prop?.Headers["AdminServiceAPIKey"];
var remoteApikey = prop?.Headers["admin-service-api-key"];
// Vergleicht die API Keys
if(remoteApikey == null || !string.Equals(remoteApikey, allowedApikey, StringComparison.Ordinal))

View File

@@ -1,4 +1,5 @@
using System;
using BS.Shared.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
@@ -13,10 +14,12 @@ namespace BeWo.Service.ServiceContracts
{
[OperationContract]
[WebGet(UriTemplate = "/Test", ResponseFormat = WebMessageFormat.Json)]
[RequireNoTenant]
string Test();
[OperationContract]
[WebInvoke(UriTemplate = "/Echo/{echo}", Method ="POST", ResponseFormat = WebMessageFormat.Json)]
[RequireNoTenant]
string Echo(string echo);
}
}

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.IdentityModel.Claims;
using System.Linq;
using System.Reflection;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;
using BS.Shared.Attributes;
using BS.Shared.Extensions;
using NHibernate.Hql.Ast.ANTLR.Tree;
namespace BeWo.Service.ServiceUtils
{
public static class ServiceHelper
{
public static T GetCustomAttribute<T>(this Message pMessage) where T : Attribute
{
// Finde Action
var lTemp = pMessage.Headers.Action?.Split("/");
if (lTemp is null)
lTemp = pMessage.Headers.To.Segments.ToList();
if (lTemp is null)
throw new ArgumentException("Kann Action nicht finden");
// Extrahiere MethodeName + ServiceInterface
string lMethodName = lTemp[lTemp.Count - 1].Trim('/');
string lServiceInterface = lTemp[lTemp.Count - 2].Trim('/');
// Hole entsprechende Methode
var name = $"BeWo.Service.ServiceContracts.I{lServiceInterface}";
var t = Type.GetType(name);
var methodInfo = t.GetMethod(lMethodName);
// Hole Custome Attribute
var attribute = methodInfo.GetCustomAttribute<T>();
return attribute;
}
}
}

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BS.Shared.Attributes
{
[AttributeUsage(AttributeTargets.Method, Inherited = true)]
public class RequireNoTenantAttribute : Attribute
{
public RequireNoTenantAttribute()
{
}
}
}

View File

@@ -133,6 +133,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AppSender\GkvSender.cs" />
<Compile Include="Attributes\RequireNoTenantAttribute.cs" />
<Compile Include="Attributes\RequirePermissionAttribute.cs" />
<Compile Include="BeWoEntityEnums.cs" />
<Compile Include="Core\AppSettingReader.cs" />