-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
830 additions
and
1 deletion.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
ControlR.Web.Server/Authz/AuthorizationPolicyBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace ControlR.Web.Server.Authz; | ||
|
||
public static class AuthorizationPolicyBuilderExtensions | ||
{ | ||
public static AuthorizationPolicyBuilder RequireServiceProviderAssertion( | ||
this AuthorizationPolicyBuilder builder, | ||
Func<IServiceProvider, AuthorizationHandlerContext, Task<bool>> assertion) | ||
{ | ||
builder.Requirements.Add(new ServiceProviderRequirement(assertion)); | ||
return builder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace ControlR.Web.Server.Authz; | ||
|
||
public class ServiceProviderRequirement(Func<IServiceProvider, AuthorizationHandlerContext, Task<bool>> assertion) | ||
: IAuthorizationRequirement | ||
{ | ||
public Func<IServiceProvider, AuthorizationHandlerContext, Task<bool>> Assertion { get; } = assertion; | ||
} |
19 changes: 19 additions & 0 deletions
19
ControlR.Web.Server/Authz/ServiceProviderRequirementHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace ControlR.Web.Server.Authz; | ||
|
||
public class ServiceProviderRequirementHandler(IServiceProvider serviceProvider) : IAuthorizationHandler | ||
{ | ||
private readonly IServiceProvider _serviceProvider = serviceProvider; | ||
|
||
public async Task HandleAsync(AuthorizationHandlerContext context) | ||
{ | ||
foreach (var requirement in context.Requirements.OfType<ServiceProviderRequirement>()) | ||
{ | ||
if (await requirement.Assertion.Invoke(_serviceProvider, context)) | ||
{ | ||
context.Succeed(requirement); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
Libraries/ControlR.Libraries.Signalr.Client/ControlR.Libraries.Signalr.Client.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>true</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.8" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ControlR.Libraries.Shared\ControlR.Libraries.Shared.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
25 changes: 25 additions & 0 deletions
25
Libraries/ControlR.Libraries.Signalr.Client/ControlR.Libraries.Signalr.Client.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.5.002.0 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ControlR.Libraries.Signalr.Client", "ControlR.Libraries.Signalr.Client.csproj", "{0B7429D5-9B67-45C1-B28E-74BF34CCA29E}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{0B7429D5-9B67-45C1-B28E-74BF34CCA29E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{0B7429D5-9B67-45C1-B28E-74BF34CCA29E}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{0B7429D5-9B67-45C1-B28E-74BF34CCA29E}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{0B7429D5-9B67-45C1-B28E-74BF34CCA29E}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {D619719E-CF9E-46E7-BA82-17563D7D496E} | ||
EndGlobalSection | ||
EndGlobal |
9 changes: 9 additions & 0 deletions
9
Libraries/ControlR.Libraries.Signalr.Client/Diagnostics/DefaultActivitySource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Diagnostics; | ||
|
||
namespace ControlR.Libraries.Signalr.Client.Diagnostics; | ||
internal static class DefaultActivitySource | ||
{ | ||
public const string Name = "ControlR.Libraries.Signalr.Client"; | ||
public static readonly ActivitySource Instance = new(Name); | ||
public static Activity? StartActivity(string name) => Instance.StartActivity(name); | ||
} |
3 changes: 3 additions & 0 deletions
3
Libraries/ControlR.Libraries.Signalr.Client/Exceptions/DynamicObjectGenerationException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace ControlR.Libraries.Signalr.Client.Exceptions; | ||
|
||
public class DynamicObjectGenerationException(string message) : Exception(message); |
58 changes: 58 additions & 0 deletions
58
Libraries/ControlR.Libraries.Signalr.Client/Extensions/IServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using Microsoft.AspNetCore.SignalR.Client; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace ControlR.Libraries.Signalr.Client.Extensions; | ||
public static class IServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// Creates a transient registration in DI for <typeparamref name="TClientImpl"/> | ||
/// that resolves to <typeparamref name="TClient"/>. | ||
/// </para> | ||
/// <para> | ||
/// Creates a transient registration in DI for <see cref="IHubConnection{THub, TClient}"/>. | ||
/// </para> | ||
/// <para> | ||
/// Consumers should use the <see cref="IHubConnection{THub, TClient}"/> interface for | ||
/// connecting to and interacting with the server. | ||
/// </para> | ||
/// </summary> | ||
/// <typeparam name="THub"> | ||
/// An interface representing the public methods on the server-side hub. | ||
/// These methods will be invokable by the client. | ||
/// </typeparam> | ||
/// <typeparam name="TClient"> | ||
/// An interface representing the public methods on the client-side hub. | ||
/// These methods will be invokable by the server. | ||
/// </typeparam> | ||
/// <typeparam name="TClientImpl"> | ||
/// An implementation of <typeparamref name="TClient"/>. These methods | ||
/// will handle the RPC invocations from the server. | ||
/// </typeparam> | ||
/// <param name="services"></param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddStronglyTypedSignalrClient<THub, TClient, TClientImpl>(this IServiceCollection services) | ||
where THub : class | ||
where TClient : class | ||
where TClientImpl : class, TClient | ||
{ | ||
if (!typeof(THub).IsInterface) | ||
{ | ||
throw new InvalidOperationException("THub must be an interface."); | ||
} | ||
|
||
if (!typeof(TClient).IsInterface) | ||
{ | ||
throw new InvalidOperationException("TClient must be an interface."); | ||
} | ||
|
||
services.TryAddTransient<IHubConnectionBuilder, HubConnectionBuilder>(); | ||
services.TryAddTransient<TClient, TClientImpl>(); | ||
services.TryAddTransient< | ||
IHubConnection<THub>, | ||
HubConnection<THub, TClient>>(); | ||
|
||
return services; | ||
} | ||
} |
Oops, something went wrong.