From c981b24c60303a48b5de9a53ac55dd1650a00b54 Mon Sep 17 00:00:00 2001 From: ThompsonNye Date: Sun, 3 Aug 2025 12:19:29 +0200 Subject: [PATCH] WIP: Add Keycloak jwt bearer --- Directory.Packages.props | 40 +++++++++++-------- .../PresentPortal.ApiService.csproj | 2 + PresentPortal.ApiService/Program.cs | 16 +++++++- .../PresentPortal.AppHost.csproj | 2 + PresentPortal.AppHost/Program.cs | 11 ++++- .../PresentPortal.Shared.csproj | 9 +++++ PresentPortal.Shared/ServiceNames.cs | 8 ++++ PresentPortal.slnx | 1 + 8 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 PresentPortal.Shared/PresentPortal.Shared.csproj create mode 100644 PresentPortal.Shared/ServiceNames.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 7563417..1d52cf9 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -4,21 +4,29 @@ true - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + \ No newline at end of file diff --git a/PresentPortal.ApiService/PresentPortal.ApiService.csproj b/PresentPortal.ApiService/PresentPortal.ApiService.csproj index 75c1a25..6adc3b7 100644 --- a/PresentPortal.ApiService/PresentPortal.ApiService.csproj +++ b/PresentPortal.ApiService/PresentPortal.ApiService.csproj @@ -8,9 +8,11 @@ + + diff --git a/PresentPortal.ApiService/Program.cs b/PresentPortal.ApiService/Program.cs index a87a3f7..cec645f 100644 --- a/PresentPortal.ApiService/Program.cs +++ b/PresentPortal.ApiService/Program.cs @@ -1,3 +1,5 @@ +using PresentPortal.Shared; + var builder = WebApplication.CreateBuilder(args); // Add service defaults & Aspire client integrations. @@ -9,6 +11,14 @@ builder.Services.AddProblemDetails(); // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi builder.Services.AddOpenApi(); +builder.Services.AddAuthentication() + .AddKeycloakJwtBearer(ServiceNames.Keycloak, "TODO", options => + { + options.Audience = "TODO"; + options.Authority = "TODO"; + // options. + }); + var app = builder.Build(); // Configure the HTTP request pipeline. @@ -19,6 +29,9 @@ if (app.Environment.IsDevelopment()) app.MapOpenApi(); } +app.UseAuthentication(); +app.UseAuthorization(); + string[] summaries = ["Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"]; @@ -34,7 +47,8 @@ app.MapGet("/weatherforecast", () => .ToArray(); return forecast; }) - .WithName("GetWeatherForecast"); + .WithName("GetWeatherForecast") + .RequireAuthorization(); app.MapDefaultEndpoints(); diff --git a/PresentPortal.AppHost/PresentPortal.AppHost.csproj b/PresentPortal.AppHost/PresentPortal.AppHost.csproj index 7d6e4ff..e0e25cf 100644 --- a/PresentPortal.AppHost/PresentPortal.AppHost.csproj +++ b/PresentPortal.AppHost/PresentPortal.AppHost.csproj @@ -13,11 +13,13 @@ + + diff --git a/PresentPortal.AppHost/Program.cs b/PresentPortal.AppHost/Program.cs index 20dc1e3..7e21bf7 100644 --- a/PresentPortal.AppHost/Program.cs +++ b/PresentPortal.AppHost/Program.cs @@ -1,8 +1,17 @@ +using PresentPortal.Shared; + var builder = DistributedApplication.CreateBuilder(args); var cache = builder.AddRedis("cache"); -var apiService = builder.AddProject("apiservice"); +var keycloak = builder.AddKeycloak("keycloak", 8080) + .WithDataVolume() + .WithExternalHttpEndpoints() + .WithLifetime(ContainerLifetime.Persistent); + +var apiService = builder.AddProject(ServiceNames.Api) + .WithReference(keycloak) + .WaitFor(keycloak); builder.AddProject("webfrontend") .WithExternalHttpEndpoints() diff --git a/PresentPortal.Shared/PresentPortal.Shared.csproj b/PresentPortal.Shared/PresentPortal.Shared.csproj new file mode 100644 index 0000000..17b910f --- /dev/null +++ b/PresentPortal.Shared/PresentPortal.Shared.csproj @@ -0,0 +1,9 @@ + + + + net9.0 + enable + enable + + + diff --git a/PresentPortal.Shared/ServiceNames.cs b/PresentPortal.Shared/ServiceNames.cs new file mode 100644 index 0000000..e2aa2bc --- /dev/null +++ b/PresentPortal.Shared/ServiceNames.cs @@ -0,0 +1,8 @@ +namespace PresentPortal.Shared; + +public static class ServiceNames +{ + public const string Api = "Api"; + + public const string Keycloak = "Keycloak"; +} \ No newline at end of file diff --git a/PresentPortal.slnx b/PresentPortal.slnx index 38dc7f5..cdf0dfb 100644 --- a/PresentPortal.slnx +++ b/PresentPortal.slnx @@ -5,6 +5,7 @@ + \ No newline at end of file