From d891093ed06787e01e0efff27dfaf331769dec41 Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Wed, 20 Mar 2024 20:26:52 +0100 Subject: [PATCH] Add Authentication service --- sources/Clients.Admin/Clients.Admin.csproj | 9 +++++ .../Domain/AuthenticationToken.cs | 9 +++++ .../WebApplicationBuilderExtensions.cs | 20 +++++++++++ sources/Clients.Admin/Program.cs | 6 ++++ .../Services/AuthenticationService.cs | 34 +++++++++++++++++++ .../Services/IAuthenticationService.cs | 8 +++++ sources/Directory.Packages.props | 1 + 7 files changed, 87 insertions(+) create mode 100644 sources/Clients.Admin/Domain/AuthenticationToken.cs create mode 100644 sources/Clients.Admin/Extensions/WebApplicationBuilderExtensions.cs create mode 100644 sources/Clients.Admin/Services/AuthenticationService.cs create mode 100644 sources/Clients.Admin/Services/IAuthenticationService.cs diff --git a/sources/Clients.Admin/Clients.Admin.csproj b/sources/Clients.Admin/Clients.Admin.csproj index 8678fe8..8f4f553 100644 --- a/sources/Clients.Admin/Clients.Admin.csproj +++ b/sources/Clients.Admin/Clients.Admin.csproj @@ -11,4 +11,13 @@ + + + + + + + + + diff --git a/sources/Clients.Admin/Domain/AuthenticationToken.cs b/sources/Clients.Admin/Domain/AuthenticationToken.cs new file mode 100644 index 0000000..f261383 --- /dev/null +++ b/sources/Clients.Admin/Domain/AuthenticationToken.cs @@ -0,0 +1,9 @@ +namespace MadWorldNL.Clients.Admin.Domain; + +public class AuthenticationToken +{ + public bool IsSuccess { get; init; } + public string AccessToken { get; init; } = string.Empty; + public string RefreshToken { get; init; } = string.Empty; + public DateTime Expires { get; init; } +} \ No newline at end of file diff --git a/sources/Clients.Admin/Extensions/WebApplicationBuilderExtensions.cs b/sources/Clients.Admin/Extensions/WebApplicationBuilderExtensions.cs new file mode 100644 index 0000000..47f3a51 --- /dev/null +++ b/sources/Clients.Admin/Extensions/WebApplicationBuilderExtensions.cs @@ -0,0 +1,20 @@ +using MadWorldNL.Clients.Admin.Services; +using Server.Presentation.Grpc.Authentication.V1; + +namespace MadWorldNL.Clients.Admin.Extensions; + +public static class WebApplicationBuilderExtensions +{ + public static void AddGrpcClients(this WebApplicationBuilder builder) + { + builder.Services.AddGrpcClient(o => + { + o.Address = new Uri("https://localhost:5001"); // Replace with your gRPC service's address + }); + } + + public static void AddAdminServices(this WebApplicationBuilder builder) + { + builder.Services.AddScoped(); + } +} \ No newline at end of file diff --git a/sources/Clients.Admin/Program.cs b/sources/Clients.Admin/Program.cs index d914171..f29a567 100644 --- a/sources/Clients.Admin/Program.cs +++ b/sources/Clients.Admin/Program.cs @@ -1,4 +1,7 @@ using MadWorldNL.Clients.Admin.Components; +using MadWorldNL.Clients.Admin.Extensions; +using MadWorldNL.Clients.Admin.Services; +using Server.Presentation.Grpc.Authentication.V1; var builder = WebApplication.CreateBuilder(args); @@ -6,6 +9,9 @@ builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); +builder.AddGrpcClients(); +builder.AddAdminServices(); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/sources/Clients.Admin/Services/AuthenticationService.cs b/sources/Clients.Admin/Services/AuthenticationService.cs new file mode 100644 index 0000000..83440c6 --- /dev/null +++ b/sources/Clients.Admin/Services/AuthenticationService.cs @@ -0,0 +1,34 @@ +using MadWorldNL.Clients.Admin.Domain; +using Server.Presentation.Grpc.Authentication.V1; + +namespace MadWorldNL.Clients.Admin.Services; + +public class AuthenticationService : IAuthenticationService +{ + private readonly Authentication.AuthenticationClient _client; + + public AuthenticationService(Authentication.AuthenticationClient client) + { + _client = client; + } + + public AuthenticationToken Login(string username, string password, string audience) + { + var request = new LoginRequest() + { + Username = username, + Password = password, + Audience = audience + }; + + var response = _client.Login(request); + + return new AuthenticationToken() + { + IsSuccess = response.IsSuccess, + AccessToken = response.AccessToken, + RefreshToken = response.RefreshToken, + Expires = response.Expiration.ToDateTime() + }; + } +} \ No newline at end of file diff --git a/sources/Clients.Admin/Services/IAuthenticationService.cs b/sources/Clients.Admin/Services/IAuthenticationService.cs new file mode 100644 index 0000000..249b5c8 --- /dev/null +++ b/sources/Clients.Admin/Services/IAuthenticationService.cs @@ -0,0 +1,8 @@ +using MadWorldNL.Clients.Admin.Domain; + +namespace MadWorldNL.Clients.Admin.Services; + +public interface IAuthenticationService +{ + AuthenticationToken Login(string username, string password, string audience); +} \ No newline at end of file diff --git a/sources/Directory.Packages.props b/sources/Directory.Packages.props index 2bb5315..efde65c 100644 --- a/sources/Directory.Packages.props +++ b/sources/Directory.Packages.props @@ -4,6 +4,7 @@ +