diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index a91bf0671..e7c2737f5 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -122,6 +122,7 @@ + diff --git a/src/Sitko.Core.Grpc.Server/BaseGrpcServerModule.cs b/src/Sitko.Core.Grpc.Server/BaseGrpcServerModule.cs index db71b020b..62289e337 100644 --- a/src/Sitko.Core.Grpc.Server/BaseGrpcServerModule.cs +++ b/src/Sitko.Core.Grpc.Server/BaseGrpcServerModule.cs @@ -13,15 +13,21 @@ public abstract class BaseGrpcServerModule : BaseApplicationModule> endpointRegistrations = new(); - public virtual void RegisterService(string? requiredAuthorizarionSchemeName) where TService : class => + public virtual void RegisterService(string? requiredAuthorizationSchemeName, bool enableGrpcWeb = false) + where TService : class => endpointRegistrations.Add(builder => { - var grpcBuidler = builder.MapGrpcService(); - if (!string.IsNullOrEmpty(requiredAuthorizarionSchemeName)) + var grpcEndpoint = builder.MapGrpcService(); + if (enableGrpcWeb) { - grpcBuidler.RequireAuthorization(new AuthorizeAttribute + grpcEndpoint = grpcEndpoint.EnableGrpcWeb(); + } + + if (!string.IsNullOrEmpty(requiredAuthorizationSchemeName)) + { + grpcEndpoint.RequireAuthorization(new AuthorizeAttribute { - AuthenticationSchemes = requiredAuthorizarionSchemeName + AuthenticationSchemes = requiredAuthorizationSchemeName, }); } }); @@ -55,6 +61,11 @@ public void ConfigureEndpoints(IApplicationContext applicationContext, IApplicationBuilder appBuilder, IEndpointRouteBuilder endpoints) { var config = GetOptions(appBuilder.ApplicationServices); + if (config.EnableGrpcWeb) + { + appBuilder.UseGrpcWeb(); + } + foreach (var endpointRegistration in endpointRegistrations) { endpointRegistration(endpoints); diff --git a/src/Sitko.Core.Grpc.Server/Discovery/DiscoveryGrpcServerModule.cs b/src/Sitko.Core.Grpc.Server/Discovery/DiscoveryGrpcServerModule.cs index b3e304549..1564b92c1 100644 --- a/src/Sitko.Core.Grpc.Server/Discovery/DiscoveryGrpcServerModule.cs +++ b/src/Sitko.Core.Grpc.Server/Discovery/DiscoveryGrpcServerModule.cs @@ -32,13 +32,12 @@ public override async Task ApplicationStarted(IApplicationContext applicationCon } } - public override void RegisterService(string? requiredAuthorizarionSchemeName) + public override void RegisterService(string? requiredAuthorizationSchemeName, bool enableGrpcWeb = false) { - base.RegisterService(requiredAuthorizarionSchemeName); + base.RegisterService(requiredAuthorizationSchemeName, enableGrpcWeb); serviceRegistrations.Add(registrar => registrar.RegisterAsync()); healthChecksRegistrations.Add(healthCheckBuilder => healthCheckBuilder.AddCheck>( $"Grpc service {typeof(TService).BaseType?.DeclaringType?.Name}")); } } - diff --git a/src/Sitko.Core.Grpc.Server/GrpcServerModuleOptions.cs b/src/Sitko.Core.Grpc.Server/GrpcServerModuleOptions.cs index 43807da63..4b936edec 100644 --- a/src/Sitko.Core.Grpc.Server/GrpcServerModuleOptions.cs +++ b/src/Sitko.Core.Grpc.Server/GrpcServerModuleOptions.cs @@ -10,7 +10,8 @@ namespace Sitko.Core.Grpc.Server; public class GrpcServerModuleOptions : BaseModuleOptions { private readonly List> serviceRegistrations = new(); - internal string? RequiredAuthorizarionSchemeName { get; private set; } + internal string? RequiredAuthorizationSchemeName { get; private set; } + internal bool EnableGrpcWeb { get; private set; } public string? Host { get; set; } public int? Port { get; set; } [JsonIgnore] public Action? ConfigureWebHostDefaults { get; set; } @@ -29,13 +30,20 @@ public class GrpcServerModuleOptions : BaseModuleOptions public GrpcServerModuleOptions RegisterService() where TService : class { - serviceRegistrations.Add(module => module.RegisterService(RequiredAuthorizarionSchemeName)); + serviceRegistrations.Add(module => module.RegisterService(RequiredAuthorizationSchemeName)); + return this; + } + + public GrpcServerModuleOptions RegisterServiceWithGrpcWeb() where TService : class + { + EnableGrpcWeb = true; + serviceRegistrations.Add(module => module.RegisterService(RequiredAuthorizationSchemeName, true)); return this; } public GrpcServerModuleOptions RequireSchemeAuthorization(string schemeName) { - RequiredAuthorizarionSchemeName = schemeName; + RequiredAuthorizationSchemeName = schemeName; return this; } } diff --git a/src/Sitko.Core.Grpc.Server/IGrpcServerModule.cs b/src/Sitko.Core.Grpc.Server/IGrpcServerModule.cs index a9d2282a3..d13d0a089 100644 --- a/src/Sitko.Core.Grpc.Server/IGrpcServerModule.cs +++ b/src/Sitko.Core.Grpc.Server/IGrpcServerModule.cs @@ -2,6 +2,6 @@ namespace Sitko.Core.Grpc.Server; public interface IGrpcServerModule { - void RegisterService(string? requiredAuthorizarionSchemeName) where TService : class; + void RegisterService(string? requiredAuthorizationSchemeName, bool enableGrpcWeb = false) where TService : class; } diff --git a/src/Sitko.Core.Grpc.Server/Sitko.Core.Grpc.Server.csproj b/src/Sitko.Core.Grpc.Server/Sitko.Core.Grpc.Server.csproj index b1759a748..e9514982b 100644 --- a/src/Sitko.Core.Grpc.Server/Sitko.Core.Grpc.Server.csproj +++ b/src/Sitko.Core.Grpc.Server/Sitko.Core.Grpc.Server.csproj @@ -6,6 +6,7 @@ +