Skip to content

Commit

Permalink
Add libraries.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Sep 28, 2024
1 parent bf115de commit eda6f5f
Show file tree
Hide file tree
Showing 13 changed files with 830 additions and 1 deletion.
14 changes: 14 additions & 0 deletions ControlR.Web.Server/Authz/AuthorizationPolicyBuilderExtensions.cs
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;
}
}
9 changes: 9 additions & 0 deletions ControlR.Web.Server/Authz/ServiceProviderRequirement.cs
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 ControlR.Web.Server/Authz/ServiceProviderRequirementHandler.cs
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);
}
}
}
}
8 changes: 7 additions & 1 deletion ControlR.sln
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ControlR.Web.ServiceDefault
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ControlR.Web.AppHost", "ControlR.Web.AppHost\ControlR.Web.AppHost.csproj", "{5FE149EC-5F31-4E79-BDD1-DD98A3DC22C3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ControlR.Libraries.Signalr.Client", "Libraries\ControlR.Libraries.Signalr.Client\ControlR.Libraries.Signalr.Client.csproj", "{CFF4CEB7-C437-44F2-BA4C-7AE5171A5E94}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -143,6 +145,10 @@ Global
{5FE149EC-5F31-4E79-BDD1-DD98A3DC22C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5FE149EC-5F31-4E79-BDD1-DD98A3DC22C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5FE149EC-5F31-4E79-BDD1-DD98A3DC22C3}.Release|Any CPU.Build.0 = Release|Any CPU
{CFF4CEB7-C437-44F2-BA4C-7AE5171A5E94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CFF4CEB7-C437-44F2-BA4C-7AE5171A5E94}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CFF4CEB7-C437-44F2-BA4C-7AE5171A5E94}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CFF4CEB7-C437-44F2-BA4C-7AE5171A5E94}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -160,9 +166,9 @@ Global
{1D8E803F-D7C4-40FE-9122-A5DF2D8B5FC2} = {5E522C75-6FF4-4ADF-99F4-F8DBFF7F5D13}
{95B98845-4F9C-4B6B-88F7-9194A2A824CA} = {6A8B754D-F60A-42FB-9C3F-E19A6BBD0987}
{E36620C7-2D12-4956-BF31-A82258A7C656} = {5E522C75-6FF4-4ADF-99F4-F8DBFF7F5D13}
{CFF4CEB7-C437-44F2-BA4C-7AE5171A5E94} = {5E522C75-6FF4-4ADF-99F4-F8DBFF7F5D13}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {61F7FB11-1E47-470C-91E2-47F8143E1572}
SolutionGuid = {078E947A-B1A3-491E-BA80-0370172FB5F9}
EndGlobalSection
EndGlobal
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>
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
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);
}
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);
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;
}
}
Loading

0 comments on commit eda6f5f

Please sign in to comment.