From 81171879d19344cf71aa7dbca05bec3a4c130397 Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Mon, 1 Apr 2024 15:21:10 +0200 Subject: [PATCH] Add Authentication Service --- sources/Clients.Admin.Web/Program.cs | 3 ++ .../wwwroot/appsettings.Development.json | 5 +++ .../wwwroot/appsettings.json | 5 +++ .../Authentications/AuthenticationService.cs | 22 ++++++++++++ .../Authentications/IAuthenticationService.cs | 8 +++++ .../Clients.Identity.Blazor.Shared.csproj | 2 ++ .../WebAssemblyHostBuilderExtensions.cs | 35 +++++++++++++++++++ .../Settings/ApiTypes.cs | 7 ++++ .../Settings/IdentitySettings.cs | 7 ++++ .../pages/Login.razor.cs | 6 ++++ sources/Directory.Packages.props | 1 + 11 files changed, 101 insertions(+) create mode 100644 sources/Clients.Admin.Web/wwwroot/appsettings.Development.json create mode 100644 sources/Clients.Admin.Web/wwwroot/appsettings.json create mode 100644 sources/Clients.Identity.Blazor.Shared/Authentications/AuthenticationService.cs create mode 100644 sources/Clients.Identity.Blazor.Shared/Authentications/IAuthenticationService.cs create mode 100644 sources/Clients.Identity.Blazor.Shared/Extensions/WebAssemblyHostBuilderExtensions.cs create mode 100644 sources/Clients.Identity.Blazor.Shared/Settings/ApiTypes.cs create mode 100644 sources/Clients.Identity.Blazor.Shared/Settings/IdentitySettings.cs create mode 100644 sources/Clients.Identity.Blazor.Shared/pages/Login.razor.cs diff --git a/sources/Clients.Admin.Web/Program.cs b/sources/Clients.Admin.Web/Program.cs index 552f533..2262d8a 100644 --- a/sources/Clients.Admin.Web/Program.cs +++ b/sources/Clients.Admin.Web/Program.cs @@ -1,11 +1,14 @@ using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using MadWorldNL.Clients.Admin.Web; +using MadWorldNL.Clients.Identity.Blazor.Shared.Extensions; var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add("#app"); builder.RootComponents.Add("head::after"); +builder.AddIdentity(); + builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); await builder.Build().RunAsync(); \ No newline at end of file diff --git a/sources/Clients.Admin.Web/wwwroot/appsettings.Development.json b/sources/Clients.Admin.Web/wwwroot/appsettings.Development.json new file mode 100644 index 0000000..3bcb71c --- /dev/null +++ b/sources/Clients.Admin.Web/wwwroot/appsettings.Development.json @@ -0,0 +1,5 @@ +{ + "Identity": { + "Host": "https://localhost:7047/" + } +} \ No newline at end of file diff --git a/sources/Clients.Admin.Web/wwwroot/appsettings.json b/sources/Clients.Admin.Web/wwwroot/appsettings.json new file mode 100644 index 0000000..3bcb71c --- /dev/null +++ b/sources/Clients.Admin.Web/wwwroot/appsettings.json @@ -0,0 +1,5 @@ +{ + "Identity": { + "Host": "https://localhost:7047/" + } +} \ No newline at end of file diff --git a/sources/Clients.Identity.Blazor.Shared/Authentications/AuthenticationService.cs b/sources/Clients.Identity.Blazor.Shared/Authentications/AuthenticationService.cs new file mode 100644 index 0000000..5e65092 --- /dev/null +++ b/sources/Clients.Identity.Blazor.Shared/Authentications/AuthenticationService.cs @@ -0,0 +1,22 @@ +using System.Net.Http.Json; +using MadWorldNL.Clients.Identity.Api.Contracts.Authentications; +using MadWorldNL.Clients.Identity.Blazor.Shared.Settings; + +namespace MadWorldNL.Clients.Identity.Blazor.Shared.Authentications; + +public class AuthenticationService : IAuthenticationService +{ + private const string Endpoint = "Authentication"; + + private readonly HttpClient _client; + public AuthenticationService(IHttpClientFactory clientFactory) + { + _client = clientFactory.CreateClient(ApiTypes.AnonymousIdentity); + } + + public async Task LoginAsync(LoginProxyRequest request) + { + var response = await _client.PostAsJsonAsync(Endpoint, request); + return await response.Content.ReadFromJsonAsync() ?? new LoginProxyResponse(); + } +} \ No newline at end of file diff --git a/sources/Clients.Identity.Blazor.Shared/Authentications/IAuthenticationService.cs b/sources/Clients.Identity.Blazor.Shared/Authentications/IAuthenticationService.cs new file mode 100644 index 0000000..fba1bd6 --- /dev/null +++ b/sources/Clients.Identity.Blazor.Shared/Authentications/IAuthenticationService.cs @@ -0,0 +1,8 @@ +using MadWorldNL.Clients.Identity.Api.Contracts.Authentications; + +namespace MadWorldNL.Clients.Identity.Blazor.Shared.Authentications; + +public interface IAuthenticationService +{ + Task LoginAsync(LoginProxyRequest request); +} \ No newline at end of file diff --git a/sources/Clients.Identity.Blazor.Shared/Clients.Identity.Blazor.Shared.csproj b/sources/Clients.Identity.Blazor.Shared/Clients.Identity.Blazor.Shared.csproj index 50a0a12..c0a8f91 100644 --- a/sources/Clients.Identity.Blazor.Shared/Clients.Identity.Blazor.Shared.csproj +++ b/sources/Clients.Identity.Blazor.Shared/Clients.Identity.Blazor.Shared.csproj @@ -11,6 +11,8 @@ + + diff --git a/sources/Clients.Identity.Blazor.Shared/Extensions/WebAssemblyHostBuilderExtensions.cs b/sources/Clients.Identity.Blazor.Shared/Extensions/WebAssemblyHostBuilderExtensions.cs new file mode 100644 index 0000000..5525a46 --- /dev/null +++ b/sources/Clients.Identity.Blazor.Shared/Extensions/WebAssemblyHostBuilderExtensions.cs @@ -0,0 +1,35 @@ +using MadWorldNL.Clients.Identity.Blazor.Shared.Authentications; +using MadWorldNL.Clients.Identity.Blazor.Shared.Settings; +using Microsoft.AspNetCore.Components.WebAssembly.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; + +namespace MadWorldNL.Clients.Identity.Blazor.Shared.Extensions; + +public static class WebAssemblyHostBuilderExtensions +{ + public static void AddIdentity(this WebAssemblyHostBuilder builder) + { + builder.Services.AddOptions() + .Bind(builder.Configuration.GetSection(IdentitySettings.Entry)); + + builder.Services.AddScoped(); + + builder.AddIdentityClients(); + } + + private static void AddIdentityClients(this WebAssemblyHostBuilder builder) + { + builder.Services.AddHttpClient(ApiTypes.AnonymousIdentity, (serviceProvider, client) => + { + var identitySettingsOption = serviceProvider.GetService>()!; + client.BaseAddress = new Uri(identitySettingsOption.Value.Host!); + }); + + builder.Services.AddHttpClient(ApiTypes.Identity, (serviceProvider, client) => + { + var identitySettingsOption = serviceProvider.GetService>()!; + client.BaseAddress = new Uri(identitySettingsOption.Value.Host!); + }); + } +} \ No newline at end of file diff --git a/sources/Clients.Identity.Blazor.Shared/Settings/ApiTypes.cs b/sources/Clients.Identity.Blazor.Shared/Settings/ApiTypes.cs new file mode 100644 index 0000000..d9fd21e --- /dev/null +++ b/sources/Clients.Identity.Blazor.Shared/Settings/ApiTypes.cs @@ -0,0 +1,7 @@ +namespace MadWorldNL.Clients.Identity.Blazor.Shared.Settings; + +public static class ApiTypes +{ + public const string AnonymousIdentity = nameof(AnonymousIdentity); + public const string Identity = nameof(Identity); +} \ No newline at end of file diff --git a/sources/Clients.Identity.Blazor.Shared/Settings/IdentitySettings.cs b/sources/Clients.Identity.Blazor.Shared/Settings/IdentitySettings.cs new file mode 100644 index 0000000..0f813d7 --- /dev/null +++ b/sources/Clients.Identity.Blazor.Shared/Settings/IdentitySettings.cs @@ -0,0 +1,7 @@ +namespace MadWorldNL.Clients.Identity.Blazor.Shared.Settings; + +public class IdentitySettings +{ + public const string Entry = "Identity"; + public string Host { get; set; } = string.Empty; +} \ No newline at end of file diff --git a/sources/Clients.Identity.Blazor.Shared/pages/Login.razor.cs b/sources/Clients.Identity.Blazor.Shared/pages/Login.razor.cs new file mode 100644 index 0000000..996665a --- /dev/null +++ b/sources/Clients.Identity.Blazor.Shared/pages/Login.razor.cs @@ -0,0 +1,6 @@ +namespace MadWorldNL.Clients.Identity.Blazor.Shared.pages; + +public partial class Login +{ + +} \ No newline at end of file diff --git a/sources/Directory.Packages.props b/sources/Directory.Packages.props index 6e77e35..8cc7ca2 100644 --- a/sources/Directory.Packages.props +++ b/sources/Directory.Packages.props @@ -18,6 +18,7 @@ +