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:
@@ -0,0 +1,17 @@
|
||||
namespace EnvelopeGenerator.Server.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Defines sender identities allowed to view Serilog UI.
|
||||
/// </summary>
|
||||
public sealed class SerilogUiAccessOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Configuration section name.
|
||||
/// </summary>
|
||||
public const string SectionName = "SerilogUiAccess";
|
||||
|
||||
/// <summary>
|
||||
/// Allowed sender identities (username and/or email).
|
||||
/// </summary>
|
||||
public string[] AllowedUser { get; set; } = [];
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Authorizes Serilog UI access only when the request can be authenticated
|
||||
/// with the sender JWT authentication scheme and matches configured allow-list.
|
||||
/// </summary>
|
||||
/// <param name="httpContextAccessor">Provides access to the current HTTP context.</param>
|
||||
/// <param name="optionsMonitor">Provides Serilog UI access allow-list configuration.</param>
|
||||
public sealed class SerilogUiSenderAuthorizationFilter(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IOptionsMonitor<SerilogUiAccessOptions> optionsMonitor) : IUiAsyncAuthorizationFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns <c>true</c> when <see cref="AuthScheme.Sender"/> authentication succeeds
|
||||
/// for the current request; otherwise returns <c>false</c>.
|
||||
/// </summary>
|
||||
public async Task<bool> 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());
|
||||
}
|
||||
}
|
||||
@@ -189,6 +189,12 @@
|
||||
"SenderFeatureAccess": {
|
||||
"AllowedReceiverEnvelopeUrlEmails": ["h.tek@digitaldata.works"]
|
||||
},
|
||||
"SerilogUiAccess": {
|
||||
"AllowedUser": [
|
||||
"TekH",
|
||||
"h.tek@digitaldata.works"
|
||||
]
|
||||
},
|
||||
"MainPageTitle": null,
|
||||
"AnnotationParams": {
|
||||
"Background": {
|
||||
|
||||
Reference in New Issue
Block a user