Skip to content

Commit

Permalink
Add Authentication service
Browse files Browse the repository at this point in the history
  • Loading branch information
oveldman committed Mar 20, 2024
1 parent 34273dc commit d891093
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 0 deletions.
9 changes: 9 additions & 0 deletions sources/Clients.Admin/Clients.Admin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ClientSdk.Grpc\ClientSdk.Grpc.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Grpc.Net.Client" />
<PackageReference Include="Grpc.Net.ClientFactory" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions sources/Clients.Admin/Domain/AuthenticationToken.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
@@ -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<Authentication.AuthenticationClient>(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<IAuthenticationService, AuthenticationService>();
}
}
6 changes: 6 additions & 0 deletions sources/Clients.Admin/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
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);

// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();

builder.AddGrpcClients();
builder.AddAdminServices();

var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
34 changes: 34 additions & 0 deletions sources/Clients.Admin/Services/AuthenticationService.cs
Original file line number Diff line number Diff line change
@@ -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()
};
}
}
8 changes: 8 additions & 0 deletions sources/Clients.Admin/Services/IAuthenticationService.cs
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions sources/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PackageVersion Include="Grpc.AspNetCore" Version="2.61.0" />
<PackageVersion Include="Grpc.AspNetCore.Server.Reflection" Version="2.61.0" />
<PackageVersion Include="Grpc.Net.Client" Version="2.61.0" />
<PackageVersion Include="Grpc.Net.ClientFactory" Version="2.61.0" />
<PackageVersion Include="Grpc.Tools" Version="2.62.0" />
<PackageVersion Include="MadWorldNL.Common" Version="1.0.3" />
<PackageVersion Include="MadWorldNL.Common.AspNetCore" Version="1.0.3" />
Expand Down

0 comments on commit d891093

Please sign in to comment.