Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #58

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Source/Euonia.Bus.Abstract/DefaultIdentityProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Security.Principal;

namespace Nerosoft.Euonia.Bus;

internal class DefaultIdentityProvider : IIdentityProvider
{
private readonly IdentityAccessor _accessor;

public DefaultIdentityProvider(IdentityAccessor accessor)
{
_accessor = accessor;
}

public IPrincipal GetIdentity(string authorization)
{
return _accessor(authorization);
}
}
16 changes: 16 additions & 0 deletions Source/Euonia.Bus.Abstract/IIdentityProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Security.Principal;

namespace Nerosoft.Euonia.Bus;

/// <summary>
/// Defines the identity provider.
/// </summary>
public interface IIdentityProvider
{
/// <summary>
/// Get the identity from authorization header.
/// </summary>
/// <param name="authorization"></param>
/// <returns></returns>
IPrincipal GetIdentity(string authorization);
}
9 changes: 8 additions & 1 deletion Source/Euonia.Bus.Abstract/IMessageContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Nerosoft.Euonia.Bus;
using System.Security.Principal;

namespace Nerosoft.Euonia.Bus;

/// <summary>
/// Interface IMessageContext
Expand Down Expand Up @@ -35,6 +37,11 @@ public interface IMessageContext : IDisposable
/// </summary>
string Authorization { get; set; }

/// <summary>
/// Gets the current user.
/// </summary>
IPrincipal User { get; }

/// <summary>
/// Gets the message request headers.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions Source/Euonia.Bus.Abstract/IdentityAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Security.Principal;

namespace Nerosoft.Euonia.Bus;

/// <summary>
/// The request context accessor.
/// </summary>
/// <param name="authorization"></param>
/// <returns></returns>
public delegate IPrincipal IdentityAccessor(string authorization);
13 changes: 10 additions & 3 deletions Source/Euonia.Bus.Abstract/MessageContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Nerosoft.Euonia.Bus;
using System.Security.Principal;

namespace Nerosoft.Euonia.Bus;

/// <summary>
/// The message context.
Expand Down Expand Up @@ -31,14 +33,16 @@ public MessageContext(object message)
/// Initializes a new instance of the <see cref="MessageContext"/> class.
/// </summary>
/// <param name="pack"></param>
public MessageContext(IRoutedMessage pack)
/// <param name="identity"></param>
public MessageContext(IRoutedMessage pack, IdentityAccessor identity = null)
: this(pack.Data)
{
MessageId = pack.MessageId;
CorrelationId = pack.CorrelationId;
ConversationId = pack.ConversationId;
RequestTraceId = pack.RequestTraceId;
Authorization = pack.Authorization;
User = identity?.Invoke(pack.Authorization);
}

/// <summary>
Expand Down Expand Up @@ -106,6 +110,9 @@ public string Authorization
set => _headers[nameof(Authorization)] = value;
}

/// <inheritdoc/>
public IPrincipal User { get; }

/// <inheritdoc />
public IReadOnlyDictionary<string, string> Headers => _headers;

Expand Down Expand Up @@ -136,7 +143,7 @@ public void Failure(Exception exception)
{
_events.HandleEvent(this, exception, nameof(Failed));
}

/// <summary>
/// Called after the message has been handled.
/// This operate will raised up the <see cref="Completed"/> event.
Expand Down
21 changes: 17 additions & 4 deletions Source/Euonia.Bus.InMemory/InMemoryDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Nerosoft.Euonia.Bus.InMemory;
using Microsoft.Extensions.DependencyInjection;

namespace Nerosoft.Euonia.Bus.InMemory;

/// <summary>
///
Expand All @@ -8,11 +10,22 @@ public class InMemoryDispatcher : DisposableObject, IDispatcher
/// <inheritdoc />
public event EventHandler<MessageDispatchedEventArgs> Delivered;

private readonly IIdentityProvider _identity;

/// <summary>
/// Initialize a new instance of <see cref="InMemoryDispatcher"/>
/// </summary>
/// <param name="provider"></param>
public InMemoryDispatcher(IServiceProvider provider)
{
_identity = provider.GetService<IIdentityProvider>();
}

/// <inheritdoc />
public async Task PublishAsync<TMessage>(RoutedMessage<TMessage> message, CancellationToken cancellationToken = default)
where TMessage : class
{
var context = new MessageContext(message);
var context = new MessageContext(message, authorization => _identity?.GetIdentity(authorization));
var pack = new MessagePack(message, context)
{
Aborted = cancellationToken
Expand All @@ -26,7 +39,7 @@ public async Task PublishAsync<TMessage>(RoutedMessage<TMessage> message, Cancel
public async Task SendAsync<TMessage>(RoutedMessage<TMessage> message, CancellationToken cancellationToken = default)
where TMessage : class
{
var context = new MessageContext(message);
var context = new MessageContext(message, authorization => _identity?.GetIdentity(authorization));
var pack = new MessagePack(message, context)
{
Aborted = cancellationToken
Expand Down Expand Up @@ -60,7 +73,7 @@ public async Task SendAsync<TMessage>(RoutedMessage<TMessage> message, Cancellat
public async Task<TResponse> SendAsync<TMessage, TResponse>(RoutedMessage<TMessage, TResponse> message, CancellationToken cancellationToken = default)
where TMessage : class
{
var context = new MessageContext(message);
var context = new MessageContext(message, authorization => _identity?.GetIdentity(authorization));
var pack = new MessagePack(message, context)
{
Aborted = cancellationToken
Expand Down
17 changes: 16 additions & 1 deletion Source/Euonia.Bus.RabbitMq/RabbitMqQueueConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Nerosoft.Euonia.Bus.RabbitMq;
/// </summary>
public class RabbitMqQueueConsumer : RabbitMqQueueRecipient, IQueueConsumer
{
private readonly IIdentityProvider _identity;

/// <summary>
/// Initializes a new instance of the <see cref="RabbitMqQueueConsumer"/> class.
/// </summary>
Expand All @@ -20,6 +22,19 @@ public RabbitMqQueueConsumer(IPersistentConnection connection, IHandlerContext h
{
}

/// <summary>
/// Initializes a new instance of the <see cref="RabbitMqQueueConsumer"/> class.
/// </summary>
/// <param name="connection"></param>
/// <param name="handler"></param>
/// <param name="options"></param>
/// <param name="identity"></param>
public RabbitMqQueueConsumer(IPersistentConnection connection, IHandlerContext handler, IOptions<RabbitMqMessageBusOptions> options, IIdentityProvider identity)
: this(connection, handler, options)
{
_identity = identity;
}

/// <inheritdoc />
public string Name => nameof(RabbitMqQueueConsumer);

Expand Down Expand Up @@ -61,7 +76,7 @@ protected override async void HandleMessageReceived(object sender, BasicDeliverE

var props = args.BasicProperties;

var context = new MessageContext();
var context = new MessageContext(message, authorization => _identity?.GetIdentity(authorization));

OnMessageReceived(new MessageReceivedEventArgs(message.Data, context));

Expand Down
17 changes: 16 additions & 1 deletion Source/Euonia.Bus.RabbitMq/RabbitMqTopicSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Nerosoft.Euonia.Bus.RabbitMq;
/// </summary>
public class RabbitMqTopicSubscriber : RabbitMqQueueRecipient, ITopicSubscriber
{
private readonly IIdentityProvider _identity;

/// <summary>
/// Initializes a new instance of the <see cref="RabbitMqTopicSubscriber"/> class.
/// </summary>
Expand All @@ -20,6 +22,19 @@ public RabbitMqTopicSubscriber(IPersistentConnection connection, IHandlerContext
{
}

/// <summary>
/// Initializes a new instance of the <see cref="RabbitMqTopicSubscriber"/> class.
/// </summary>
/// <param name="connection"></param>
/// <param name="handler"></param>
/// <param name="options"></param>
/// <param name="identity"></param>
public RabbitMqTopicSubscriber(IPersistentConnection connection, IHandlerContext handler, IOptions<RabbitMqMessageBusOptions> options, IIdentityProvider identity)
: this(connection, handler, options)
{
_identity = identity;
}

/// <inheritdoc />
public string Name => nameof(RabbitMqTopicSubscriber);

Expand Down Expand Up @@ -68,7 +83,7 @@ protected override async void HandleMessageReceived(object sender, BasicDeliverE

var message = DeserializeMessage(args.Body.ToArray(), type);

var context = new MessageContext();
var context = new MessageContext(message, authorization => _identity?.GetIdentity(authorization));

OnMessageReceived(new MessageReceivedEventArgs(message.Data, context));

Expand Down
23 changes: 23 additions & 0 deletions Source/Euonia.Bus/BusConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,27 @@ public BusConfigurator SetConventions(Action<MessageConventionBuilder> configure
configure?.Invoke(ConventionBuilder);
return this;
}

/// <summary>
/// Register the message identity provider.
/// </summary>
/// <param name="accessor"></param>
/// <returns></returns>
public BusConfigurator SetIdentityProvider(IdentityAccessor accessor)
{
Service.TryAddSingleton<IIdentityProvider>(_ => new DefaultIdentityProvider(accessor));
return this;
}

/// <summary>
/// Register the message identity provider.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public BusConfigurator SetIdentityProvider<T>()
where T : class, IIdentityProvider
{
Service.TryAddSingleton<IIdentityProvider, T>();
return this;
}
}
2 changes: 1 addition & 1 deletion Source/Euonia.Bus/Core/HandlerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private void ConcurrentDictionarySafeRegister<TKey, TValue>(TKey key, TValue val
}
}

private static Expression[] GetArguments(MethodBase method, object message, MessageContext context, CancellationToken cancellationToken)
private static Expression[] GetArguments(MethodInfo method, object message, MessageContext context, CancellationToken cancellationToken)
{
var parameterInfos = method.GetParameters();
var arguments = new Expression[parameterInfos.Length];
Expand Down
1 change: 1 addition & 0 deletions Source/Euonia.Bus/Core/ServiceBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public Task PublishAsync<TMessage>(TMessage message, PublishOptions options, Act
{
MessageId = options.MessageId ?? Guid.NewGuid().ToString(),
RequestTraceId = context?.TraceIdentifier ?? options.RequestTraceId ?? Guid.NewGuid().ToString("N"),
Authorization = context?.Authorization,
};
metadataSetter?.Invoke(pack.Metadata);
return _dispatcher.PublishAsync(pack, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static Task HandleExceptionAsync(HttpContext httpContext, Exception exce
var response = new
{
status = statusCode,
message = exception.Message,
message = exception is AggregateException ex ? ex.InnerException.Message : exception.Message,
details = GetErrors(exception)
};

Expand Down
2 changes: 1 addition & 1 deletion project.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>8.1.5</Version>
<Version>8.1.8</Version>
<Authors>damon</Authors>
<Company>Nerosoft Ltd.</Company>
<Product>Euonia</Product>
Expand Down
Loading