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

Authentication provider doesn't have access to RemotingSession. #99

Merged
merged 2 commits into from
Dec 7, 2024
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
1 change: 1 addition & 0 deletions CoreRemoting.Tests/CoreRemoting.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="9.0.0" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
Expand Down
57 changes: 57 additions & 0 deletions CoreRemoting.Tests/DisposableTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using CoreRemoting.Toolbox;
using System.Threading.Tasks;
using Xunit;

namespace CoreRemoting.Tests
{
public class DisposableTests
{
[Fact]
public void Disposable_executes_action_on_Dispose()
{
var disposed = false;

void Dispose() =>
disposed = true;

using (Disposable.Create(Dispose))
Assert.False(disposed);

Assert.True(disposed);
}

[Fact]
public async Task AsyncDisposable_executes_action_on_DisposeAsync()
{
var disposed = false;

async Task DisposeTask()
{
await Task.Yield();
disposed = true;
}

await using (Disposable.Create(DisposeTask))
Assert.False(disposed);

Assert.True(disposed);
}

[Fact]
public async Task AsyncTaskDisposable_executes_action_on_DisposeAsync()
{
var disposed = false;

async ValueTask DisposeAsync()
{
await Task.Yield();
disposed = true;
}

await using (Disposable.Create(DisposeAsync))
Assert.False(disposed);

Assert.True(disposed);
}
}
}
47 changes: 43 additions & 4 deletions CoreRemoting.Tests/RpcTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using CoreRemoting.Authentication;
using CoreRemoting.Channels;
using CoreRemoting.Serialization;
using CoreRemoting.Tests.ExternalTypes;
Expand Down Expand Up @@ -832,8 +833,10 @@ public void Authentication_is_taken_into_account_and_RejectCall_event_is_fired()
void RejectCall(object sender, ServerRpcContext e) =>
rejectedMethod = e.MethodCallMessage.MethodName;

_serverFixture.Server.Config.AuthenticationRequired = true;
_serverFixture.Server.RejectCall += RejectCall;
var server = _serverFixture.Server;
server.RejectCall += RejectCall;
server.Config.AuthenticationRequired = true;

try
{
using var client = new RemotingClient(new ClientConfig()
Expand All @@ -859,8 +862,44 @@ void RejectCall(object sender, ServerRpcContext e) =>
}
finally
{
_serverFixture.Server.Config.AuthenticationRequired = false;
_serverFixture.Server.RejectCall -= RejectCall;
server.Config.AuthenticationRequired = false;
server.RejectCall -= RejectCall;
}
}

[Fact]
public void Authentication_handler_has_access_to_the_current_session()
{
var server = _serverFixture.Server;
var authProvider = server.Config.AuthenticationProvider;
server.Config.AuthenticationRequired = true;
server.Config.AuthenticationProvider = new FakeAuthProvider
{
AuthenticateFake = c => RemotingSession.Current != null
};

try
{
using var client = new RemotingClient(new ClientConfig()
{
ConnectionTimeout = 0,
InvocationTimeout = 0,
SendTimeout = 0,
Channel = ClientChannel,
MessageEncryption = false,
ServerPort = _serverFixture.Server.Config.NetworkPort,
Credentials = [new Credential()],
});

client.Connect();

var proxy = client.CreateProxy<ITestService>();
Assert.Equal("123", proxy.Reverse("321"));
}
finally
{
server.Config.AuthenticationProvider = authProvider;
server.Config.AuthenticationRequired = false;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions CoreRemoting/IRemotingServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public interface IRemotingServer : IDisposable
event EventHandler<ServerRpcContext> AfterCall;

/// <summary>
/// Event: Fires if an error occurs.
/// Event: Fires when an RPC call is rejected before BeginCall event. .
/// </summary>
event EventHandler<Exception> Error;
event EventHandler<ServerRpcContext> RejectCall;

/// <summary>
/// Event: Fires when an RPC call is rejected before BeginCall event. .
/// Event: Fires if an error occurs.
/// </summary>
event EventHandler<ServerRpcContext> RejectCall;
event EventHandler<Exception> Error;

/// <summary>
/// Gets the unique name of this server instance.
Expand Down
19 changes: 5 additions & 14 deletions CoreRemoting/RemotingSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using CoreRemoting.Encryption;
using CoreRemoting.Serialization;
using Serialize.Linq.Nodes;
using CoreRemoting.Toolbox;

namespace CoreRemoting
{
Expand Down Expand Up @@ -258,6 +259,8 @@ private void OnReceiveMessage(byte[] rawMessage)

_currentlyProcessedMessagesCounter.AddCount(1);

CurrentSession.Value = this;

try
{
var message = _server.Serializer.Deserialize<WireMessage>(rawMessage);
Expand Down Expand Up @@ -285,6 +288,8 @@ private void OnReceiveMessage(byte[] rawMessage)
finally
{
_currentlyProcessedMessagesCounter.Signal();

CurrentSession.Value = null;
}
});
}
Expand Down Expand Up @@ -424,8 +429,6 @@ private void ProcessRpcMessage(WireMessage request)

try
{
CurrentSession.Value = this;

if (_server.Config.AuthenticationRequired && !_isAuthenticated)
throw new NetworkException("Session is not authenticated.");

Expand Down Expand Up @@ -470,19 +473,13 @@ private void ProcessRpcMessage(WireMessage request)
serializedResult =
_server.Serializer.Serialize(serverRpcContext.Exception);
}
finally
{
CurrentSession.Value = null;
}

object result = null;

if (serverRpcContext.Exception == null)
{
try
{
CurrentSession.Value = this;

((RemotingServer)_server).OnBeforeCall(serverRpcContext);

result = method.Invoke(serverRpcContext.ServiceInstance,
Expand Down Expand Up @@ -543,10 +540,6 @@ private void ProcessRpcMessage(WireMessage request)
serializedResult =
_server.Serializer.Serialize(serverRpcContext.Exception);
}
finally
{
CurrentSession.Value = null;
}

if (!oneWay)
{
Expand Down Expand Up @@ -587,8 +580,6 @@ private void ProcessRpcMessage(WireMessage request)

_rawMessageTransport.SendMessage(
_server.Serializer.Serialize(methodResultMessage));

CurrentSession.Value = null;
}

private MethodInfo GetMethodInfo(MethodCallMessage callMessage, Type serviceInterfaceType, Type[] parameterTypes)
Expand Down
52 changes: 52 additions & 0 deletions CoreRemoting/Toolbox/Disposable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Threading.Tasks;

namespace CoreRemoting.Toolbox
{
/// <summary>
/// Helper class to create disposable primitives.
/// </summary>
public static class Disposable
{
private class SyncDisposable(Action disposeAction) : IDisposable
{
void IDisposable.Dispose() =>
disposeAction?.Invoke();
}

/// <summary>
/// Creates a disposable object.
/// </summary>
/// <param name="disposeAction">An action to invoke on disposal.</param>
public static IDisposable Create(Action disposeAction) =>
new SyncDisposable(disposeAction);

private class AsyncDisposable(
Func<ValueTask> disposeAsync,
Func<Task> disposeTaskAsync) : IAsyncDisposable
{
async ValueTask IAsyncDisposable.DisposeAsync()
{
if (disposeAsync != null)
await disposeAsync();

if (disposeTaskAsync != null)
await disposeTaskAsync();
}
}

/// <summary>
/// Creates an asynchronous disposable object.
/// </summary>
/// <param name="disposeAsync">An action to invoke on disposal.</param>
public static IAsyncDisposable Create(Func<ValueTask> disposeAsync) =>
new AsyncDisposable(disposeAsync, null);

/// <summary>
/// Creates an asynchronous disposable object.
/// </summary>
/// <param name="disposeAsync">An action to invoke on disposal.</param>
public static IAsyncDisposable Create(Func<Task> disposeAsync) =>
new AsyncDisposable(null, disposeAsync);
}
}
Loading