From bee85bf92aab84087798ffa9f3f16336acef2fce Mon Sep 17 00:00:00 2001 From: Ariel Costas Guerrero Date: Thu, 19 Mar 2026 18:56:34 +0100 Subject: Basic backoffice for alert management --- src/Enmarcha.Backend/Program.cs | 66 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) (limited to 'src/Enmarcha.Backend/Program.cs') diff --git a/src/Enmarcha.Backend/Program.cs b/src/Enmarcha.Backend/Program.cs index 587da78..7ca0b34 100644 --- a/src/Enmarcha.Backend/Program.cs +++ b/src/Enmarcha.Backend/Program.cs @@ -1,11 +1,15 @@ using System.Text.Json.Serialization; using Enmarcha.Backend; +using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Enmarcha.Backend.Configuration; +using Enmarcha.Backend.Data; using Enmarcha.Backend.Services; using Enmarcha.Backend.Services.Geocoding; using Enmarcha.Backend.Services.Processors; using Enmarcha.Backend.Services.Providers; +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.WebUtilities; +using Microsoft.EntityFrameworkCore; using OpenTelemetry.Logs; using OpenTelemetry.Resources; using OpenTelemetry.Trace; @@ -130,7 +134,7 @@ builder.Services.AddOpenTelemetry() }); builder.Services - .AddControllers() + .AddControllersWithViews() .AddJsonOptions(options => { options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); @@ -139,6 +143,59 @@ builder.Services builder.Services.AddHttpClient(); builder.Services.AddMemoryCache(); +builder.Services.AddDbContext(options => +{ + options.UseNpgsql( + builder.Configuration.GetConnectionString("Database"), + o => o.UseNetTopologySuite() + ) + .UseCamelCaseNamingConvention(); +}); + +builder.Services.AddIdentityApiEndpoints() + .AddEntityFrameworkStores(); + +var auth0Domain = builder.Configuration["Auth0:Domain"] ?? ""; +var auth0ClientId = builder.Configuration["Auth0:ClientId"] ?? ""; + +builder.Services.AddAuthentication() + .AddCookie("Backoffice", options => + { + options.LoginPath = "/backoffice/auth/login"; + }) + .AddOpenIdConnect("Auth0", options => + { + options.Authority = $"https://{auth0Domain}/"; + options.ClientId = auth0ClientId; + options.ClientSecret = builder.Configuration["Auth0:ClientSecret"]; + options.ResponseType = "code"; + options.CallbackPath = "/backoffice/auth/callback"; + options.SignInScheme = "Backoffice"; + options.Scope.Clear(); + options.Scope.Add("openid"); + options.Scope.Add("profile"); + options.Scope.Add("email"); + options.SaveTokens = true; + options.Events = new OpenIdConnectEvents + { + OnRedirectToIdentityProviderForSignOut = context => + { + var logoutUri = $"https://{auth0Domain}/v2/logout?client_id={Uri.EscapeDataString(auth0ClientId)}"; + var returnTo = context.Properties.RedirectUri; + if (!string.IsNullOrEmpty(returnTo)) + { + var req = context.Request; + if (!returnTo.StartsWith("http", StringComparison.OrdinalIgnoreCase)) + returnTo = $"{req.Scheme}://{req.Host}{req.PathBase}{returnTo}"; + logoutUri += $"&returnTo={Uri.EscapeDataString(returnTo)}"; + } + context.Response.Redirect(logoutUri); + context.HandleResponse(); + return Task.CompletedTask; + } + }; + }); + builder.Services.AddSingleton(); builder.Services.AddSingleton(); @@ -161,6 +218,7 @@ builder.Services.AddScoped(); // builder.Services.AddKeyedScoped("Nominatim"); builder.Services.AddHttpClient(); builder.Services.AddHttpClient(); +builder.Services.AddHttpClient(); builder.Services.AddHttpClient(); builder.Services.AddHttpClient(); builder.Services.AddHttpClient(); @@ -169,6 +227,12 @@ builder.Services.AddHttpClient(); var app = builder.Build(); +app.UseStaticFiles(); +app.UseAuthentication(); +app.UseAuthorization(); + +app.MapGroup("/api/identity").MapIdentityApi(); + app.Use(async (context, next) => { if (context.Request.Headers.TryGetValue("X-Session-Id", out var sessionId)) -- cgit v1.3