Add Serilog UI access control with allow-list support

Introduced a new `SerilogUiAccessOptions` class to define an allow-list of usernames and emails for accessing the Serilog UI.

Added `SerilogUiSenderAuthorizationFilter` to enforce access control by authenticating requests using the `AuthScheme.Sender` scheme and validating against the configured allow-list.

Updated `Program.cs` to:
- Configure `SerilogUiAccessOptions` from `appsettings.json`.
- Register and apply the `SerilogUiSenderAuthorizationFilter` as an async authorization filter for Serilog UI.
- Add middleware to handle `/serilog-ui` requests and redirect unauthorized users to `/sender`.
- Reintroduce `app.UseSerilogUi()` after the custom middleware.
- Add routes `/log` and `/logs` to redirect to `/serilog-ui`.

Updated `appsettings.json` to include a `SerilogUiAccess` section for configuring allowed users.

These changes ensure that only authenticated and authorized users can access the Serilog UI.
This commit is contained in:
2026-11-25 16:46:13 +01:00
parent dfeb56aa9c
commit a84712b708
4 changed files with 105 additions and 4 deletions

View File

@@ -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<SerilogUiSenderAuthorizationFilter>();
builder.Services.AddSerilogUi(options =>
{
options.AddScopedAsyncAuthFilter<SerilogUiSenderAuthorizationFilter>();
options.UseSqliteServer(dbOpt =>
{
dbOpt.WithConnectionString($"Data Source={sqliteDbPath}");
@@ -347,6 +349,7 @@ try
builder.Services.Configure<BlazorServerOptions>(config.GetSection(BlazorServerOptions.SectionName));
builder.Services.Configure<CacheOptions>(config.GetSection(CacheOptions.SectionName));
builder.Services.Configure<SenderFeatureAccessOptions>(config.GetSection(SenderFeatureAccessOptions.SectionName));
builder.Services.Configure<SerilogUiAccessOptions>(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<SerilogUiSenderAuthorizationFilter>();
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();