using DigitalData.Auth.Claims; using DigitalData.Auth.Client; using DigitalData.Core.Abstractions; using DigitalData.Core.Abstractions.Security.Extensions; using DigitalData.Core.API; using DigitalData.Core.Application; using EnvelopeGenerator.Application; using EnvelopeGenerator.Application.Common.Interfaces; using EnvelopeGenerator.Domain.Constants; using EnvelopeGenerator.Infrastructure; using EnvelopeGenerator.Infrastructure.Crypt.DependencyInjection; using EnvelopeGenerator.Infrastructure.Doc; using EnvelopeGenerator.Infrastructure.Notification; using EnvelopeGenerator.Server; using EnvelopeGenerator.Server.Client.Services; using EnvelopeGenerator.Server.Components; using EnvelopeGenerator.Server.Models; using EnvelopeGenerator.Server.Options; using EnvelopeGenerator.Server.Services; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Localization; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using Serilog; using Serilog.Ui.Core.Extensions; using Serilog.Ui.SqliteDataProvider.Extensions; using Serilog.Ui.Web.Extensions; using Scalar.AspNetCore; using System.Globalization; using MudBlazor.Services; using EnvelopeGenerator.Server.BackgroundServices; var tempConfig = new ConfigurationBuilder() .SetBasePath(AppContext.BaseDirectory) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: false) .AddJsonFile("appsettings.Secrets.json", optional: true, reloadOnChange: false) .Build(); var logDirectoryRaw = tempConfig.GetValue("Application:LogDirectory") ?? "logs"; var logDirectory = Path.IsPathRooted(logDirectoryRaw) ? logDirectoryRaw : Path.Combine(AppContext.BaseDirectory, logDirectoryRaw); Directory.CreateDirectory(logDirectory); var sqliteDbPath = Path.Combine(logDirectory, "logs.db"); Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning) .MinimumLevel.Override("Microsoft.AspNetCore", Serilog.Events.LogEventLevel.Warning) .MinimumLevel.Override("Microsoft.EntityFrameworkCore", Serilog.Events.LogEventLevel.Warning) .MinimumLevel.Override("System", Serilog.Events.LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}") .WriteTo.File( path: Path.Combine(logDirectory, "EnvelopeGenerator.Server-.log"), rollingInterval: RollingInterval.Day, retainedFileCountLimit: 30, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}") .WriteTo.SQLite(sqliteDbPath, storeTimestampInUtc: true) .CreateBootstrapLogger(); try { Log.Information("Starting EnvelopeGenerator.Server"); var builder = WebApplication.CreateBuilder(args); // Load YARP configuration from yarp.json builder.Configuration.AddJsonFile("yarp.json", optional: true, reloadOnChange: true); builder.Configuration.AddJsonFile("appsettings.Secrets.json", optional: true, reloadOnChange: true); builder.Host.UseSerilog((context, services, configuration) => configuration .ReadFrom.Configuration(context.Configuration) .ReadFrom.Services(services) .Enrich.FromLogContext() .WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}") .WriteTo.File( path: Path.Combine(logDirectory, "EnvelopeGenerator.Server-.log"), rollingInterval: RollingInterval.Day, retainedFileCountLimit: 30, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}") .WriteTo.SQLite(sqliteDbPath, storeTimestampInUtc: true)); var config = builder.Configuration; var deferredProvider = new DeferredServiceProvider(); var blazorServerOptions = config.GetSection(BlazorServerOptions.SectionName) .Get() ?? new BlazorServerOptions(); // Add Blazor services builder.Services.AddRazorComponents() .AddInteractiveServerComponents() .AddHubOptions(options => { options.MaximumReceiveMessageSize = blazorServerOptions.MaximumReceiveMessageSize; }) .AddInteractiveWebAssemblyComponents(); builder.Services.AddMudServices(); // Add API Controllers builder.Services.AddControllers() .AddJsonOptions(options => { options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles; }); builder.Services.AddHttpClient(); // YARP Reverse Proxy (for forwarding auth requests to AuthHub) builder.Services.AddReverseProxy() .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy")); // HttpContextAccessor needed for SSR HttpClient configuration builder.Services.AddHttpContextAccessor(); // Named HttpClient for internal API calls // SenderAuthCookieHandler forwards the browser's Cookie header so that // Blazor Server components can call cookie-authenticated endpoints (AuthScheme.Sender). builder.Services.AddTransient(); builder.Services.AddHttpClient("EnvelopeGenerator.Server", (sp, client) => { var httpContextAccessor = sp.GetRequiredService(); var request = httpContextAccessor.HttpContext?.Request; if (request != null) { // Set base address to current host for SSR scenarios client.BaseAddress = new Uri($"{request.Scheme}://{request.Host}"); } }) .AddHttpMessageHandler(); // CORS Policy var allowedOrigins = config.GetSection("AllowedOrigins").Get() ?? throw new InvalidOperationException("AllowedOrigins section is missing in the configuration."); builder.Services.AddCors(options => { options.AddPolicy("AllowSpecificOriginsPolicy", builder => { builder.WithOrigins(allowedOrigins) .SetIsOriginAllowedToAllowWildcardSubdomains() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); }); }); // Swagger/OpenAPI builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "signFLOW Absender-API", Description = "Eine API zur Verwaltung der Erstellung, des Versands und der Nachverfolgung von Umschl�gen in der signFLOW-Anwendung.", Contact = new OpenApiContact { Name = "Digital Data GmbH", Url = new Uri("https://digitaldata.works/digitale-signatur#kontakt"), Email = "info-flow@digitaldata.works" }, }); options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Name = "Authorization", Type = SecuritySchemeType.Http, Scheme = "bearer", BearerFormat = "JWT", In = ParameterLocation.Header, Description = "JWT-Autorisierungs-Header unter Verwendung des Bearer-Schemas.", }); options.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = Microsoft.OpenApi.Models.ReferenceType.SecurityScheme, Id = "Bearer" } }, Array.Empty() } }); var xmlFiles = Directory.GetFiles(AppContext.BaseDirectory, "*.xml"); foreach (var xmlFile in xmlFiles) { options.IncludeXmlComments(xmlFile); } }); builder.Services.AddScoped(); builder.Services.AddSerilogUi(options => { options.AddScopedAsyncAuthFilter(); options.UseSqliteServer(dbOpt => { dbOpt.WithConnectionString($"Data Source={sqliteDbPath}"); dbOpt.WithTable("Logs"); }); }); // Database Context var useDbMigration = Environment.GetEnvironmentVariable("MIGRATION_TEST_MODE") == true.ToString() || config.GetValue("UseDbMigration"); var cnnStrName = useDbMigration ? "DbMigrationTest" : "Default"; var connStr = config.GetConnectionString(cnnStrName) ?? throw new InvalidOperationException($"Connection string '{cnnStrName}' is missing in the application configuration."); builder.Services.Configure(cs => cs.Value = connStr); builder.Services.AddDbContext(options => options.UseSqlServer(connStr)); // Authentication - AuthHub builder.Services.AddAuthHubClient(config.GetSection("AuthClientParams")); var authTokenKeys = config.GetOrDefault(); builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(AuthScheme.Sender, opt => { opt.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKeyResolver = (token, securityToken, identifier, parameters) => { var clientParams = deferredProvider.GetOptions(); var publicKey = clientParams!.PublicKeys.Get(authTokenKeys.Issuer, authTokenKeys.Audience); return [publicKey.SecurityKey]; }, ValidateIssuer = true, ValidIssuer = authTokenKeys.Issuer, ValidateAudience = true, ValidAudience = authTokenKeys.Audience, }; opt.Events = new JwtBearerEvents { OnMessageReceived = context => { if (context.Token is null) { if (context.Request.Cookies.TryGetValue(authTokenKeys.Cookie, out var cookieToken) && cookieToken is not null) context.Token = cookieToken; else if (context.Request.Query.TryGetValue(authTokenKeys.QueryString, out var queryStrToken)) context.Token = queryStrToken; } return Task.CompletedTask; } }; }) .AddJwtBearer(AuthScheme.Receiver, opt => { opt.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKeyResolver = (token, securityToken, identifier, parameters) => { var clientParams = deferredProvider.GetOptions(); var publicKey = clientParams!.PublicKeys.Get(authTokenKeys.Issuer, authTokenKeys.Audience); return [publicKey.SecurityKey]; }, ValidateIssuer = true, ValidIssuer = authTokenKeys.Issuer, ValidateAudience = true, ValidAudience = authTokenKeys.Audience, }; opt.Events = new JwtBearerEvents { OnMessageReceived = context => { var paths = context.Request.Path.Value?.Split('/', StringSplitOptions.RemoveEmptyEntries); var envelopeKey = paths?.LastOrDefault(); if (envelopeKey is not null) { var cookieName = CookieNames.GetEnvelopeReceiverCookieName(authTokenKeys.Cookie, envelopeKey); if (context.Request.Cookies.TryGetValue(cookieName, out var cookieToken) && cookieToken is not null) context.Token = cookieToken; } return Task.CompletedTask; }, OnTokenValidated = context => { var paths = context.Request.Path.Value?.Split('/', StringSplitOptions.RemoveEmptyEntries); var envelopeKey = paths?.LastOrDefault(); var sub = context.Principal?.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value ?? context.Principal?.FindFirst("sub")?.Value; if (envelopeKey is null || sub != envelopeKey) context.Fail("Envelope key in the path does not match the token subject."); return Task.CompletedTask; } }; }); // Cookie Authentication builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.Cookie.HttpOnly = true; options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest; options.Cookie.SameSite = SameSiteMode.Strict; options.LoginPath = "/api/auth/login"; options.LogoutPath = "/api/auth/logout"; options.SlidingExpiration = true; }); // Authorization Policies builder.Services.AddAuthorizationBuilder() .AddPolicy(AuthPolicy.Sender, policy => policy .RequireRole(Role.Sender) .AddAuthenticationSchemes(AuthScheme.Sender)) .AddPolicy(AuthPolicy.Receiver, policy => policy .AddAuthenticationSchemes(AuthScheme.Receiver) .RequireAuthenticatedUser() .RequireRole(Role.Receiver.Full, "receiver")) .AddPolicy(AuthPolicy.ReceiverTFA, policy => policy.RequireRole(Role.Receiver.TFA)) // Combined policy: accepts both Sender and Receiver tokens. // Used by endpoints accessible to both roles (e.g. UserPreferences). .AddPolicy(AuthPolicy.SenderOrReceiver, policy => policy .AddAuthenticationSchemes(AuthScheme.Sender, AuthScheme.Receiver) .RequireAuthenticatedUser()); // LDAP Directory Search builder.ConfigureBySection(); builder.Services.AddDirectorySearchService(config.GetSection("DirectorySearchOptions")); // Localization builder.Services.AddCookieBasedLocalizer(); // Cache options 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 => { config.GetSection("Cache:SqlServer").Bind(options); if (string.IsNullOrWhiteSpace(options.ConnectionString)) { options.ConnectionString = connStr; } }); // Envelope Generator Infrastructure & Application Services #pragma warning disable CS0618 builder.Services .AddEnvelopeGeneratorInfrastructureServices(opt => { opt.AddDbTriggerParams(config); opt.AddDbContext((provider, options) => { var logger = provider.GetRequiredService>(); options.UseSqlServer(connStr) .LogTo(log => logger.LogInformation("{log}", log), Microsoft.Extensions.Logging.LogLevel.Trace) .EnableSensitiveDataLogging() .EnableDetailedErrors(); }); opt.AddSQLExecutor(executor => executor.ConnectionString = connStr); }) .AddEnvelopeGeneratorServices(config) .AddNotificationInfrastructure(config); #pragma warning restore CS0618 // User Preferences (theme, dark mode, grid layouts — stored in TBDD_CACHE) // Registration handled by Infrastructure DependencyInjection (AddEnvelopeGeneratorInfrastructureServices) // CustomImages — logo sources and heights, configurable per deployment via appsettings.json builder.Services.Configure( config.GetSection(EnvelopeGenerator.Server.Models.CustomImagesOptions.SectionName)); // Client-side services that MainLayout depends on must also be registered here // so that Blazor Auto prerender (SSR pass) can resolve them on the server. // ThemeService.InitializeAsync() uses IJSRuntime but is only called in OnAfterRenderAsync // (never during prerender), so registering it server-side is safe. builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); // Business Services (Server specific) builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddDocInfrastructure(); builder.Services.AddCryptInfrastructure(config); builder.Services.AddSingleton(); // EnvelopeService with HttpClient factory (for SSR scenarios) builder.Services.AddScoped(); // DocReceiverElementService (SignatureService alternative) builder.Services.AddScoped(); // SSR Authentication Service (for Envelope Receiver pages) builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddHostedService(); // DevExpress Server-Side Services (CRITICAL for DxPdfViewer) builder.Services.AddDevExpressBlazor(); builder.Services.AddDevExpressServerSideBlazorPdfViewer(); // PdfSharp font resolver — required for .NET 8 (no system font access without it) PdfSharp.Fonts.GlobalFontSettings.FontResolver = PdfSharpFontResolver.Instance; // Configuration Options builder.Services.Configure( builder.Configuration.GetSection("ApiOptions")); builder.Services.Configure( builder.Configuration.GetSection("PdfViewerOptions")); builder.Services.AddScoped(); var app = builder.Build(); deferredProvider.Factory = () => app.Services; // Exception handling middleware for API controllers app.UseMiddleware(); app.UseSerilogRequestLogging(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseWebAssemblyDebugging(); } else { app.UseExceptionHandler("/Error", createScopeForErrors: true); app.UseHsts(); } var useSwagger = app.Environment.IsDevelopment() || config.GetValue("UseSwagger"); var useScalar = app.Environment.IsDevelopment() || config.GetValue("UseScalar"); if (useSwagger) { app.UseSwagger(); app.UseSwaggerUI(); } if (useScalar) { if (!useSwagger) app.UseSwagger(); // Scalar requires the OpenAPI JSON endpoint app.MapScalarApiReference(); } // Set CORS policy app.UseCors("AllowSpecificOriginsPolicy"); // Localization (default German; can be switched via cookie) var defaultCulture = new CultureInfo("de-DE"); IList list = [ defaultCulture, new CultureInfo("en-US"), new CultureInfo("fr-FR") ]; var requestLocalizationOptions = new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture(defaultCulture), SupportedCultures = list, SupportedUICultures = list, RequestCultureProviders = [new CookieRequestCultureProvider()] }; requestLocalizationOptions.ApplyCurrentCultureToResponseHeaders = true; app.UseRequestLocalization(requestLocalizationOptions); app.UseHttpsRedirection(); app.UseDefaultFiles(); app.UseStaticFiles(); app.UseAntiforgery(); 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(); // YARP Reverse Proxy - forwards unmatched requests to configured backends app.MapReverseProxy(); // Blazor routing app.MapRazorComponents() .AddInteractiveServerRenderMode() .AddInteractiveWebAssemblyRenderMode() .AddAdditionalAssemblies(typeof(EnvelopeGenerator.Server.Client._Imports).Assembly); Log.Information("EnvelopeGenerator.Server started successfully"); app.Run(); } catch (Exception ex) { Log.Fatal(ex, "EnvelopeGenerator.Server failed to start"); throw; } finally { Log.CloseAndFlush(); }