From 13004c6e9e653ad38d4e6708220776566f3835df Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Sun, 31 Mar 2024 18:37:13 +0200 Subject: [PATCH] Start with bff --- .../Clients.Admin.BFF/Clients.Admin.BFF.http | 6 ---- sources/Clients.Admin.BFF/Program.cs | 27 +---------------- .../appsettings.Development.json | 4 +++ sources/Clients.Admin.BFF/appsettings.json | 4 +++ .../Authentications/LoginProxyRequest.cs | 7 +++++ .../WebApplicationBuilderExtensions.cs | 17 +++++++++++ .../Clients.Identity.Api.Shared.csproj | 3 +- .../Endpoints/EndpointsExtensions.cs | 18 ++++++++++- .../Services/AuthenticationService.cs | 30 +++++++++++++++++++ .../Settings/IdentitySettings.cs | 9 ++++++ 10 files changed, 91 insertions(+), 34 deletions(-) delete mode 100644 sources/Clients.Admin.BFF/Clients.Admin.BFF.http create mode 100644 sources/Clients.Identity.Api.Shared/Authentications/LoginProxyRequest.cs create mode 100644 sources/Clients.Identity.Api.Shared/Services/AuthenticationService.cs create mode 100644 sources/Clients.Identity.Api.Shared/Settings/IdentitySettings.cs diff --git a/sources/Clients.Admin.BFF/Clients.Admin.BFF.http b/sources/Clients.Admin.BFF/Clients.Admin.BFF.http deleted file mode 100644 index c863400..0000000 --- a/sources/Clients.Admin.BFF/Clients.Admin.BFF.http +++ /dev/null @@ -1,6 +0,0 @@ -@Clients.Admin.BFF_HostAddress = http://localhost:5144 - -GET {{Clients.Admin.BFF_HostAddress}}/weatherforecast/ -Accept: application/json - -### diff --git a/sources/Clients.Admin.BFF/Program.cs b/sources/Clients.Admin.BFF/Program.cs index 4fcc79e..b11d41e 100644 --- a/sources/Clients.Admin.BFF/Program.cs +++ b/sources/Clients.Admin.BFF/Program.cs @@ -22,29 +22,4 @@ app.UseHttpsRedirection(); app.AddIdentityEndpoints(); -var summaries = new[] -{ - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" -}; - -app.MapGet("/weatherforecast", () => - { - var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast - ( - DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - Random.Shared.Next(-20, 55), - summaries[Random.Shared.Next(summaries.Length)] - )) - .ToArray(); - return forecast; - }) - .WithName("GetWeatherForecast") - .WithOpenApi(); - -app.Run(); - -record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) -{ - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); -} \ No newline at end of file +app.Run(); \ No newline at end of file diff --git a/sources/Clients.Admin.BFF/appsettings.Development.json b/sources/Clients.Admin.BFF/appsettings.Development.json index 0c208ae..86cb9b7 100644 --- a/sources/Clients.Admin.BFF/appsettings.Development.json +++ b/sources/Clients.Admin.BFF/appsettings.Development.json @@ -1,4 +1,8 @@ { + "Identity": { + "Audience": "https://admin.mad-world.nl", + "Host": " http://localhost:5266" + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/sources/Clients.Admin.BFF/appsettings.json b/sources/Clients.Admin.BFF/appsettings.json index 10f68b8..30beb42 100644 --- a/sources/Clients.Admin.BFF/appsettings.json +++ b/sources/Clients.Admin.BFF/appsettings.json @@ -1,4 +1,8 @@ { + "Identity": { + "Audience": "https://admin.mad-world.nl", + "Host": " http://localhost:5266" + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/sources/Clients.Identity.Api.Shared/Authentications/LoginProxyRequest.cs b/sources/Clients.Identity.Api.Shared/Authentications/LoginProxyRequest.cs new file mode 100644 index 0000000..fc436f9 --- /dev/null +++ b/sources/Clients.Identity.Api.Shared/Authentications/LoginProxyRequest.cs @@ -0,0 +1,7 @@ +namespace MadWorldNL.Clients.Identity.Api.Shared.Authentications; + +public class LoginProxyRequest +{ + public string Email { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; +} \ No newline at end of file diff --git a/sources/Clients.Identity.Api.Shared/Builders/WebApplicationBuilderExtensions.cs b/sources/Clients.Identity.Api.Shared/Builders/WebApplicationBuilderExtensions.cs index 21e151a..f050978 100644 --- a/sources/Clients.Identity.Api.Shared/Builders/WebApplicationBuilderExtensions.cs +++ b/sources/Clients.Identity.Api.Shared/Builders/WebApplicationBuilderExtensions.cs @@ -1,4 +1,9 @@ +using MadWorldNL.Clients.Identity.Api.Shared.Services; +using MadWorldNL.Clients.Identity.Api.Shared.Settings; using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Server.Presentation.Grpc.Authentication.V1; namespace MadWorldNL.Clients.Identity.Api.Shared.Builders; @@ -6,6 +11,18 @@ public static class WebApplicationBuilderExtensions { public static void AddIdentity(this WebApplicationBuilder builder) { + var identitySettings = builder.Configuration.GetSection(IdentitySettings.Entry); + builder.Services.AddOptions() + .Bind(identitySettings) + .ValidateDataAnnotations(); + + builder.Services.AddGrpcClient(option => + { + var identityHost = identitySettings.GetValue(nameof(IdentitySettings.Host))!; + option.Address = new Uri(identityHost); + }); + + builder.Services.AddScoped(); } } \ No newline at end of file diff --git a/sources/Clients.Identity.Api.Shared/Clients.Identity.Api.Shared.csproj b/sources/Clients.Identity.Api.Shared/Clients.Identity.Api.Shared.csproj index a51512f..dfd2b68 100644 --- a/sources/Clients.Identity.Api.Shared/Clients.Identity.Api.Shared.csproj +++ b/sources/Clients.Identity.Api.Shared/Clients.Identity.Api.Shared.csproj @@ -6,11 +6,12 @@ + - + diff --git a/sources/Clients.Identity.Api.Shared/Endpoints/EndpointsExtensions.cs b/sources/Clients.Identity.Api.Shared/Endpoints/EndpointsExtensions.cs index 5398fde..84b943e 100644 --- a/sources/Clients.Identity.Api.Shared/Endpoints/EndpointsExtensions.cs +++ b/sources/Clients.Identity.Api.Shared/Endpoints/EndpointsExtensions.cs @@ -1,4 +1,9 @@ +using MadWorldNL.Clients.Identity.Api.Shared.Authentications; +using MadWorldNL.Clients.Identity.Api.Shared.Services; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Server.Presentation.Grpc.Authentication.V1; namespace MadWorldNL.Clients.Identity.Api.Shared.Endpoints; @@ -6,6 +11,17 @@ public static class EndpointsExtensions { public static void AddIdentityEndpoints(this WebApplication app) { - + app.AddAuthenticationEndpoints(); + } + + private static void AddAuthenticationEndpoints(this WebApplication app) + { + var authenticationEndpoints = app.MapGroup("/Authentication") + .WithTags("Authentication"); + + authenticationEndpoints.MapPost("/Login", + ([AsParameters] LoginProxyRequest request, [FromServices] AuthenticationService authenticationService) => + authenticationService.AuthenticateAsync(request)) + .WithName("Login"); } } \ No newline at end of file diff --git a/sources/Clients.Identity.Api.Shared/Services/AuthenticationService.cs b/sources/Clients.Identity.Api.Shared/Services/AuthenticationService.cs new file mode 100644 index 0000000..294bb7e --- /dev/null +++ b/sources/Clients.Identity.Api.Shared/Services/AuthenticationService.cs @@ -0,0 +1,30 @@ +using MadWorldNL.Clients.Identity.Api.Shared.Authentications; +using MadWorldNL.Clients.Identity.Api.Shared.Settings; +using Microsoft.Extensions.Options; +using Server.Presentation.Grpc.Authentication.V1; + +namespace MadWorldNL.Clients.Identity.Api.Shared.Services; + +public sealed class AuthenticationService +{ + private readonly Authentication.AuthenticationClient _client; + private readonly IdentitySettings _identitySettings; + + public AuthenticationService(Authentication.AuthenticationClient client, IOptions identitySettings) + { + _client = client; + _identitySettings = identitySettings.Value; + } + + public async Task AuthenticateAsync(LoginProxyRequest proxyRequest) + { + var request = new LoginRequest + { + Audience = _identitySettings.Audience, + Username = proxyRequest.Email, + Password = proxyRequest.Password + }; + + return await _client.LoginAsync(request); + } +} \ No newline at end of file diff --git a/sources/Clients.Identity.Api.Shared/Settings/IdentitySettings.cs b/sources/Clients.Identity.Api.Shared/Settings/IdentitySettings.cs new file mode 100644 index 0000000..b446a35 --- /dev/null +++ b/sources/Clients.Identity.Api.Shared/Settings/IdentitySettings.cs @@ -0,0 +1,9 @@ +namespace MadWorldNL.Clients.Identity.Api.Shared.Settings; + +public class IdentitySettings +{ + public const string Entry = "Identity"; + + public string Audience { get; set; } = string.Empty; + public string Host { get; set; } = string.Empty; +} \ No newline at end of file