diff --git a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Options/SerilogUiAccessOptions.cs b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Options/SerilogUiAccessOptions.cs new file mode 100644 index 00000000..640ba24e --- /dev/null +++ b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Options/SerilogUiAccessOptions.cs @@ -0,0 +1,17 @@ +namespace EnvelopeGenerator.Server.Options; + +/// +/// Defines sender identities allowed to view Serilog UI. +/// +public sealed class SerilogUiAccessOptions +{ + /// + /// Configuration section name. + /// + public const string SectionName = "SerilogUiAccess"; + + /// + /// Allowed sender identities (username and/or email). + /// + public string[] AllowedUser { get; set; } = []; +} diff --git a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Program.cs b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Program.cs index 80ee8ad3..e4136eb9 100644 --- a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Program.cs +++ b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Program.cs @@ -7,7 +7,6 @@ using DigitalData.Core.API; using DigitalData.Core.Application; using EnvelopeGenerator.Application; using EnvelopeGenerator.Application.Common.Interfaces; -using EnvelopeGenerator.Application.Common.Interfaces.Doc; using EnvelopeGenerator.Domain.Constants; using EnvelopeGenerator.Infrastructure; using EnvelopeGenerator.Infrastructure.Crypt.DependencyInjection; @@ -21,9 +20,9 @@ using EnvelopeGenerator.Server.Options; using EnvelopeGenerator.Server.Services; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Localization; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using Serilog; @@ -198,8 +197,11 @@ try } }); + builder.Services.AddScoped(); + builder.Services.AddSerilogUi(options => { + options.AddScopedAsyncAuthFilter(); options.UseSqliteServer(dbOpt => { dbOpt.WithConnectionString($"Data Source={sqliteDbPath}"); @@ -347,6 +349,7 @@ try builder.Services.Configure(config.GetSection(BlazorServerOptions.SectionName)); builder.Services.Configure(config.GetSection(CacheOptions.SectionName)); builder.Services.Configure(config.GetSection(SenderFeatureAccessOptions.SectionName)); + builder.Services.Configure(config.GetSection(SerilogUiAccessOptions.SectionName)); // Distributed Cache - SQL Server builder.Services.AddDistributedSqlServerCache(options => @@ -476,8 +479,6 @@ try app.MapScalarApiReference(); } - app.UseSerilogUi(); - // Set CORS policy app.UseCors("AllowSpecificOriginsPolicy"); @@ -508,6 +509,26 @@ try app.UseAuthentication(); app.UseAuthorization(); + app.Use(async (context, next) => + { + if (context.Request.Path.StartsWithSegments("/serilog-ui", StringComparison.OrdinalIgnoreCase)) + { + var serilogUiAuthFilter = context.RequestServices.GetRequiredService(); + if (!await serilogUiAuthFilter.AuthorizeAsync()) + { + context.Response.Redirect("/sender"); + return; + } + } + + await next(); + }); + + app.UseSerilogUi(); + + app.MapGet("/log", () => Results.Redirect("/serilog-ui")); + app.MapGet("/logs", () => Results.Redirect("/serilog-ui")); + // API Controllers (map before Blazor routing) app.MapControllers(); diff --git a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Services/SerilogUiSenderAuthorizationFilter.cs b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Services/SerilogUiSenderAuthorizationFilter.cs new file mode 100644 index 00000000..e9d32d7d --- /dev/null +++ b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Services/SerilogUiSenderAuthorizationFilter.cs @@ -0,0 +1,57 @@ +using Microsoft.AspNetCore.Authentication; +using EnvelopeGenerator.Server.Options; +using Microsoft.Extensions.Options; +using Serilog.Ui.Core.Interfaces; +using System.Security.Claims; + +namespace EnvelopeGenerator.Server.Services; + +/// +/// Authorizes Serilog UI access only when the request can be authenticated +/// with the sender JWT authentication scheme and matches configured allow-list. +/// +/// Provides access to the current HTTP context. +/// Provides Serilog UI access allow-list configuration. +public sealed class SerilogUiSenderAuthorizationFilter( + IHttpContextAccessor httpContextAccessor, + IOptionsMonitor optionsMonitor) : IUiAsyncAuthorizationFilter +{ + /// + /// Returns true when authentication succeeds + /// for the current request; otherwise returns false. + /// + public async Task AuthorizeAsync() + { + var httpContext = httpContextAccessor.HttpContext; + if (httpContext is null) + return false; + + var authResult = await httpContext.AuthenticateAsync(AuthScheme.Sender); + if (!authResult.Succeeded) + return false; + + var principal = authResult.Principal; + if (principal is null) + return false; + + var configured = optionsMonitor.CurrentValue; + var allowedUsers = configured.AllowedUser + .Where(v => !string.IsNullOrWhiteSpace(v)) + .Select(v => v.Trim()) + .ToHashSet(StringComparer.OrdinalIgnoreCase); + + if (allowedUsers.Count == 0) + return false; + + var username = principal.FindFirstValue(ClaimTypes.Name) + ?? principal.Identity?.Name; + + if (!string.IsNullOrWhiteSpace(username) && allowedUsers.Contains(username.Trim())) + return true; + + var email = principal.FindFirstValue(ClaimTypes.Email) + ?? principal.FindFirstValue("email"); + + return !string.IsNullOrWhiteSpace(email) && allowedUsers.Contains(email.Trim()); + } +} diff --git a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/appsettings.json b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/appsettings.json index d3480f22..ec716aad 100644 --- a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/appsettings.json +++ b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/appsettings.json @@ -189,6 +189,12 @@ "SenderFeatureAccess": { "AllowedReceiverEnvelopeUrlEmails": ["h.tek@digitaldata.works"] }, + "SerilogUiAccess": { + "AllowedUser": [ + "TekH", + "h.tek@digitaldata.works" + ] + }, "MainPageTitle": null, "AnnotationParams": { "Background": {